]> git.donarmstrong.com Git - lilypond.git/blobdiff - lilypond-texi2html.init
Merge branch 'master' of ssh://kainhofer@git.sv.gnu.org/srv/git/lilypond into dev...
[lilypond.git] / lilypond-texi2html.init
index e743d6f5512d83309b424a681dbafa8af8b16083..5d31b054b699efbefed164b702e0d6ff351a12d1 100644 (file)
@@ -16,7 +16,8 @@
 ###    to every page; implemented by:
 ###           lilypond_print_element_header -- building of the TOC
 ###           lilypond_toc_body -- generation of customized TOC output
-###           print_lilypond_page_foot -- output of the TOC
+###           lilypond_print_page_head -- start <div id="main">
+###           print_lilypond_page_foot -- closing id=main, output of footer & TOC
 ### -) External refs are formatted only as "Text of the node" (not as >>see
 ###    "NODE" section "SECTION" in "BOOK"<< like with default texi2html). Also,
 ###    the leading "(book-name)" is removed.
@@ -26,6 +27,8 @@
 ###    cells; For this, I heavily extend the texi2html code to allow for
 ###    differently aligned cells and for multi-line tables);
 ###    Implemented in lilypond_print_navigation
+### -) Different formatting than the default: example uses the same formatting
+###    as quote.
 ### -) Allow translated section titles: All section titles can be translated,
 ###    the original (English) title is associated with @translationof. This is
 ###    needed, because the file name / anchor is generated from the original
@@ -38,7 +41,7 @@
 ###           lilypond_unknown (handling of @translationof, in case 
 ###                             extract_texi_filenames.py messes up...)
 ###           split_at_numbered_sections (correct file name: use the map)
-###           lilypond_init_out (read in the externally created map from disk)
+###           lilypond_init_map (read in the externally created map from disk)
 ###           lilypond_external_href (load the map for xrefs, use the correct 
 ###                                   link target)
 ###
@@ -72,8 +75,14 @@ $Texi2HTML::Config::external_ref         = \&lilypond_external_ref;
 $Texi2HTML::Config::external_href        = \&lilypond_external_href;
 $Texi2HTML::Config::toc_body             = \&lilypond_toc_body;
 $Texi2HTML::Config::css_lines            = \&lilypond_css_lines;
-$Texi2HTML::Config::init_out             = \&lilypond_init_out;
 $Texi2HTML::Config::unknown              = \&lilypond_unknown;
+$Texi2HTML::Config::print_page_head      = \&lilypond_print_page_head;
+
+# Examples should be formatted similar to quotes:
+$Texi2HTML::Config::complex_format_map->{'example'} = {
+  'begin' => q{"<blockquote><pre class=\"example\">"},
+  'end' => q{"</pre></blockquote>\n"},
+ };
 
 
 my @section_to_filename;
