Skip to content

Commit

Permalink
cct13#9689#morerows in thead exceeds thead line count
Browse files Browse the repository at this point in the history
  • Loading branch information
boskarine committed Dec 18, 2023
1 parent a2c02ec commit c854826
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
4 changes: 4 additions & 0 deletions xpl/docx2hub.xpl
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@
"Repeat as header row at the top of each page" check box in the Table Properties dialog box on the Row tab.
</p:documentation>
</p:option>
<p:option name="normalize-thead" select="'yes'">
<p:documentation>morerows attributes in &lt;thead> can span &lt;tbody> rows, too. Whether to normalize it and make this rows part of the &lt;thead> to avoid unbalanced rows.</p:documentation>
</p:option>

<p:import href="http:https://xmlcalabash.com/extension/steps/library-1.0.xpl"/>
<p:import href="http:https://transpect.io/calabash-extensions/unzip-extension/unzip-declaration.xpl"/>
Expand Down Expand Up @@ -649,6 +652,7 @@
<p:with-option name="hub-version" select="$hub-version"/>
<p:with-param name="fail-on-error" select="$fail-on-error"/>
<p:with-param name="heuristic-character-replacements" select="$heuristic-character-replacements"/>
<p:with-param name="normalize-thead" select="$normalize-thead"/>
</tr:xslt-mode>

<p:add-attribute match="/*" attribute-name="xml:base" name="rebase">
Expand Down
83 changes: 83 additions & 0 deletions xsl/join-runs.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

<xsl:param name="terminate-on-unbalanced-instrTexts" select="'yes'" as="xs:string*"/>
<xsl:param name="mml-version" as="xs:string?"/>
<xsl:param name="normalize-thead" select="'yes'"/>

<xsl:template match="/*" mode="docx2hub:join-runs" priority="-0.2">
<!-- no copy-namespaces="no" in order to suppress excessive namespace declarations on every element -->
Expand Down Expand Up @@ -2139,5 +2140,87 @@
<xsl:value-of select="."/>
</xsl:copy>
</xsl:template>

<xsl:template match="dbk:tgroup[$normalize-thead='yes'][dbk:thead//dbk:entry[@morerows ne '0']]" mode="docx2hub:join-runs">
<xsl:copy>
<xsl:apply-templates select="@*" mode="#current"/>
<xsl:apply-templates select="node() except (dbk:thead,dbk:tbody)" mode="#current"/>
<xsl:choose>
<xsl:when test="tr:check-for-morerows(dbk:thead/dbk:row)">
<xsl:call-template name="check-body-rows">
<xsl:with-param name="tbody" select="dbk:tbody/dbk:row"/>
<xsl:with-param name="thead" select="dbk:thead/dbk:row"/>
<xsl:with-param name="count" select="tr:count-morerows(dbk:thead/dbk:row)">
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="dbk:thead|dbk:tbody" mode="#current"/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>

<xsl:template name="check-body-rows">
<xsl:param name="tbody" as="node() *"/>
<xsl:param name="thead" as="node() *"/>
<xsl:param name="count" as="xs:double"/>

<xsl:variable name="new_thead">
<xsl:sequence select="$thead"/>
<xsl:sequence select="$tbody[position() lt ($count + 1)]" />
</xsl:variable>

<xsl:choose>
<xsl:when test="count($tbody) lt $count">
<xsl:processing-instruction name="letex" select="'D2T_010: Fehlerhaftes Attribut (morerows erstreckt sich über Ende des tbody)'"/>
<thead>
<xsl:apply-templates select="$new_thead" mode="#current"/>
</thead>
<tbody>
<xsl:apply-templates select="$tbody[position() gt $count]" mode="#current"/>
</tbody>
</xsl:when>
<xsl:when test="tr:check-for-morerows($new_thead)">
<xsl:call-template name="check-body-rows">
<xsl:with-param name="tbody" select="$tbody[position() gt $count]"/>
<xsl:with-param name="thead" select="$new_thead"/>
<xsl:with-param name="count" select="tr:count-morerows($new_thead)">
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<thead>
<xsl:apply-templates select="$new_thead" mode="#current"/>
</thead>
<xsl:if test="count($tbody[position() gt $count]) gt 0">
<tbody>
<xsl:apply-templates select="$tbody[position() gt $count]" mode="#current"/>
</tbody>
</xsl:if>
</xsl:otherwise>
</xsl:choose>

</xsl:template>

<xsl:function name="tr:count-morerows">
<xsl:param name="input-rows"/>

<xsl:variable name="count_rows" as="xs:double *">
<xsl:for-each select="$input-rows/descendant::dbk:entry[@morerows ne '0']">
<xsl:sequence select="@morerows - count(parent::dbk:row/following-sibling::dbk:row)"/>
</xsl:for-each>
</xsl:variable>
<xsl:copy-of select="max($count_rows)"/>

</xsl:function>

<xsl:function name="tr:check-for-morerows">
<xsl:param name="input-rows"/>

<xsl:variable name="missing_rows" select="if (some $x in $input-rows/descendant::dbk:entry[@morerows ne '0'] satisfies (($x/@morerows - count($x/parent::dbk:row/following-sibling::dbk:row)) gt 0)) then true() else false()" />
<xsl:copy-of select="$missing_rows"/>

</xsl:function>

</xsl:stylesheet>

0 comments on commit c854826

Please sign in to comment.