Skip to content

Commit

Permalink
centralize heading level computation in HTML outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispy-snps committed Nov 19, 2023
1 parent 950ae01 commit 573f4a1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 60 deletions.
10 changes: 1 addition & 9 deletions src/main/plugins/org.dita.html5/xsl/task.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -696,16 +696,8 @@ See the accompanying LICENSE file for applicable license.
<xsl:template match="*" mode="generate-task-label">
<xsl:param name="use-label"/>
<xsl:if test="$GENERATE-TASK-LABELS='YES'">
<xsl:variable name="headLevel">
<xsl:variable name="headCount" select="count(ancestor::*[contains(@class,' topic/topic ')]) + 1"
as="xs:integer"/>
<xsl:choose>
<xsl:when test="$headCount > 6">h6</xsl:when>
<xsl:otherwise>h<xsl:value-of select="$headCount"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<div class="tasklabel">
<xsl:element name="{$headLevel}">
<xsl:element name="h{dita2html:get-heading-level(.)}">
<xsl:attribute name="class">sectiontitle tasklabel</xsl:attribute>
<xsl:value-of select="$use-label"/>
</xsl:element>
Expand Down
51 changes: 30 additions & 21 deletions src/main/plugins/org.dita.html5/xsl/topic.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,7 @@ See the accompanying LICENSE file for applicable license.
<!-- Condensed topic title into single template without priorities; use $headinglevel to set heading.
If desired, somebody could pass in the value to manually set the heading level -->
<xsl:template match="*[contains(@class, ' topic/topic ')]/*[contains(@class, ' topic/title ')]">
<xsl:param name="headinglevel" as="xs:integer">
<xsl:choose>
<xsl:when test="count(ancestor::*[contains(@class, ' topic/topic ')]) > 6">6</xsl:when>
<xsl:otherwise><xsl:sequence select="count(ancestor::*[contains(@class, ' topic/topic ')])"/></xsl:otherwise>
</xsl:choose>
</xsl:param>
<xsl:param name="headinglevel" as="xs:integer" select="dita2html:get-heading-level(.)"/>
<xsl:element name="h{$headinglevel}">
<xsl:attribute name="class" select="concat('topictitle', $headinglevel)"/>
<xsl:call-template name="commonattributes">
Expand All @@ -289,6 +284,33 @@ See the accompanying LICENSE file for applicable license.
<xsl:template match="*[contains(@class, ' topic/titlealts ')]"/>


<!-- =========== HEADING LEVELS =========== -->

<!-- returns heading level (1 through 6) appropriate at the specified element -->
<xsl:function name="dita2html:get-heading-level" as="xs:integer">
<xsl:param name="element" as="element()"/>
<xsl:sequence select="min((6, count($element/ancestor-or-self::*[dita2html:is-heading-level(.)])))"/>
</xsl:function>

<!-- returns true() for elements that count as a heading level -->
<!-- (this is an accessor function to the moded templates below) -->
<xsl:function name="dita2html:is-heading-level" as="xs:boolean?">
<xsl:param name="element" as="element()"/>
<xsl:apply-templates select="$element" mode="dita2html:is-heading-level"/>
</xsl:function>
<xsl:mode name="dita2html:is-heading-level" on-no-match="deep-skip"/>

<!-- <topic> always increments the heading level (title or not) -->
<xsl:template match="*[contains-token(@class, 'topic/topic')]" mode="dita2html:is-heading-level">
<xsl:sequence select="true()"/>
</xsl:template>

<!-- <section> and <example> with titles increment the heading level -->
<xsl:template match="*[tokenize(@class) = ('topic/section', 'topic/example')][*[contains-token(@class, 'topic/title')] or @spectitle]" mode="dita2html:is-heading-level">
<xsl:sequence select="true()"/>
</xsl:template>


<!-- =========== BODY/SECTION (not sensitive to nesting depth) =========== -->

<xsl:template match="*[contains(@class, ' topic/body ')]" name="topic.body">
Expand Down Expand Up @@ -2040,15 +2062,8 @@ See the accompanying LICENSE file for applicable license.
</xsl:choose>
</xsl:variable>

<xsl:variable name="headCount" select="count(ancestor::*[contains(@class, ' topic/topic ')]) + 1"/>
<xsl:variable name="headLevel">
<xsl:choose>
<xsl:when test="$headCount > 6">h6</xsl:when>
<xsl:otherwise>h<xsl:value-of select="$headCount"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>

<!-- based on graceful defaults, build an appropriate section-level heading -->
<xsl:variable name="headLevel" as="xs:string" select="'h' || dita2html:get-heading-level(.)"/>
<xsl:choose>
<xsl:when test="not($heading = '')">
<xsl:if test="normalize-space($heading) = ''">
Expand Down Expand Up @@ -2076,13 +2091,7 @@ See the accompanying LICENSE file for applicable license.

