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