From f5e081b911797fa6897a883b0f1f3e35daf170ad Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Wed, 15 Mar 2023 20:49:47 -0700
Subject: [PATCH v1 5/5] wip: docs: copy or inline css

---
 doc/src/sgml/Makefile                    |  4 +---
 doc/src/sgml/meson.build                 |  2 +-
 doc/src/sgml/stylesheet-common.xsl       | 21 +++++++++++++++++++++
 doc/src/sgml/stylesheet-hh.xsl           | 10 +++++++++-
 doc/src/sgml/stylesheet-html-nochunk.xsl | 20 ++++++++++++++++++++
 doc/src/sgml/stylesheet.xsl              | 15 ++++++---------
 6 files changed, 58 insertions(+), 14 deletions(-)

diff --git a/doc/src/sgml/Makefile b/doc/src/sgml/Makefile
index 1b098f983ec..3f971f6eb33 100644
--- a/doc/src/sgml/Makefile
+++ b/doc/src/sgml/Makefile
@@ -53,7 +53,7 @@ else
 XSLTPROC = $(missing) xsltproc
 endif
 
-override XSLTPROCFLAGS += --stringparam pg.version '$(VERSION)'
+override XSLTPROCFLAGS += --xincludestyle --stringparam pg.version '$(VERSION)'
 
 
 GENERATED_SGML = version.sgml \
@@ -144,14 +144,12 @@ html: html-stamp
 
 html-stamp: stylesheet.xsl postgres-full.xml $(ALL_IMAGES)
 	$(XSLTPROC) $(XMLINCLUDE) $(XSLTPROCFLAGS) $(XSLTPROC_HTML_FLAGS) $(wordlist 1,2,$^)
-	cp $(srcdir)/stylesheet.css html/
 	touch $@
 
 htmlhelp: htmlhelp-stamp
 
 htmlhelp-stamp: stylesheet-hh.xsl postgres-full.xml $(ALL_IMAGES)
 	$(XSLTPROC) $(XMLINCLUDE) $(XSLTPROCFLAGS) $(wordlist 1,2,$^)
-	cp $(srcdir)/stylesheet.css htmlhelp/
 	touch $@
 
 # single-page HTML