@@ -147,21 +156,19 @@ sub load_map_file ($)
 {
     my $mapfile = shift;
     my $node_map = ();
-    # TODO: Load and parse the file
-    
-    if (open(XREFFILE, $mapfilename)) {
-        @nodelist = <XREFFILE>;
-        close (XREFFILE);
-        # parse the tab-separated entries and insert them into the map:
-        foreach $node (@nodelist) {
-            chomp($node);
-            my @entries = split(/\t/, $node);
-            if (length (@entries) == 3) {
+
+    if (open(XREFFILE, $mapfile)) {
+        while ( <XREFFILE> ) {
+            # parse the tab-separated entries and insert them into the map:
+            chomp($_);
+            my @entries = split(/\t/, $_);
+            if (scalar (@entries) == 3) {
               $node_map->{$entries[0]} = [$entries[1], $entries[2]];
             } else {
-              print STDERR "Invalid entry in the node file $mapfile: $node\n";
+              print STDERR "Invalid entry in the node file $mapfile: $_\n";
             }
         }
+        close (XREFFILE);
     } else {
         print STDERR "Unable to load the map file $mapfile\n";
     }
@@ -169,6 +176,25 @@ sub load_map_file ($)
 }
 
 
+# Split the given path into dir and basename (with .texi removed). Used mainly
+# to get the path/basename of the original texi input file
+sub split_texi_filename ($)
+{
+  my $docu = shift;
+  my $docu_dir, $docu_name;
+  if ($docu =~ /(.*\/)/) {
+    chop($docu_dir = $1);
+    $docu_name = $docu;
+    $docu_name =~ s/.*\///;
+  } else {
+     $docu_dir = '.';
+     $docu_name = $docu;
+  }
+  $docu_name =~ s/\.te?x(i|info)?$//;
+  return ($docu_dir, $docu_name);
+}
+
+
 
 
 
@@ -227,13 +253,24 @@ sub split_at_numbered_sections($$$)
   my $docu_name = shift;
   my $docu_ext = $Texi2HTML::Config::EXTENSION;
 
-  my $node_name = $element->{'node_ref'}->{'no_texi'};
+  my $node_name = main::remove_texi($element->{'node_ref'}->{'texi'});
+  # the snippets page does not use nodes for the snippets, so in this case
+  # we'll have to use the section name!
+  if ($node_name eq '') {
+    $node_name = main::remove_texi($element->{'texi'});
+  }
+
+  # If we have an entry in the section<=>filename map, use that one, otherwise
+  # generate the filename/anchor here. In the latter case, external manuals
+  # will not be able to retrieve the file name for xrefs!!! Still, I already
+  # had that code, so I'll leave it in in case something goes wrong with the
+  # extract_texi_filenames.py script in the lilypond build process!
   if (exists ($node_to_filename_map->{$node_name})) {
-    (my $filename, my $anchor) = @$node_to_filename_map->{$node_name};
+    (my $filename, my $anchor) = @{$node_to_filename_map->{$node_name}};
     $filename .= ".$docu_ext" if (defined($docu_ext));
-    print "Stored target: " + $node_to_filename_map->{$node_name};
+
     $element->{id} = $anchor;
-    if ($filename == $lastfilename) {
+    if ($filename eq $lastfilename) {
       $$element{doc_nr} = $docnr;
     } else {
       $docnr += 1;
@@ -246,22 +283,19 @@ sub split_at_numbered_sections($$$)
     # TOC, footer, about etc. are called with undefined $element and $type == "toc"|"stoc"|"foot"|"about"
     return;
   } else {
-    # If we have an entry in the section<=>filename map, use that one, otherwise
-    # generate the filename/anchor here. In the latter case, external manuals
-    # will not be able to retrieve the file name for xrefs!!! Still, I already
-    # had that code, so I'll leave it in in case something goes wrong with the
-    #extract_texi_filenames.py script in the lilypond build process!
-    # TODO: lookup node name in nodename<=>filename map
+    print STDERR "WARNING: Node '$node_name' was NOT found in the map\n"
+        unless ($node_name eq '') or ($element->{'tag'} eq 'unnumberedsec')
+               or ($node_name =~ /NOT REALLY USED/);
 
     # derive the name of the anchor (i.e. the part after # in the links!),
     # don't use texi2html's SECx.x default!
-    
     my $sec_name = main::remove_texi($element->{'texi'});
-    if ($element->{'node_ref'}->{'texi'} ne '') { # if we have a node, use its name:
+    # if we have a node, use its name:
+    if ($element->{'node_ref'}->{'texi'} ne '') { 
       $sec_name = main::remove_texi($element->{'node_ref'}->{'texi'});
     }
     my $anchor = $sec_name;
-    if ($$element{translationof}) {
+    if ($element->{translationof}) {
       $anchor = main::remove_texi($$element{translationof});
     }
     # normalize to the same file name as texinfo
@@ -269,7 +303,7 @@ sub split_at_numbered_sections($$$)
     $$element{id} = $anchor;
     # Numbered sections will get a filename Node_title, unnumbered sections will use
     # the file name of the previous numbered section:
-    if (($element->{number}) or ($lastfilename eq '')) {
+    if (($element->{number}) or ($lastfilename eq '') or ($element->{level} == 1)) {
       my $filename = $anchor;
       $filename .= ".$docu_ext" if (defined($docu_ext));
       $docnr += 1;
@@ -285,15 +319,19 @@ sub split_at_numbered_sections($$$)
   return;
 }
 
-sub lilypond_init_out ()
+
+## Load the map file for the corrently processed texi file. We do this 
+#  (mis-)using a command init handler, since texi2html does not have any 
+#  other hooks that are called after THISDOC is filled but before phase 2
+#  of the texi2html conversion.
+sub lilypond_init_map ()
 {
-    print "Init out\n";
-    t2h_default_init_out ();
-#     my $map_filename = "$Texi2HTML::THISDOC{'destination_directory'}$Texi2HTML::THISDOC{'file_base_name'}_xref.map";
-    my $map_filename = "$Texi2HTML::THISDOC{'file_base_name'}_xref.map";
+    my ($docu_dir, $docu_name) = split_texi_filename ($Texi2HTML::THISDOC{'input_file_name'});
+    my $map_filename = "$docu_dir/${docu_name}.xref-map";
     $node_to_filename_map = load_map_file ($map_filename);
-    print Dumper ($node_to_filename_map);
 }
+push @Texi2HTML::Config::command_handler_init, \&lilypond_init_map;
+
 
 
 #############################################################################
@@ -348,7 +386,7 @@ sub lilypond_unknown($$$$$)
     # necessary here at all. Still, I'll leave the code in just in case the
     # python script messed up ;-)
     if ($pass == 1 and $macro eq "translationof") {
-      if (ref($state->{'element'})=='HASH') {
+      if (ref($state->{'element'}) eq 'HASH') {
         $state->{'element'}->{'translationof'} = main::normalise_space($line);
       }
       return ('', true, undef, undef);
@@ -376,180 +414,34 @@ sub lilypond_external_href($$$)
   my $file = shift;
   my $original_func = \&t2h_default_external_href;
 
-  # TODO:
   # 1) Keep a hash of book->section_map
-  # 2) if not file in keys hash => try to load the map (assign empty map is non-existent => will load only once!)
-  # 3) if node in the section=>(file, anchor) map, replace node_id and node_xhtml_id by the map's values
+  # 2) if not file in keys hash => try to load the map (assign empty map if 
+  #    non-existent => will load only once!)
+  # 3) if node in the section=>(file, anchor) map, replace node_id and 
+  #    node_xhtml_id by the map's values
   # 4) call the t2h_default_external_href with these values (or the old ones if not found)
-  print STDERR "lilypond_external_href: texi_node='$node', node_file='$node_id', node_xhtml_id='$node_xhtml_id', file='$file'\n";
-  if (($node_id ne '') and defined($file)) {
-    if (!exists($translated_books{$file})) {
-      print STDERR "Map for book $file not yet loaded, trying to initialize\n";
-      # TODO: Load the file...
-      $translated_books{$file}={};
+
+  if (($node_id ne '') and defined($file) and ($node_id ne 'Top')) {
+    my $map_name = $file;
+    $map_name =~ s/-big-page//;
+
+    # Load the map if we haven't done so already
+    if (!exists($translated_books{$map_name})) {
+      my ($docu_dir, $docu_name) = split_texi_filename ($Texi2HTML::THISDOC{'input_file_name'});
+      my $map_filename = "$docu_dir/${map_name}.xref-map";
+      $translated_books{$map_name} = load_map_file ($map_filename);
     }
-    my $section_name_map = $translated_books{$file};
-    if (exists($section_name_map->{$node_id})) {
-      print STDERR "Found key $node_id in section_name_map\n";
-      # TODO: Assign the new values to $file, $node_id and $node_xhtml_id!
-      # TODO: Am I doing this right?
 
-      (my $filename, my $anchor) = @$section_name_map->{$node_id};
-      $filename .= ".$docu_ext" if (defined($docu_ext));
-      print "Stored target: " + $node_to_filename_map->{$node_name};
-      
-      $node_xhtml_id = $anchor;
-      $file = $filename;
+    # look up translation. use these values instead of the old filename/anchor
+    my $section_name_map = $translated_books{$map_name};
+    my $node_text = main::remove_texi($node);
+    if (defined($section_name_map->{$node_text})) {
+      (my $node_id, $node_hxmlt_id) = @{$section_name_map->{$node_text}};
     } else {
-      print STDERR "Unable to find key $node_id in section_name_map\n";
+      print STDERR "Unable to find key '$node_text' in section_name_map for book $map_name\n";
     }
   }
-# Code copied from texi2html:
-# I'll need to find the correct path to the map file and texi2html already
-# seems to do a good job for this with the following code, which I will 
-# need to strip down to those parts that we really need:
-#
-#     $file = '' if (!defined($file));
-#     my $default_target_split = $EXTERNAL_CROSSREF_SPLIT;
-#     my $target_split;
-#     my $target_mono;
-#     my $href_split;
-#     my $href_mono;
-#     if ($file ne '')
-#     {
-#          if ($NEW_CROSSREF_STYLE)
-#          {
-#              $file =~ s/\.[^\.]*$//;
-#              $file =~ s/^.*\///;
-#              my $href;
-#              if (exists($Texi2HTML::THISDOC{'htmlxref'}->{$file}))
-#              {
-#                   if (exists($Texi2HTML::THISDOC{'htmlxref'}->{$file}->{'split'}))
-#                   {
-#                        $target_split = 1;
-#                        $href_split =  $Texi2HTML::THISDOC{'htmlxref'}->{$file}->{'split'}->{'href'};
-#                   }
-#                   if (exists($Texi2HTML::THISDOC{'htmlxref'}->{$file}->{'mono'}))
-#                   {
-#                        $target_mono = 1;
-#                        $href_mono =  $Texi2HTML::THISDOC{'htmlxref'}->{$file}->{'mono'}->{'href'};
-#                   }
-#              }
-#
-#              if ((not $target_mono) and (not $target_split))
-#              { # nothing specified for that manual, use default
-#                   $target_split = $default_target_split;
-#              }
-#              elsif ($target_split and $target_mono)
-#              { # depends on the splitting of the manual
-#                   $target_split = $SPLIT;
-#              }
-#              elsif ($target_mono)
-#              { # only mono specified
-#                   $target_split = 0;
-#              }
-#
-#              if ($target_split)
-#              {
-#                   if (defined($href_split))
-#                   {
-#                        $file = "$href_split";
-#                   }
-#                   elsif (defined($EXTERNAL_DIR))
-#                   {
-#                        $file = "$EXTERNAL_DIR/$file";
-#                   }
-#                   elsif ($SPLIT)
-#                   {
-#                        $file = "../$file";
-#                   }
-#                   $file .= "/";
-#              }
-#              else
-#              {# target not split
-#                   if (defined($href_mono))
-#                   {
-#                        $file = "$href_mono";
-#                   }
-#                   else
-#                   {
-#                        if (defined($EXTERNAL_DIR))
-#                        {
-#                             $file = "$EXTERNAL_DIR/$file";
-#                        }
-#                        elsif ($SPLIT)
-#                        {
-#                            $file = "../$file";
-#                        }
-#                        $file .= "." . $NODE_FILE_EXTENSION;
-#                   }
-#              }
-#          }
-#          else
-#          {
-#              $file .= "/";
-#              if (defined($EXTERNAL_DIR))
-#              {
-#                  $file = $EXTERNAL_DIR . $file;
-#              }
-#              else
-#              {
-#                  $file = '../' . $file;
-#              }
-#          }
-#     }
-#     else
-#     {
-#         $target_split = $default_target_split;
-#     }
-#     if ($node eq '')
-#     {
-#          if ($NEW_CROSSREF_STYLE)
-#          {
-#              if ($target_split)
-#              {
-#                  return $file . $TOP_NODE_FILE . '.' . $NODE_FILE_EXTENSION . '#Top';
-#                  # or ?
-#                  #return $file . '#Top';
-#              }
-#              else
-#              {
-#                   return $file . '#Top';
-#              }
-#          }
-#          else
-#          {
-#              return $file;
-#          }
-#     }
-#     my $target;
-#     if ($NEW_CROSSREF_STYLE)
-#     {
-#          $node = $node_id;
-#          $target = $node_xhtml_id;
-#     }
-#     else
-#     {
-#          $node = main::remove_texi($node);
-#          $node =~ s/[^\w\.\-]/-/g;
-#     }
-#     my $file_basename = $node;
-#     $file_basename = $TOP_NODE_FILE if ($node =~ /^top$/i);
-#     if ($NEW_CROSSREF_STYLE)
-#     {
-#         if ($target_split)
-#         {
-#             return $file . $file_basename . ".$NODE_FILE_EXTENSION" . '#' . $target;
-#         }
-#         else
-#         {
-#             return $file . '#' . $target;
-#         }
-#     }
-#     else
-#     {
-#         return $file . $file_basename . ".$NODE_FILE_EXTENSION";
-#     }
+
   if (defined $file) {
     return &$original_func($node, $node_id, $node_hxmlt_id, $file);
   } else {
@@ -583,7 +475,7 @@ sub generate_ly_toc_entries($$$$)
   return() if (not defined($element) or exists($element->{'index_page'}));
   my @result = ();
   my $level = $element->{'toc_level'};
-  my $is_parent_of_current = $element->{'number'} && $element_path->{$element->{'number'}};
+  my $is_parent_of_current = $element->{'id'} && $element_path->{$element->{'id'}};
   my $print_children = ( ($level < $maxlevel) or $is_parent_of_current );
   my $ind = '  ' x $level;
   my $this_css_class = $is_parent_of_current ? " class=\"toc_current\"" : "";
@@ -604,7 +496,7 @@ sub generate_ly_toc_entries($$$$)
     # if no child nodes were generated, e.g. for the index, where expanded pages
     # are ignored, don't generate a list at all...
     if (@child_result) {
-      push (@result, "$ind<ul$NO_BULLET_LIST_ATTRIBUTE>");
+      push (@result, "\n$ind<ul$NO_BULLET_LIST_ATTRIBUTE>\n");
       push (@result, @child_result);
       push (@result, "$ind</ul></li>\n");
     }
@@ -622,33 +514,49 @@ sub lilypond_generate_page_toc_body($)
     my $element = shift;
     my $current_element = $element;
     my %parentelements;
-    $parentelements{$element->{'number'}} = 1;
+    $parentelements{$element->{'id'}} = 1;
     # Find the path to the current element
     while ( defined($current_element->{'sectionup'}) and
            ($current_element->{'sectionup'} ne $current_element) )
     {
-      $parentelements{$current_element->{'sectionup'}->{'number'}} = 1
-              if ($current_element->{'sectionup'}->{'number'} ne '');
+      $parentelements{$current_element->{'sectionup'}->{'id'}} = 1
+              if ($current_element->{'sectionup'}->{'id'} ne '');
       $current_element = $current_element->{'sectionup'};
     }
     return () if not defined($current_element);
     # Create the toc entries recursively
-    my @toc_entries = ("<div class=\"contents\">", "<ul$NO_BULLET_LIST_ATTRIBUTE>");
+    my @toc_entries = ("<div class=\"contents\">\n", "<ul$NO_BULLET_LIST_ATTRIBUTE>\n");
     my $children = $current_element->{'section_childs'};
     foreach ( @$children ) {
       push (@toc_entries, generate_ly_toc_entries($_, \%parentelements, $page_toc_depth, False));
     }
-    push (@toc_entries, "</ul>");
-    push (@toc_entries, "</div>");
+    push (@toc_entries, "</ul>\n");
+    push (@toc_entries, "</div>\n");
     return @toc_entries;
 }
 
+sub lilypond_print_toc_div ($$)
+{
+  my $fh = shift;
+  my $tocref = shift;
+  my @lines = @$tocref;
+  # use default TOC if no custom lines have been generated
+  @lines = @default_toc if (not @lines);
+  if (@lines) {
+    print $fh "\n\n<div id=\"tocframe\">\n";
+    print $fh '<h4> ' . $Texi2HTML::NAME{'Contents'}  . "</h4>\n";
+    foreach my $line (@lines) {
+      print $fh $line;
+    }
+    print $fh "</div>\n\n";
+  }
+}
 
 # Create the custom TOC for this page (partially folded, current page is
 # highlighted) and store it in a global variable. The TOC is written out after
 # the html contents (but positioned correctly using CSS), so that browsers with
 # css turned off still show the contents first.
-my @this_page_toc = ();
+our @this_page_toc = ();
 sub lilypond_print_element_header
 {
   my $fh = shift;
@@ -672,6 +580,13 @@ sub lilypond_toc_body($)
     return T2H_GPL_toc_body($elements_list);
 }
 
+# Print out the TOC in a <div> at the beginning of the page
+sub lilypond_print_page_head($)
+{
+    my $fh = shift;
+    T2H_DEFAULT_print_page_head($fh);
+    print $fh "<div id=\"main\">\n";
+}
 
 # Print out the TOC in a <div> at the end of th page, which will be formatted as a
 # sidebar mimicking a TOC frame
@@ -680,20 +595,12 @@ sub print_lilypond_page_foot($)
   my $fh = shift;
   my $program_string = &$program_string();
   print $fh "<p><font size='-1'>$program_string</font><br>$PRE_BODY_CLOSE</p>\n";
+  print $fh "<!-- FOOTER -->\n\n";
+  print $fh "<!-- end div#main here -->\n</div>\n\n";
 
-  # Print the TOC frame:
-  my @lines = @this_page_toc;
-  # use default TOC if no custom lines have been generated
-  @lines = @default_toc if (not @lines);
-  if (@lines) {
-    print $fh "\n\n<div id=\"tocframe\">";
-    print $fh '<h4> ' . $Texi2HTML::NAME{'Contents'}  . "</h4>\n";
-    foreach my $line (@lines) {
-      print $fh $line;
-    }
-    print $fh "</div>";
-    @this_page_toc = ();
-  }
+  # Print the TOC frame and reset the TOC:
+  lilypond_print_toc_div ($fh, \@this_page_toc);
+  @this_page_toc = ();
 
   # Close the page:
   print $fh "</body>\n</html>\n";
@@ -911,7 +818,7 @@ sub lilypond_print_navigation
 #############################################################################
 
 # For split pages, use index.html as start page!
-if ($Texi2HTML::Config::SPLIT == 'section') {
+if ($Texi2HTML::Config::SPLIT eq 'section') {
   $Texi2HTML::Config::TOP_FILE = 'index.html';
 }