<!--

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

# Copyright © 2013 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="xs tps">





    
    <xd:doc type="stylesheet">

        
<xd:short>

            Contains conversion logic for character and soft styles

        
</xd:short>

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

    
</xd:doc>






    
<!--

        @Description:   This module handles basic style processing and provides an interface for

                        processing custom character styles.

                        The idea behind the custom characters processing module is to provide an

                        ability to install custom character types for specific JATS styles

                        regardless of the nesting. Nested tps:c/tps:c are flattened on

                        post-processing stage (see core-post-processing.xsl).



        @Example:       For instance, I want to install 'article-title-bold-italic' character type

                        for any combination of bold//italic and italic//bold within article-title

                        and 'bold-italic' for all bold//italic and italic//bold matches across

                        the whole document:



                        <xsl:template match="article-title//*[tps:select-style(., ('italic', 'bold'))]"

                                      mode="set-c-type" priority="10">

                            <xsl:sequence select="'article-title-bold-italic'"/>

                        </xsl:template>



                        <xsl:template match="*[tps:select-style(., ('bold', 'italic'))]" mode="set-c-type">

                            <xsl:sequence select="'bold-italic'"/>

                        </xsl:template>



        @Note:          Currently the supported styles are those which are referenced in

                        tps:is-style-element() function: bold, italic, underline, sub, sup.

                        Extending the function shouldn't make any significant effect on the

                        existing code.

        
-->



    
<xsl:template match="node()" mode="set-c-type" />





    
<xsl:template match="element()[tps:is-style-element(.)][tps:has-custom-c-type(.)]" priority="10">

        
<tps:c type="{tps:get-custom-c-type(.)}">

            
<xsl:apply-templates />

        
</tps:c>

    
</xsl:template>





    
<xsl:function name="tps:has-custom-c-type" as="xs:boolean">

        
<xsl:param name="el" as="element()" />



        
<xsl:sequence select="exists(tps:get-custom-c-type($el))" />

    
</xsl:function>





    
<xsl:function name="tps:get-custom-c-type" as="xs:string?">

        
<xsl:param name="el" as="element()" />



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

            
<xsl:apply-templates select="$el" mode="set-c-type" />

        
</xsl:variable>



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

    
</xsl:function>





    
<xsl:template match="bold">

        
<tps:style type="{$style.type.bold}">

            
<xsl:apply-templates />

        
</tps:style>

    
</xsl:template>





    
<xsl:template match="italic">

        
<tps:style type="{$style.type.italic}">

            
<xsl:apply-templates />

        
</tps:style>

    
</xsl:template>





    
<xsl:template match="underline">

        
<tps:style type="{$style.type.underline}">

            
<xsl:apply-templates />

        
</tps:style>

    
</xsl:template>





    
<xsl:template match="sub">

        
<tps:style type="{$style.type.sub}">

            
<xsl:apply-templates />

        
</tps:style>

    
</xsl:template>





    
<xsl:template match="sup">

        
<tps:style type="{$style.type.sup}">

            
<xsl:apply-templates />

        
</tps:style>

    
</xsl:template>





    
<xsl:function name="tps:select-style" as="element()?">

        
<xsl:param name="el" as="element()" />

        
<xsl:param name="styles-to-match" as="xs:string+" />



        
<xsl:variable name="contextual-styles" select="tps:get-contextual-styles($el)" />



        
<xsl:sequence select="if (exists($contextual-styles)) then $el[tps:is-subsequence($styles-to-match, $contextual-styles)] else ()" />

    
</xsl:function>





    
<xsl:function name="tps:get-contextual-styles" as="xs:string*">

        
<xsl:param name="el" as="element()" />

        
<xsl:variable name="styles" select="$el/ancestor-or-self::*[tps:is-style-element(.)]/name()" />



        
<xsl:sequence select="distinct-values($styles)" />

    
</xsl:function>





    
<xsl:function name="tps:is-style-element" as="xs:boolean">

        
<xsl:param name="el" as="element()" />



        
<xsl:sequence select="exists($el[self::bold or self::italic or self::underline or self::sup or self::sub])" />

    
</xsl:function>



</xsl:stylesheet>













































































v