Skip to content

Commit

Permalink
Add social-media-opengraph & social-media-twitter
Browse files Browse the repository at this point in the history
  • Loading branch information
tomschr committed May 5, 2023
1 parent 4a3cfd3 commit 9308270
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 51 deletions.
60 changes: 9 additions & 51 deletions suse2022-ns/xhtml/docbook.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<xsl:include href="titlepage.templates.xsl"/>

<xsl:include href="tracker.meta.xsl"/>
<xsl:include href="meta.xsl"/>


<!-- Actual templates start here -->
Expand Down Expand Up @@ -248,7 +249,6 @@
<xsl:with-param name="ellipsize.after" select="$socialmedia.title.length"/>
</xsl:call-template>
</xsl:variable>

<xsl:variable name="socialmedia.preview">
<xsl:choose>
<!-- We ignore:
Expand Down Expand Up @@ -348,57 +348,15 @@

<xsl:apply-templates select="." mode="head.keywords.content"/>

<xsl:if test="$canonical-url-base != ''">
<xsl:variable name="ischunk">
<xsl:call-template name="chunk"/>
</xsl:variable>
<xsl:variable name="filename">
<xsl:choose>
<xsl:when test="$ischunk = 1">
<xsl:apply-templates mode="chunk-filename" select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat($root.filename,$html.ext)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="canonical.url">
<xsl:value-of select="concat($canonical-url-base,'/',$filename)"/>
</xsl:variable>
<xsl:call-template name="social-media-opengraph">
<xsl:with-param name="socialmedia.title" select="$socialmedia.title"/>
<xsl:with-param name="socialmedia.description" select="$socialmedia.description"/>
</xsl:call-template>

<link rel="canonical" href="{$canonical.url}"/>
<xsl:text>&#10;</xsl:text>
<!-- These Open Graph and Twitter Cards properties need a canonical URL -->
<meta property="og:url" content="{$canonical.url}"/>
<xsl:text>&#10;</xsl:text>
<meta property="og:image" content="{concat($canonical-url-base,'/',$socialmedia.preview)}"/>
<xsl:text>&#10;</xsl:text>
<meta name="twitter:image" content="{concat($canonical-url-base,'/',$socialmedia.preview)}"/>
<xsl:text>&#10;</xsl:text>

<xsl:call-template name="meta-generator"/>
</xsl:if>

<!-- The following properties "work" without a canonical URL being defined
(but both the Open Graph and Twitter Cards implementations are incomplete
without the above tags, better than nothing though). -->
<meta property="og:title" content="{$socialmedia.title}"/>
<xsl:text>&#10;</xsl:text>
<meta property="og:description" content="{$socialmedia.description}"/>
<xsl:text>&#10;</xsl:text>
<meta property="og:type" content="{$opengraph.type}"/>
<xsl:text>&#10;</xsl:text>

<meta name="twitter:card" content="{$twittercards.type}"/>
<xsl:text>&#10;</xsl:text>
<meta name="twitter:title" content="{$socialmedia.title}"/>
<xsl:text>&#10;</xsl:text>
<meta name="twitter:description" content="{$socialmedia.description}"/>
<xsl:text>&#10;</xsl:text>
<xsl:if test="string-length($twittercards.twitter.account) &gt; 0">
<meta name="twitter:site" content="{$twittercards.twitter.account}"/>
<xsl:text>&#10;</xsl:text>
</xsl:if>
<xsl:call-template name="social-media-twitter">
<xsl:with-param name="socialmedia.title" select="$socialmedia.title"/>
<xsl:with-param name="socialmedia.description" select="$socialmedia.description"/>
</xsl:call-template>
</xsl:template>


Expand Down
125 changes: 125 additions & 0 deletions suse2022-ns/xhtml/meta.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">

<xsl:template match="d:meta[@name='productname']" mode="meta">
<xsl:variable name="productname" select="normalize-space(string(d:productname[1]))"/>
<xsl:value-of select="$productname"/>
</xsl:template>

<xsl:template match="d:meta[@name='productname']/d:productname[1]/@version" mode="meta">
<xsl:value-of select="."/>
</xsl:template>


<xsl:template name="social-media-opengraph">
<xsl:param name="node" select="."/>
<xsl:param name="socialmedia.title"/>
<xsl:param name="socialmedia.description"/>
<xsl:param name="socialmedia.preview"/>
<xsl:param name="canonical-url-base"/>
<xsl:variable name="ischunk">
<xsl:call-template name="chunk"/>
</xsl:variable>
<xsl:variable name="filename">
<xsl:choose>
<xsl:when test="$ischunk = 1">
<xsl:apply-templates mode="chunk-filename" select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat($root.filename,$html.ext)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="canonical.url" select="concat($canonical-url-base,'/',$filename)"/>
<xsl:variable name="meta.nodes" select="d:info/d:meta|ancestor::*/d:info/d:meta"/>

