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