]> Lady’s Gitweb - Shrine-XSLT/blobdiff - transform.xslt
Add basic microdata support
[Shrine-XSLT] / transform.xslt
index e8ecb3d55fe1ec7047b7c20eb7319673d2a49aca..876841fafbc1636159ff259b368298f5e4f4b06a 100644 (file)
@@ -22,9 +22,13 @@ ___
 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
 If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
 -->
+<!DOCTYPE xslt:transform [
+       <!ENTITY Atom "http://www.w3.org/2005/Atom">
+       <!ENTITY xhtml "http://www.w3.org/1999/xhtml">
+]>
 <xslt:transform
-       xmlns:atom="http://www.w3.org/2005/Atom"
-       xmlns:html="http://www.w3.org/1999/xhtml"
+       xmlns:atom="&Atom;"
+       xmlns:html="&xhtml;"
        xmlns:xslt="http://www.w3.org/1999/XSL/Transform"
        version="1.0"
 >
@@ -82,14 +86,14 @@ If a copy of the MPL was not distributed with this file, You can obtain one at h
        -->
        <xslt:template match="*" mode="content">
                <xslt:element name="{local-name()}">
-                       <xslt:for-each select="@*[not(starts-with(name(), 'data-shrine-')) and not(name()='slot' and starts-with(., 'shrine-'))]">
+                       <xslt:for-each select="@*[not(starts-with(name(), 'data-shrine-')) and not((name()='slot' or name()='itemprop') and starts-with(., 'shrine-'))]">
                                <xslt:copy/>
                        </xslt:for-each>
                        <xslt:for-each select="*|text()">
                                <xslt:choose>
                                        <xslt:when test="@slot[starts-with(., 'shrine-')]">
                                                <xslt:comment>
-                                                       <text> placeholder for slotted element </text>
+                                                       <xslt:text> placeholder for slotted element </xslt:text>
                                                </xslt:comment>
                                        </xslt:when>
                                        <xslt:otherwise>
@@ -100,6 +104,15 @@ If a copy of the MPL was not distributed with this file, You can obtain one at h
                </xslt:element>
        </xslt:template>
 
+       <!--
+               Drop `<meta>` elements which only provide shrine microdata.
+       -->
+       <xslt:template match="html:meta[not(@name)][starts-with(@itemprop, 'shrine-')]" mode="content">
+               <xslt:comment>
+                       <xslt:text> placeholder for metadata element </xslt:text>
+               </xslt:comment>
+       </xslt:template>
+
        <!--
                Process text content; just make a copy.
        -->
@@ -155,10 +168,20 @@ If a copy of the MPL was not distributed with this file, You can obtain one at h
                        <xslt:for-each select="@*">
                                <xslt:copy/>
                        </xslt:for-each>
-                       <xslt:if test="not($source//html:title[@slot='shrine-head'])">
-                               <title>
-                                       <xslt:apply-templates select="$source//html:h1" mode="text"/>
-                               </title>
+                       <xslt:if test="not(html:title) and not($source//html:title[@slot='shrine-head'])">
+                               <xslt:choose>
+                                       <xslt:when test="$source//*[@itemprop='shrine-title']">
+                                               <xslt:apply-templates select="$source//*[@itemprop='shrine-title'][1]" mode="microdata">
+                                                       <xslt:with-param name="tagname" select="'title'"/>
+                                                       <xslt:with-param name="plaintext" select="true()"/>
+                                               </xslt:apply-templates>
+                                       </xslt:when>
+                                       <xslt:when test="$source//html:h1">
+                                               <title>
+                                                       <xslt:apply-templates select="$source//html:h1[1]" mode="text"/>
+                                               </title>
+                                       </xslt:when>
+                               </xslt:choose>
                        </xslt:if>
                        <meta name="generator" content="https://github.com/marrus-sh/shrine-xslt"/>
                        <xslt:apply-templates mode="template"/>
@@ -246,7 +269,7 @@ If a copy of the MPL was not distributed with this file, You can obtain one at h
        -->
        <xslt:template match="atom:feed" mode="feed">
                <xslt:text>&#x0A;</xslt:text> <!-- ensure a newline between the doctype and the feed element -->
-               <feed xmlns="http://www.w3.org/2005/Atom">
+               <feed xmlns="&Atom;">
                        <xslt:for-each select="@*">
                                <xslt:copy/>
                        </xslt:for-each>
@@ -306,38 +329,97 @@ If a copy of the MPL was not distributed with this file, You can obtain one at h
        -->
        <xslt:template match="atom:entry[atom:link[@rel='alternate'][starts-with(@href, '/')][@type='text/html']]" mode="feed">
                <xslt:variable name="entryhref" select="atom:link[@rel='alternate'][starts-with(@href, '/')][@type='text/html'][1]/@href"/>
