From 37c880b04e13efa36b67832c647255ee80e726bd Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Sun, 19 Mar 2023 19:29:41 -0700
Subject: [PATCH v3 4/4] VERY WIP: parallel doc generation

---
 doc/src/sgml/meson.build          | 17 +++++++++++
 doc/src/sgml/postgres.sgml        |  2 +-
 doc/src/sgml/stylesheet.xsl       | 49 +++++++++++++++++++++++++++++++
 doc/src/sgml/xmltools_dep_wrapper |  5 ++++
 4 files changed, 72 insertions(+), 1 deletion(-)

diff --git a/doc/src/sgml/meson.build b/doc/src/sgml/meson.build
index d9c46224dc9..c6195965a71 100644
--- a/doc/src/sgml/meson.build
+++ b/doc/src/sgml/meson.build
@@ -121,6 +121,23 @@ if xsltproc_bin.found()
   )
   alldocs += html
 
+  parts = ['bookinfo', 'preface', 'tutorial', 'sql', 'admin', 'client-interfaces', 'server-programming', 'reference', 'internals', 'appendixes', 'biblio', 'bookindex']
+  html_parts = []
+  foreach part : parts
+      html_parts += custom_target('html-@0@'.format(part),
+      input: ['stylesheet.xsl', postgres_full_xml],
+      output: 'html-stamp-@0@'.format(part),
+      depfile: 'html.d',
+      depends: doc_generated,
+      command: [
+        xmltools_wrapper, '--tool', xsltproc_bin, '--stamp', '@OUTPUT0@', '--',
+        '-o', '@OUTDIR@/', xsltproc_flags, '--stringparam', 'rootid', part, '@INPUT@',
+      ],
+      build_by_default: false,
+    )
+  endforeach
+  alias_target('html-parts', html_parts)
+
   install_doc_html = custom_target('install-html',
     output: 'install-html',
     command: [
diff --git a/doc/src/sgml/postgres.sgml b/doc/src/sgml/postgres.sgml
index 2e271862fc1..a169fd6f30a 100644
--- a/doc/src/sgml/postgres.sgml
+++ b/doc/src/sgml/postgres.sgml
@@ -23,7 +23,7 @@ break is not needed in a wider output rendering.
 <book id="postgres">
  <title>PostgreSQL &version; Documentation</title>
 
- <bookinfo>
+ <bookinfo id="bookinfo">
   <corpauthor>The PostgreSQL Global Development Group</corpauthor>
   <productname>PostgreSQL</productname>
   <productnumber>&version;</productnumber>
diff --git a/doc/src/sgml/stylesheet.xsl b/doc/src/sgml/stylesheet.xsl
index 65606ca3dbe..f9986d17f4a 100644
--- a/doc/src/sgml/stylesheet.xsl
+++ b/doc/src/sgml/stylesheet.xsl
@@ -28,6 +28,55 @@
  <xsl:value-of select="substring-after(., '/')"/>
 </xsl:template>
 
+<!-- Emit index.html and legalnotice.html -->
+<xsl:template name="pg.write.bookinfo">
+   <xsl:for-each select="/book">
+     <xsl:call-template name="process-chunk">
+       <xsl:with-param name="prev" select="/dontexist"/>
+       <xsl:with-param name="next" select="preface"/>
+       <xsl:with-param name="content">
+
+         <div>
+           <xsl:apply-templates select="." mode="common.html.attributes"/>
+           <xsl:call-template name="id.attribute">
+             <xsl:with-param name="conditional" select="0"/>
+           </xsl:call-template>
+
+           <xsl:call-template name="book.titlepage"/>
+
+           <xsl:apply-templates select="dedication" mode="dedication"/>
+           <xsl:apply-templates select="acknowledgements" mode="acknowledgements"/>
+
+           <xsl:variable name="toc.params">
+             <xsl:call-template name="find.path.params">
+               <xsl:with-param name="table" select="normalize-space($generate.toc)"/>
+             </xsl:call-template>
+           </xsl:variable>
+
+           <xsl:call-template name="make.lots">
+             <xsl:with-param name="toc.params" select="$toc.params"/>
+             <xsl:with-param name="toc">
+               <xsl:call-template name="division.toc">
+                 <xsl:with-param name="toc.title.p" select="contains($toc.params, 'title')"/>
+               </xsl:call-template>
+             </xsl:with-param>
+           </xsl:call-template>
+         </div>
+       </xsl:with-param>
+     </xsl:call-template>
+   </xsl:for-each>
+</xsl:template>
+
+<xsl:template match="/book/bookinfo" mode="process.root" priority="2">
+  <xsl:choose>
+    <xsl:when test="$rootid = 'bookinfo'">
+      <xsl:call-template name="pg.write.bookinfo"/>
+    </xsl:when>
+    <xsl:otherwise>
+      <xsl:apply-imports/>
+    </xsl:otherwise>
+  </xsl:choose>
+</xsl:template>
 
 <!--
 Customization of header
diff --git a/doc/src/sgml/xmltools_dep_wrapper b/doc/src/sgml/xmltools_dep_wrapper
index dd96f784268..a5f5981dcee 100644
--- a/doc/src/sgml/xmltools_dep_wrapper
+++ b/doc/src/sgml/xmltools_dep_wrapper
@@ -14,6 +14,7 @@ parser = argparse.ArgumentParser(
 parser.add_argument('--targetname', type=str, required=False, nargs='+')
 parser.add_argument('--depfile', type=str, required=False)
 parser.add_argument('--tool', type=str, required=True)
+parser.add_argument('--stamp', type=str, required=False, default=None)
 parser.add_argument('flags', nargs='*')
 
 args = parser.parse_args()
@@ -51,4 +52,8 @@ else:
     command = [args.tool] + args.flags
     res = subprocess.run(command)
 
+if args.stamp is not None:
+    with open(args.stamp, "w") as f:
+        pass
+
 exit(res.returncode)
-- 
2.38.0

