Commits

shl...@iglu.org.il@cec68495-dca5-4e2b-845c-11fdaaa4f967  committed 4b5a91a

Optimise the clean-up-docbook-5-xsl for DocBook 5/xhtml-1_1.

  • Participants
  • Parent commits 662928d

Comments (0)

Files changed (3)

 
 DOCBOOK5_XSL_STYLESHEETS_PATH := $(HOME)/Download/unpack/file/docbook/docbook-xsl-ns-snapshot
 
-DOCBOOK5_XSL_STYLESHEETS_XHTML_PATH := $(DOCBOOK5_XSL_STYLESHEETS_PATH)/xhtml
+DOCBOOK5_XSL_STYLESHEETS_XHTML_PATH := $(DOCBOOK5_XSL_STYLESHEETS_PATH)/xhtml-1_1
 DOCBOOK5_XSL_STYLESHEETS_ONECHUNK_PATH := $(DOCBOOK5_XSL_STYLESHEETS_PATH)/onechunk
 DOCBOOK5_XSL_STYLESHEETS_FO_PATH := $(DOCBOOK5_XSL_STYLESHEETS_PATH)/fo
 
 	xsltproc --path  $(DOCBOOK5_XSL_STYLESHEETS_XHTML_PATH) -o $@ $(DOCBOOK5_XSL_ONECHUNK_XSLT_STYLESHEET) $<
 
 $(DOCBOOK5_RENDERED_DIR)/%.xhtml: $(DOCBOOK5_ALL_IN_ONE_XHTML_DIR)/%/all-in-one.xhtml
-	./bin/clean-up-docbook-xsl-xhtml.pl -o $@ $<
+	./bin/clean-up-docbook-5-xsl-xhtml-1_1.pl -o $@ $<
 
 $(DOCBOOK5_FO_DIR)/%.fo: $(DOCBOOK5_SOURCES_DIR)/%.xml
 	xsltproc --path $(DOCBOOK5_XSL_STYLESHEETS_FO_PATH) -o $@ $(DOCBOOK5_XSL_FO_XSLT_STYLESHEET) $<

File bin/clean-up-docbook-5-xsl-xhtml-1_1.pl

+#!/usr/bin/perl 
+
+use strict;
+use warnings;
+
+use XML::LibXML;
+use XML::LibXML::XPathContext;
+use Getopt::Long;
+use IO::All;
+
+my $out_fn;
+
+GetOptions(
+    "output|o=s" => \$out_fn,
+);
+
+# Input the filename
+my $filename = shift(@ARGV)
+    or die "Give me a filename as a command argument: myscript FILENAME";
+
+sub _slurp
+{
+    my $filename = shift;
+
+    open my $in, "<", $filename
+        or die "Cannot open '$filename' for slurping - $!";
+
+    binmode $in, ":encoding(utf-8)";
+
+    local $/;
+    my $contents = <$in>;
+
+    close($in);
+
+    return $contents;
+}
+
+{
+    my ($body_node) = $xpc->findnodes('//xhtml:body');
+
+    my $s = _slurp($filename);
+
+    $s =~ s{\A.*?<body[^>]*>}{}sm;
+    $s =~ s{</body>.*\z}{}ms;
+
+    # It's a kludge
+    $s =~ s{ lang="en"}{}g;
+    $s =~ s{ xml:lang="en"}{}g;
+    $s =~ s{ type="(?:1|disc)"}{}g;
+    $s =~ s{<hr[^/]*/>}{<hr />}g;
+    $s =~ s{ target="_top"}{}g;
+
+    # Fixed in Perl 6...
+    $s =~ s{<(/?)h(\d)}{"<".$1."h".($2+2)}ge;
+
+    open my $out, ">", $out_fn;
+    binmode $out, ":utf8";
+    print {$out} $s;
+    close($out);
+}
+

File bin/clean-up-docbook-xsl-xhtml.pl

     # It's a kludge
     $s =~ s{ lang="en"}{}g;
     $s =~ s{ xml:lang="en"}{}g;
-    $s =~ s{ type="(1|disc)"}{}g;
+    $s =~ s{ type="(?:1|disc)"}{}g;
     $s =~ s{<hr[^/]*/>}{<hr />}g;
     $s =~ s{ target="_top"}{}g;