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