Skip to content

Commit

Permalink
Merge pull request #14 from dita-ot/feature/strip-keys
Browse files Browse the repository at this point in the history
Remove key and branch filter artefacts
  • Loading branch information
jelovirt authored Mar 5, 2023
2 parents 4e1bc1b + 901acac commit 8d534a3
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
30 changes: 30 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,41 @@
depends="build-init,
preprocess">
<makeurl property="output.dir.uri" file="${output.dir}"/>
<local name="_normalize-strip-keyref"/>
<condition property="_normalize-strip-keyref" value="true" else="false">
<and>
<not>
<isset property="preprocess.keyref.skip"/>
</not>
<or>
<istrue value="${normalize-strip-keyref}"/>
<not>
<isset property="normalize-strip-keyref"/>
</not>
</or>
</and>
</condition>
<local name="_normalize-strip-branch-filter"/>
<condition property="_normalize-strip-branch-filter" value="true" else="false">
<and>
<not>
<isset property="preprocess.branch-filter.skip"/>
</not>
<or>
<istrue value="${normalize-strip-branch-filter}"/>
<not>
<isset property="normalize-strip-branch-filter"/>
</not>
</or>
</and>
</condition>
<!--pipeline message="Normalize DITA files" taskname="normalize"-->
<xslt in="${dita.temp.dir}/.job.xml" out="${dita.temp.dir}/.job.xml.temp"
style="${dita.plugin.org.dita.normalize.dir}/xsl/normalize.xsl"
taskname="normalize">
<param name="output.dir.uri" expression="${output.dir.uri}"/>
<param name="normalize-strip-keyref" expression="${_normalize-strip-keyref}"/>
<param name="normalize-strip-branch-filter" expression="${_normalize-strip-branch-filter}"/>
<xmlcatalog refid="dita.catalog"/>
</xslt>
<!--/pipeline-->
Expand Down
11 changes: 10 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
See the accompanying license.txt file for applicable licenses.
-->
<plugin id="org.dita.normalize">
<transtype name="dita" desc="Normalized DITA"/>
<transtype name="dita" desc="Normalized DITA">
<param name="normalize-strip-keyref" desc="Remove key artefacts after key resolution." type="enum">
<val default="true">yes</val>
<val>no</val>
</param>
<param name="normalize-strip-branch-filter" desc="Remove branch filter artefacts after branch filter resolution." type="enum">
<val default="true">yes</val>
<val>no</val>
</param>
</transtype>
<feature extension="dita.conductor.target.relative" file="conductor.xml"/>
</plugin>
44 changes: 42 additions & 2 deletions xsl/normalize.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

<xsl:import href="plugin:org.dita.base:xsl/common/uri-utils.xsl"/>

<xsl:param name="output.dir.uri"/>
<xsl:param name="output.dir.uri" as="xs:string"/>
<xsl:param name="normalize-strip-keyref" as="xs:string"/>
<xsl:param name="normalize-strip-branch-filter" as="xs:string"/>

<xsl:function name="dita-ot:get-fragment" as="xs:string">
<xsl:param name="input" as="xs:string"/>
Expand Down Expand Up @@ -134,31 +136,51 @@
</xsl:choose>
</xsl:function>

<!-- Remove default attributes from schema -->
<xsl:template match="@class | @domains | @specializations | @xtrf | @xtrc | @ditaarch:DITAArchVersion"
priority="10"/>

<!-- Remove processing time PIs -->
<xsl:template match="processing-instruction('workdir') |
processing-instruction('workdir-uri') |
processing-instruction('path2project') |
processing-instruction('path2project-uri') |
processing-instruction('path2rootmap-uri') |
processing-instruction('ditaot') |
processing-instruction('doctype-public') |
processing-instruction('doctype-system') |
@dita-ot:* |
@mapclass"
priority="10"/>

<!-- Remove cascade inserted by preprocess -->
<xsl:template match="*[number(@ditaarch:DITAArchVersion) &lt; 1.3]/@cascade"/>

<!-- Rename elements to match class type -->
<xsl:template match="*[@class]" priority="-5">
<xsl:element name="{tokenize(tokenize(normalize-space(@class), '\s+')[last()], '/')[last()]}"
namespace="{namespace-uri()}">
<xsl:apply-templates select="node() | @*"/>
</xsl:element>
</xsl:template>

<!-- Remove resolved keydefs -->
<xsl:template match="*[contains(@class, ' map/topicref ')]
[exists(@keys) and @processing-role = 'resource-only']
[$normalize-strip-keyref = 'true']"/>

<!-- Remove resolved key definition and references -->
<xsl:template match="@keyref[$normalize-strip-keyref = 'true'] |
@keys[$normalize-strip-keyref = 'true']"/>

<!-- Remove resolved ditavalrefs -->
<xsl:template match="*[contains(@class, ' ditavalref-d/ditavalref ')]
[$normalize-strip-branch-filter = 'true']"/>

<!-- Rewrite link target -->
<xsl:template match="*[contains(@class, ' topic/xref ') or
contains(@class, ' topic/xref ') or
contains(@class, ' topic/link ') or
(contains(@class, ' topic/keyword') and exists(@href)) or
contains(@class, ' map/topicref ')]
[empty(@format) or @format = 'dita']
[empty(@scope) or @scope = 'local']/@href">
Expand All @@ -181,6 +203,24 @@
</xsl:choose>
</xsl:template>

<!-- Rename keyword that has been converted into a link using keys -->
<xsl:template match="*[contains(@class, ' topic/keyword ') and exists(@href)]">
<xref>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="class">- topic/xref </xsl:attribute>
<xsl:apply-templates select="node()"/>
</xref>
</xsl:template>

<!-- Remove default attributes -->
<xsl:template match="*[contains(@class, ' topic/pre ')]/@xml:space[. = 'preserve'] |
@scope[. = 'local'] |
@format[. = 'dita'] |
*[contains(@class, ' topic/xref ')]/@locktitle"/>

<xsl:template match="*[contains(@class, ' subjectScheme/')]" priority="5"/>

<!-- Remove colname inserted by preprocess -->
<xsl:template match="*[contains(@class, ' topic/entry ')]/@colname">
<xsl:if test="empty(../@namest) and empty(../@nameend)">
<xsl:copy-of select="."/>
Expand Down

0 comments on commit 8d534a3

Please sign in to comment.