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