+               <xslt:variable name="srchref">
+                       <xslt:text>./sources</xslt:text>
+                       <xslt:choose>
+                               <xslt:when test="substring($entryhref, string-length($entryhref))='/'">
+                                       <xslt:value-of select="substring($entryhref, 1, string-length($entryhref) - 1)"/>
+                                       <xslt:text>.xml</xslt:text>
+                               </xslt:when>
+                               <xslt:when test="substring($entryhref, string-length($entryhref) - 4)='.html'">
+                                       <xslt:value-of select="substring($entryhref, 1, string-length($entryhref) - 4)"/>
+                                       <xslt:text>xml</xslt:text>
+                               </xslt:when>
+                               <xslt:otherwise>
+                                       <xslt:value-of select="$entryhref"/>
+                               </xslt:otherwise>
+                       </xslt:choose>
+               </xslt:variable>
+               <xslt:variable name="srcdoc" select="document($srchref)"/>
                <xslt:text>&#x0A;&#x09;</xslt:text>
-               <entry xmlns="http://www.w3.org/2005/Atom">
+               <entry xmlns="&Atom;">
                        <xslt:for-each select="@*">
                                <xslt:copy/>
                        </xslt:for-each>
                        <xslt:apply-templates select="text()[following-sibling::*]|*" mode="feed"/>
                        <xslt:if test="not(atom:id)">
                                <xslt:text>&#x0A;&#x09;&#x09;</xslt:text>
-                               <id>
-                                       <xslt:choose>
-                                               <xslt:when test="contains($baseiri, '://')">
-                                                       <xslt:text>oai:</xslt:text>
-                                                       <xslt:value-of select="substring-after($baseiri, '://')"/>
-                                                       <xslt:text>:</xslt:text>
-                                               </xslt:when>
-                                               <xslt:otherwise>
-                                                       <xslt:value-of select="$baseiri"/>
-                                                       <xslt:text>/</xslt:text>
-                                               </xslt:otherwise>
-                                       </xslt:choose>
-                                       <xslt:value-of select="$entryhref"/>
-                               </id>
+                               <xslt:choose>
+                                       <xslt:when test="$srcdoc//*[@itemprop='shrine-id']">
+                                               <xslt:apply-templates select="$srcdoc//*[@itemprop='shrine-id'][1]" mode="microdata">
+                                                       <xslt:with-param name="tagname" select="'id'"/>
+                                                       <xslt:with-param name="namespace" select="'&Atom;'"/>
+                                                       <xslt:with-param name="plaintext" select="true()"/>
+                                               </xslt:apply-templates>
+                                       </xslt:when>
+                                       <xslt:otherwise>
+                                               <id>
+                                                       <xslt:choose>
+                                                               <xslt:when test="contains($baseiri, '://')">
+                                                                       <xslt:text>oai:</xslt:text>
+                                                                       <xslt:value-of select="substring-after($baseiri, '://')"/>
+                                                                       <xslt:text>:</xslt:text>
+                                                               </xslt:when>
+                                                               <xslt:otherwise>
+                                                                       <xslt:value-of select="$baseiri"/>
+                                                                       <xslt:text>/</xslt:text>
+                                                               </xslt:otherwise>
+                                                       </xslt:choose>
+                                                       <xslt:value-of select="$entryhref"/>
+                                               </id>
+                                       </xslt:otherwise>
+                               </xslt:choose>
                        </xslt:if>
                        <xslt:if test="not(atom:title)">
                                <xslt:text>&#x0A;&#x09;&#x09;</xslt:text>
-                               <title xml:lang="en">Untitled</title>
+                               <xslt:choose>
+                                       <xslt:when test="$srcdoc//*[@itemprop='shrine-title']">
+                                               <xslt:apply-templates select="$srcdoc//*[@itemprop='shrine-title'][1]" mode="microdata">
+                                                       <xslt:with-param name="tagname" select="'title'"/>
+                                                       <xslt:with-param name="namespace" select="'&Atom;'"/>
+                                               </xslt:apply-templates>
+                                       </xslt:when>
+                                       <xslt:when test="$srcdoc//html:h1">
+                                               <xslt:apply-templates select="$srcdoc//html:h1[1]" mode="microdata">
+                                                       <xslt:with-param name="tagname" select="'title'"/>
+                                                       <xslt:with-param name="namespace" select="'&Atom;'"/>
+                                               </xslt:apply-templates>
+                                       </xslt:when>
+                                       <xslt:when test="$srcdoc//html:title">
+                                               <xslt:apply-templates select="$srcdoc//html:title[1]" mode="microdata">
+                                                       <xslt:with-param name="tagname" select="'title'"/>
+                                                       <xslt:with-param name="namespace" select="'&Atom;'"/>
+                                               </xslt:apply-templates>
+                                       </xslt:when>
+                                       <xslt:otherwise>
+                                               <title xml:lang="en">Untitled</title>
+                                       </xslt:otherwise>
+                               </xslt:choose>
                        </xslt:if>
                        <xslt:if test="not(atom:updated)">
                                <xslt:text>&#x0A;&#x09;&#x09;</xslt:text>
