Commits

Shlomi Fish  committed d52b4cc

Add the clean-up-docbook-5 Perl program+stylesheet.

  • Participants
  • Parent commits 9f8ab50

Comments (0)

Files changed (2)

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

+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use autodie;
+
+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 $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;
+
+    $s =~ s/[ \t]+$//gms;
+    open my $out, ">", $out_fn;
+    binmode $out, ":utf8";
+    print {$out} $s;
+    close($out);
+}
+

File bin/clean-up-docbook-xhtml-1.1.xslt

+<xsl:stylesheet version = '1.0'
+    xmlns="http://www.w3.org/1999/xhtml"
+    xmlns:xhtml="http://www.w3.org/1999/xhtml"
+    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
+    >
+
+    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"
+        doctype-public="-//W3C//DTD XHTML 1.1//EN"
+        doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
+        />
+
+    <xsl:template match="node()|@*">
+        <xsl:copy>
+            <xsl:apply-templates select="node()|@*"/>
+        </xsl:copy>
+    </xsl:template>
+
+    <xsl:template match="node()[xhtml:a/@id]">
+        <xsl:copy>
+            <xsl:copy-of select="xhtml:a/@id"/>
+            <xsl:apply-templates select="node()|@*"/>
+        </xsl:copy>
+    </xsl:template>
+
+    <xsl:template match="xhtml:h3[@class='author']">
+        <xsl:element name="h2">
+            <xsl:copy-of select="xhtml:a/@id"/>
+            <xsl:apply-templates select="node()|@*"/>
+        </xsl:element>
+    </xsl:template>
+
+    <xsl:template match="xhtml:a/@id"/>
+
+</xsl:stylesheet>
+