| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- <?xml version="1.0" encoding="utf-8"?>
- <!-- This file is part of the DITA Open Toolkit project.
- See the accompanying license.txt file for applicable licenses.-->
- <!-- (c) Copyright IBM Corp. 2004, 2005 All Rights Reserved. -->
- <!-- Common utilities that can be used by DITA transforms -->
- <xsl:stylesheet version="2.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
- xmlns:xs="http://www.w3.org/2001/XMLSchema"
- xmlns:dita-ot="http://dita-ot.sourceforge.net/ns/201007/dita-ot"
- exclude-result-prefixes="xs dita-ot">
- <xsl:include href="functions.xsl"/>
- <xsl:param name="defaultLanguage" select="'en'" as="xs:string"/>
- <xsl:param name="DEFAULTLANG" select="if (/*/@xml:lang) then /*/@xml:lang else $defaultLanguage" as="xs:string"/>
- <xsl:param name="variableFiles.url" select="'plugin:org.dita.base:xsl/common/strings.xml'"/>
-
- <xsl:variable name="pixels-per-inch" select="number(96)"/>
- <xsl:key name="id" match="*[@id]" use="@id"/>
- <!-- Function to determine the current language, and return it in lower case -->
- <xsl:template name="getLowerCaseLang">
- <xsl:value-of select="dita-ot:get-current-language(.)"/>
- </xsl:template>
- <xsl:template match="*" mode="get-first-topic-lang">
- <xsl:sequence select="dita-ot:get-first-topic-language(.)"/>
- </xsl:template>
- <xsl:template match="*" mode="get-render-direction">
- <xsl:param name="lang">
- <xsl:apply-templates select="/*" mode="get-first-topic-lang"/>
- </xsl:param>
- <xsl:variable name="l" select="tokenize($lang, '-')[1]" as="xs:string"/>
- <xsl:choose>
- <xsl:when test="$l = 'ar'">rtl</xsl:when>
- <xsl:when test="$l = 'arc'">rtl</xsl:when>
- <xsl:when test="$l = 'bcc'">rtl</xsl:when>
- <xsl:when test="$l = 'bqi'">rtl</xsl:when>
- <xsl:when test="$l = 'ckb'">rtl</xsl:when>
- <xsl:when test="$l = 'dv'">rtl</xsl:when>
- <xsl:when test="$l = 'fa'">rtl</xsl:when>
- <xsl:when test="$l = 'glk'">rtl</xsl:when>
- <xsl:when test="$l = 'he'">rtl</xsl:when>
- <xsl:when test="$l = 'lrc'">rtl</xsl:when>
- <xsl:when test="$l = 'mzn'">rtl</xsl:when>
- <xsl:when test="$l = 'pnb'">rtl</xsl:when>
- <xsl:when test="$l = 'ps'">rtl</xsl:when>
- <xsl:when test="$l = 'sd'">rtl</xsl:when>
- <xsl:when test="$l = 'ug'">rtl</xsl:when>
- <xsl:when test="$l = 'ur'">rtl</xsl:when>
- <xsl:when test="$l = 'yi'">rtl</xsl:when>
- <xsl:otherwise>ltr</xsl:otherwise>
- </xsl:choose>
- </xsl:template>
- <xsl:variable name="stringFiles" select="document($variableFiles.url)/langlist/lang" as="element(lang)*"/>
-
- <!-- Deprecated. Use getVariable template instead. -->
- <xsl:template name="getString">
- <xsl:param name="stringName"/>
-
- <xsl:call-template name="output-message">
- <xsl:with-param name="id" select="'DOTX066W'"/>
- <xsl:with-param name="msgparams">%1=getString</xsl:with-param>
- </xsl:call-template>
- <xsl:call-template name="getVariable">
- <xsl:with-param name="id" select="string($stringName)"/>
- </xsl:call-template>
- </xsl:template>
- <xsl:template name="getVariable">
- <xsl:param name="id" as="xs:string"/>
- <xsl:param name="params" as="node()*"/>
- <xsl:sequence select="dita-ot:get-variable(., $id, $params)"/>
- </xsl:template>
- <xsl:template name="findString">
- <xsl:param name="id" as="xs:string"/>
- <xsl:param name="params" as="node()*"/>
- <xsl:param name="ancestorlang" as="xs:string*"/>
- <xsl:param name="defaultlang" as="xs:string*"/>
- <xsl:param name="originallang" as="xs:string*" select="$ancestorlang[1]"/>
- <xsl:variable name="l" select="($ancestorlang, $defaultlang)[1]" as="xs:string?"/>
- <xsl:choose>
- <xsl:when test="exists($l)">
- <xsl:variable name="stringfile" select="$stringFiles[@xml:lang = $l]/@filename" as="xs:string*"/>
- <xsl:variable name="str" as="element()*">
- <xsl:for-each select="$stringfile">
- <xsl:sequence select="document(., $stringFiles[1])/*/*[@name = $id or @id = $id]"/><!-- strings/str/@name opentopic-vars:vars/opentopic-vars:variable/@id -->
- </xsl:for-each>
- </xsl:variable>
- <xsl:choose>
- <xsl:when test="exists($str)">
- <xsl:apply-templates select="$str[last()]" mode="processVariableBody">
- <xsl:with-param name="params" select="$params"/>
- </xsl:apply-templates>
- <xsl:if test="empty($ancestorlang)">
- <xsl:call-template name="output-message">
- <xsl:with-param name="id" select="'DOTX001W'"/>
- <xsl:with-param name="msgparams">%1=<xsl:value-of select="$id"/>;%2=<xsl:value-of select="$originallang"/>;%3=<xsl:value-of select="$DEFAULTLANG"/></xsl:with-param>
- </xsl:call-template>
- </xsl:if>
- </xsl:when>
- <xsl:otherwise>
- <xsl:call-template name="findString">
- <xsl:with-param name="id" select="$id"/>
- <xsl:with-param name="params" select="$params"/>
- <xsl:with-param name="ancestorlang" select="$ancestorlang[position() gt 1]"/>
- <xsl:with-param name="defaultlang" select="if (exists($ancestorlang)) then $defaultlang else $defaultlang[position() gt 1]"/>
- <xsl:with-param name="originallang" select="$originallang"/>
- </xsl:call-template>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:when>
- <xsl:otherwise>
- <xsl:value-of select="$id"/>
- <xsl:call-template name="output-message">
- <xsl:with-param name="id" select="'DOTX052W'"/>
- <xsl:with-param name="msgparams">%1=<xsl:value-of select="$id"/></xsl:with-param>
- </xsl:call-template>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:template>
-
- <!-- Support legacy variable syntax -->
- <xsl:template match="str" mode="processVariableBody">
- <xsl:param name="params"/>
- <xsl:copy-of select="node()"/>
- </xsl:template>
- <xsl:template match="variable" mode="processVariableBody">
- <xsl:param name="params"/>
-
- <xsl:for-each select="node()">
- <xsl:choose>
- <xsl:when test="self::param">
- <xsl:variable name="param-name" select="@ref-name"/>
- <xsl:copy-of select="$params[name() = $param-name]/node()"/>
- </xsl:when>
- <xsl:when test="self::variableref">
- <xsl:call-template name="getVariable">
- <xsl:with-param name="id" select="@refid"/>
- <xsl:with-param name="params" select="$params"/>
- </xsl:call-template>
- </xsl:when>
- <xsl:otherwise>
- <xsl:copy-of select="."/>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:for-each>
- </xsl:template>
-
- <xsl:template name="length-to-pixels">
- <xsl:param name="dimen"/>
- <!-- We handle units of cm, mm, in, pt, pc, px. We also accept em,
- but just treat 1em=1pc. An omitted unit is taken as px. -->
-
- <xsl:variable name="units" select="substring($dimen, string-length($dimen) - 1)"/>
- <xsl:variable name="numeric-value" select="number(substring($dimen, 1, string-length($dimen) - 2))"/>
- <xsl:choose>
- <xsl:when test="string(number($dimen)) != 'NaN'">
- <!-- Since $units is a number, the input was unitless, so we default
- the unit to pixels and just return the input value -->
- <xsl:value-of select="round(number($dimen))"/>
- </xsl:when>
- <xsl:when test="string($numeric-value) = 'NaN'">
- <!-- If the input isn't valid, just return 100% -->
- <xsl:value-of select="'100%'"/>
- </xsl:when>
- <xsl:when test="$units='cm'">
- <xsl:value-of select="round($numeric-value * $pixels-per-inch div 2.54)"/>
- </xsl:when>
- <xsl:when test="$units='mm'">
- <xsl:value-of select="round($numeric-value * $pixels-per-inch div 25.4)"/>
- </xsl:when>
- <xsl:when test="$units='in'">
- <xsl:value-of select="round($numeric-value * $pixels-per-inch)"/>
- </xsl:when>
- <xsl:when test="$units='pt'">
- <xsl:value-of select="round($numeric-value * $pixels-per-inch div 72)"/>
- </xsl:when>
- <xsl:when test="$units='pc'">
- <xsl:value-of select="round($numeric-value * $pixels-per-inch div 6)"/>
- </xsl:when>
- <xsl:when test="$units='px'">
- <xsl:value-of select="round($numeric-value)"/>
- </xsl:when>
- <xsl:when test="$units='em'">
- <xsl:value-of select="round($numeric-value * $pixels-per-inch div 6)"/>
- </xsl:when>
- <xsl:otherwise>
- <!-- If the input isn't valid, just return 100% -->
- <xsl:value-of select="'100%'"/>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:template>
-
- <!-- replace all the blank in file name or directory with %20 -->
- <xsl:template name="replace-blank">
- <xsl:param name="file-origin"></xsl:param>
- <xsl:choose>
- <xsl:when test="contains($file-origin,' ')">
- <xsl:call-template name="replace-blank">
- <xsl:with-param name="file-origin">
- <xsl:value-of select="substring-before($file-origin,' ')"/>%20<xsl:value-of select="substring-after($file-origin,' ')"/>
- </xsl:with-param>
- </xsl:call-template>
- </xsl:when>
- <xsl:otherwise>
- <xsl:value-of select="$file-origin"/>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:template>
-
- <!-- Return the portion of an HREF value up to the file's extension. This assumes
- that the file has an extension, and that the topic and/or element ID does not
- contain a period. Written to allow references such as com.example.dita.files/file.dita#topic -->
- <!-- Deprecated: use replace-extension instead -->
- <xsl:template match="*" mode="parseHrefUptoExtension">
- <xsl:param name="href" select="@href"/>
-
- <xsl:call-template name="output-message">
- <xsl:with-param name="id" select="'DOTX069W'"/>
- <xsl:with-param name="msgparams">%1=parseHrefUptoExtension</xsl:with-param>
- </xsl:call-template>
- <xsl:variable name="uptoDot"><xsl:value-of select="substring-before($href,'.')"/></xsl:variable>
- <xsl:variable name="afterDot"><xsl:value-of select="substring-after($href,'.')"/></xsl:variable>
- <xsl:value-of select="$uptoDot"/>
- <xsl:choose>
- <!-- No more periods, so this is at the extension -->
- <xsl:when test="not(contains($afterDot,'.'))"/>
- <!-- Multiple slashes; at least one must be a directory, so it's before the extension -->
- <xsl:when test="contains(substring-after($afterDot,'/'),'/')">
- <xsl:text>.</xsl:text>
- <xsl:value-of select="substring-before($afterDot,'/')"/>
- <xsl:text>/</xsl:text>
- <xsl:apply-templates select="." mode="parseHrefUptoExtension"><xsl:with-param name="href" select="substring-after($afterDot,'/')"/></xsl:apply-templates>
- </xsl:when>
- <!-- Multiple periods, no slashes, no topic or element ID, so the file name contains more periods -->
- <xsl:when test="not(contains($afterDot,'#'))">
- <xsl:text>.</xsl:text>
- <xsl:apply-templates select="." mode="parseHrefUptoExtension"><xsl:with-param name="href" select="$afterDot"/></xsl:apply-templates>
- </xsl:when>
- <!-- Multiple periods, no slashes, with #. Move to next period. Needs additional work to support
- IDs containing periods. -->
- <xsl:otherwise>
- <xsl:text>.</xsl:text>
- <xsl:apply-templates select="." mode="parseHrefUptoExtension"><xsl:with-param name="href" select="$afterDot"/></xsl:apply-templates>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:template>
-
- <!-- Get filename base -->
- <xsl:template name="getFileName">
- <xsl:param name="filename"/>
- <xsl:param name="extension"/>
- <xsl:choose>
- <xsl:when test="contains($filename, $extension)">
- <xsl:call-template name="substring-before-last">
- <xsl:with-param name="text" select="$filename"/>
- <xsl:with-param name="delim" select="$extension"/>
- </xsl:call-template>
- </xsl:when>
- <xsl:otherwise>
- <xsl:value-of select="$filename"/>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:template>
-
- <xsl:function name="dita-ot:strip-fragment" as="xs:string">
- <xsl:param name="href" as="xs:string"/>
- <xsl:value-of select="if (contains($href, '#')) then substring-before($href, '#') else $href"/>
- </xsl:function>
-
- <!-- Replace file extension in a URI -->
- <xsl:template name="replace-extension" as="xs:string">
- <xsl:param name="filename" as="xs:string"/>
- <xsl:param name="extension" as="xs:string"/>
- <xsl:param name="ignore-fragment" select="false()"/>
- <xsl:variable name="f" as="xs:string">
- <xsl:value-of>
- <xsl:call-template name="substring-before-last">
- <xsl:with-param name="text">
- <xsl:choose>
- <xsl:when test="contains($filename, '#')">
- <xsl:value-of select="substring-before($filename, '#')"/>
- </xsl:when>
- <xsl:otherwise>
- <xsl:value-of select="$filename"/>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:with-param>
- <xsl:with-param name="delim" select="'.'"/>
- </xsl:call-template>
- </xsl:value-of>
- </xsl:variable>
- <xsl:value-of>
- <xsl:if test="string($f)">
- <xsl:value-of select="concat($f, $extension)"/>
- </xsl:if>
- <xsl:if test="not($ignore-fragment) and contains($filename, '#')">
- <xsl:value-of select="concat('#', substring-after($filename, '#'))"/>
- </xsl:if>
- </xsl:value-of>
- </xsl:template>
-
- <xsl:function name="dita-ot:substring-before-last" as="xs:string?">
- <xsl:param name="text" as="xs:string"/>
- <xsl:param name="delim" as="xs:string"/>
-
- <xsl:call-template name="substring-before-last">
- <xsl:with-param name="text" select="$text"/>
- <xsl:with-param name="delim" select="$delim" />
- </xsl:call-template>
- </xsl:function>
-
- <xsl:template name="substring-before-last" as="xs:string?">
- <xsl:param name="text" as="xs:string"/>
- <xsl:param name="delim" as="xs:string"/>
- <xsl:param name="acc" as="xs:string?"/>
-
- <xsl:choose>
- <xsl:when test="string($text) and string($delim)">
- <xsl:variable name="tail" select="substring-after($text, $delim)" />
- <xsl:choose>
- <xsl:when test="contains($tail, $delim)">
- <xsl:call-template name="substring-before-last">
- <xsl:with-param name="text" select="$tail" />
- <xsl:with-param name="delim" select="$delim" />
- <xsl:with-param name="acc" select="concat($acc, substring-before($text, $delim), $delim)"/>
- </xsl:call-template>
- </xsl:when>
- <xsl:otherwise>
- <xsl:value-of select="concat($acc, substring-before($text, $delim))"/>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:when>
- <xsl:otherwise>
- <xsl:sequence select="$acc"/>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:template>
- <xsl:template match="processing-instruction('workdir-uri')" mode="get-work-dir">
- <xsl:value-of select="."/>
- </xsl:template>
- <xsl:template match="processing-instruction('path2project-uri')" mode="get-path2project">
- <xsl:choose>
- <!-- Backwards compatibility with path2project that is empty when current directory is the root directory -->
- <xsl:when test=". = './'"/>
- <xsl:otherwise>
- <xsl:value-of select="."/>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:template>
- <xsl:template match="processing-instruction('path2project')" mode="get-path2project">
- <xsl:call-template name="get-path2project">
- <xsl:with-param name="s" select="."/>
- </xsl:call-template>
- </xsl:template>
- <xsl:template name="get-path2project">
- <!-- Deal with being handed a Windows backslashed path by accident. -->
- <!-- This code only changes \ to / and doesn't handle the many other situations
- where a URI differs from a file path. Hopefully they don't occur in path2proj anyway. -->
- <xsl:param name="s"/>
- <xsl:choose>
- <xsl:when test="contains($s, '\')">
- <xsl:value-of select="substring-before($s, '\')"/>
- <xsl:text>/</xsl:text>
- <xsl:call-template name="get-path2project">
- <xsl:with-param name="s" select="substring-after($s, '\')"/>
- </xsl:call-template>
- </xsl:when>
- <xsl:otherwise>
- <xsl:value-of select="$s"/>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:template>
- <xsl:function name="dita-ot:get-closest-topic" as="element()">
- <xsl:param name="n" as="node()"/>
- <xsl:sequence
- select="$n/ancestor-or-self::*[contains(@class, ' topic/topic ')][1]"/>
- </xsl:function>
- <xsl:template name="dita-ot:normalize-uri" as="xs:string">
- <xsl:param name="src" as="xs:string*"/>
- <xsl:param name="res" select="()" as="xs:string*"/>
-
- <xsl:choose>
- <xsl:when test="empty($src)">
- <xsl:value-of select="$res" separator="/"/>
- </xsl:when>
- <xsl:when test="$src[1] = '.'">
- <xsl:call-template name="dita-ot:normalize-uri">
- <xsl:with-param name="src" select="$src[position() ne 1]"/>
- <xsl:with-param name="res" select="$res"/>
- </xsl:call-template>
- </xsl:when>
- <xsl:when test="$src[1] = '..' and exists($res) and not($res[position() eq last()] = ('..', ''))">
- <xsl:call-template name="dita-ot:normalize-uri">
- <xsl:with-param name="src" select="$src[position() ne 1]"/>
- <xsl:with-param name="res" select="$res[position() ne last()]"/>
- </xsl:call-template>
- </xsl:when>
- <xsl:otherwise>
- <xsl:call-template name="dita-ot:normalize-uri">
- <xsl:with-param name="src" select="$src[position() ne 1]"/>
- <xsl:with-param name="res" select="($res, $src[1])"/>
- </xsl:call-template>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:template>
-
- <xsl:function name="dita-ot:relativize" as="xs:anyURI">
- <xsl:param name="base" as="xs:anyURI"/>
- <xsl:param name="uri" as="xs:anyURI"/>
-
- <xsl:variable name="b" select="tokenize($base, '/')" as="xs:string+"/>
- <xsl:variable name="u" select="tokenize($uri, '/')" as="xs:string+"/>
-
- <xsl:sequence select="dita-ot:relativize.strip-and-prefix($b, $u)"/>
- </xsl:function>
-
- <xsl:function name="dita-ot:relativize.strip-and-prefix" as="xs:anyURI">
- <xsl:param name="a" as="xs:string+"/>
- <xsl:param name="b" as="xs:string+"/>
- <xsl:choose>
- <xsl:when test="$a[1] = $b[1]">
- <xsl:sequence select="dita-ot:relativize.strip-and-prefix($a[position() ne 1], $b[position() ne 1])"/>
- </xsl:when>
- <xsl:otherwise>
- <xsl:variable name="res" as="xs:string+">
- <xsl:for-each select="$a[position() ne 1]">../</xsl:for-each>
- <xsl:value-of select="$b" separator="/"/>
- </xsl:variable>
- <xsl:sequence select="xs:anyURI(string-join($res, ''))"/>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:function>
- </xsl:stylesheet>
|