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