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