From 94eeb7ff9d6402992d9ea6713be30d01c240c9ab Mon Sep 17 00:00:00 2001
From: Guillem Jover <guillem@debian.org>
Date: Tue, 10 Jan 2017 09:29:45 +0100
Subject: [PATCH 5/8] Add new DebianDoc-SGML to DocBook conversion script

This is a conversion script, that should automate the whole process
so that it can hopefully be repeated at any later point and is able
handle modifications to the documents sources.
---
 tools/sgml2docbook | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 116 insertions(+)
 create mode 100755 tools/sgml2docbook

diff --git a/tools/sgml2docbook b/tools/sgml2docbook
new file mode 100755
index 0000000..2273d32
--- /dev/null
+++ b/tools/sgml2docbook
@@ -0,0 +1,116 @@
+#!/bin/sh
+
+set -e
+set -x
+
+cleanup=yes
+tidyxml=no
+
+files="menu-policy.sgml perl-policy.sgml upgrading-checklist.sgml policy.sgml"
+
+export LC_ALL=C
+
+# Generate fake entity files to be able to process the SGML and XML.
+(
+  echo '<!entity version "@@@VERSION@@@">'
+  echo '<!entity date    "@@@DATE@@@">'
+) >version.ent
+(
+  echo '<?xml version="1.0" encoding="utf-8"?>'
+  echo '<!ENTITY version "@@@VERSION@@@">'
+  echo '<!ENTITY date    "@@@DATE@@@">'
+) >version.xml
+
+for sgml in $files; do
+  base=$(basename $sgml .sgml)
+
+  # Remove invalid entities, encode other entities, and wrap
+  sed -e 's/ *&copy; *//g' \
+      -e 's/, &date;//g' \
+      -e 's/&\([a-z]*\);/@@@\1@@@/g' <$sgml >$base.new.sgml
+
+  # Do the conversion.
+  debiandoc2dbk -1 -l en $base.new.sgml
+
+  # Recode, and fixup markup.
+  iconv -f iso-8859-15 -t utf-8 <$base.new.dbk |
+    sed -e 's/@@@\([a-z]*\)@@@/\&\1;/g' \
+        -e 's/&version;/@@@version@@@/g' \
+        -e 's/<!-- put date -->/@@@date@@@/' \
+        -e '/<!-- Include entity/d' \
+        -e 's/<!-- \(<!ENTITY .*\)"version.ent"\(>.*\) *-->/\1"version.xml"\2/g' \
+        -e 's:<personname>\([^ ]*\) *\([^ ]*\)</personname>:<personname><firstname>\1</firstname><surname>\2</surname></personname>:g' \
+        -e 's:<personname>\(.*The Debian Policy mailing List.*\)</personname>:<othername>\1</othername>:gi' \
+        -e 's/ *$//g' \
+      >$base.xml
+
+  if [ "$base" = "policy" ]; then
+    # Fixup IDs, and insert entity.
+    sed -i \
+        -e '/^]>/i<!-- current Debian changes file format -->\
+<!ENTITY changesversion "1.8">' \
+        -e 's:<literal>"</literal>:&<!-- balance " -->:' \
+        -e 's/\(id\|linkend\)="\(pkg-alternatives\|pkg-binarypkg\|pkg-conffiles\|pkg-controlfields\|pkg-diversions\|pkg-scope\|pkg-sourcepkg\)"/\1="ap-\2"/g' \
+        -e 's/\(id\|linkend\)="\(archive\|binary\|controlfields\|customized-programs\|docs\|files\|maintainerscripts\|opersys\|relationships\|scope\|sharedlibs\|source\)"/\1="ch-\2"/' \
+        -e '/\(id\|linkend\)="\(ap-\|ch-\|s-\|s[0-9]\)[^"]*"/!s/\(id\|linkend\)="\([^"]*\)"/\1="s-\2"/g' \
+      $base.xml
+  elif [ "$base" = "perl-policy" ]; then
+    # Fixup IDs.
+    sed -i \
+        -e 's/\(id\|linkend\)="perl6"/\1="ap-perl6"/' \
+        -e 's/\(id\|linkend\)="module-packages"/\1="ch-module_packages"/' \
+        -e 's/\(id\|linkend\)="\(embed\|perl\|programs\|site\|files\)"/\1="ch-\2"/' \
+        -e '/\(id\|linkend\)="\(ap-\|ch-\|ch[0-9]\|s-\|s[0-9]\)[^"]*"/!s/\(id\|linkend\)="\([^"]*\)"/\1="s-\2"/g' \
+      $base.xml
+  elif [ "$base" = "upgrading-checklist" ]; then
+    # Fixup IDs.
+    sed -i -e 's/id="\([0-9]\)/id="s-\1/g' \
+      $base.xml
+  fi
+
+  case "$tidyxml" in
+  xmllint)
+    xmllint --encode utf-8 --format --nonet --postvalid $base.xml \
+      | sponge $base.xml
+    ;;
+  pandoc)
+    # Converts from book to an article, does not preserve entity declarations,
+    # nice output.
+    pandoc --normalize --chapters -s -f docbook -t docbook $base.xml \
+      | sponge $base.xml
+    ;;
+  tidy)
+    # Messes up spacing, and joins tags and words.
+    tidy -raw -wrap 76 --indent yes --quiet yes \
+         -xml --input-xml yes --output-xml yes \
+         --preserve-entities yes --vertical-space yes --add-xml-space yes \
+         -i -m $base.xml
+    ;;
+  esac
+
+  # Restore entities.
+  sed -i -e 's/@@@\([a-z]*\)@@@/\&\1;/g' $base.xml
+
+  # Test generating single file.
+  xsltproc --nonet --output "$base-1.html" \
+    xsl/html-single.xsl $base.xml
+
+  # Test generating chunked files.
+  mkdir -p $base.html
+  xsltproc --nonet --stringparam base.dir $base.html/ \
+    xsl/html-chunk.xsl $base.xml
+
+  # Update git tree.
+  git add $base.xml
+  git rm $base.sgml
+
+  # Cleanup temporary files.
+  if [ $cleanup = yes ]; then
+    rm -f $base.new.*
+  fi
+done
+
+# Cleanup temporary files.
+if [ $cleanup = yes ]; then
+  rm -f version.ent version.xml
+fi
-- 
2.12.2.564.g063fe858b8

