[yelp-tools] yelp-check: Adding validate subcommand for Mallard



commit 2807cedae09c2540194a7ded6a2726199e2ecaca
Author: Shaun McCance <shaunm gnome org>
Date:   Thu May 5 12:03:20 2011 -0400

    yelp-check: Adding validate subcommand for Mallard

 configure.ac        |   10 +++++
 tools/yelp-check.in |   44 ++++++++++++++++++++++++
 tools/yelp-new.in   |    2 +-
 xslt/Makefile.am    |    6 +++
 xslt/mal-rng.xsl    |   94 +++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 155 insertions(+), 1 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 63eda7f..9799977 100644
--- a/configure.ac
+++ b/configure.ac
@@ -41,6 +41,15 @@ PKG_CHECK_MODULES(GNOME_DOC_UTILS,
 
 XSL_PATH="`$PKG_CONFIG --variable=xsltdir yelp-xsl`"
 
+DATADIR=`(
+    case $prefix in
+        NONE) prefix=$ac_default_prefix ;;
+        *) ;;
+    esac
+    eval echo $(eval echo $datadir)/yelp-tools
+)`
+AC_SUBST([DATADIR])
+
 XSL_MAL_CACHE="$XSL_PATH""/mallard/cache/mal-cache.xsl"
 AC_SUBST(XSL_MAL_CACHE)
 
@@ -81,6 +90,7 @@ tools/Makefile
 tools/yelp-build
 tools/yelp-check
 tools/yelp-new
+xslt/Makefile
 ])
 
 AC_OUTPUT
diff --git a/tools/yelp-check.in b/tools/yelp-check.in
index 1d135d2..0f7ca9b 100755
--- a/tools/yelp-check.in
+++ b/tools/yelp-check.in
@@ -1,4 +1,5 @@
 #!/bin/sh
+# yelp-check
 # Copyright (C) 2011 Shaun McCance <shaunm gnome org>
 #
 # This program is free software; you can redistribute it and/or modify
@@ -16,6 +17,7 @@
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
 xsl_mal_link='@XSL_MAL_LINK@'
+xsl_mal_rng='@DATADIR@/xslt/mal-rng.xsl'
 
 urlencode () {
     echo "$1" | LANG=C awk '
@@ -42,6 +44,7 @@ yelp_usage () {
         echo "Commands:"
         echo "  orphans       Find orphaned pages in a Mallard document"
         echo "  links         Find broken xref links in a Mallard document"
+        echo "  validate      Validate files against a DTD or RNG"
     ) 1>&2
 }
 yelp_usage_links () {
@@ -66,6 +69,15 @@ yelp_usage_orphans () {
         echo "  -c CACHE      Use the existing Mallard cache CACHE"
     ) 1>&2
 }
