Skip to content

Commit

Permalink
Merge pull request #622 from openSUSE/metadata-warnings
Browse files Browse the repository at this point in the history
Fix JSON-LD generation for single & chunked HTML
  • Loading branch information
tomschr authored Mar 27, 2024
2 parents 8d10229 + a6a93db commit d4fd140
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 50 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
79 changes: 70 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 @@ -584,6 +604,13 @@
<xsl:when test="normalize-space($node/d:info/d:date) != ''">
<xsl:value-of select="normalize-space(string($node/d:info/d:date))"/>
</xsl:when>
<!-- Try to find a date from the ancestor nodes -->
<xsl:when test="$node/ancestor-or-self::*/d:info/d:meta[@name='published']">
<xsl:value-of select="normalize-space(string($node/ancestor-or-self::*/d:info/d:meta[@name='published']))"/>
</xsl:when>
<xsl:when test="$node/ancestor-or-self::*/d:info/d:revhistory/d:revision[1]/d:date">
<xsl:value-of select="normalize-space(string($node/ancestor-or-self::*/d:info/d:revhistory/d:revision[1]/d:date))"/>
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:variable name="date">
Expand Down Expand Up @@ -755,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 Expand Up @@ -858,6 +898,27 @@
</xsl:variable>
<xsl:variable name="format-node" select="exsl:node-set($candicate-format-node)/*/*"/>

<!--
<xsl:message>JSON-LD json-ld-webpages
dcfile=<xsl:value-of select="$dcfile"/>
filepart=<xsl:value-of select="$filepart"/>
docsetid=<xsl:value-of select="$candidate-docsetid"/>
productid=<xsl:value-of select="$candidate-productid"/>
format-node=<xsl:value-of select="count($format-node)"/>
</xsl:message>
-->

<xsl:if test="count($format-node) = 0">
<xsl:call-template name="log.message">
<xsl:with-param name="level">error</xsl:with-param>
<xsl:with-param name="context-desc">JSON-LD</xsl:with-param>
<xsl:with-param name="message">
<xsl:text>Could not create WebPage type for DC file </xsl:text>
<xsl:value-of select="$dcfile"/>
</xsl:with-param>
</xsl:call-template>
</xsl:if>

<xsl:for-each select="$format-node">
<xsl:variable name="attr" select="."/>
<xsl:variable name="candidate-url" select="concat($json-ld-fallback-author-url, '/',
Expand Down
12 changes: 7 additions & 5 deletions suse2022-ns/xhtml/meta.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<xsl:variable name="content" select="normalize-space(@content)"/>

<xsl:choose>
<xsl:when test="$content">
<xsl:when test="$content != ''">
<xsl:if test="$include.html.dublincore">
<meta name="DC.creator" content="{$content}" />
</xsl:if>
Expand All @@ -45,7 +45,7 @@
<xsl:variable name="content" select="normalize-space(@content)"/>

<xsl:choose>
<xsl:when test="$content">
<xsl:when test="$content != ''">
<xsl:if test="$include.html.dublincore">
<meta name="DCTERMS.modified" content="{$content}" />
</xsl:if>
Expand All @@ -63,11 +63,13 @@
</xsl:template>

<xsl:template match="d:meta[@name='category']" mode="meta">
<xsl:variable name="content" select="normalize-space(@content)"/>
<xsl:variable name="content" select="*"/>

<xsl:choose>
<xsl:when test="$content">
<meta name="category" content="{$content}"/>
<xsl:when test="$content != ''">
<xsl:for-each select="d:phrase">
<meta name="category" content="."/>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="log.message">
Expand Down

0 comments on commit d4fd140

Please sign in to comment.