From f7f88de6726a5663d90da5a454c9b3a1ba037fd1 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Wed, 15 Mar 2023 16:45:14 -0700
Subject: [PATCH v2 5/8] docs: html: copy images to output as part of xslt
 build

---
 doc/src/sgml/Makefile              |  2 --
 doc/src/sgml/stylesheet-common.xsl | 22 ++++++++++++++++++++++
 doc/src/sgml/stylesheet-hh.xsl     |  6 ++++++
 doc/src/sgml/stylesheet.xsl        |  7 ++++++-
 4 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/doc/src/sgml/Makefile b/doc/src/sgml/Makefile
index b96c7cbf223..1b098f983ec 100644
--- a/doc/src/sgml/Makefile
+++ b/doc/src/sgml/Makefile
@@ -144,7 +144,6 @@ html: html-stamp
 
 html-stamp: stylesheet.xsl postgres-full.xml $(ALL_IMAGES)
 	$(XSLTPROC) $(XMLINCLUDE) $(XSLTPROCFLAGS) $(XSLTPROC_HTML_FLAGS) $(wordlist 1,2,$^)
-	cp $(ALL_IMAGES) html/
 	cp $(srcdir)/stylesheet.css html/
 	touch $@
 
@@ -152,7 +151,6 @@ htmlhelp: htmlhelp-stamp
 
 htmlhelp-stamp: stylesheet-hh.xsl postgres-full.xml $(ALL_IMAGES)
 	$(XSLTPROC) $(XMLINCLUDE) $(XSLTPROCFLAGS) $(wordlist 1,2,$^)
-	cp $(ALL_IMAGES) htmlhelp/
 	cp $(srcdir)/stylesheet.css htmlhelp/
 	touch $@
 
diff --git a/doc/src/sgml/stylesheet-common.xsl b/doc/src/sgml/stylesheet-common.xsl
index 761484c7fef..d2928f86eb7 100644
--- a/doc/src/sgml/stylesheet-common.xsl
+++ b/doc/src/sgml/stylesheet-common.xsl
@@ -101,4 +101,26 @@
   <xsl:apply-templates select="." mode="xref"/>
 </xsl:template>
 
+
+<!--
+  Support for copying images to the output directory, so the output is self
+  contained.
+-->
+<xsl:template name="write-image">
+ <xsl:variable name="input_filename">
+   <xsl:value-of select="imagedata/@fileref"/>
+ </xsl:variable>
+
+ <!-- references images directly, without images/ -->
+ <xsl:variable name="output_filename">
+   <xsl:value-of select="concat($chunk.base.dir, substring-after($input_filename, '/'))"/>
+ </xsl:variable>
+
+ <xsl:call-template name="write.chunk">
+  <xsl:with-param name="filename" select="$output_filename"/>
+  <xsl:with-param name="content" select="document($input_filename)"/>
+ </xsl:call-template>
+</xsl:template>
+
+
 </xsl:stylesheet>
diff --git a/doc/src/sgml/stylesheet-hh.xsl b/doc/src/sgml/stylesheet-hh.xsl
index 6f4b706dac6..568ccf36d2a 100644
--- a/doc/src/sgml/stylesheet-hh.xsl
+++ b/doc/src/sgml/stylesheet-hh.xsl
@@ -39,6 +39,12 @@
 </xsl:template>
 
 
+<xsl:template match="imageobject">
+  <xsl:call-template name="write-image"/>
+  <!-- copy images to the output directory, so the output is self contained -->
+  <xsl:apply-templates select="imagedata"/>
+</xsl:template>
+
 <!-- strip directory name from image filerefs -->
 <xsl:template match="imagedata/@fileref">
  <xsl:value-of select="substring-after(., '/')"/>
diff --git a/doc/src/sgml/stylesheet.xsl b/doc/src/sgml/stylesheet.xsl
index b6141303abd..f9163e7d946 100644
--- a/doc/src/sgml/stylesheet.xsl
+++ b/doc/src/sgml/stylesheet.xsl
@@ -29,7 +29,12 @@
 </xsl:param>
 
 
-<!-- strip directory name from image filerefs -->
+<xsl:template match="imageobject">
+  <xsl:call-template name="write-image"/>
+  <!-- copy images to the output directory, so the output is self contained -->
+  <xsl:apply-templates select="imagedata"/>
+</xsl:template>
+
 <xsl:template match="imagedata/@fileref">
  <xsl:value-of select="substring-after(., '/')"/>
 </xsl:template>
-- 
2.38.0