diff --git a/doc/src/sgml/meson.build b/doc/src/sgml/meson.build
index 2da737c8af4..c581c7246f0 100644
--- a/doc/src/sgml/meson.build
+++ b/doc/src/sgml/meson.build
@@ -104,7 +104,7 @@ if xsltproc_bin.found()
   xsltproc_flags = [
     '--nonet',
     '--stringparam', 'pg.version', pg_version,
-    '--param', 'website.stylesheet', '1'
+    '--xincludestyle',
   ]
 
   xsltproc = xmltools_wrapper + [
diff --git a/doc/src/sgml/stylesheet-common.xsl b/doc/src/sgml/stylesheet-common.xsl
index d2928f86eb7..23dea8f29bf 100644
--- a/doc/src/sgml/stylesheet-common.xsl
+++ b/doc/src/sgml/stylesheet-common.xsl
@@ -41,6 +41,8 @@
 <xsl:param name="variablelist.term.break.after">1</xsl:param>
 <xsl:param name="variablelist.term.separator"></xsl:param>
 <xsl:param name="xref.with.number.and.title" select="0"></xsl:param>
+<!-- currently htmlhelp and other html have no common stylesheet -->
+<xsl:param name="website.stylesheet" select="0"/>
 
 
 <!-- Change display of some elements -->
@@ -122,5 +124,24 @@
  </xsl:call-template>
 </xsl:template>
 
+<xsl:template name="stylesheet-reference">
+  <xsl:choose>
+    <xsl:when test="$website.stylesheet = 0">stylesheet.css
+    <xsl:variable name="styleout" select="concat($base.dir, 'stylesheet.css')"/>
+    <xsl:call-template name="write.chunk">
+      <xsl:with-param name="filename" select="$styleout"/>
+      <xsl:with-param name="quiet" select="0"/> <!-- XXX: remove -->
+      <xsl:with-param name="omit-xml-declaration" select="'yes'"/>
+      <xsl:with-param name="content">
+        <!-- xinclude is only processed in toplevel stylesheet -->
+        <xsl:call-template name="stylesheet-contents"/>
+      </xsl:with-param>
+    </xsl:call-template>
+    </xsl:when>
+    <xsl:otherwise>
+      https://www.postgresql.org/media/css/docs-complete.css
+    </xsl:otherwise>
+  </xsl:choose>
+</xsl:template>
 
 </xsl:stylesheet>
diff --git a/doc/src/sgml/stylesheet-hh.xsl b/doc/src/sgml/stylesheet-hh.xsl
index 568ccf36d2a..6f6c6c64626 100644
--- a/doc/src/sgml/stylesheet-hh.xsl
+++ b/doc/src/sgml/stylesheet-hh.xsl
@@ -1,5 +1,6 @@
 <?xml version='1.0'?>
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+                xmlns:xi="http://www.w3.org/2001/XInclude"
                 version='1.0'>
 
 <xsl:import href="http://docbook.sourceforge.net/release/xsl/current/htmlhelp/htmlhelp.xsl"/>
@@ -9,7 +10,6 @@
 <xsl:param name="htmlhelp.use.hhk" select="'1'"/>
 
 <xsl:param name="base.dir" select="'htmlhelp/'"></xsl:param>
-<xsl:param name="html.stylesheet" select="'stylesheet.css'"></xsl:param>
 <xsl:param name="use.id.as.filename" select="'1'"></xsl:param>
 <xsl:param name="manifest.in.base.dir" select="1"/>
 <xsl:param name="make.valid.html" select="1"></xsl:param>
@@ -19,6 +19,14 @@
 <xsl:param name="chunker.output.indent" select="'yes'"/>
 <xsl:param name="chunk.quietly" select="1"></xsl:param>
 
+<xsl:param name="html.stylesheet">
+  <xsl:call-template name="stylesheet-reference"/>
+</xsl:param>
+
+<xsl:template name="stylesheet-contents">
+  <xi:include href="stylesheet.css" parse="text"/>
+</xsl:template>
+
 
 <!-- Change display of some elements -->
 
diff --git a/doc/src/sgml/stylesheet-html-nochunk.xsl b/doc/src/sgml/stylesheet-html-nochunk.xsl
index 8167127b93a..560f6a0b0a7 100644
--- a/doc/src/sgml/stylesheet-html-nochunk.xsl
+++ b/doc/src/sgml/stylesheet-html-nochunk.xsl
@@ -1,5 +1,7 @@
 <?xml version='1.0'?>
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+                xmlns:xi="http://www.w3.org/2001/XInclude"
+                xmlns="http://www.w3.org/1999/xhtml"
                 version='1.0'>
 
 <xsl:import href="http://docbook.sourceforge.net/release/xsl/current/xhtml/docbook.xsl"/>
@@ -7,6 +9,24 @@
 <xsl:include href="stylesheet-html-common.xsl" />
 <xsl:include href="stylesheet-speedup-xhtml.xsl" />
 
+
+<xsl:param name="generate.css.header" select="1"/>
+<xsl:template name="generate.css.headers">
+  <xsl:choose>
+    <!-- inline css style sheet -->
+    <xsl:when test="$website.stylesheet = 0">
+      <style type="text/css">
+        <xi:include href="stylesheet.css" parse="text"/>
+      </style>
+    </xsl:when>
+    <!-- link to website -->
+    <xsl:otherwise>
+      <link rel="stylesheet" type="text/css" href="https://www.postgresql.org/media/css/docs-complete.css"/>
+    </xsl:otherwise>
+  </xsl:choose>
+</xsl:template>
+
+
 <!-- embed SVG images into output file -->
 <xsl:template match="imagedata[@format='SVG']">
   <xsl:variable name="filename">
diff --git a/doc/src/sgml/stylesheet.xsl b/doc/src/sgml/stylesheet.xsl
index f9163e7d946..c551b9cd809 100644
--- a/doc/src/sgml/stylesheet.xsl
+++ b/doc/src/sgml/stylesheet.xsl
@@ -1,7 +1,8 @@
 <?xml version='1.0'?>
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 version='1.0'
-                xmlns="http://www.w3.org/1999/xhtml">
+                xmlns="http://www.w3.org/1999/xhtml"
+                xmlns:xi="http://www.w3.org/2001/XInclude">
 
 <xsl:import href="http://docbook.sourceforge.net/release/xsl/current/xhtml/chunk.xsl"/>
 <xsl:include href="stylesheet-common.xsl" />
@@ -17,17 +18,13 @@
 <xsl:param name="chunk.quietly" select="1"></xsl:param>
 <xsl:param name="admon.style"></xsl:param>  <!-- handled by CSS stylesheet -->
 
-<xsl:param name="website.stylesheet" select="0"/>
-
 <xsl:param name="html.stylesheet">
-  <xsl:choose>
-    <xsl:when test="$website.stylesheet = 0">stylesheet.css</xsl:when>
-    <xsl:otherwise>
-      https://www.postgresql.org/media/css/docs-complete.css
-    </xsl:otherwise>
-  </xsl:choose>
+  <xsl:call-template name="stylesheet-reference"/>
 </xsl:param>
 
+<xsl:template name="stylesheet-contents">
+  <xi:include href="stylesheet.css" parse="text"/>
+</xsl:template>
 
 <xsl:template match="imageobject">
   <xsl:call-template name="write-image"/>
-- 
2.38.0

