<!--

# =============================================================================

# Copyright © 2016 Typéfi Systems. All rights reserved.

#

# Unless required by applicable law or agreed to in writing, software

# is distributed on an "as is" basis, without warranties or conditions of any

# kind, either express or implied.

# =============================================================================

-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tps="http://www.typefi.com/ContentXML" xmlns:xd="http://www.pnp-software.com/XSLTdoc" version="2.0" exclude-result-prefixes="#all">





    
    <xd:doc type="stylesheet">

        
<xd:short>

            Contains service code for functioning of 
<code>$text.character.mapping</code>.

            This mapping variable should be used for applying character and soft styles to certain symbols or unicode ranges.

        
</xd:short>

        
<xd:detail>

            This module is not designed for customization.

            To specify a custom replacement rule, override the variable 
<code>$text.character.mapping</code> instead.

            The mapping has effect only if 
<code>$text.character.mapping.enabled</code> variable is <code>true()</code>.

        
</xd:detail>

        
<xd:cvsId>   $Revision$</xd:cvsId>

    
</xd:doc>






    
<xsl:function name="tps:get-character-mapping-result">

        
<xsl:param name="string" as="xs:string" />



        
<xsl:variable name="regex" select="tps:construct-character-mapping-regex($text.character.mapping)" />



        
<xsl:variable name="result">

            
<xsl:choose>

                
<xsl:when test="tps:is-non-empty-regular-expression($regex)">

                    
<xsl:call-template name="apply-character-mapping-with-regex">

                        
<xsl:with-param name="string" select="$string" />

                        
<xsl:with-param name="regex" select="$regex" />

                    
</xsl:call-template>

                
</xsl:when>

                
<xsl:otherwise>

                    
<xsl:copy-of select="$string" />

                
</xsl:otherwise>

            
</xsl:choose>

        
</xsl:variable>



        
<xsl:sequence select="$result" />

    
</xsl:function>





    
<xsl:template name="apply-character-mapping-with-regex">

        
<xsl:param name="string" as="xs:string" />

        
<xsl:param name="regex" as="xs:string" required="yes" />



        
<xsl:analyze-string select="$string" regex="{$regex}">

            
<xsl:matching-substring>

                
<xsl:call-template name="process-matching-text" />

            
</xsl:matching-substring>

            
<xsl:non-matching-substring>

                
<xsl:value-of select="." />

            
</xsl:non-matching-substring>

        
</xsl:analyze-string>

    
</xsl:template>





    
<xsl:template name="process-matching-text">

        
<xsl:for-each select="1 to count($text.character.mapping)">

            
<xsl:variable name="current-regex-group" select="." />



            
<xsl:if test="regex-group($current-regex-group)">

                
<xsl:call-template name="process-character-match">

                    
<xsl:with-param name="current-regex-group" select="$current-regex-group" />

                
</xsl:call-template>

            
</xsl:if>

        
</xsl:for-each>

    
</xsl:template>





    
<xsl:template name="process-character-match">

        
<xsl:param name="current-regex-group" as="xs:integer" />

        
<xsl:variable name="mapping-rule" select="$text.character.mapping[$current-regex-group]" />



        
<xsl:choose>

            
<xsl:when test="exists($mapping-rule/@wrap)">

                
<xsl:element name="{name($mapping-rule/element()[1])}">

                    
<xsl:copy-of select="$mapping-rule/element()[1]/attribute()" />

                    
<xsl:copy-of select="regex-group($current-regex-group)" />

                
</xsl:element>

            
</xsl:when>

            
<xsl:otherwise>

                
<xsl:copy-of select="$mapping-rule/element()[1]" />

            
</xsl:otherwise>

        
</xsl:choose>

    
</xsl:template>





    
<!--

        @Example: ([&#x80;-&#x100;]+)|([&#x2032;&#x1234;&#x4321;]+)|([&#x2032;]+)

        
-->

    
<xsl:function name="tps:construct-character-mapping-regex" as="xs:string?">

        
<xsl:param name="mapping-configuration" as="item()*" />



        
<xsl:variable name="result" as="xs:string*">

            
<xsl:apply-templates select="$mapping-configuration" mode="construct-character-mapping-regex" />

        
</xsl:variable>



        
<xsl:sequence select="string-join($result, '|')" />

    
</xsl:function>





    
    <xd:doc mode="construct-character-mapping-regex">

        
<xd:detail>This mode should not be customized. Override the variable <code>$text.character.mapping</code> instead.</xd:detail>

    
</xd:doc>






    
<xsl:template match="node()" mode="construct-character-mapping-regex" />





    
<xsl:template match="range[exists(@from) and exists(@to)]" mode="construct-character-mapping-regex" priority="10">

        
<xsl:value-of select="concat('([', @from , '-', @to, '])')" />

    
</xsl:template>





    
<xsl:template match="range[exists(@from) and exists(@to)][lower-case(@wrap) = ('yes', 'true')]" mode="construct-character-mapping-regex" priority="20">

        
<xsl:value-of select="concat('([', @from , '-', @to, ']+)')" />

    
</xsl:template>





    
<xsl:template match="char[@values]" mode="construct-character-mapping-regex" priority="10">

        
<xsl:value-of select="concat('([', @values, '])')" />

    
</xsl:template>





    
<xsl:template match="char[@values][lower-case(@wrap) = ('yes', 'true')]" mode="construct-character-mapping-regex" priority="20">

        
<xsl:value-of select="concat('([', @values, ']+)')" />

    
</xsl:template>



</xsl:stylesheet>













































































v