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