Commit a155cdc0 authored by Wendell Piez's avatar Wendell Piez

Restoring old list XSLTs; adjustment for #104 (paragraph-level properties)

parent 5dbe0417
......@@ -77,7 +77,7 @@ Note the following mappings:
<!-- We can strip 'span' elements when they are coextensive with their wrapping p and
have nothing but @style to offer, as the latter is being promoted. -->
<xsl:template match="p//span[empty(@class)]
[normalize-space(.) = normalize-space(ancestor::p[1]) and count(../(* except tab)) eq 1]">
[normalize-space(.) = normalize-space(ancestor::p[1]) and count(../*) eq 1]">
<xsl:apply-templates/>
</xsl:template>
......
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsw="http://coko.foundation/xsweet"
xmlns="http://www.w3.org/1999/xhtml"
xpath-default-namespace="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="#all">
<xsl:output method="xml" indent="no" omit-xml-declaration="yes"/>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<!-- This comes out of XSweet. -->
<xsl:template match="div[@class='docx-body']">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:call-template name="sequence-lists"/>
</xsl:copy>
</xsl:template>
<!-- We can also subject sections to sectioning if they have anything more than a single
header, or if their header is preceded by contents.
Other sections should be considered already well-structured and passed through intact. -->
<!-- (But a better approach to this may be to strip sections on the way in, then rebuild them.) -->
<!--<xsl:template match="section[exists( (h1|h2|h3|h4|h5|h6)[1]/(preceding-sibling::* |
following-sibling::*/(self::h1|self::h2|self::h3|self::h4|self::h5|self::h6)))]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:call-template name="sequence-by-headers"/>
</xsl:copy>
</xsl:template>-->
<xsl:template name="sequence-lists">
<!-- The template emits a sequence of groups, each containing a cluster of elements belonging to a header
(or not, if elements precede any headers). While it is flat, the level of each
group is represented directly as @level - making induction easier over the groups. -->
<xsl:for-each-group select="*" group-adjacent="exists(xsw:list-level(.))">
<!-- grouping key is true() for nominal list items, false if not. -->
<xsl:choose>
<xsl:when test="current-grouping-key()">
<xsl:for-each-group select="current-group()" group-adjacent="xsw:list-level(.)">
<xsw:list level="{current-grouping-key()}">
<xsl:apply-templates select="current-group()"/>
</xsw:list>
</xsl:for-each-group>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:template>
<xsl:function name="xsw:list-level" as="xs:string?">
<xsl:param name="whose" as="element()"/>
<xsl:variable name="list-assignment" select="tokenize($whose/@style,'\s*;\s*')[matches(.,'xsweet-list-level:')]"/>
<!-- level is only the value, ordinarily a whole number (zero or positive integer) -->
<xsl:sequence select="if (exists($list-assignment)) then replace($list-assignment,'^.*list-level:\s*','') else ()"/>
</xsl:function>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsw="http://coko.foundation/xsweet"
xmlns="http://www.w3.org/1999/xhtml"
xpath-default-namespace="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="#all">
<xsl:output method="xml" indent="no" omit-xml-declaration="yes"/>
<xsl:template match="node() | @*">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="div">
<xsl:for-each-group select="*" group-adjacent="exists(self::xsw:list)">
<xsl:choose>
<xsl:when test="not(current-grouping-key())">
<xsl:apply-templates select="current-group()"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="list-assembly">
<xsl:with-param name="who" select="current-group()"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:template>
<!-- Groups can be unwrapped since the induced list structure takes care of everything. -->
<xsl:template match="xsw:list">
<xsl:apply-templates/>
</xsl:template>
<xsl:template name="list-assembly">
<!-- first time in, by default, who consists of runs of xsw:list children of the calling div -->
<xsl:param name="who" required="yes" as="element(xsw:list)*"/>
<!-- $who is a run of one or more contiguous siblings.
When $who is empty we fall through so recursion is safe as long as $who is reduced. -->
<xsl:param name="nominal-level" select="0" as="xs:integer"/>
<xsl:for-each-group select="$who" group-starting-with="xsw:list[@level &lt;= $nominal-level]">
<!-- Within $subsequence the elements are (sub)sequenced again at boundaries wherever @level <= $level.
However, only subgroups where @level=$level are 'here' (at this level). -->
<xsl:variable name="me" select="current-group()[@level = $nominal-level]"/>
<!-- When empty($me), we want no section here, because we have no ("correct") header.
But the group is passed in again at the next deeper level, so subgrouping occurs.
Note that this template call is a no-op when $me exists (i.e. we have a group with a header). -->
<xsl:call-template name="list-assembly">
<xsl:with-param name="who" select="current-group()[empty($me)]"/>
<xsl:with-param name="nominal-level" select="$nominal-level + 1"/>
</xsl:call-template>
<!-- When $me exists, the group is on the right level. -->
<xsl:if test="exists($me)">
<ul>
<!-- Now we emit the group (to be unwrapped by a receiving template). -->
<xsl:apply-templates select="$me"/>
<!-- Next we subgroup the remaining groups within the section created for $me.
We do this by assembling again, at $level+1, any remaining elements in the (sub)sequence. -->
<!-- Note the new $who is empty when $me is the only group left. -->
<xsl:call-template name="list-assembly">
<xsl:with-param name="who" select="current-group() except $me"/><!-- Everything still in doc order. -->
<xsl:with-param name="nominal-level" select="$nominal-level + 1"/>
</xsl:call-template>
</ul>
</xsl:if>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment