Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check date types #584

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions suse2022-ns/common/check-types.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Purpose:
Checks more complex data types

Input:
A string

Output:
Single XHTML file

See Also:

Authors: Thomas Schraitle <[email protected]>

-->

<!DOCTYPE xsl:stylesheet [
<!ENTITY ascii.uc "ABCDEFGHIJKLMNOPQRSTUVWXYZ">
<!ENTITY ascii.lc "abcdefghijklmnopqrstuvwxy">
]>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:d="http://docbook.org/ns/docbook"
xmlns:dm="urn:x-suse:ns:docmanager"
xmlns:exsl="http://exslt.org/common"
xmlns:date="http://exslt.org/dates-and-times"
xmlns="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="exsl date d dm">


<!--
Template function is-valid-iso-8601-date

Parameter text (string):
the string to check for the date format "YYYY-MM-DD"
Return: bool
true() if the date matches the ISO8601 format, otherwise false()
-->
<xsl:template name="is-valid-iso-8601-date">
<!-- Checks, if $text contains "YYYY-MM-DD" -->
<xsl:param name="text"/>
<xsl:param name="check-day" select="1"/>
<xsl:variable name="year" select="number(substring-before($text, '-'))"/>
<xsl:variable name="month" select="number(substring($text, 6, 2))"/>
<xsl:variable name="day" select="number(substring($text, 9, 2))"/>
<xsl:variable name="bool-check-day">
<xsl:call-template name="is-valid-boolean">
<xsl:with-param name="text" select="$check-day"/>
</xsl:call-template>
</xsl:variable>

<!-- <xsl:message>is-valid-iso-8601-date:
text="<xsl:value-of select="$text"/>"
year="<xsl:value-of select="$year"/>"
month="<xsl:value-of select="$month"/>"
day="<xsl:value-of select="$day"/>"
boolean($check-day)=<xsl:value-of select="$bool-check-day"/>
</xsl:message>-->

<xsl:choose>
<xsl:when test="string-length($text) != 10">
<xsl:value-of select="false()"/>
</xsl:when>
<xsl:when test="$year = 'NaN' or $month = 'NaN'">
<xsl:value-of select="false()"/>
</xsl:when>
<xsl:when test="$bool-check-day = true() and number($day) = 'NaN'">
<xsl:value-of select="false()"/>
</xsl:when>
<xsl:when test="$year &lt; 1999">
<xsl:value-of select="false()"/>
</xsl:when>
<xsl:when test="$month &lt; 1 or $month > 12">
<xsl:value-of select="false()"/>
</xsl:when>
<xsl:when test="$bool-check-day = true() and ($day &lt; 1 or $day > 31)">
<xsl:value-of select="false()"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="true()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>


<!--
Template function is-boolean

Parameter text (string):
the string to check if it's a boolean. Valid values are:
* 1, on, yes, YES for true()
* 0, off, no, NO, "" for false()
Return: bool
true() if it's a bool, false() otherwise
-->
<xsl:template name="is-valid-boolean">
<xsl:param name="text"/>
<xsl:variable name="lc.text" select="translate($text, '&ascii.uc;', '&ascii.lc;')"/>

<xsl:choose>
<xsl:when test="$lc.text = 'yes' or $lc.text = 'on' or $lc.text = 'true'">
<xsl:value-of select="true()"/>
</xsl:when>
<xsl:when test="$lc.text = 'no' or $lc.text = 'off' or $lc.text = 'false'">
<xsl:value-of select="true()"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="boolean($text)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>
1 change: 1 addition & 0 deletions suse2022-ns/xhtml/docbook.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

<xsl:include href="../VERSION.xsl"/>

<xsl:include href="../common/check-types.xsl"/>
<xsl:include href="../common/dates-revisions.xsl"/>
<xsl:include href="../common/labels.xsl"/>
<xsl:include href="../common/titles.xsl"/>
Expand Down
45 changes: 41 additions & 4 deletions suse2022-ns/xhtml/json-ld.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<xsl:template name="generate-json-ld">
<xsl:param name="node"/>
<xsl:if test="$generate.json-ld != 0">
<xsl:message>INFO: Going to generate JSON-LD...</xsl:message>
<xsl:message>INFO: Generating JSON-LD...</xsl:message>
<script type="application/ld+json">
{
"@context": "http://schema.org",
Expand Down Expand Up @@ -297,17 +297,36 @@
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:variable name="date-check">
<xsl:call-template name="is-valid-iso-8601-date">
<xsl:with-param name="text" select="$date"/>
</xsl:call-template>
</xsl:variable>
<!-- If day is missing in YYYY-MM-DD, append "-01" and try again to build a new date -->
<xsl:variable name="new-date" select="concat($date, '-01')"/>
<xsl:variable name="date-check-new">
<xsl:call-template name="is-valid-iso-8601-date">
<xsl:with-param name="text" select="$new-date" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="candidate-date">
<xsl:choose>
<xsl:when test="$date-check-new"><xsl:value-of select="$new-date"/></xsl:when>
<xsl:when test="$date-check"><xsl:value-of select="$date"/></xsl:when>
<xsl:otherwise/>
</xsl:choose>
</xsl:variable>

<xsl:choose>
<xsl:when test="$date != ''">
"datePublished": "<xsl:value-of select="$date"/>",
<xsl:when test="$candidate-date != ''">
"datePublished": "<xsl:value-of select="$candidate-date"/>",
</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">JSON-LD</xsl:with-param>
<xsl:with-param name="message">
<xsl:text>Could not create "datePublished" property as no element was appropriate.</xsl:text>
<xsl:text>Could not create "datePublished" property as no element/value was appropriate.</xsl:text>
</xsl:with-param>
</xsl:call-template>
</xsl:otherwise>
Expand All @@ -326,6 +345,24 @@
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:variable name="date-check">
<xsl:call-template name="is-valid-iso-8601-date">
<xsl:with-param name="text" select="$date"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="new-date" select="concat($date, '-01')"/>
<xsl:variable name="date-check-new">
<xsl:call-template name="is-valid-iso-8601-date">
<xsl:with-param name="text" select="$new-date" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="candidate-date">
<xsl:choose>
<xsl:when test="$date-check-new"><xsl:value-of select="$new-date"/></xsl:when>
<xsl:when test="$date-check"><xsl:value-of select="$date"/></xsl:when>
<xsl:otherwise/>
</xsl:choose>
</xsl:variable>

<xsl:choose>
<xsl:when test="$date != ''">
Expand Down