<xsl:template match="*[contains(@class, ' topic/section ')]/*[contains(@class, ' topic/title ')] |
*[contains(@class, ' topic/example ')]/*[contains(@class, ' topic/title ')]" name="topic.section_title">
<xsl:param name="headLevel">
<xsl:variable name="headCount" select="count(ancestor::*[contains(@class, ' topic/topic ')])+1"/>
<xsl:choose>
<xsl:when test="$headCount > 6">h6</xsl:when>
<xsl:otherwise>h<xsl:value-of select="$headCount"/></xsl:otherwise>
</xsl:choose>
</xsl:param>
<xsl:param name="headLevel" as="xs:string" select="'h' || dita2html:get-heading-level(.)"/>
<xsl:element name="{$headLevel}">
<xsl:attribute name="class">sectiontitle</xsl:attribute>
<xsl:call-template name="commonattributes">
Expand Down
51 changes: 30 additions & 21 deletions src/main/plugins/org.dita.xhtml/xsl/xslhtml/dita2htmlImpl.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,7 @@ See the accompanying LICENSE file for applicable license.
<!-- Condensed topic title into single template without priorities; use $headinglevel to set heading.
If desired, somebody could pass in the value to manually set the heading level -->
<xsl:template match="*[contains(@class, ' topic/topic ')]/*[contains(@class, ' topic/title ')]">
<xsl:param name="headinglevel" as="xs:integer">
<xsl:choose>
<xsl:when test="count(ancestor::*[contains(@class, ' topic/topic ')]) > 6">6</xsl:when>
<xsl:otherwise><xsl:sequence select="count(ancestor::*[contains(@class, ' topic/topic ')])"/></xsl:otherwise>
</xsl:choose>
</xsl:param>
<xsl:param name="headinglevel" as="xs:integer" select="dita2html:get-heading-level(.)"/>
<xsl:element name="h{$headinglevel}">
<xsl:attribute name="class" select="concat('topictitle', $headinglevel)"/>
<xsl:call-template name="commonattributes">
Expand Down Expand Up @@ -352,6 +347,33 @@ See the accompanying LICENSE file for applicable license.
<xsl:template match="*[contains(@class, ' topic/titlealts ')]"/>


<!-- =========== HEADING LEVELS =========== -->

<!-- returns heading level (1 through 6) appropriate at the specified element -->
<xsl:function name="dita2html:get-heading-level" as="xs:integer">
<xsl:param name="element" as="element()"/>
<xsl:sequence select="min((6, count($element/ancestor-or-self::*[dita2html:is-heading-level(.)])))"/>
</xsl:function>

<!-- returns true() for elements that count as a heading level -->
<!-- (this is an accessor function to the moded templates below) -->
<xsl:function name="dita2html:is-heading-level" as="xs:boolean?">
<xsl:param name="element" as="element()"/>
<xsl:apply-templates select="$element" mode="dita2html:is-heading-level"/>
</xsl:function>
<xsl:mode name="dita2html:is-heading-level" on-no-match="deep-skip"/>

<!-- <topic> always increments the heading level (title or not) -->
<xsl:template match="*[contains-token(@class, 'topic/topic')]" mode="dita2html:is-heading-level">
<xsl:sequence select="true()"/>
</xsl:template>

<!-- <section> and <example> with titles increment the heading level -->
<xsl:template match="*[tokenize(@class) = ('topic/section', 'topic/example')][*[contains-token(@class, 'topic/title')] or @spectitle]" mode="dita2html:is-heading-level">
<xsl:sequence select="true()"/>
</xsl:template>


<!-- =========== BODY/SECTION (not sensitive to nesting depth) =========== -->

<xsl:template match="*[contains(@class, ' topic/body ')]" name="topic.body">
Expand Down Expand Up @@ -2180,15 +2202,8 @@ See the accompanying LICENSE file for applicable license.
</xsl:choose>
</xsl:variable>

<xsl:variable name="headCount" select="count(ancestor::*[contains(@class, ' topic/topic ')]) + 1"/>
<xsl:variable name="headLevel">
<xsl:choose>
<xsl:when test="$headCount > 6">h6</xsl:when>
<xsl:otherwise>h<xsl:value-of select="$headCount"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>

<!-- based on graceful defaults, build an appropriate section-level heading -->
<xsl:variable name="headLevel" as="xs:string" select="'h' || dita2html:get-heading-level(.)"/>
<xsl:choose>
<xsl:when test="not($heading = '')">
<xsl:if test="normalize-space($heading) = ''">
Expand Down Expand Up @@ -2217,13 +2232,7 @@ See the accompanying LICENSE file for applicable license.

<xsl:template match="*[contains(@class, ' topic/section ')]/*[contains(@class, ' topic/title ')] |
*[contains(@class, ' topic/example ')]/*[contains(@class, ' topic/title ')]" name="topic.section_title">
<xsl:param name="headLevel">
<xsl:variable name="headCount" select="count(ancestor::*[contains(@class, ' topic/topic ')])+1"/>
<xsl:choose>
<xsl:when test="$headCount > 6">h6</xsl:when>
<xsl:otherwise>h<xsl:value-of select="$headCount"/></xsl:otherwise>
</xsl:choose>
</xsl:param>
<xsl:param name="headLevel" as="xs:string" select="'h' || dita2html:get-heading-level(.)"/>
<xsl:element name="{$headLevel}">
<xsl:attribute name="class">sectiontitle</xsl:attribute>
<xsl:call-template name="commonattributes">
Expand Down
10 changes: 1 addition & 9 deletions src/main/plugins/org.dita.xhtml/xsl/xslhtml/taskdisplay.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -709,16 +709,8 @@ See the accompanying LICENSE file for applicable license.
<xsl:template match="*" mode="generate-task-label">
<xsl:param name="use-label"/>
<xsl:if test="$GENERATE-TASK-LABELS='YES'">
<xsl:variable name="headLevel">
<xsl:variable name="headCount" select="count(ancestor::*[contains(@class,' topic/topic ')]) + 1"
as="xs:integer"/>
<xsl:choose>
<xsl:when test="$headCount > 6">h6</xsl:when>
<xsl:otherwise>h<xsl:value-of select="$headCount"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<div class="tasklabel">
<xsl:element name="{$headLevel}">
<xsl:element name="h{dita2html:get-heading-level(.)}">
<xsl:attribute name="class">sectiontitle tasklabel</xsl:attribute>
<xsl:value-of select="$use-label"/>
</xsl:element>
Expand Down

0 comments on commit 573f4a1

Please sign in to comment.