]> git.donarmstrong.com Git - lilypond.git/blob - lilypond-texi2html.init
Merge branch 'master' of ssh://kainhofer@git.sv.gnu.org/srv/git/lilypond into dev...
[lilypond.git] / lilypond-texi2html.init
1 #!/usr/bin/env perl
2
3 # {
4 package Texi2HTML::Config;
5
6 my $lastfilename;
7 my $docnr = 0;
8 my $page_toc_depth = 2;
9
10 use Data::Dumper;
11 $Data::Dumper::Maxdepth = 2;
12
13 sub print_element_info($) 
14 {
15   my $element = shift;
16   print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
17   print "Element: $element\n";
18   print Dumper($element);
19 }
20
21
22 # This function makes sure that files are only generated for numbered sections,
23 # but not for unnumbered ones. It is called after texi2html has done its own
24 # splitting and simply returns the filename for the node given as first argument
25 # Nodes with the same filename will be printed out to the same filename, so 
26 # this really all we need. Also, make sure that the file names for sections
27 # are derived from the section title. We also might want to name the anchors
28 # according to node titles, which works by simply overriding the id element of 
29 # the $element hash.
30 sub split_at_numbered_sections($$$)
31 {
32   my $element = shift;
33   my $type = shift;
34   my $docu_name = shift;
35   my $docu_ext = $Texi2HTML::Config::EXTENSION;
36   if ( $Texi2HTML::THISDOC{'current_lang'} ) {
37     $docu_ext = $Texi2HTML::THISDOC{'current_lang'} . "." . $docu_ext;
38   }
39
40 #   if ($$element{number} eq "1.1") {
41 #     print_element_info ($element);
42 #   }
43
44   # TOC, footer, about etc. are called with undefined $element and $type == "toc"|"stoc"|"foot"|"about"
45   if ($type eq "toc" or $type eq "stoc" or $type eq "foot" or $type eq "about") {
46     my $filename = "lilypond_$type";
47     $filename .= ".$docu_ext" if (defined($docu_ext));
48 #     print "Standard file of type $type\n";
49     return $filename;
50   } else {
51     # derive the name of the anchor (i.e. the part after # in the links!), 
52     # don't use texi2html's SECx.x default!
53     my $anchor = main::remove_texi($$element{texi});
54     my $trof = $$element{translationof};
55     if ($trof) {
56       print "We have a translationof: $trof\n";
57       print Dumper($element);
58 #       $anchor = $trof;
59     }
60     $anchor =~ tr/\ ?:'/-/d;
61     $$element{id} = $anchor;
62     # Numbered sections will get a filename Section_1.1.2, unnumbered sections will use 
63     # the file name of the previous numbered section:
64     if ($$element{number}) {
65       my $filename = $anchor;
66       $filename .= ".$docu_ext" if (defined($docu_ext));
67       $docnr += 1;
68       $$element{doc_nr} = $docnr;
69       $lastfilename = $filename;
70       return $filename;
71     } else {
72       $$element{doc_nr} = $docnr;
73       return $lastfilename;
74     }
75   }
76
77   return;
78 }
79
80
81 # Print a link in a menu. Since we split at sections, but still want access keys, 
82 # we have to duplicate this function here :-((((
83 sub lilypond_menu_link($$$$$$)
84 {
85     my $entry = shift;
86     my $state = shift;
87     my $href = shift;
88     my $node = shift;
89     my $name = shift;
90     my $ending = shift;
91     if (($entry eq '') or $NODE_NAME_IN_MENU or $state->{'preformatted'})
92     {
93         $name .= ':' if ($name ne '');
94         $entry = "$MENU_SYMBOL$name$node";
95     }
96     $menu_entry_index++;
97     my $accesskey;
98     $accesskey = "accesskey=\"$menu_entry_index\"" if ($USE_ACCESSKEY and ($menu_entry_index < 10));
99     $entry = &$anchor ('', $href, $entry, $accesskey) if (defined($href));
100     return $entry if ($SIMPLE_MENU); 
101     if ($state->{'preformatted'})
102     {
103         return '<tr><td>' . main::do_preformatted($entry . $ending, $state);
104     }
105     return "<tr><td align=\"left\" valign=\"top\">$entry</td><td>&nbsp;&nbsp;</td>";
106 }
107
108
109 # The default formatting of external refs returns e.g. 
110 # "(lilypond-internals)Timing_translator", while we simply want "Timing_translator".
111 # Solution: Remove all (...) from the file_and_node argument before calling
112 # the default handler!
113 sub lilypond_external_ref($$$$$$)
114 {
115   my $type = shift;
116   my $section = shift;
117   my $book = shift;
118   my $file_node = shift;
119   my $href = shift;
120   my $cross_ref = shift;
121
122   $file_node =~ s/\(.*\)//;
123   return t2h_default_external_ref($type, $section, $book, $file_node, $href, $cross_ref);
124 }
125
126
127 # recursively generate the TOC entries for the element and its children (which
128 # are only shown up to maxlevel. All ancestors of the current element are also 
129 # shown with their immediate children, irrespective of their level.
130 sub generate_ly_toc_entries($$$)
131 {
132   my $element = shift;
133   my $element_path = shift;
134   my $maxlevel = shift;
135   # Skip undefined sections, plus all sections generated by index splitting
136   return() if (not defined($element) or exists($element->{'index_page'}));
137   my @result = ();
138   my $level = $element->{'toc_level'};
139   my $is_parent_of_current = $element_path->{$element->{'number'}};
140   my $print_children = ( ($level < $maxlevel) or $is_parent_of_current );
141   my $ind = '  ' x $level;
142   my $this_css_class = $is_parent_of_current ? " class=\"toc_current\"" : "";
143
144   my $entry = "$ind<li$this_css_class>" . &$anchor ($element->{'tocid'}, "$element->{'file'}#$element->{'id'}",$element->{'text'});
145
146   my $children = $element->{'section_childs'};
147   if ( $print_children and defined($children) and (ref($children) eq "ARRAY") ) {
148     push (@result, $entry);
149     my @child_result = ();
150     foreach (@$children) {
151       push (@child_result, generate_ly_toc_entries($_, $element_path, $maxlevel));
152     }
153     # if no child nodes were generated, e.g. for the index, where expanded pages
154     # are ignored, don't generate a list at all...
155     if (@child_result) {
156       push (@result, "$ind<ul$NO_BULLET_LIST_ATTRIBUTE>");
157       push (@result, @child_result);
158       push (@result, "$ind</ul></li>\n");
159     }
160   } else {
161     push (@result, $entry . "</li>\n");
162   }
163   return @result;
164 }
165
166
167 # Print a customized TOC, containing only the first two levels plus the whole
168 # path to the current page
169 sub lilypond_generate_page_toc_body($)
170 {
171     my $element = shift;
172     my $current_element = $element;
173     my %parentelements;
174     $parentelements{$element->{'number'}} = 1;
175     # Find the path to the current element
176     while ( defined($current_element->{'sectionup'}) and 
177            ($current_element->{'sectionup'} ne $current_element) )
178     {
179       $parentelements{$current_element->{'sectionup'}->{'number'}} = 1
180               if ($current_element->{'sectionup'}->{'number'} ne '');
181       $current_element = $current_element->{'sectionup'};
182     }
183     return () if not defined($current_element);
184     # Create the toc entries recursively
185     my @toc_entries = ("<div class=\"contents\">", "<ul$NO_BULLET_LIST_ATTRIBUTE>");
186     my $children = $current_element->{'section_childs'};
187     foreach ( @$children ) {
188       push (@toc_entries, generate_ly_toc_entries($_, \%parentelements, $page_toc_depth));
189     }
190     push (@toc_entries, "</ul>");
191     push (@toc_entries, "</div>");
192     return @toc_entries;
193 }
194
195 my @this_page_toc = ();
196
197 sub lilypond_print_element_header
198 {
199   my $fh = shift;
200   my $first_in_page = shift;
201   my $previous_is_top = shift;
202   if ($first_in_page and not @this_page_toc) {
203     if (defined($Texi2HTML::THIS_ELEMENT)) {
204       # Create the TOC for this page
205       @this_page_toc = lilypond_generate_page_toc_body($Texi2HTML::THIS_ELEMENT);
206     }
207   }
208   return T2H_DEFAULT_print_element_header( $fh, $first_in_page, $previous_is_top);
209 }
210
211
212
213
214
215
216
217
218
219
220 # Print out the TOC in a <div> at the end of th page, which will be formatted as a
221 # sidebar mimicking a TOC frame
222 sub print_lilypond_page_foot($)
223 {
224   my $fh = shift;
225 #   my @lines = @{$Texi2HTML::OVERVIEW};
226 #   my $lines = $Texi2HTML::TOC_LINES;
227   my @lines = @this_page_toc;
228   if (not @lines) {
229     print "We have no toc lines, generate generic ones for main::element_top::\n";
230 #     print Dumper( $main::element_top );
231 #     print Dumper( $main::section_top );
232 #     print Dumper( $main::node_top );
233 #     @lines = lilypond_generate_page_toc_body( $main::element_top );
234 #     @lines = lilypond_generate_page_toc_body($Texi2HTML::THIS_ELEMENT);
235     @lines = @$Texi2HTML::TOC_LINES;
236   }
237   if (@lines) {
238     print $fh "<div id=\"tocframe\">";
239     print $fh '<h4> ' . $Texi2HTML::NAME{'Contents'}  . "</h4>\n";
240     foreach my $line (@lines) {
241       print $fh $line;
242     }
243     print $fh "</div>";
244     @this_page_toc = ();
245   }
246   T2H_DEFAULT_print_page_foot($fh);
247 }
248
249
250
251
252
253
254
255 sub get_navigation_text
256 {
257   my $button = shift;
258   my $text = $NAVIGATION_TEXT{$button};
259   if ( ($button eq 'Back') or ($button eq 'FastBack') ) {
260     $text = $text . $Texi2HTML::NODE{$button} . "&nbsp;";
261   } elsif ( ($button eq 'Forward') or ($button eq 'FastForward') ) {
262     $text = "&nbsp;" . $Texi2HTML::NODE{$button} . $text;
263   } elsif ( $button eq 'Up' ) {
264     $text = "&nbsp;Up:&nbsp;" . $Texi2HTML::NODE{$button} . "&nbsp;";
265   }
266   return $text;
267 }
268
269
270 # Don't automatically create left-aligned table cells for every link, but 
271 # instead create a <td> only on an appropriate '(left|right|center)-aligned-cell-n'
272 # button text. It's alignment as well as the colspan will be taken from the
273 # name of the button. Also, add 'newline' button text to create a new table
274 # row. The texts of the buttons are generated by get_navigation_text and 
275 # will contain the name of the next/previous section/chapter.
276 sub lilypond_print_navigation
277 {
278     my $fh = shift;
279     my $buttons = shift;
280     my $vertical = shift;
281     my $spacing = 1;
282 #     print $fh '<table cellpadding="', $spacing, '" cellspacing="', $spacing,
283 #       "\" border=\"0\" class=\"nav_table\">\n";
284     print $fh "<table class=\"nav_table\">\n";
285
286     print $fh "<tr>" unless $vertical;
287     my $beginofline = 1;
288     foreach my $button (@$buttons)
289     {
290         print $fh qq{<tr valign="top" align="left">\n} if $vertical;
291         # Allow (left|right|center)-aligned-cell and newline as buttons!
292         if ( $button =~ /^(.*)-aligned-cell-(.*)$/ ) 
293         {
294           print $fh qq{</td>} unless $beginofline;
295           print $fh qq{<td valign="middle" align="$1" colspan="$2">};
296           $beginofline = 0;
297         } 
298         elsif ( $button eq 'newline' ) 
299         {
300           print $fh qq{</td>} unless $beginofline;
301           print $fh qq{</tr>};
302           print $fh qq{<tr>};
303           $beginofline = 1;
304
305         } 
306         elsif (ref($button) eq 'CODE')
307         {
308             &$button($fh, $vertical);
309         }
310         elsif (ref($button) eq 'SCALAR')
311         {
312             print $fh "$$button" if defined($$button);
313         }
314         elsif (ref($button) eq 'ARRAY')
315         {
316             my $text = $button->[1];
317             my $button_href = $button->[0];
318             # verify that $button_href is simple text and text is a reference
319             if (defined($button_href) and !ref($button_href) 
320                and defined($text) and (ref($text) eq 'SCALAR') and defined($$text))
321             {             # use given text
322                 if ($Texi2HTML::HREF{$button_href})
323                 {
324                   my $anchor_attributes = '';
325                   if ($USE_ACCESSKEY and (defined($BUTTONS_ACCESSKEY{$button_href})) and ($BUTTONS_ACCESSKEY{$button_href} ne ''))
326                   {
327                       $anchor_attributes = "accesskey=\"$BUTTONS_ACCESSKEY{$button_href}\"";
328                   }
329                   if ($USE_REL_REV and (defined($BUTTONS_REL{$button_href})) and ($BUTTONS_REL{$button_href} ne ''))
330                   {
331                       $anchor_attributes .= " rel=\"$BUTTONS_REL{$button_href}\"";
332                   }
333                   print $fh "" .
334                         &$anchor('',
335                                     $Texi2HTML::HREF{$button_href},
336                                     $$text,
337                                     $anchor_attributes
338                                    ) 
339                                     ;
340                 }
341                 else
342                 {
343                   print $fh $$text;
344                 }
345             }
346         }
347         elsif ($button eq ' ')
348         {                       # handle space button
349             print $fh
350                 ($ICONS && $ACTIVE_ICONS{' '}) ?
351                     &$button_icon_img($BUTTONS_NAME{$button}, $ACTIVE_ICONS{' '}) :
352                         $NAVIGATION_TEXT{' '};
353             #next;
354         }
355         elsif ($Texi2HTML::HREF{$button})
356         {                       # button is active
357             my $btitle = $BUTTONS_GOTO{$button} ?
358                 'title="' . $BUTTONS_GOTO{$button} . '"' : '';
359             if ($USE_ACCESSKEY and (defined($BUTTONS_ACCESSKEY{$button})) and ($BUTTONS_ACCESSKEY{$button} ne ''))
360             {
361                 $btitle .= " accesskey=\"$BUTTONS_ACCESSKEY{$button}\"";
362             }
363             if ($USE_REL_REV and (defined($BUTTONS_REL{$button})) and ($BUTTONS_REL{$button} ne ''))
364             {
365                 $btitle .= " rel=\"$BUTTONS_REL{$button}\"";
366             }
367             if ($ICONS && $ACTIVE_ICONS{$button})
368             {                   # use icon
369                 print $fh '' .
370                     &$anchor('',
371                         $Texi2HTML::HREF{$button},
372                         &$button_icon_img($BUTTONS_NAME{$button},
373                                    $ACTIVE_ICONS{$button},
374                                    $Texi2HTML::SIMPLE_TEXT{$button}),
375                         $btitle
376                       );
377             }
378             else
379             {                   # use text
380                 print $fh
381                     '[' .
382                         &$anchor('',
383                                     $Texi2HTML::HREF{$button},
384                                     $NAVIGATION_TEXT{$button},
385                                     $btitle
386                                    ) .
387                                        ']';
388             }
389         }
390         else
391         {                       # button is passive
392             print $fh
393                 $ICONS && $PASSIVE_ICONS{$button} ?
394                     &$button_icon_img($BUTTONS_NAME{$button},
395                                           $PASSIVE_ICONS{$button},
396                                           $Texi2HTML::SIMPLE_TEXT{$button}) :
397
398                                               "[" . $NAVIGATION_TEXT{$button} . "]";
399         }
400         print $fh "</td>\n" if $vertical;
401         print $fh "</tr>\n" if $vertical;
402     }
403     print $fh "</td>" unless $beginofline;
404     print $fh "</tr>" unless $vertical;
405     print $fh "</table>\n";
406 }
407
408
409 @Texi2HTML::Config::SECTION_BUTTONS =
410     ('left-aligned-cell-1', 'FastBack', 
411      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
412      'right-aligned-cell-1', 'FastForward',
413      'newline',
414      'left-aligned-cell-2', 'Back',
415      'center-aligned-cell-1', 'Up',
416      'right-aligned-cell-2', 'Forward'
417     );
418
419 # buttons for misc stuff
420 @Texi2HTML::Config::MISC_BUTTONS = ('center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About');
421
422 # buttons for chapter file footers
423 # (and headers but only if SECTION_NAVIGATION is false)
424 @Texi2HTML::Config::CHAPTER_BUTTONS =
425     ('left-aligned-cell-1', 'FastBack', 
426      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
427      'right-aligned-cell-1', 'FastForward',
428     );
429
430 # buttons for section file footers
431 @Texi2HTML::Config::SECTION_FOOTER_BUTTONS =
432     ('left-aligned-cell-1', 'FastBack', 
433      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
434      'right-aligned-cell-1', 'FastForward',
435      'newline',
436      'left-aligned-cell-2', 'Back',
437      'center-aligned-cell-1', 'Up',
438      'right-aligned-cell-2', 'Forward'
439     );
440
441 @Texi2HTML::Config::NODE_FOOTER_BUTTONS =
442     ('left-aligned-cell-1', 'FastBack', 
443      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
444      'right-aligned-cell-1', 'FastForward',
445      'newline',
446      'left-aligned-cell-2', 'Back',
447      'center-aligned-cell-1', 'Up',
448      'right-aligned-cell-2', 'Forward'
449     );
450
451 # $Texi2HTML::Config::SPLIT = 'section';
452 @Texi2HTML::Config::CSS_REFS = ("lilypond.css");
453 $Texi2HTML::Config::USE_ACCESSKEY = 1;
454 $Texi2HTML::Config::USE_LINKS = 1;
455 $Texi2HTML::Config::USE_REL_REV = 1;
456 $Texi2HTML::Config::element_file_name = \&split_at_numbered_sections;
457 $Texi2HTML::Config::print_element_header = \&lilypond_print_element_header;
458 $Texi2HTML::Config::print_page_foot = \&print_lilypond_page_foot;
459 $Texi2HTML::Config::print_navigation = \&lilypond_print_navigation;
460 $Texi2HTML::Config::external_ref = \&lilypond_external_ref;
461 $Texi2HTML::Config::menu_link = \&lilypond_menu_link;
462
463
464 # For split pages, use index(.lang).html as start page!
465 if ($Texi2HTML::Config::SPLIT == 'section') {
466   my $lng = $Texi2HTML::THISDOC{'current_lang'};
467   if ($lng and ($lng ne "en")) {
468     $Texi2HTML::Config::TOP_FILE = 'index.'.$lng.'.html';
469   } else {
470     $Texi2HTML::Config::TOP_FILE = 'index.html';
471   }
472 }
473
474
475
476 # Try to make use of @translationof to generate files according to the original
477 # English section title...
478 sub lilypond_unknown($$$$$)
479 {
480     my $macro = shift;
481     my $line = shift;
482     my $pass = shift;
483     my $stack = shift;
484     my $state = shift;
485     
486     if ($pass == 1 and $macro eq "translationof") {
487       if (ref($state->{'element'})=='HASH') {
488         $state->{'element'}->{'translationof'} = $line;
489       }
490       print "UNKNOWN: macro:$macro, line:$line, pass:$pass, stack:$stack, state:$state\n";
491       print Dumper($stack);
492       print Dumper($state);
493     }
494     if ($pass == 2 and $macro eq "translationof") {
495 #       if (ref($state->{'element'})=='HASH') {
496 # #         $state->{'element'}->{'translationonf'} = $line;
497 #       }
498       print "UNKNOWN: macro:$macro, line:$line, pass:$pass, stack:$stack, state:$state\n";
499       print Dumper($stack);
500       print Dumper($state);
501       return ($line, 0, undef, undef);
502     } else {
503       return t2h_default_unknown($macro, $line, $pass, $stack, $state);
504     }
505 }
506 $Texi2HTML::Config::unknown                  = \&lilypond_unknown;
507
508
509
510 return 1;