]> git.donarmstrong.com Git - lilypond.git/blob - lilypond-texi2html.init
51bef9a2385a5484a96197755ff47b84112139b8
[lilypond.git] / lilypond-texi2html.init
1 #!/usr/bin/env perl
2
3 # {
4 package Texi2HTML::Config;
5
6 my $lastfilename;
7 my $docnr = 0;
8 my $page_toc_depth = 2;
9
10 use Data::Dumper;
11 $Data::Dumper::Maxdepth = 2;
12
13 sub print_element_info($) 
14 {
15   my $element = shift;
16   print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
17   print "Element: $element\n";
18   print Dumper($element);
19 }
20
21
22 # This function makes sure that files are only generated for numbered sections,
23 # but not for unnumbered ones. It is called after texi2html has done its own
24 # splitting and simply returns the filename for the node given as first argument
25 # Nodes with the same filename will be printed out to the same filename, so 
26 # this really all we need. Also, make sure that the file names for sections
27 # are derived from the section title. We also might want to name the anchors
28 # according to node titles, which works by simply overriding the id element of 
29 # the $element hash.
30 sub split_at_numbered_sections($$$)
31 {
32   my $element = shift;
33   my $type = shift;
34   my $docu_name = shift;
35   my $docu_ext = $Texi2HTML::Config::EXTENSION;
36
37 #   if ($$element{number} eq "1.1") {
38 #     print_element_info ($element);
39 #   }
40
41   # TOC, footer, about etc. are called with undefined $element and $type == "toc"|"stoc"|"foot"|"about"
42   if ($type eq "toc" or $type eq "stoc" or $type eq "foot" or $type eq "about") {
43     return;
44   } else {
45     # derive the name of the anchor (i.e. the part after # in the links!), 
46     # don't use texi2html's SECx.x default!
47     my $anchor = main::remove_texi($$element{texi});
48     if ($$element{translationof}) {
49       $anchor = main::remove_texi($$element{translationof});
50     }
51     # FIXME: Use the same file name normalization as makeinfo does!!!
52     $anchor =~ tr/\ ?:'\\\(\)/-/d;
53     $$element{id} = $anchor;
54     # Numbered sections will get a filename Section_1.1.2, unnumbered sections will use 
55     # the file name of the previous numbered section:
56     if ($$element{number}) {
57       my $filename = $anchor;
58       $filename .= ".$docu_ext" if (defined($docu_ext));
59       $docnr += 1;
60       $$element{doc_nr} = $docnr;
61       $lastfilename = $filename;
62       return $filename;
63     } else {
64       $$element{doc_nr} = $docnr;
65       return $lastfilename;
66     }
67   }
68
69   return;
70 }
71
72
73 # Print a link in a menu. Since we split at sections, but still want access keys, 
74 # we have to duplicate this function here :-((((
75 sub lilypond_menu_link($$$$$$)
76 {
77     my $entry = shift;
78     my $state = shift;
79     my $href = shift;
80     my $node = shift;
81     my $name = shift;
82     my $ending = shift;
83     if (($entry eq '') or $NODE_NAME_IN_MENU or $state->{'preformatted'})
84     {
85         $name .= ':' if ($name ne '');
86         $entry = "$MENU_SYMBOL$name$node";
87     }
88     $menu_entry_index++;
89     my $accesskey;
90     $accesskey = "accesskey=\"$menu_entry_index\"" if ($USE_ACCESSKEY and ($menu_entry_index < 10));
91     $entry = &$anchor ('', $href, $entry, $accesskey) if (defined($href));
92     return $entry if ($SIMPLE_MENU); 
93     if ($state->{'preformatted'})
94     {
95         return '<tr><td>' . main::do_preformatted($entry . $ending, $state);
96     }
97     return "<tr><td align=\"left\" valign=\"top\">$entry</td><td>&nbsp;&nbsp;</td>";
98 }
99
100
101 # The default formatting of external refs returns e.g. 
102 # "(lilypond-internals)Timing_translator", while we simply want "Timing_translator".
103 # Solution: Remove all (...) from the file_and_node argument before calling
104 # the default handler!
105 sub lilypond_external_ref($$$$$$)
106 {
107   my $type = shift;
108   my $section = shift;
109   my $book = shift;
110   my $file_node = shift;
111   my $href = shift;
112   my $cross_ref = shift;
113
114   $file_node =~ s/\(.*\)//;
115   return t2h_default_external_ref($type, $section, $book, $file_node, $href, $cross_ref);
116 }
117
118
119 # recursively generate the TOC entries for the element and its children (which
120 # are only shown up to maxlevel. All ancestors of the current element are also 
121 # shown with their immediate children, irrespective of their level.
122 sub generate_ly_toc_entries($$$)
123 {
124   my $element = shift;
125   my $element_path = shift;
126   my $maxlevel = shift;
127   # Skip undefined sections, plus all sections generated by index splitting
128   return() if (not defined($element) or exists($element->{'index_page'}));
129   my @result = ();
130   my $level = $element->{'toc_level'};
131   my $is_parent_of_current = $element_path->{$element->{'number'}};
132   my $print_children = ( ($level < $maxlevel) or $is_parent_of_current );
133   my $ind = '  ' x $level;
134   my $this_css_class = $is_parent_of_current ? " class=\"toc_current\"" : "";
135
136   my $entry = "$ind<li$this_css_class>" . &$anchor ($element->{'tocid'}, "$element->{'file'}#$element->{'id'}",$element->{'text'});
137
138   my $children = $element->{'section_childs'};
139   if ( $print_children and defined($children) and (ref($children) eq "ARRAY") ) {
140     push (@result, $entry);
141     my @child_result = ();
142     foreach (@$children) {
143       push (@child_result, generate_ly_toc_entries($_, $element_path, $maxlevel));
144     }
145     # if no child nodes were generated, e.g. for the index, where expanded pages
146     # are ignored, don't generate a list at all...
147     if (@child_result) {
148       push (@result, "$ind<ul$NO_BULLET_LIST_ATTRIBUTE>");
149       push (@result, @child_result);
150       push (@result, "$ind</ul></li>\n");
151     }
152   } else {
153     push (@result, $entry . "</li>\n");
154   }
155   return @result;
156 }
157
158
159 # Print a customized TOC, containing only the first two levels plus the whole
160 # path to the current page
161 sub lilypond_generate_page_toc_body($)
162 {
163     my $element = shift;
164     my $current_element = $element;
165     my %parentelements;
166     $parentelements{$element->{'number'}} = 1;
167     # Find the path to the current element
168     while ( defined($current_element->{'sectionup'}) and 
169            ($current_element->{'sectionup'} ne $current_element) )
170     {
171       $parentelements{$current_element->{'sectionup'}->{'number'}} = 1
172               if ($current_element->{'sectionup'}->{'number'} ne '');
173       $current_element = $current_element->{'sectionup'};
174     }
175     return () if not defined($current_element);
176     # Create the toc entries recursively
177     my @toc_entries = ("<div class=\"contents\">", "<ul$NO_BULLET_LIST_ATTRIBUTE>");
178     my $children = $current_element->{'section_childs'};
179     foreach ( @$children ) {
180       push (@toc_entries, generate_ly_toc_entries($_, \%parentelements, $page_toc_depth));
181     }
182     push (@toc_entries, "</ul>");
183     push (@toc_entries, "</div>");
184     return @toc_entries;
185 }
186
187 my @this_page_toc = ();
188
189 sub lilypond_print_element_header
190 {
191   my $fh = shift;
192   my $first_in_page = shift;
193   my $previous_is_top = shift;
194   if ($first_in_page and not @this_page_toc) {
195     if (defined($Texi2HTML::THIS_ELEMENT)) {
196       # Create the TOC for this page
197       @this_page_toc = lilypond_generate_page_toc_body($Texi2HTML::THIS_ELEMENT);
198     }
199   }
200   return T2H_DEFAULT_print_element_header( $fh, $first_in_page, $previous_is_top);
201 }
202
203
204
205
206
207
208
209
210
211
212 # Print out the TOC in a <div> at the end of th page, which will be formatted as a
213 # sidebar mimicking a TOC frame
214 sub print_lilypond_page_foot($)
215 {
216   my $fh = shift;
217 #   my @lines = @{$Texi2HTML::OVERVIEW};
218 #   my $lines = $Texi2HTML::TOC_LINES;
219   my @lines = @this_page_toc;
220   if (not @lines) {
221     print "We have no toc lines, generate generic ones for main::element_top::\n";
222     @lines = @$Texi2HTML::TOC_LINES;
223   }
224   if (@lines) {
225     print $fh "<div id=\"tocframe\">";
226     print $fh '<h4> ' . $Texi2HTML::NAME{'Contents'}  . "</h4>\n";
227     foreach my $line (@lines) {
228       print $fh $line;
229     }
230     print $fh "</div>";
231     @this_page_toc = ();
232   }
233   T2H_DEFAULT_print_page_foot($fh);
234 }
235
236
237
238
239
240
241
242 sub get_navigation_text
243 {
244   my $button = shift;
245   my $text = $NAVIGATION_TEXT{$button};
246   if ( ($button eq 'Back') or ($button eq 'FastBack') ) {
247     $text = $text . $Texi2HTML::NODE{$button} . "&nbsp;";
248   } elsif ( ($button eq 'Forward') or ($button eq 'FastForward') ) {
249     $text = "&nbsp;" . $Texi2HTML::NODE{$button} . $text;
250   } elsif ( $button eq 'Up' ) {
251     $text = "&nbsp;".$text.":&nbsp;" . $Texi2HTML::NODE{$button} . "&nbsp;";
252   }
253   return $text;
254 }
255
256
257 # Don't automatically create left-aligned table cells for every link, but 
258 # instead create a <td> only on an appropriate '(left|right|center)-aligned-cell-n'
259 # button text. It's alignment as well as the colspan will be taken from the
260 # name of the button. Also, add 'newline' button text to create a new table
261 # row. The texts of the buttons are generated by get_navigation_text and 
262 # will contain the name of the next/previous section/chapter.
263 sub lilypond_print_navigation
264 {
265     my $fh = shift;
266     my $buttons = shift;
267     my $vertical = shift;
268     my $spacing = 1;
269 #     print $fh '<table cellpadding="', $spacing, '" cellspacing="', $spacing,
270 #       "\" border=\"0\" class=\"nav_table\">\n";
271     print $fh "<table class=\"nav_table\">\n";
272
273     print $fh "<tr>" unless $vertical;
274     my $beginofline = 1;
275     foreach my $button (@$buttons)
276     {
277         print $fh qq{<tr valign="top" align="left">\n} if $vertical;
278         # Allow (left|right|center)-aligned-cell and newline as buttons!
279         if ( $button =~ /^(.*)-aligned-cell-(.*)$/ ) 
280         {
281           print $fh qq{</td>} unless $beginofline;
282           print $fh qq{<td valign="middle" align="$1" colspan="$2">};
283           $beginofline = 0;
284         } 
285         elsif ( $button eq 'newline' ) 
286         {
287           print $fh qq{</td>} unless $beginofline;
288           print $fh qq{</tr>};
289           print $fh qq{<tr>};
290           $beginofline = 1;
291
292         } 
293         elsif (ref($button) eq 'CODE')
294         {
295             &$button($fh, $vertical);
296         }
297         elsif (ref($button) eq 'SCALAR')
298         {
299             print $fh "$$button" if defined($$button);
300         }
301         elsif (ref($button) eq 'ARRAY')
302         {
303             my $text = $button->[1];
304             my $button_href = $button->[0];
305             # verify that $button_href is simple text and text is a reference
306             if (defined($button_href) and !ref($button_href) 
307                and defined($text) and (ref($text) eq 'SCALAR') and defined($$text))
308             {             # use given text
309                 if ($Texi2HTML::HREF{$button_href})
310                 {
311                   my $anchor_attributes = '';
312                   if ($USE_ACCESSKEY and (defined($BUTTONS_ACCESSKEY{$button_href})) and ($BUTTONS_ACCESSKEY{$button_href} ne ''))
313                   {
314                       $anchor_attributes = "accesskey=\"$BUTTONS_ACCESSKEY{$button_href}\"";
315                   }
316                   if ($USE_REL_REV and (defined($BUTTONS_REL{$button_href})) and ($BUTTONS_REL{$button_href} ne ''))
317                   {
318                       $anchor_attributes .= " rel=\"$BUTTONS_REL{$button_href}\"";
319                   }
320                   print $fh "" .
321                         &$anchor('',
322                                     $Texi2HTML::HREF{$button_href},
323                                     get_navigation_text($$text),
324                                     $anchor_attributes
325                                    );
326                 }
327                 else
328                 {
329                   print $fh get_navigation_text($$text);
330                 }
331             }
332         }
333         elsif ($button eq ' ')
334         {                       # handle space button
335             print $fh
336                 ($ICONS && $ACTIVE_ICONS{' '}) ?
337                     &$button_icon_img($BUTTONS_NAME{$button}, $ACTIVE_ICONS{' '}) :
338                         $NAVIGATION_TEXT{' '};
339             #next;
340         }
341         elsif ($Texi2HTML::HREF{$button})
342         {                       # button is active
343             my $btitle = $BUTTONS_GOTO{$button} ?
344                 'title="' . $BUTTONS_GOTO{$button} . '"' : '';
345             if ($USE_ACCESSKEY and (defined($BUTTONS_ACCESSKEY{$button})) and ($BUTTONS_ACCESSKEY{$button} ne ''))
346             {
347                 $btitle .= " accesskey=\"$BUTTONS_ACCESSKEY{$button}\"";
348             }
349             if ($USE_REL_REV and (defined($BUTTONS_REL{$button})) and ($BUTTONS_REL{$button} ne ''))
350             {
351                 $btitle .= " rel=\"$BUTTONS_REL{$button}\"";
352             }
353             if ($ICONS && $ACTIVE_ICONS{$button})
354             {                   # use icon
355                 print $fh '' .
356                     &$anchor('',
357                         $Texi2HTML::HREF{$button},
358                         &$button_icon_img($BUTTONS_NAME{$button},
359                                    $ACTIVE_ICONS{$button},
360                                    $Texi2HTML::SIMPLE_TEXT{$button}),
361                         $btitle
362                       );
363             }
364             else
365             {                   # use text
366                 print $fh
367                     '[' .
368                         &$anchor('',
369                                     $Texi2HTML::HREF{$button},
370                                     get_navigation_text ($button),
371                                     $btitle
372                                    ) .
373                                        ']';
374             }
375         }
376         else
377         {                       # button is passive
378             print $fh
379                 $ICONS && $PASSIVE_ICONS{$button} ?
380                     &$button_icon_img($BUTTONS_NAME{$button},
381                                           $PASSIVE_ICONS{$button},
382                                           $Texi2HTML::SIMPLE_TEXT{$button}) :
383
384                                               "[" . get_navigation_text($button) . "]";
385         }
386         print $fh "</td>\n" if $vertical;
387         print $fh "</tr>\n" if $vertical;
388     }
389     print $fh "</td>" unless $beginofline;
390     print $fh "</tr>" unless $vertical;
391     print $fh "</table>\n";
392 }
393
394
395 @Texi2HTML::Config::SECTION_BUTTONS =
396     ('left-aligned-cell-1', 'FastBack', 
397      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
398      'right-aligned-cell-1', 'FastForward',
399      'newline',
400      'left-aligned-cell-2', 'Back',
401      'center-aligned-cell-1', 'Up',
402      'right-aligned-cell-2', 'Forward'
403     );
404
405 # buttons for misc stuff
406 @Texi2HTML::Config::MISC_BUTTONS = ('center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About');
407
408 # buttons for chapter file footers
409 # (and headers but only if SECTION_NAVIGATION is false)
410 @Texi2HTML::Config::CHAPTER_BUTTONS =
411     ('left-aligned-cell-1', 'FastBack', 
412      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
413      'right-aligned-cell-1', 'FastForward',
414     );
415
416 # buttons for section file footers
417 @Texi2HTML::Config::SECTION_FOOTER_BUTTONS =
418     ('left-aligned-cell-1', 'FastBack', 
419      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
420      'right-aligned-cell-1', 'FastForward',
421      'newline',
422      'left-aligned-cell-2', 'Back',
423      'center-aligned-cell-1', 'Up',
424      'right-aligned-cell-2', 'Forward'
425     );
426
427 @Texi2HTML::Config::NODE_FOOTER_BUTTONS =
428     ('left-aligned-cell-1', 'FastBack', 
429      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
430      'right-aligned-cell-1', 'FastForward',
431      'newline',
432      'left-aligned-cell-2', 'Back',
433      'center-aligned-cell-1', 'Up',
434      'right-aligned-cell-2', 'Forward'
435     );
436
437 # $Texi2HTML::Config::SPLIT = 'section';
438 @Texi2HTML::Config::CSS_REFS = ("lilypond.css");
439 $Texi2HTML::Config::USE_ACCESSKEY = 1;
440 $Texi2HTML::Config::USE_LINKS = 1;
441 $Texi2HTML::Config::USE_REL_REV = 1;
442 $Texi2HTML::Config::element_file_name = \&split_at_numbered_sections;
443 $Texi2HTML::Config::print_element_header = \&lilypond_print_element_header;
444 $Texi2HTML::Config::print_page_foot = \&print_lilypond_page_foot;
445 $Texi2HTML::Config::print_navigation = \&lilypond_print_navigation;
446 $Texi2HTML::Config::external_ref = \&lilypond_external_ref;
447 $Texi2HTML::Config::menu_link = \&lilypond_menu_link;
448
449
450 # For split pages, use index(.lang).html as start page!
451 if ($Texi2HTML::Config::SPLIT == 'section') {
452   my $lng = $Texi2HTML::THISDOC{'current_lang'};
453   if ($lng and ($lng ne "en")) {
454     $Texi2HTML::Config::TOP_FILE = 'index.'.$lng.'.html';
455   } else {
456     $Texi2HTML::Config::TOP_FILE = 'index.html';
457   }
458 }
459
460 if ($Texi2HTML::THISDOC{'current_lang'}) {
461   $Texi2HTML::Config::EXTENSION = $Texi2HTML::THISDOC{'current_lang'} . "." . 
462         $docu_ext = $Texi2HTML::Config::EXTENSION;;
463 }
464
465
466
467 # Try to make use of @translationof to generate files according to the original
468 # English section title...
469 sub lilypond_unknown($$$$$)
470 {
471     my $macro = shift;
472     my $line = shift;
473     my $pass = shift;
474     my $stack = shift;
475     my $state = shift;
476
477     # the @translationof macro provides the original English section title, 
478     # which should be used for file/anchor naming, while the title will be
479     # translated to each language
480     if ($pass == 1 and $macro eq "translationof") {
481       if (ref($state->{'element'})=='HASH') {
482         $state->{'element'}->{'translationof'} = main::normalise_space($line);
483       }
484       return ('', true, undef, undef);
485     } else {
486       return t2h_default_unknown($macro, $line, $pass, $stack, $state);
487     }
488 }
489 $Texi2HTML::Config::unknown                  = \&lilypond_unknown;
490
491
492
493 return 1;