+yelp_usage_validate () {
+    (
+        echo "Usage: yelp-check validate <FILES>"
+        echo ""
+        echo "  Validate FILES against the appropriate DTD or RNG."
+        echo "  For Mallard pages, perform automatic RNG merging"
+        echo "  based on the version attribute."
+    ) 1>&2
+}
 
 if [ $# = 0 ]; then
     yelp_usage
@@ -209,6 +221,35 @@ yelp_orphans () {
     fi
 }
 
+yelp_validate_page () {
+    # Using temp files because pipes create subshells, making it really
+    # hard to return the right exit status in a portable way.
+    check_rng_file=`mktemp`
+    check_out_file=`mktemp`
+    xsltproc -o "$check_rng_file" "$xsl_mal_rng" "$1"
+    xmllint --noout --xinclude --noent --relaxng "$check_rng_file" "$1" > "$check_out_file" 2>&1
+    ret="$?"
+    cat "$check_out_file" | grep -v 'validates$'
+    rm "$check_rng_file" "$check_out_file"
+    exit $ret;
+}
+
+yelp_validate () {
+    if [ "$#" = "0" -o "x$1" = "x--help" ]; then
+        yelp_usage_orphans
+        exit 1
+    fi
+    for xml in "$@"; do
+        if [ -d "$xml" ]; then
+            for page in "$xml"/*.page; do
+                yelp_validate_page "$page"
+            done
+        else
+            yelp_validate_page "$xml"
+        fi
+    done
+}
+
 cmd="$1"
 shift
 case "x$cmd" in
@@ -218,6 +259,9 @@ case "x$cmd" in
     "xorphans")
         yelp_orphans "$@"
         ;;
+    "xvalidate")
+        yelp_validate "$@"
+        ;;
     *)
         yelp_usage
         ;;
diff --git a/tools/yelp-new.in b/tools/yelp-new.in
index 507c940..5abfc9d 100755
--- a/tools/yelp-new.in
+++ b/tools/yelp-new.in
@@ -1,5 +1,5 @@
 #!/bin/sh
-# yelp-new - Create a new Mallard or DocBook file
+# yelp-new
 # Copyright (C) 2010 Shaun McCance <shaunm gnome org>
 #
 # This program is free software; you can redistribute it and/or modify
diff --git a/xslt/Makefile.am b/xslt/Makefile.am
new file mode 100644
index 0000000..31384d7
--- /dev/null
+++ b/xslt/Makefile.am
@@ -0,0 +1,6 @@
+xsldir=$(datadir)/yelp-tools/xslt
+
+xsl_DATA =				\
+	mal-rng.xsl
+
+EXTRA_DIST=$(xsl_DATA)
diff --git a/xslt/mal-rng.xsl b/xslt/mal-rng.xsl
new file mode 100644
index 0000000..9f80c65
--- /dev/null
+++ b/xslt/mal-rng.xsl
@@ -0,0 +1,94 @@
+<xsl:stylesheet
+    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
+    xmlns:str="http://exslt.org/strings";
+    xmlns:exsl="http://exslt.org/common";
+    xmlns:mal="http://projectmallard.org/1.0/";
+    xmlns:rng="http://relaxng.org/ns/structure/1.0";
+    xmlns="http://relaxng.org/ns/structure/1.0";
+    exclude-result-prefixes="str exsl"
+    version="1.0">
+
+<xsl:template match="/*">
+  <xsl:variable name="version">
+    <xsl:choose>
+      <xsl:when test="string(@version) != ''">
+        <xsl:value-of select="@version"/>
+      </xsl:when>
+      <xsl:otherwise>
+        <xsl:value-of select="'1.0'"/>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:variable>
+  <xsl:variable name="uris">
+    <xsl:for-each select="str:split($version)">
+      <xsl:choose>
+        <xsl:when test="contains(., ':')">
+          <xsl:value-of select="."/>
+        </xsl:when>
+        <xsl:when test="contains(., '/')">
+          <xsl:variable name="ext" select="substring-before(., '/')"/>
+          <xsl:variable name="ver" select="substring-after(., '/')"/>
+          <xsl:text>http://projectmallard.org/</xsl:text>
+          <xsl:value-of select="concat($ext, '/', $ver, '/', $ext, '-', $ver, '.rng')"/>
+        </xsl:when>
+        <xsl:otherwise>
+          <xsl:text>http://projectmallard.org/</xsl:text>
+          <xsl:value-of select="concat(., '/mallard-', ., '.rng')"/>
+        </xsl:otherwise>
+      </xsl:choose>
+      <xsl:text> </xsl:text>
+    </xsl:for-each>
+  </xsl:variable>
+  <xsl:variable name="nss_">
+    <xsl:for-each select="str:split($uris)">
+      <xsl:for-each select="document(.)/*">
+        <nsName ns="{ ns}"/>
+      </xsl:for-each>
+    </xsl:for-each>
+    <nsName ns=""/>
+  </xsl:variable>
+  <xsl:variable name="nss" select="exsl:node-set($nss_)/*"/>
+  <grammar>
+    <xsl:for-each select="str:split($uris)">
+      <xsl:for-each select="document(.)/rng:grammar">
+        <xsl:apply-templates mode="rng.mode" select="*">
+          <xsl:with-param name="ns" select="string(@ns)"/>
+          <xsl:with-param name="nss" select="$nss"/>
+        </xsl:apply-templates>
+      </xsl:for-each>
+    </xsl:for-each>
+  </grammar>
+</xsl:template>
+
+<xsl:template mode="rng.mode" match="text()">
+  <xsl:value-of select="."/>
+</xsl:template>
+
+<xsl:template mode="rng.mode" match="*">
+  <xsl:param name="ns"/>
+  <xsl:param name="nss"/>
+  <xsl:copy>
+    <xsl:for-each select="@*">
+      <xsl:copy-of select="."/>
+    </xsl:for-each>
+    <xsl:if test="self::rng:element and not(@ns)">
+      <xsl:attribute name="ns">
+        <xsl:value-of select="$ns"/>
+      </xsl:attribute>
+    </xsl:if>
+    <xsl:apply-templates mode="rng.mode">
+      <xsl:with-param name="ns" select="$ns"/>
+      <xsl:with-param name="nss" select="$nss"/>
+    </xsl:apply-templates>
+  </xsl:copy>
+</xsl:template>
+
+<xsl:template mode="rng.mode" match="rng:anyName/rng:except">
+  <xsl:param name="ns"/>
+  <xsl:param name="nss"/>
+  <except>
+    <xsl:copy-of select="$nss"/>
+  </except>
+</xsl:template>
+
+</xsl:stylesheet>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]