Skip to content

Commit

Permalink
Fix JSON-LD generation for single & regular HTML
Browse files Browse the repository at this point in the history
* Create "handle-json-ld" named template that handles both single and
  regular HTML
* Introduce xsl:choose inside json-ld-headline to handle the different
  cases better
  • Loading branch information
tomschr committed Mar 27, 2024
1 parent 79ead1d commit 9a5d0ad
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 39 deletions.
19 changes: 1 addition & 18 deletions suse2022-ns/xhtml/chunk.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,6 @@

<xsl:template match="/">
<xsl:apply-imports/>
<xsl:choose>
<xsl:when test="$dcfilename != ''">
<xsl:call-template name="generate-json-ld-external">
<xsl:with-param name="node" select="*[1]" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="log.message">
<xsl:with-param name="level">WARN</xsl:with-param>
<xsl:with-param name="context-desc">
<xsl:text>JSON-LD</xsl:text>
</xsl:with-param>
<xsl:with-param name="message">
<xsl:text>The parameter $dcfilename is unset. Cannot create the external JSON file.</xsl:text>
</xsl:with-param>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
<xsl:call-template name="handle-json-ld"/>
</xsl:template>
</xsl:stylesheet>
47 changes: 29 additions & 18 deletions suse2022-ns/xhtml/docbook.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -700,29 +700,40 @@
</xsl:template>


<xsl:template name="handle-json-ld">
<xsl:choose>
<xsl:when test="$rootid != '' and $dcfilename != ''">
<xsl:call-template name="generate-json-ld-external">
<xsl:with-param name="node" select="key('id', $rootid)" />
<xsl:with-param name="first" select="1"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="$dcfilename != ''">
<xsl:call-template name="generate-json-ld-external">
<xsl:with-param name="node" select="*[1]" />
<xsl:with-param name="first" select="1"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="log.message">
<xsl:with-param name="level">WARN</xsl:with-param>
<xsl:with-param name="context-desc">
<xsl:text>JSON-LD</xsl:text>
</xsl:with-param>
<xsl:with-param name="message">
<xsl:text>The parameter $dcfilename is unset. Cannot create the external JSON file.</xsl:text>
</xsl:with-param>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<!-- ############################################################## -->
<!-- This template is called when creating single HTML -->
<xsl:template match="/">
<xsl:apply-imports/>
<xsl:if test="$is.chunk = 0">
<xsl:choose>
<xsl:when test="$dcfilename != ''">
<xsl:call-template name="generate-json-ld-external">
<xsl:with-param name="node" select="." />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="log.message">
<xsl:with-param name="level">WARN</xsl:with-param>
<xsl:with-param name="context-desc">
<xsl:text>JSON-LD</xsl:text>
</xsl:with-param>
<xsl:with-param name="message">
<xsl:text>The parameter $dcfilename is unset. Cannot create the external JSON file.</xsl:text>
</xsl:with-param>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
<xsl:call-template name="handle-json-ld"/>
</xsl:if>
</xsl:template>

Expand Down
26 changes: 23 additions & 3 deletions suse2022-ns/xhtml/json-ld.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
<!-- ========================================================== -->
<xsl:template name="generate-json-ld-external">
<xsl:param name="node" select="."/>
<xsl:param name="first" select="0" />
<xsl:variable name="base">
<xsl:call-template name="get-basename">
<xsl:with-param name="node" select="$node"/>
Expand Down Expand Up @@ -236,7 +237,7 @@
</xsl:call-template>
</xsl:variable>

<xsl:if test="$generate.json-ld.external != 0">
<xsl:if test="$generate.json-ld.external != 0 and $first = 1">
<xsl:variable name="lang">
<xsl:call-template name="l10n.language"/>
</xsl:variable>
Expand Down Expand Up @@ -361,8 +362,27 @@

<xsl:template name="json-ld-headline">
<xsl:param name="node" select="."/>
<xsl:variable name="headline"
select="normalize-space(($node/d:info/d:meta[@name='title'] | $node/d:info/d:title | $node/d:title)[last()])"/>
<xsl:variable name="candidate-headline">
<xsl:choose>
<xsl:when test="$node/d:info/d:meta[@name='title']">
<xsl:value-of select="$node/d:info/d:meta[@name='title']"/>
</xsl:when>
<xsl:when test="$node/d:info/d:title">
<xsl:value-of select="$node/d:info/d:title"/>
</xsl:when>
<xsl:when test="$node/d:title">
<xsl:value-of select="$node/d:title"/>
</xsl:when>
<!--<xsl:when test="$node/ancestor-or-self::*/d:info/d:meta[@name='title']">
<xsl:value-of select="($node/ancestor-or-self::*/d:info/d:meta[@name='title'])[last()]"/>
</xsl:when>-->
<!--<xsl:when test="$node/ancestor-or-self::*/d:info/d:title">
<xsl:value-of select="($node/ancestor-or-self::*/d:info/d:title)[last()]"/>
</xsl:when>-->
</xsl:choose>
</xsl:variable>
<xsl:variable name="headline" select="normalize-space($candidate-headline)"/>

"headline": "<xsl:value-of select="translate($headline, '&quot;', '')"/>",
</xsl:template>

Expand Down

0 comments on commit 9a5d0ad

Please sign in to comment.