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 64cd7a6
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 45 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
51 changes: 42 additions & 9 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 Expand Up @@ -762,25 +782,38 @@

<xsl:template name="json-ld-about">
<xsl:param name="node" select="."/>

"about": [
<xsl:variable name="tasks">
<xsl:call-template name="json-ld-task">
<xsl:with-param name="node" select="$node"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="webpages">
<xsl:call-template name="json-ld-webpages">
<xsl:with-param name="node" select="$node"/>
</xsl:call-template>
</xsl:variable>

"about": [
<xsl:value-of select="$tasks"/>
<xsl:if test="normalize-space($webpages) != ''">
<xsl:if test="normalize-space($tasks) != ''">
<xsl:text>,&#10;</xsl:text>
</xsl:if>
<xsl:value-of select="$webpages"/>
</xsl:if>
],
</xsl:template>

<xsl:template name="json-ld-task">
<xsl:param name="node" select="."/>
<xsl:variable name="task" select="$node/d:info/d:meta[@name='task']/d:phrase"/>
<xsl:if test="count($task) > 0">
<xsl:for-each select="$task">{
<xsl:variable name="tasks" select="$node/d:info/d:meta[@name='task']/d:phrase"/>
<xsl:if test="count($tasks) > 0">
<xsl:for-each select="$tasks">{
"@type": "Thing",
"name": "<xsl:value-of select="normalize-space(.)"/>"
},<!-- <== This comma is essential here. -->
}<xsl:if test="position() &lt; last()">
<xsl:text>,&#10; </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:template>
Expand Down

0 comments on commit 64cd7a6

Please sign in to comment.