Skip to content

Commit

Permalink
centralize heading level computation in HTML outputs
Browse files Browse the repository at this point in the history
Signed-off-by: chrispy <[email protected]>
  • Loading branch information
chrispy-snps committed Nov 21, 2023
1 parent 950ae01 commit e892a5c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 66 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
59 changes: 35 additions & 24 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,35 @@ 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:template match="*" mode="dita2html:is-heading-level">
<xsl:sequence select="false()"/>
</xsl:template>

<!-- <topic> always increments the heading level (title or not) -->
<xsl:template match="*[contains(@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="*[contains(@class, 'topic/section ') or contains(@class, 'topic/example ')][*[contains(@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 +2064,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:integer" select="dita2html:get-heading-level(.)"/>
<xsl:choose>
<xsl:when test="not($heading = '')">
<xsl:if test="normalize-space($heading) = ''">
Expand All @@ -2059,14 +2076,14 @@ See the accompanying LICENSE file for applicable license.
<xsl:with-param name="headLevel" select="$headLevel"/>
</xsl:apply-templates>
<xsl:if test="@spectitle and not(*[contains(@class, ' topic/title ')])">
<xsl:element name="{$headLevel}">
<xsl:element name="h{$headLevel}">
<xsl:attribute name="class">sectiontitle</xsl:attribute>
<xsl:value-of select="@spectitle"/>
</xsl:element>
</xsl:if>
</xsl:when>
<xsl:when test="$defaulttitle">
<xsl:element name="{$headLevel}">
<xsl:element name="h{$headLevel}">
<xsl:attribute name="class">sectiontitle</xsl:attribute>
<xsl:value-of select="$defaulttitle"/>
</xsl:element>
Expand All @@ -2076,14 +2093,8 @@ 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:element name="{$headLevel}">
<xsl:param name="headLevel" as="xs:integer" select="dita2html:get-heading-level(.)"/>
<xsl:element name="h{$headLevel}">
<xsl:attribute name="class">sectiontitle</xsl:attribute>
<xsl:call-template name="commonattributes">
<xsl:with-param name="default-output-class" select="'sectiontitle'"/>
Expand Down
59 changes: 35 additions & 24 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,35 @@ 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:template match="*" mode="dita2html:is-heading-level">
<xsl:sequence select="false()"/>
</xsl:template>

<!-- <topic> always increments the heading level (title or not) -->
<xsl:template match="*[contains(@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="*[contains(@class, 'topic/section ') or contains(@class, 'topic/example ')][*[contains(@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 +2204,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:integer" select="dita2html:get-heading-level(.)"/>
<xsl:choose>
<xsl:when test="not($heading = '')">
<xsl:if test="normalize-space($heading) = ''">
Expand All @@ -2199,14 +2216,14 @@ See the accompanying LICENSE file for applicable license.
<xsl:with-param name="headLevel" select="$headLevel"/>
</xsl:apply-templates>
<xsl:if test="@spectitle and not(*[contains(@class, ' topic/title ')])">
<xsl:element name="{$headLevel}">
<xsl:element name="h{$headLevel}">
<xsl:attribute name="class">sectiontitle</xsl:attribute>
<xsl:value-of select="@spectitle"/>
</xsl:element>
</xsl:if>
</xsl:when>
<xsl:when test="$defaulttitle">
<xsl:element name="{$headLevel}">
<xsl:element name="h{$headLevel}">
<xsl:attribute name="class">sectiontitle</xsl:attribute>
<xsl:value-of select="$defaulttitle"/>
</xsl:element>
Expand All @@ -2217,14 +2234,8 @@ 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:element name="{$headLevel}">
<xsl:param name="headLevel" as="xs:integer" select="dita2html:get-heading-level(.)"/>
<xsl:element name="h{$headLevel}">
<xsl:attribute name="class">sectiontitle</xsl:attribute>
<xsl:call-template name="commonattributes">
<xsl:with-param name="default-output-class" select="'sectiontitle'"/>
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 e892a5c

Please sign in to comment.