]> git.donarmstrong.com Git - lilypond.git/blob - lilypond-texi2html.init
Merge branch 'master' into lilypond/translation
[lilypond.git] / lilypond-texi2html.init
1 #!/usr/bin/env perl
2 # -*- coding: utf-8; -*-
3
4 ### texi2html customization script for Lilypond
5 ### Author: Reinhold Kainhofer <reinhold@kainhofer.com>, 2008.
6 ###         Some code parts copied from texi2html and adapted. These functions
7 ###         were written mainly by Patrice Dumas
8 ### License: GPLv2+
9 ###
10 ###
11 ### Features implemented here:
12 ### -) For split manuals, the main page is index.html.
13 ### -) All @unnumbered* sections are placed into the same file
14 ###    (implemented by split_at_numbered_sections)
15 ### -) Use our custom CSS file, with IE-specific fixes in another CSS file,
16 ###    impelmented by lilypond_css_lines
17 ### -) TOC (folded, with the current page highlighted) in an overflown <div>
18 ###    is added to every page; implemented by:
19 ###           lilypond_print_element_header -- building of the TOC
20 ###           lilypond_toc_body -- generation of customized TOC output
21 ###           lilypond_print_page_head -- start <div id="main">
22 ###           print_lilypond_page_foot -- closing id=main, output of footer & TOC
23 ### -) External refs are formatted only as "Text of the node" (not as >>see
24 ###    "NODE" section "SECTION" in "BOOK"<< like with default texi2html). Also,
25 ###    the leading "(book-name)" is removed.
26 ###    Implemented by overriding lilypond_external_ref
27 ### -) Navigation bars on top/bottom of the page and between sections are not
28 ###    left-aligned, but use a combination of left/center/right aligned table
29 ###    cells; For this, I heavily extend the texi2html code to allow for
30 ###    differently aligned cells and for multi-line tables);
31 ###    Implemented in lilypond_print_navigation
32 ### -) Different formatting than the default: example uses the same formatting
33 ###    as quote.
34 ### -) Allow translated section titles: All section titles can be translated,
35 ###    the original (English) title is associated with @translationof. This is
36 ###    needed, because the file name / anchor is generated from the original
37 ###    English title, since otherwise language-autoselection would break with
38 ###    posted links.
39 ###    Since it is then no longer possible to obtain the file name from the
40 ###    section title, I keep a sectionname<=>filename/anchor around. This way,
41 ###    xrefs from other manuals can simply load that map and retrieve the
42 ###    correct file name for the link. Implemented in:
43 ###           lilypond_unknown (handling of @translationof, in case
44 ###                             extract_texi_filenames.py messes up...)
45 ###           lilypond_element_file_name (correct file name: use the map)
46 ###           lilypond_element_target_name (correct anchor: use the map)
47 ###           lilypond_init_map (read in the externally created map from disk)
48 ###           lilypond_external_href (load the map for xrefs, use the correct
49 ###                                   link target)
50 ### -) The HTML anchors for all sections are derived from the node name /
51 ###    section title (pre-generated in the .xref-map file). Implemented by:
52 ###           lilypond_element_target_name (adjust section anchors)
53 ### -) Use the standard footnote format "<sup>nr</sup> text" instead of the
54 ###    ugly format of texi2html (<h3>(nr)</h3><p>text</p>). Implemented in
55 ###           makeinfo_like_foot_line_and_ref
56 ###           makeinfo_like_foot_lines
57 ###           makeinfo_like_paragraph
58 ###
59 ###
60 ### Useful helper functions:
61 ### -) texinfo_file_name($node_name): returns a texinfo-compatible file name
62 ###    for the given string $node_name (whitespace trimmed/replaced by -,
63 ###    non-standard chars replaced by _xxxx (ascii char code) and forced to
64 ###    start with a letter by prepending t_g if necessary)
65
66
67 package Texi2HTML::Config;
68
69 #############################################################################
70 ### TRANSLATIONS
71 #############################################################################
72
73 use utf8;
74 my $LY_LANGUAGES = {};
75 $LY_LANGUAGES->{'fr'} = {
76     'Back to Documentation Index' => 'Retour à l\'accueil de la documentation',
77 };
78 $LY_LANGUAGES->{'es'} = {
79     'Back to Documentation Index' => 'Volver al índice de la documentación',
80 };
81 $LY_LANGUAGES->{'de'} = {
82     'Back to Documentation Index' => 'Zur Dokumentationsübersicht',
83 };
84 $LY_LANGUAGES->{'ja'} = {
85     'Back to Documentation Index' => 'ドキュメント インデックスに戻る',
86 };
87
88
89 sub ly_get_string () {
90     my $lang = $Texi2HTML::THISDOC{current_lang};
91     my $string = shift;
92     if ($lang and $lang ne "en" and $LY_LANGUAGES->{$lang}->{$string}) {
93         return $LY_LANGUAGES->{$lang}->{$string};
94     } else {
95         return $string;
96     }
97 }
98
99
100 #############################################################################
101 ###  SETTINGS FOR TEXI2HTML
102 #############################################################################
103
104 @Texi2HTML::Config::CSS_REFS      = (
105     {FILENAME => "lilypond-mccarty.css", TITLE => "Patrick McCarty's design"}
106 );
107 @Texi2HTML::Config::ALT_CSS_REFS      = (
108     {FILENAME => "lilypond.css", TITLE => "Andrew Hawryluk's design" },
109     {FILENAME => "lilypond-blue.css", TITLE => "Kurt Kroon's blue design" },
110 );
111 $Texi2HTML::Config::USE_ACCESSKEY = 1;
112 $Texi2HTML::Config::USE_LINKS     = 1;
113 $Texi2HTML::Config::USE_REL_REV   = 1;
114 $Texi2HTML::Config::SPLIT_INDEX   = 0;
115 $Texi2HTML::Config::SEPARATED_FOOTNOTES = 0; # Print footnotes on same page, not separated
116 if ($Texi2HTML::Config::SPLIT eq 'section') {
117   $Texi2HTML::Config::element_file_name    = \&lilypond_element_file_name;
118 }
119 $Texi2HTML::Config::element_target_name  = \&lilypond_element_target_name;
120 $Texi2HTML::Config::print_element_header = \&lilypond_print_element_header;
121 $Texi2HTML::Config::print_page_foot      = \&print_lilypond_page_foot;
122 $Texi2HTML::Config::print_navigation     = \&lilypond_print_navigation;
123 $Texi2HTML::Config::external_ref         = \&lilypond_external_ref;
124 $Texi2HTML::Config::external_href        = \&lilypond_external_href;
125 $Texi2HTML::Config::toc_body             = \&lilypond_toc_body;
126 $Texi2HTML::Config::css_lines            = \&lilypond_css_lines;
127 $Texi2HTML::Config::unknown              = \&lilypond_unknown;
128 $Texi2HTML::Config::print_page_head      = \&lilypond_print_page_head;
129 # $Texi2HTML::Config::foot_line_and_ref    = \&lilypond_foot_line_and_ref;
130 $Texi2HTML::Config::foot_line_and_ref  = \&makeinfo_like_foot_line_and_ref;
131 $Texi2HTML::Config::foot_lines         = \&makeinfo_like_foot_lines;
132 $Texi2HTML::Config::paragraph          = \&makeinfo_like_paragraph;
133 $Texi2HTML::Config::init_out             = \&lilypond_init_out;
134
135
136
137 # Examples should be formatted similar to quotes:
138 $Texi2HTML::Config::complex_format_map->{'example'} = {
139   'begin' => q{"<blockquote><pre class=\"example\">"},
140   'end' => q{"</pre></blockquote>\n"},
141   'style' => 'code',
142  };
143
144 %Texi2HTML::config::misc_pages_targets = (
145    'Overview' => 'Overview',
146    'Contents' => 'Contents',
147    'About' => 'About'
148 );
149
150
151 my @section_to_filename;
152
153
154
155
156 #############################################################################
157 ###  DEBUGGING
158 #############################################################################
159
160 use Data::Dumper;
161 $Data::Dumper::Maxdepth = 2;
162
163 sub print_element_info($)
164 {
165   my $element = shift;
166   print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
167   print "Element: $element\n";
168   print Dumper($element);
169 }
170
171
172
173
174
175 #############################################################################
176 ###  HELPER FUNCTIONS
177 #############################################################################
178
179 # Convert a given node name to its proper file name (normalization as explained
180 # in the texinfo manual:
181 # http://www.gnu.org/software/texinfo/manual/texinfo/html_node/HTML-Xref-Node-Name-Expansion.html
182 sub texinfo_file_name($)
183 {
184   my $text = shift;
185   my $result = '';
186   # File name normalization by texinfo:
187   # 1/2: letters and numbers are left unchanged
188   # 3/4: multiple, leading and trailing whitespace is removed
189   $text = main::normalise_space($text);
190   # 5/6: all remaining spaces are converted to '-', all other 7- or 8-bit
191   #      chars are replaced by _xxxx (xxxx=ascii character code)
192   while ($text ne '') {
193     if ($text =~ s/^([A-Za-z0-9]+)//o) { # number or letter stay unchanged
194       $result .= $1;
195     } elsif ($text =~ s/^ //o) { # space -> '-'
196       $result .= '-';
197     } elsif ($text =~ s/^(.)//o) { # Otherwise use _xxxx (ascii char code)
198       my $ccode = ord($1);
199       if ( $ccode <= 0xFFFF ) {
200         $result .= sprintf("_%04x", $ccode);
201       } else {
202         $result .= sprintf("__%06x", $ccode);
203       }
204     }
205   }
206   # 7: if name does not begin with a letter, prepend 't_g' (so it starts with a letter)
207   if ($result !~ /^[a-zA-Z]/) {
208     $result = 't_g' . $result;
209   }
210   # DONE
211   return $result
212 }
213
214
215 # Load a file containing a nodename<=>filename map (tab-sepatared, i.e.
216 # NODENAME\tFILENAME\tANCHOR
217 # Returns a ref to a hash "Node title" => ["FilenameWithoutExt", "Anchor"]
218 sub load_map_file ($)
219 {
220     my $mapfile = shift;
221     my $node_map = ();
222
223     if (open(XREFFILE, $mapfile)) {
224         my $line;
225         while ( $line = <XREFFILE> ) {
226             # parse the tab-separated entries and insert them into the map:
227             chomp($line);
228             my @entries = split(/\t/, $line);
229             if (scalar (@entries) == 3) {
230               $node_map->{$entries[0]} = [$entries[1], $entries[2]];
231             } else {
232               print STDERR "Invalid entry in the node file $mapfile: $line\n";
233             }
234         }
235         close (XREFFILE);
236     } else {
237         print STDERR "WARNING: Unable to load the map file $mapfile\n";
238     }
239     return $node_map;
240 }
241
242
243 # Split the given path into dir and basename (with .texi removed). Used mainly
244 # to get the path/basename of the original texi input file
245 sub split_texi_filename ($)
246 {
247   my $docu = shift;
248   my ($docu_dir, $docu_name);
249   if ($docu =~ /(.*\/)/) {
250     chop($docu_dir = $1);
251     $docu_name = $docu;
252     $docu_name =~ s/.*\///;
253   } else {
254      $docu_dir = '.';
255      $docu_name = $docu;
256   }
257   $docu_name =~ s/\.te?x(i|info)?$//;
258   return ($docu_dir, $docu_name);
259 }
260
261
262
263
264
265 #############################################################################
266 ###  CSS HANDLING
267 #############################################################################
268
269 # Include our standard CSS file, not hard-coded CSS code directly in the HTML!
270 # For IE, conditionally include the lilypond-ie-fixes.css style sheet
271 sub lilypond_css_lines ($$)
272 {
273     my $import_lines = shift;
274     my $rule_lines = shift;
275     return if (defined($Texi2HTML::THISDOC{'CSS_LINES'}));
276     if (@$rule_lines or @$import_lines)
277     {
278         $Texi2HTML::THISDOC{'CSS_LINES'} = "<style type=\"text/css\">\n<!--\n";
279         $Texi2HTML::THISDOC{'CSS_LINES'} .= join('',@$import_lines) . "\n" if (@$import_lines);
280         $Texi2HTML::THISDOC{'CSS_LINES'} .= join('',@$rule_lines) . "\n" if (@$rule_lines);
281         $Texi2HTML::THISDOC{'CSS_LINES'} .= "-->\n</style>\n";
282     }
283     foreach my $ref (@CSS_REFS)
284     {
285         $Texi2HTML::THISDOC{'CSS_LINES'} .= "<link rel=\"stylesheet\" type=\"text/css\" title=\"$ref->{TITLE}\" href=\"$ref->{FILENAME}\">\n";
286     }
287     foreach my $ref (@Texi2HTML::Config::ALT_CSS_REFS)
288     {
289         $Texi2HTML::THISDOC{'CSS_LINES'} .= "<link rel=\"alternate stylesheet\" type=\"text/css\" href=\"$ref->{FILENAME}\" title=\"$ref->{TITLE}\">\n";
290     }
291     $Texi2HTML::THISDOC{'CSS_LINES'} .= "<!--[if lte IE 7]>\n<link href=\"lilypond-ie-fixes.css\" rel=\"stylesheet\" type=\"text/css\">\n<![endif]-->\n";
292 }
293
294
295
296
297
298 #############################################################################
299 ###  SPLITTING BASED ON NUMBERED SECTIONS
300 #############################################################################
301
302 my $lastfilename;
303 my $docnr = 0;
304 my $node_to_filename_map = ();
305
306
307 # This function makes sure that files are only generated for numbered sections,
308 # but not for unnumbered ones. It is called after texi2html has done its own
309 # splitting and simply returns the filename for the node given as first argument
310 # Nodes with the same filename will be printed out to the same filename, so
311 # this really all we need. Also, make sure that the file names for sections
312 # are derived from the section title. We also might want to name the anchors
313 # according to node titles, which works by simply overriding the id element of
314 # the $element hash.
315 # If an external nodename<=>filename/anchor map file is found (loaded in
316 # lilypond_init_out, use the externally created values, otherwise use the
317 # same logic here.
318 sub lilypond_element_file_name($$$)
319 {
320   my $element = shift;
321   my $type = shift;
322   my $docu_name = shift;
323   my $docu_ext = $Texi2HTML::Config::EXTENSION;
324
325   my $node_name = main::remove_texi($element->{'node_ref'}->{'texi'});
326   # the snippets page does not use nodes for the snippets, so in this case
327   # we'll have to use the section name!
328   if ($node_name eq '') {
329     $node_name = main::remove_texi($element->{'texi'});
330   }
331
332   # If we have an entry in the section<=>filename map, use that one, otherwise
333   # generate the filename/anchor here. In the latter case, external manuals
334   # will not be able to retrieve the file name for xrefs!!! Still, I already
335   # had that code, so I'll leave it in in case something goes wrong with the
336   # extract_texi_filenames.py script in the lilypond build process!
337   if (exists ($node_to_filename_map->{$node_name})) {
338     (my $filename, my $anchor) = @{$node_to_filename_map->{$node_name}};
339     $filename .= ".$docu_ext" if (defined($docu_ext));
340
341     # unnumbered sections (except those at top-level!) always go to the same
342     # file as the previous numbered section
343     if (not ($element->{number}) and not ($lastfilename eq '') and ($element->{level} > 1)) {
344       $filename = $lastfilename;
345     }
346     if (($filename eq $lastfilename)) {
347       $$element{doc_nr} = $docnr;
348     } else {
349       $docnr += 1;
350       $$element{doc_nr} = $docnr;
351       $lastfilename = $filename;
352     }
353     return $filename;
354
355   } elsif ($type eq "top" or $type eq "toc" or $type eq "doc" or $type eq "stoc" or $type eq "foot" or $type eq "about") {
356     return;
357   } else {
358     print STDERR "WARNING: Node '$node_name' was NOT found in the map\n"
359         unless ($node_name eq '') or ($element->{'tag'} eq 'unnumberedsec')
360                or ($node_name =~ /NOT REALLY USED/);
361
362     # Numbered sections will get a filename Node_title, unnumbered sections will use
363     # the file name of the previous numbered section:
364     if (($element->{number}) or ($lastfilename eq '') or ($element->{level} == 1)) {
365       # normalize to the same file name as texinfo
366       if ($element->{translationof}) {
367         $node_name = main::remove_texi($element->{translationof});
368       }
369       my $filename = texinfo_file_name($node_name);
370       $filename .= ".$docu_ext" if (defined($docu_ext));
371       $docnr += 1;
372       $$element{doc_nr} = $docnr;
373       $lastfilename = $filename;
374       return $filename;
375     } else {
376       $$element{doc_nr} = $docnr;
377       return $lastfilename;
378     }
379   }
380
381   return;
382 }
383
384 sub lilypond_element_target_name($$$)
385 {
386   my $element = shift;
387   my $target = shift;
388   my $id = shift;
389   # Target is based on node name (or sec name for secs without node attached)
390   my $node_name = main::remove_texi($element->{'node_ref'}->{'texi'});
391   if ($node_name eq '') {
392     $node_name = main::remove_texi($element->{'texi'});
393   }
394
395   # If we have an entry in the section<=>filename map, use that one, otherwise
396   # generate the anchor here.
397   if (exists ($node_to_filename_map->{$node_name})) {
398     (my $filename, $target) = @{$node_to_filename_map->{$node_name}};
399   } else {
400     my $anchor = $node_name;
401     if ($element->{translationof}) {
402       $anchor = main::remove_texi($element->{translationof});
403     }
404     # normalize to the same file name as texinfo
405     $target = texinfo_file_name($anchor);
406   }
407   # TODO: Once texi2html correctly prints out the target and not the id for
408   #       the sections, change this back to ($id, $target)
409   return ($target, $target);
410 }
411
412
413 ## Load the map file for the corrently processed texi file. We do this
414 #  using a command init handler, since texi2html does not have any
415 #  other hooks that are called after THISDOC is filled but before phase 2
416 #  of the texi2html conversion.
417 sub lilypond_init_map ()
418 {
419     my ($docu_dir, $docu_name) = split_texi_filename ($Texi2HTML::THISDOC{'input_file_name'});
420     my $map_filename = main::locate_include_file ("${docu_name}.$Texi2HTML::THISDOC{current_lang}.xref-map")
421         || main::locate_include_file ("${docu_name}.xref-map");
422     $node_to_filename_map = load_map_file ($map_filename);
423 }
424 push @Texi2HTML::Config::command_handler_init, \&lilypond_init_map;
425
426
427
428 #############################################################################
429 ###  CLEANER LINK TITLE FOR EXTERNAL REFS
430 #############################################################################
431
432 # The default formatting of external refs returns e.g.
433 # "(lilypond-internals)Timing_translator", so we remove all (...) from the
434 # file_and_node argument. Also, we want only a very simple format, so we don't
435 # even call the default handler!
436 sub lilypond_external_ref($$$$$$)
437 {
438   my $type = shift;
439   my $section = shift;
440   my $book = shift;
441   my $file_node = shift;
442   my $href = shift;
443   my $cross_ref = shift;
444
445   my $displaytext = '';
446
447   # 1) if we have a cross ref name, that's the text to be displayed:
448   # 2) For the top node, use the (printable) name of the manual, unless we
449   #    have an explicit cross ref name
450   # 3) In all other cases use the section name
451   if ($cross_ref ne '') {
452     $displaytext = $cross_ref;
453   } elsif (($section eq '') or ($section eq 'Top')) {
454     $displaytext = $book;
455   } else {
456     $displaytext = $section;
457   }
458
459   $displaytext = &$anchor('', $href, $displaytext) if ($displaytext ne '');
460   return &$I('%{node_file_href}', { 'node_file_href' => $displaytext });
461
462 #  Default: format as "see <a ..>NODE</a> section 'SECTION' in BOOK". We don't want this!
463 #   return t2h_default_external_ref($type, $section, $book, $file_node, $href, $cross_ref);
464 }
465
466
467
468
469
470 #############################################################################
471 ###  HANDLING TRANSLATED SECTIONS: handle @translationof, secname<->filename
472 ###                  map stored on disk, xrefs in other manuals load that map
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     # the @translationof macro provides the original English section title,
487     # which should be used for file/anchor naming, while the title will be
488     # translated to each language
489     # It is already used by extract_texi_filenames.py, so this should not be
490     # necessary here at all. Still, I'll leave the code in just in case the
491     # python script messed up ;-)
492     if ($pass == 1 and $macro eq "translationof") {
493       if (ref($state->{'element'}) eq 'HASH') {
494         $state->{'element'}->{'translationof'} = main::normalise_space($line);
495       }
496       return ('', 1, undef, undef);
497     } else {
498       return t2h_default_unknown($macro, $line, $pass, $stack, $state);
499     }
500 }
501
502
503
504
505 my %translated_books = ();
506 # Construct a href to an external source of information.
507 # node is the node with texinfo @-commands
508 # node_id is the node transliterated and transformed as explained in the
509 #         texinfo manual
510 # node_xhtml_id is the node transformed such that it is unique and can
511 #     be used to make an html cross ref as explained in the texinfo manual
512 # file is the file in '(file)node'
513 sub lilypond_external_href($$$)
514 {
515   my $node = shift;
516   my $node_id = shift;
517   my $node_hxmlt_id = shift;
518   my $file = shift;
519   my $original_func = \&t2h_default_external_href;
520
521   # 1) Keep a hash of book->section_map
522   # 2) if not file in keys hash => try to load the map (assign empty map if
523   #    non-existent => will load only once!)
524   # 3) if node in the section=>(file, anchor) map, replace node_id and
525   #    node_xhtml_id by the map's values
526   # 4) call the t2h_default_external_href with these values (or the old ones if not found)
527
528   if (($node_id ne '') and defined($file) and ($node_id ne 'Top')) {
529     my $map_name = $file;
530     $map_name =~ s/-big-page//;
531
532     # Load the map if we haven't done so already
533     if (!exists($translated_books{$map_name})) {
534       my ($docu_dir, $docu_name) = split_texi_filename ($Texi2HTML::THISDOC{'input_file_name'});
535       my $map_filename = main::locate_include_file ("${map_name}.$Texi2HTML::THISDOC{current_lang}.xref-map")
536           || main::locate_include_file ("${map_name}.xref-map");
537       $translated_books{$map_name} = load_map_file ($map_filename);
538     }
539
540     # look up translation. use these values instead of the old filename/anchor
541     my $section_name_map = $translated_books{$map_name};
542     my $node_text = main::remove_texi($node);
543     if (defined($section_name_map->{$node_text})) {
544       ($node_id, $node_hxmlt_id) = @{$section_name_map->{$node_text}};
545     } else {
546       print STDERR "WARNING: Unable to find node '$node_text' in book $map_name.\n";
547     }
548   }
549
550   if (defined $file) {
551     return &$original_func($node, $node_id, $node_hxmlt_id, $file);
552   } else {
553     return &$original_func($node, $node_id, $node_hxmlt_id);
554   }
555 }
556
557
558
559
560
561 #############################################################################
562 ###  CUSTOM TOC FOR EACH PAGE (in a frame on the left)
563 #############################################################################
564
565 my $page_toc_depth = 2;
566 my @default_toc = [];
567
568
569 # Initialize the toc_depth to 1 if the command-line option -D=short_toc is given
570 sub lilypond_init_toc_depth ()
571 {
572   if (exists($main::value{'short_toc'}) and not exists($main::value{'bigpage'})) {
573     $page_toc_depth = 1;
574   }
575 }
576 # We can't use a command-handler, because texinfo variables are not yet 
577 # available at that stage:
578 # push @Texi2HTML::Config::command_handler_init, \&lilypond_init_toc_depth;
579
580 # Initialize the toc_depth to 1 if the command-line option -D=short_toc is given
581 sub lilypond_init_out()
582 {
583   my $rval = t2h_default_init_out();
584   lilypond_init_toc_depth ();
585   return $rval;
586 }
587
588 # recursively generate the TOC entries for the element and its children (which
589 # are only shown up to maxlevel. All ancestors of the current element are also
590 # shown with their immediate children, irrespective of their level.
591 # Unnumbered entries are only printed out if they are at top-level or 2nd level 
592 # or their parent element is an ancestor of the currently viewed node.
593 # The conditions to call this method to print the entry for a child node is:
594 # -) the parent is an ancestor of the current page node
595 # -) the parent is a numbered element at top-level toplevel (i.e. show numbered 
596 #    and unnumbered 2nd-level children of numbered nodes)
597 # -) the child element is a numbered node below level maxlevel
598 sub generate_ly_toc_entries($$$)
599 {
600   my $element = shift;
601   my $element_path = shift;
602   my $maxlevel = shift;
603   # Skip undefined sections, plus all sections generated by index splitting
604   return() if (not defined($element) or exists($element->{'index_page'}));
605   my @result = ();
606   my $level = $element->{'toc_level'};
607   my $is_parent_of_current = $element->{'id'} && $element_path->{$element->{'id'}};
608   my $ind = '  ' x $level;
609   my $this_css_class = $is_parent_of_current ? " class=\"toc_current\"" : "";
610
611   my $entry = "$ind<li$this_css_class>" . &$anchor ($element->{'tocid'}, "$element->{'file'}#$element->{'target'}",$element->{'text'});
612
613   push (@result, $entry);
614   my $children = $element->{'section_childs'};
615   if (defined($children) and (ref($children) eq "ARRAY")) {
616     my $force_children = $is_parent_of_current or ($level == 1 and $element->{'number'});
617     my @child_result = ();
618     foreach my $c (@$children) {
619       my $is_numbered_child = defined ($c->{'number'});
620       my $below_maxlevel = $c->{'toc_level'} le $maxlevel;
621       if ($force_children or ($is_numbered_child and $below_maxlevel)) {
622         my @child_res = generate_ly_toc_entries($c, $element_path, $maxlevel);
623         push (@child_result, @child_res);
624       }
625     }
626     # if no child nodes were generated, e.g. for the index, where expanded pages
627     # are ignored, don't generate a list at all...
628     if (@child_result) {
629       push (@result, "\n$ind<ul$NO_BULLET_LIST_ATTRIBUTE>\n");
630       push (@result, @child_result);
631       push (@result, "$ind</ul>\n");
632     }
633   }
634   push (@result, "$ind</li>\n");
635   return @result;
636 }
637
638
639 # Print a customized TOC, containing only the first two levels plus the whole
640 # path to the current page
641 sub lilypond_generate_page_toc_body($)
642 {
643     my $element = shift;
644     my $current_element = $element;
645     my %parentelements;
646     $parentelements{$element->{'id'}} = 1;
647     # Find the path to the current element
648     while ( defined($current_element->{'sectionup'}) and
649            ($current_element->{'sectionup'} ne $current_element) )
650     {
651       $parentelements{$current_element->{'sectionup'}->{'id'}} = 1
652               if ($current_element->{'sectionup'}->{'id'} ne '');
653       $current_element = $current_element->{'sectionup'};
654     }
655     return () if not defined($current_element);
656     # Create the toc entries recursively
657     my @toc_entries = ("<div class=\"contents\">\n", "<ul$NO_BULLET_LIST_ATTRIBUTE>\n");
658     my $children = $current_element->{'section_childs'};
659     foreach ( @$children ) {
660       push (@toc_entries, generate_ly_toc_entries($_, \%parentelements, $page_toc_depth));
661     }
662     push (@toc_entries, "</ul>\n");
663     push (@toc_entries, "</div>\n");
664     return @toc_entries;
665 }
666
667 sub lilypond_print_toc_div ($$)
668 {
669   my $fh = shift;
670   my $tocref = shift;
671   my @lines = @$tocref;
672   # use default TOC if no custom lines have been generated
673   @lines = @default_toc if (not @lines);
674   if (@lines) {
675   
676     print $fh "\n\n<div id=\"tocframe\">\n";
677     
678     # Remove the leading "GNU LilyPond --- " from the manual title
679     my $topname = $Texi2HTML::NAME{'Top'};
680     $topname =~ s/^GNU LilyPond(:| &[mn]dash;) //;
681     
682     # construct the top-level Docs index (relative path and including language!)
683     my $lang = $Texi2HTML::THISDOC{current_lang};
684     if ($lang and $lang ne "en") {
685       $lang .= ".";
686     } else {
687       $lang = "";
688     }
689     my $reldir = "";
690     $reldir = "../" if ($Texi2HTML::Config::SPLIT eq 'section');
691     my $uplink = $reldir."index.${lang}html";
692
693     print $fh "<p class=\"toc_uplink\"><a href=\"$uplink\" 
694          title=\"Documentation Index\">&lt;&lt; " .
695          &ly_get_string ('Back to Documentation Index') .
696          "</a></p>\n";
697
698     print $fh '<h4 class="toc_header"> ' . &$anchor('',
699                                     $Texi2HTML::HREF{'Top'},
700                                     $topname,
701                                     'title="Start of the manual"'
702                                    ) . "</h4>\n";
703     foreach my $line (@lines) {
704       print $fh $line;
705     }
706     print $fh "</div>\n\n";
707   }
708 }
709
710 # Create the custom TOC for this page (partially folded, current page is
711 # highlighted) and store it in a global variable. The TOC is written out after
712 # the html contents (but positioned correctly using CSS), so that browsers with
713 # css turned off still show the contents first.
714 our @this_page_toc = ();
715 sub lilypond_print_element_header
716 {
717   my $first_in_page = shift;
718   my $previous_is_top = shift;
719   if ($first_in_page and not @this_page_toc) {
720     if (defined($Texi2HTML::THIS_ELEMENT)) {
721       # Create the TOC for this page
722       @this_page_toc = lilypond_generate_page_toc_body($Texi2HTML::THIS_ELEMENT);
723     }
724   }
725   return T2H_DEFAULT_print_element_header( $first_in_page, $previous_is_top);
726 }
727
728 # Generate the HTML output for the TOC
729 sub lilypond_toc_body($)
730 {
731     my $elements_list = shift;
732     # Workaround: Initialize the toc-depth here, because init_out is called
733     # only later in texi2html 1.83
734     lilypond_init_toc_depth ();
735     # Generate a default TOC for pages without THIS_ELEMENT
736     @default_toc = lilypond_generate_page_toc_body(@$elements_list[0]);
737     return T2H_GPL_toc_body($elements_list);
738 }
739
740 # Print out the TOC in a <div> at the beginning of the page
741 sub lilypond_print_page_head($)
742 {
743     my $fh = shift;
744     T2H_DEFAULT_print_page_head($fh);
745     print $fh "<div id=\"main\">\n";
746 }
747
748 # Print out the TOC in a <div> at the end of th page, which will be formatted as a
749 # sidebar mimicking a TOC frame
750 sub print_lilypond_page_foot($)
751 {
752   my $fh = shift;
753   my $program_string = &$program_string();
754 #   print $fh "<p><font size='-1'>$program_string</font><br>$PRE_BODY_CLOSE</p>\n";
755   print $fh "<!-- FOOTER -->\n\n";
756   print $fh "<!-- end div#main here -->\n</div>\n\n";
757
758   # Print the TOC frame and reset the TOC:
759   lilypond_print_toc_div ($fh, \@this_page_toc);
760   @this_page_toc = ();
761
762   # Close the page:
763   print $fh "</body>\n</html>\n";
764 }
765
766
767
768
769
770 #############################################################################
771 ###  NICER / MORE FLEXIBLE NAVIGATION PANELS
772 #############################################################################
773
774 sub get_navigation_text
775 {
776   my $button = shift;
777   my $text = $NAVIGATION_TEXT{$button};
778   if ( ($button eq 'Back') or ($button eq 'FastBack') ) {
779     $text = $text . $Texi2HTML::NODE{$button} . "&nbsp;";
780   } elsif ( ($button eq 'Forward') or ($button eq 'FastForward') ) {
781     $text = "&nbsp;" . $Texi2HTML::NODE{$button} . $text;
782   } elsif ( $button eq 'Up' ) {
783     $text = "&nbsp;".$text.":&nbsp;" . $Texi2HTML::NODE{$button} . "&nbsp;";
784   }
785   return $text;
786 }
787
788
789 # Don't automatically create left-aligned table cells for every link, but
790 # instead create a <td> only on an appropriate '(left|right|center)-aligned-cell-n'
791 # button text. It's alignment as well as the colspan will be taken from the
792 # name of the button. Also, add 'newline' button text to create a new table
793 # row. The texts of the buttons are generated by get_navigation_text and
794 # will contain the name of the next/previous section/chapter.
795 sub lilypond_print_navigation
796 {
797     my $buttons = shift;
798     my $vertical = shift;
799     my $spacing = 1;
800     my $result = "<table class=\"nav_table\">\n";
801
802     $result .= "<tr>" unless $vertical;
803     my $beginofline = 1;
804     foreach my $button (@$buttons)
805     {
806         $result .= qq{<tr valign="top" align="left">\n} if $vertical;
807         # Allow (left|right|center)-aligned-cell and newline as buttons!
808         if ( $button =~ /^(.*)-aligned-cell-(.*)$/ )
809         {
810           $result .= qq{</td>} unless $beginofline;
811           $result .= qq{<td valign="middle" align="$1" colspan="$2">};
812           $beginofline = 0;
813         }
814         elsif ( $button eq 'newline' )
815         {
816           $result .= qq{</td>} unless $beginofline;
817           $result .= qq{</tr>};
818           $result .= qq{<tr>};
819           $beginofline = 1;
820
821         }
822         elsif (ref($button) eq 'CODE')
823         {
824             $result .= &$button($vertical);
825         }
826         elsif (ref($button) eq 'SCALAR')
827         {
828             $result .= "$$button" if defined($$button);
829         }
830         elsif (ref($button) eq 'ARRAY')
831         {
832             my $text = $button->[1];
833             my $button_href = $button->[0];
834             # verify that $button_href is simple text and text is a reference
835             if (defined($button_href) and !ref($button_href) 
836                and defined($text) and (ref($text) eq 'SCALAR') and defined($$text))
837             {             # use given text
838                 if ($Texi2HTML::HREF{$button_href})
839                 {
840                   my $anchor_attributes = '';
841                   if ($USE_ACCESSKEY and (defined($BUTTONS_ACCESSKEY{$button_href})) and ($BUTTONS_ACCESSKEY{$button_href} ne ''))
842                   {
843                       $anchor_attributes = "accesskey=\"$BUTTONS_ACCESSKEY{$button_href}\"";
844                   }
845                   if ($USE_REL_REV and (defined($BUTTONS_REL{$button_href})) and ($BUTTONS_REL{$button_href} ne ''))
846                   {
847                       $anchor_attributes .= " rel=\"$BUTTONS_REL{$button_href}\"";
848                   }
849                   $result .=  "" .
850                         &$anchor('',
851                                     $Texi2HTML::HREF{$button_href},
852                                     get_navigation_text($$text),
853                                     $anchor_attributes
854                                    );
855                 }
856                 else
857                 {
858                   $result .=  get_navigation_text($$text);
859                 }
860             }
861         }
862         elsif ($button eq ' ')
863         {                       # handle space button
864             $result .= 
865                 ($ICONS && $ACTIVE_ICONS{' '}) ?
866                     &$button_icon_img($BUTTONS_NAME{$button}, $ACTIVE_ICONS{' '}) :
867                         $NAVIGATION_TEXT{' '};
868             #next;
869         }
870         elsif ($Texi2HTML::HREF{$button})
871         {                       # button is active
872             my $btitle = $BUTTONS_GOTO{$button} ?
873                 'title="' . $BUTTONS_GOTO{$button} . '"' : '';
874             if ($USE_ACCESSKEY and (defined($BUTTONS_ACCESSKEY{$button})) and ($BUTTONS_ACCESSKEY{$button} ne ''))
875             {
876                 $btitle .= " accesskey=\"$BUTTONS_ACCESSKEY{$button}\"";
877             }
878             if ($USE_REL_REV and (defined($BUTTONS_REL{$button})) and ($BUTTONS_REL{$button} ne ''))
879             {
880                 $btitle .= " rel=\"$BUTTONS_REL{$button}\"";
881             }
882             if ($ICONS && $ACTIVE_ICONS{$button})
883             {                   # use icon
884                 $result .= '' .
885                     &$anchor('',
886                         $Texi2HTML::HREF{$button},
887                         &$button_icon_img($BUTTONS_NAME{$button},
888                                    $ACTIVE_ICONS{$button},
889                                    $Texi2HTML::SIMPLE_TEXT{$button}),
890                         $btitle
891                       );
892             }
893             else
894             {                   # use text
895                 $result .= 
896                     '[' .
897                         &$anchor('',
898                                     $Texi2HTML::HREF{$button},
899                                     get_navigation_text($button),
900                                     $btitle
901                                    ) .
902                                        ']';
903             }
904         }
905         else
906         {                       # button is passive
907             $result .= 
908                 $ICONS && $PASSIVE_ICONS{$button} ?
909                     &$button_icon_img($BUTTONS_NAME{$button},
910                                           $PASSIVE_ICONS{$button},
911                                           $Texi2HTML::SIMPLE_TEXT{$button}) :
912
913                                               "[" . get_navigation_text($button) . "]";
914         }
915         $result .= "</td>\n" if $vertical;
916         $result .= "</tr>\n" if $vertical;
917     }
918     $result .= "</td>" unless $beginofline;
919     $result .= "</tr>" unless $vertical;
920     $result .= "</table>\n";
921     return $result;
922 }
923
924
925 @Texi2HTML::Config::SECTION_BUTTONS =
926     ('left-aligned-cell-1', 'FastBack',
927      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
928      'right-aligned-cell-1', 'FastForward',
929      'newline',
930      'left-aligned-cell-2', 'Back',
931      'center-aligned-cell-1', 'Up',
932      'right-aligned-cell-2', 'Forward'
933     );
934
935 # buttons for misc stuff
936 @Texi2HTML::Config::MISC_BUTTONS = ('center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About');
937
938 # buttons for chapter file footers
939 # (and headers but only if SECTION_NAVIGATION is false)
940 @Texi2HTML::Config::CHAPTER_BUTTONS =
941     ('left-aligned-cell-1', 'FastBack',
942      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
943      'right-aligned-cell-1', 'FastForward',
944     );
945
946 # buttons for section file footers
947 @Texi2HTML::Config::SECTION_FOOTER_BUTTONS =
948     ('left-aligned-cell-1', 'FastBack',
949      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
950      'right-aligned-cell-1', 'FastForward',
951      'newline',
952      'left-aligned-cell-2', 'Back',
953      'center-aligned-cell-1', 'Up',
954      'right-aligned-cell-2', 'Forward'
955     );
956
957 @Texi2HTML::Config::NODE_FOOTER_BUTTONS =
958     ('left-aligned-cell-1', 'FastBack',
959      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
960      'right-aligned-cell-1', 'FastForward',
961      'newline',
962      'left-aligned-cell-2', 'Back',
963      'center-aligned-cell-1', 'Up',
964      'right-aligned-cell-2', 'Forward'
965     );
966
967
968
969
970
971 #############################################################################
972 ###  FOOTNOTE FORMATTING
973 #############################################################################
974
975 # Format footnotes in a nicer way: Instead of printing the number in a separate
976 # (nr) heading line, use the standard way of prepending <sup>nr</sup> immediately
977 # before the fn text.
978
979
980 # The following code is copied from texi2html's examples/makeinfo.init and
981 # should be updated when texi2html makes some changes there!
982
983 my $makekinfo_like_footnote_absolute_number = 0;
984
985 sub makeinfo_like_foot_line_and_ref($$$$$$$$)
986 {
987     my $foot_num = shift;
988     my $relative_num = shift;
989     my $footid = shift;
990     my $docid = shift;
991     my $from_file = shift;
992     my $footnote_file = shift;
993     my $lines = shift;
994     my $state = shift;
995
996     $makekinfo_like_footnote_absolute_number++;
997
998     # this is a bit obscure, this allows to add an anchor only if formatted
999     # as part of the document.
1000     $docid = '' if ($state->{'outside_document'} or $state->{'multiple_pass'});
1001
1002     if ($from_file eq $footnote_file)
1003     {
1004         $from_file = $footnote_file = '';
1005     }
1006
1007     my $foot_anchor = "<sup>" . &$anchor($docid, "$footnote_file#$footid", $relative_num) . "</sup>";
1008     $foot_anchor = &$anchor($docid, "$footnote_file#$footid", "($relative_num)") if ($state->{'preformatted'});
1009
1010 #    unshift @$lines, "<li>";
1011 #    push @$lines, "</li>\n";
1012     return ($lines, $foot_anchor);
1013 }
1014
1015 sub makeinfo_like_foot_lines($)
1016 {
1017     my $lines = shift;
1018     unshift @$lines, "<hr>\n<h4>$Texi2HTML::I18n::WORDS->{'Footnotes_Title'}</h4>\n";
1019 #<ol type=\"1\">\n";
1020 #    push @$lines, "</ol>";
1021     return $lines;
1022 }
1023
1024 my %makekinfo_like_paragraph_in_footnote_nr;
1025
1026 sub makeinfo_like_paragraph ($$$$$$$$$$$$$)
1027 {
1028     my $text = shift;
1029     my $align = shift;
1030     my $indent = shift;
1031     my $paragraph_command = shift;
1032     my $paragraph_command_formatted = shift;
1033     my $paragraph_number = shift;
1034     my $format = shift;
1035     my $item_nr = shift;
1036     my $enumerate_style = shift;
1037     my $number = shift;
1038     my $command_stack_at_end = shift;
1039     my $command_stack_at_begin = shift;
1040     my $state = shift;
1041 #print STDERR "format: $format\n" if (defined($format));
1042 #print STDERR "paragraph @$command_stack_at_end; @$command_stack_at_begin\n";
1043     $paragraph_command_formatted = '' if (!defined($paragraph_command_formatted) or
1044           exists($special_list_commands{$format}->{$paragraph_command}));
1045     return '' if ($text =~ /^\s*$/);
1046     foreach my $style(t2h_collect_styles($command_stack_at_begin))
1047     {
1048        $text = t2h_begin_style($style, $text);
1049     }
1050     foreach my $style(t2h_collect_styles($command_stack_at_end))
1051     {
1052        $text = t2h_end_style($style, $text);
1053     }
1054     if (defined($paragraph_number) and defined($$paragraph_number))
1055     {
1056          $$paragraph_number++;
1057          return $text  if (($format eq 'itemize' or $format eq 'enumerate') and
1058             ($$paragraph_number == 1));
1059     }
1060     my $open = '<p';
1061     if ($align)
1062     {
1063         $open .= " align=\"$paragraph_style{$align}\"";
1064     }
1065     my $footnote_text = '';
1066     if (defined($command_stack_at_begin->[0]) and $command_stack_at_begin->[0] eq 'footnote')
1067     {
1068         my $state = $Texi2HTML::THISDOC{'state'};
1069         $makekinfo_like_paragraph_in_footnote_nr{$makekinfo_like_footnote_absolute_number}++;
1070         if ($makekinfo_like_paragraph_in_footnote_nr{$makekinfo_like_footnote_absolute_number} <= 1)
1071         {
1072            $open.=' class="footnote"';
1073            my $document_file = $state->{'footnote_document_file'};
1074            if ($document_file eq $state->{'footnote_footnote_file'})
1075            {
1076                $document_file = '';
1077            }
1078            my $docid = $state->{'footnote_place_id'};
1079            my $doc_state = $state->{'footnote_document_state'};
1080            $docid = '' if ($doc_state->{'outside_document'} or $doc_state->{'multiple_pass'});
1081            my $foot_label = &$anchor($state->{'footnote_footnote_id'},
1082                  $document_file . "#$state->{'footnote_place_id'}",
1083                  "$state->{'footnote_number_in_page'}");
1084            $footnote_text = "<small>[${foot_label}]</small> ";
1085         }
1086     }
1087     return $open.'>'.$footnote_text.$text.'</p>';
1088 }
1089
1090
1091 #############################################################################
1092 ###  OTHER SETTINGS
1093 #############################################################################
1094
1095 # For split pages, use index.html as start page!
1096 if ($Texi2HTML::Config::SPLIT eq 'section') {
1097   $Texi2HTML::Config::TOP_FILE = 'index.html';
1098 }
1099
1100
1101 return 1;