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