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