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