]> 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 048a7f33d0a8d142ce62ea53f480d636b5981d2c..5d31b054b699efbefed164b702e0d6ff351a12d1 100644 (file)
@@ -27,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
@@ -39,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)
 ###
@@ -73,10 +75,15 @@ $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;
 
@@ -149,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";
     }
@@ -171,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);
+}
+
+
 
 
 
@@ -229,14 +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));
-    # TODO: Does this work?
-    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;
@@ -249,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
@@ -288,14 +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 ()
 {
-    t2h_default_init_out ();
-    # TODO: find correct path to the map file
-#     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);
 }
+push @Texi2HTML::Config::command_handler_init, \&lilypond_init_map;
+
 
 
 #############################################################################
@@ -350,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);
@@ -378,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 {
@@ -928,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';
 }