<xsl:if test="$canonical-url-base != ''">
<!-- These Open Graph and Twitter Cards properties need a canonical URL -->
<link rel="canonical" href="{$canonical.url}"/>
<xsl:text>&#10;</xsl:text>
<!-- These Open Graph and Twitter Cards properties need a canonical URL -->
<meta property="og:url" content="{$canonical.url}"/>
<xsl:text>&#10;</xsl:text>
<meta property="og:image" content="{concat($canonical-url-base,'/',$socialmedia.preview)}"/>
<xsl:text>&#10;</xsl:text>
<xsl:call-template name="meta-generator"/>
</xsl:if>

<xsl:message>social-media-opengraph: <xsl:value-of select="local-name($node)"/></xsl:message>

<xsl:choose>
<xsl:when test="$meta.nodes[@name='title']">
<xsl:message>#############: <xsl:value-of select="normalize-space($meta.nodes[@name='title'][last()])"/></xsl:message>
<meta property="og:title" content="{normalize-space($meta.nodes[@name='title'][last()])}"/>
</xsl:when>
<xsl:otherwise>
<meta property="og:title" content="{$socialmedia.title}"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>&#10;</xsl:text>

<xsl:choose>
<xsl:when test="$meta.nodes[@name='description']">
<meta property="og:description" content="{normalize-space($meta.nodes[@name='description'][last()])}" />
</xsl:when>
<xsl:otherwise>
<meta property="og:description" content="{$socialmedia.description}"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>&#10;</xsl:text>

<meta property="og:type" content="{$opengraph.type}"/>
<xsl:text>&#10;</xsl:text>
</xsl:template>


<xsl:template name="social-media-twitter">
<xsl:param name="node" select="."/>
<xsl:param name="socialmedia.title"/>
<xsl:param name="socialmedia.description"/>
<xsl:param name="socialmedia.preview"/>
<xsl:param name="canonical-url-base"/>
<xsl:variable name="meta.nodes" select="d:info/d:meta|ancestor::*/d:info/d:meta"/>

<meta name="twitter:card" content="{$twittercards.type}"/>
<xsl:text>&#10;</xsl:text>

<xsl:if test="$canonical-url-base != ''">
<meta name="twitter:image" content="{concat($canonical-url-base,'/',$socialmedia.preview)}"/>
<xsl:text>&#10;</xsl:text>
</xsl:if>

<xsl:choose>
<xsl:when test="$meta.nodes[@name='title']">
<meta property="twitter:title" content="{normalize-space($meta.nodes[@name='title'][last()])}" />
</xsl:when>
<xsl:otherwise>
<meta name="twitter:title" content="{$socialmedia.title}"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>&#10;</xsl:text>

<xsl:choose>
<xsl:when test="$meta.nodes[@name='description']">
<meta name="twitter:description" content="{normalize-space($meta.nodes[@name='description'][last()])}" />
</xsl:when>
<xsl:otherwise>
<meta name="twitter:description" content="{$socialmedia.description}"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>&#10;</xsl:text>

<xsl:if test="string-length($twittercards.twitter.account) &gt; 0">
<meta name="twitter:site" content="{$twittercards.twitter.account}"/>
<xsl:text>&#10;</xsl:text>
</xsl:if>
</xsl:template>

</xsl:stylesheet>
8 changes: 8 additions & 0 deletions suse2022-ns/xhtml/titlepage.templates.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@
In general, we want the long/official version, though. Dito for the
productnumber below. -->
<xsl:param name="prefer-abbreviation" select="0"/>
<xsl:variable name="meta.nodes" select="d:info/d:meta|ancestor::*/d:info/d:meta"/>

<!--
First we search for all productname[@role='abbrev'], starting in the nearest node followed
by its ancestors.
-->
<xsl:choose>
<xsl:when test="$meta.nodes[@name='productname'] and $prefer-abbreviation = 0">/
<xsl:apply-templates select="$meta.nodes[@name='productname'][last()]" mode="meta"/>
</xsl:when>
<xsl:when test="*/d:productname[@role='abbrev'] and $prefer-abbreviation = 1">
<xsl:apply-templates select="(*/d:productname[@role='abbrev'])[last()]"/>
</xsl:when>
Expand All @@ -53,10 +57,14 @@
<xsl:template name="product.number">
<!-- See comment in product.name... -->
<xsl:param name="prefer-abbreviation" select="0"/>
<xsl:variable name="meta.nodes" select="d:info/d:meta|ancestor::*/d:info/d:meta"/>

<!-- FIXME: This choose mechanism is a little wonky around inheritance and
abbreviation preference. May need a bit more think. -->
<xsl:choose>
<xsl:when test="$meta.nodes[@name='productname'] and $prefer-abbreviation = 0">
<xsl:apply-templates select="$meta.nodes[@name='productname'][last()]/d:productname[1]/@version" mode="meta"/>
</xsl:when>
<xsl:when test="*/d:productnumber[@role='abbrev'] and $prefer-abbreviation = 1">
<xsl:apply-templates select="(*/d:productnumber[@role='abbrev'])[last()]"/>
</xsl:when>
Expand Down

0 comments on commit 9308270

Please sign in to comment.