-                               <updated>
-                                       <xslt:value-of select="$datetime"/>
-                               </updated>
+                               <xslt:choose>
+                                       <xslt:when test="$srcdoc//*[@itemprop='shrine-updated']">
+                                               <xslt:apply-templates select="$srcdoc//*[@itemprop='shrine-updated'][1]" mode="microdata">
+                                                       <xslt:with-param name="tagname" select="'updated'"/>
+                                                       <xslt:with-param name="namespace" select="'&Atom;'"/>
+                                                       <xslt:with-param name="plaintext" select="true()"/>
+                                               </xslt:apply-templates>
+                                       </xslt:when>
+                                       <xslt:otherwise>
+                                               <xslt:value-of select="$datetime"/>
+                                       </xslt:otherwise>
+                               </xslt:choose>
                        </xslt:if>
                        <xslt:text>&#x0A;&#x09;</xslt:text> <!-- newline before close tag -->
                </entry>
@@ -345,10 +427,10 @@ If a copy of the MPL was not distributed with this file, You can obtain one at h
 
        <!--
                Process feed link elements.
-               This simply rewrites absolute links to be relative.
+               This simply rewrites relative links to be absolute.
        -->
        <xslt:template match="atom:link[starts-with(@href, '/')]" mode="feed">
-               <link xmlns="http://www.w3.org/2005/Atom" rel="{@rel}" href="{$baseiri}{@href}">
+               <link xmlns="&Atom;" rel="{@rel}" href="{$baseiri}{@href}">
                        <xslt:for-each select="@*[local-name()!='rel' and local-name()!='href']">
                                <xslt:copy/>
                        </xslt:for-each>
@@ -369,6 +451,60 @@ If a copy of the MPL was not distributed with this file, You can obtain one at h
                </xslt:choose>
        </xslt:template>
 
+       <!--
+               Provide the appropriate microdata for the provided element.
+       -->
+       <xslt:template match="*|text()" mode="microdata">
+               <xslt:param name="tagname"/>
+               <xslt:param name="namespace"/>
+               <xslt:param name="plaintext" select="false()"/>
+               <xslt:element name="{$tagname}" namespace="{$namespace}">
+                       <xslt:if test="@lang">
+                               <xslt:attribute name="xml:lang">
+                                       <xslt:value-of select="@lang"/>
+                               </xslt:attribute>
+                       </xslt:if>
+                       <xslt:choose>
+                               <xslt:when test="self::text()|self::html:title|self::html:time[not(@datetime)]">
+                                       <xslt:apply-templates select="." mode="text"/>
+                               </xslt:when>
+                               <xslt:when test="self::html:meta[@content]">
+                                       <xslt:value-of select="@content"/>
+                               </xslt:when>
+                               <xslt:when test="self::html:audio|self::html:embed|self::html:iframe|self::html:img|self::html:source|self::html:track|self::html:video">
+                                       <xslt:value-of select="@src"/>
+                               </xslt:when>
+                               <xslt:when test="self::html:a|self::html:area|self::html:link">
+                                       <xslt:value-of select="@href"/>
+                               </xslt:when>
+                               <xslt:when test="self::html:object">
+                                       <xslt:value-of select="@data"/>
+                               </xslt:when>
+                               <xslt:when test="self::html:data|self::html:meter">
+                                       <xslt:value-of select="@value"/>
+                               </xslt:when>
+                               <xslt:when test="self::html:time[@datetime]">
+                                       <xslt:value-of select="@datetime"/>
+                               </xslt:when>
+                               <xslt:otherwise>
+                                       <xslt:choose>
+                                               <xslt:when test="$plaintext">
+                                                       <xslt:apply-templates mode="text"/>
+                                               </xslt:when>
+                                               <xslt:otherwise>
+                                                       <xslt:if test="$namespace='&Atom;'">
+                                                               <xslt:attribute name="type">xhtml</xslt:attribute>
+                                                       </xslt:if>
+                                                       <div xmlns="&xhtml;">
+                                                               <xslt:apply-templates mode="content"/>
+                                                       </div>
+                                               </xslt:otherwise>
+                                       </xslt:choose>
+                               </xslt:otherwise>
+                       </xslt:choose>
+               </xslt:element>
+       </xslt:template>
+
        <!--
                Set up output.
                Note that this relies on “default” output method detection specified in X·S·L·T in order to work.
This page took 0.061143 seconds and 4 git commands to generate.