commit:     9b663cce9ed88ec5b86cfde8d050017d23894798
Author:     Ulrich Müller <ulm <AT> gentoo <DOT> org>
AuthorDate: Wed Feb 26 20:21:54 2020 +0000
Commit:     Ulrich Müller <ulm <AT> gentoo <DOT> org>
CommitDate: Sun Mar  1 21:40:46 2020 +0000
URL:        https://gitweb.gentoo.org/proj/devmanual.git/commit/?id=9b663cce

Makefile, depend.xsl: Use XSLT to generate the list of dependencies.

Each HTML file must depend on its XML file with all its descendants
(for the contents tree), all its ancestors (for breadcrumbs), and the
previous and next documents (for backward and forward links).

Trying to generate the list of dependencies with make seems hopeless
(especially, how would one determine the previous and next documents?).
OTOH, the necessary templates already exist in devbook.xsl. Therefore,
add a simple depend.xsl on top of it, which outputs a dependency list
as plain text.

Closes: https://bugs.gentoo.org/710918
Signed-off-by: Ulrich Müller <ulm <AT> gentoo.org>

 .gitignore |  1 +
 Makefile   | 21 +++++++++++++--------
 depend.xsl | 33 +++++++++++++++++++++++++++++++++
 3 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/.gitignore b/.gitignore
index ce644c7..7c45b92 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
 *.html
 *.png
+.depend
 documents.js
 eclass-reference/

diff --git a/Makefile b/Makefile
index 4879792..be1224f 100644
--- a/Makefile
+++ b/Makefile
@@ -47,20 +47,23 @@ documents.js: bin/build_search_documents.py $(XMLS)
        rsvg-convert --output=$@ $<
 
 # Secondary expansion allows us to use the automatic variable $@ in
-# the prerequisites. When it is used (and we have no idea when that
-# is, so we assume always) our <include href="foo"> tag induces a
-# dependency on the output of all subdirectories of the current
-# directories. This wacky rule finds all of those subdirectories by
-# looking for text.xml in them, and then replaces "text.xml" in the
-# path with "index.html".
+# the prerequisites.
 #
 # We use the pattern %.html rather than the more-sensible %index.html
 # because the latter doesn't match our top-level index.html target.
 #
 .SECONDEXPANSION:
-%.html: $$(dir $$@)text.xml devbook.xsl xsl/*.xsl $$(subst 
text.xml,index.html,$$(wildcard $$(dir $$@)*/text.xml))
+%.html: $$(dir $$@)text.xml devbook.xsl xsl/*.xsl
        xsltproc --param offline "$(OFFLINE)" devbook.xsl $< > $@
 
+# Each HTML file must depend on its XML file with all its descendants
+# (for the contents tree), all its ancestors (for breadcrumbs), and
+# the previous and next documents (for backward and forward links).
+# Generate the list of dependencies with XSLT, which appears to be a
+# better tool for this than make.
+.depend: $(XMLS) depend.xsl devbook.xsl
+       @xsltproc depend.xsl $(XMLS) | sed ':x;s%[^ /]*/\.\./%%;tx' > $@
+
 install: all
        set -e; \
        for file in $(HTMLS) $(ECLASS_HTMLS) $(IMAGES); do \
@@ -89,6 +92,8 @@ tidy: $(HTMLS) $(ECLASS_HTMLS)
        exit $${status}
 
 clean:
-       @rm -f $(HTMLS) $(IMAGES) documents.js
+       @rm -f $(HTMLS) $(IMAGES) documents.js .depend
 
 .PHONY: all prereq build install validate tidy clean
+
+-include .depend

diff --git a/depend.xsl b/depend.xsl
new file mode 100644
index 0000000..e0ee66c
--- /dev/null
+++ b/depend.xsl
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
+  xmlns:exslt="http://exslt.org/common";
+  extension-element-prefixes="exslt xsl"
+  exclude-result-prefixes="exslt xsl">
+
+<xsl:import href="devbook.xsl"/>
+<xsl:output method="text"/>
+
+<xsl:template match="/">
+  <xsl:variable name="refs">
+    <!-- all descendants -->
+    <xsl:call-template name="contentsTree"/>
+    <!-- all ancestors -->
+    <xsl:call-template name="printParentDocs"/>
+    <!-- previous and next documents -->
+    <xsl:call-template name="findPrevious"/>
+    <xsl:call-template name="findNext"/>
+  </xsl:variable>
+  <xsl:variable name="self" select="/guide/@self"/>
+  <xsl:value-of select="concat($self, 'index.html:')"/>
+  <xsl:for-each select="exslt:node-set($refs)//a/@href[. != '#']">
+    <!-- At this point, the path can contain ".." components and
+         should be canonicalised, but string processing in XPath 1.0
+         sucks (no pattern matching!). It is easier to pipe the output
+         through sed instead. -->
+    <xsl:value-of select="concat(' ', $self,
+        substring-before(., 'index.html'), 'text.xml')"/>
+  </xsl:for-each>
+  <xsl:value-of select="$newline"/>
+</xsl:template>
+
+</xsl:stylesheet>

Reply via email to