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