sysmodellibs/sysmodelgen/extra/css-module.xsl
changeset 7 3c36c452f013
equal deleted inserted replaced
6:5b32dc297d05 7:3c36c452f013
       
     1 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
       
     2  	xmlns:s="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:exslt="http://exslt.org/common"
       
     3   	exclude-result-prefixes="s exslt" >
       
     4 
       
     5 	<xsl:variable name="css">
       
     6 		<css><xsl:copy-of select="/*/@xml:lang"/>
       
     7 			<xsl:apply-templates select="//style | //s:style" mode="css"/>
       
     8 		</css>
       
     9 	</xsl:variable>
       
    10 <!-- debugging <xsl:template match="/*"><xsl:copy-of select="$css"/></xsl:template>-->
       
    11 
       
    12  
       
    13 <xsl:template match="s:style" mode="unstyle"/>
       
    14 
       
    15 <xsl:template match="*" mode="unstyle">
       
    16 	<xsl:copy>
       
    17 		<xsl:apply-templates select="@*" mode="unstyle"/>
       
    18 		<xsl:call-template name="style"/>
       
    19 		<xsl:apply-templates select="node()" mode="unstyle"/>
       
    20 	</xsl:copy>
       
    21 </xsl:template>
       
    22 
       
    23 <xsl:template match="@*|text()" mode="unstyle"><xsl:copy-of select="."/></xsl:template>
       
    24 <xsl:template match="@style" mode="unstyle"/>
       
    25 
       
    26 
       
    27 <xsl:template match="*" mode="style-value"><xsl:param name="property"/>
       
    28 	<xsl:variable name="this">
       
    29 		<xsl:copy>
       
    30 			<xsl:apply-templates select="@*" mode="unstyle"/>
       
    31 			<xsl:call-template name="style"/>
       
    32 		</xsl:copy>
       
    33 	</xsl:variable>
       
    34 	<xsl:value-of select="exslt:node-set($this)/*/@*[name()=$property]"/>
       
    35 </xsl:template>
       
    36 
       
    37 
       
    38 <xsl:template name="style">
       
    39 	<xsl:variable name="cur" select="."/>
       
    40 	<xsl:variable name="styles">
       
    41 		<xsl:for-each select="exslt:node-set($css)/css//item[
       
    42 			( contains(concat(' ',normalize-space($cur/@class),' '),concat(' ',@class,' ')) or not(@class) ) and 
       
    43 			( @id = $cur/@id or not(@id) ) and
       
    44 			( not(starts-with(@psuedo,'lang(')) or lang(substring-before(substring-after(@psuedo,'lang(')),')')) and
       
    45 			( @tag = local-name($cur) or not(@tag) ) and not(ancestor::media)]">
       
    46 			<xsl:variable name="found">
       
    47 				<xsl:if test="parent::parent">
       
    48 					<xsl:apply-templates select="parent::parent" mode="match">
       
    49 						<xsl:with-param name="svg" select="$cur"/>
       
    50 					</xsl:apply-templates>
       
    51 				</xsl:if>
       
    52 				<xsl:if test="not(parent::parent)">1</xsl:if>
       
    53 			</xsl:variable>
       
    54 			<xsl:if test="$found!=''">
       
    55 				<xsl:copy-of select="*"/>
       
    56 			</xsl:if>
       
    57 		</xsl:for-each>
       
    58 		<xsl:apply-templates select="@style" mode="css"/>
       
    59 	</xsl:variable>
       
    60 	<!--  debug <xsl:copy-of select="$styles"/>-->
       
    61 	<xsl:for-each select="exslt:node-set($styles)/*">
       
    62 		<xsl:if test="not(preceding-sibling::*[name()=name(current()) and @bang='important']) and not($cur/@*[name()=name(current())])">
       
    63 		<xsl:attribute name="{name()}">
       
    64 			<xsl:value-of select="."/>
       
    65 		</xsl:attribute>
       
    66 		</xsl:if>
       
    67 	</xsl:for-each>
       
    68 </xsl:template>
       
    69 
       
    70 <xsl:template match="parent" mode="match">
       
    71 	<xsl:param name="svg"/>
       
    72 	<xsl:variable name="this" select="."/>
       
    73 	<xsl:variable name="match" select="$svg/ancestor::*[
       
    74 		( contains(concat(' ',normalize-space(@class),' '),concat(' ',$this/@class,' ')) or not($this/@class) ) and 
       
    75 		( $this/@id = @id or not($this/@id) ) and
       
    76 		( not(starts-with($this/@psuedo,'lang(')) or lang(substring-before(substring-after($this/@psuedo,'lang(')),')')) and
       
    77 		( $this/@tag = local-name() or not($this/@tag) )]"/>
       
    78 	<xsl:choose>
       
    79 		<xsl:when test="$match and parent::parent">
       
    80 			<xsl:for-each select="$match">
       
    81 				<xsl:apply-templates select="parent::parent" mode="match">
       
    82 					<xsl:with-param name="svg" select="current()"/>
       
    83 				</xsl:apply-templates>
       
    84 			</xsl:for-each>
       
    85 		</xsl:when>
       
    86 		<xsl:when test="$match">1</xsl:when>
       
    87 	</xsl:choose>
       
    88 </xsl:template>
       
    89 
       
    90 
       
    91 <xsl:template match="*" mode="css">
       
    92 	<xsl:call-template name="cssitem">
       
    93 		<xsl:with-param name="text">
       
    94 			<xsl:call-template name="nocomments"/>
       
    95 		</xsl:with-param>
       
    96 	</xsl:call-template>
       
    97 </xsl:template>
       
    98 
       
    99 <xsl:template match="@style" mode="css">
       
   100 	<xsl:call-template name="parse-properties">
       
   101 		<xsl:with-param select="." name="text"/>
       
   102 	</xsl:call-template>
       
   103 </xsl:template>
       
   104 
       
   105 <xsl:template name="cssitem"><xsl:param name="text" select="."/>
       
   106 	<xsl:variable name="pre" select="normalize-space(substring-before($text,'{'))"/>
       
   107 	<xsl:variable name="post" select="substring-after($text,'{')"/>
       
   108 	<xsl:choose>
       
   109 		<xsl:when test="contains($pre,'@')">
       
   110 			<xsl:variable name="cnt">
       
   111 				<xsl:call-template name="endgroup">
       
   112 					<xsl:with-param name="text" select="$post"/>
       
   113 				</xsl:call-template>
       
   114 			</xsl:variable>		
       
   115 			<xsl:element name="{substring-before(substring-after($pre,'@'),' ')}">
       
   116 				<xsl:attribute name="type">
       
   117 					<xsl:value-of select="normalize-space(substring-after(substring-after($pre,'@'),' '))"/>
       
   118 				</xsl:attribute>
       
   119 				<xsl:call-template name="cssitem">
       
   120 					<xsl:with-param name="text" select="$cnt"/>
       
   121 				</xsl:call-template>
       
   122 		</xsl:element>
       
   123 				<xsl:call-template name="cssitem">
       
   124 					<xsl:with-param name="text" select="substring($post,string-length($cnt) +  2)"/>
       
   125 				</xsl:call-template>
       
   126 		</xsl:when>
       
   127 		<xsl:otherwise>
       
   128 			<xsl:call-template name="parse-list">
       
   129 				<xsl:with-param name="list" select="$pre"/>
       
   130 				<xsl:with-param name="value-text" select="substring-before($post,'}')"/>
       
   131 			</xsl:call-template>
       
   132 			<xsl:variable name="next"  select="substring-after($post,'}')"/>
       
   133 			<xsl:if test="normalize-space($next)!=''">
       
   134 				<xsl:call-template name="cssitem">
       
   135 					<xsl:with-param name="text" select="$next"/>
       
   136 				</xsl:call-template>
       
   137 			</xsl:if>
       
   138 		</xsl:otherwise>
       
   139 	</xsl:choose>
       
   140 </xsl:template>
       
   141 
       
   142 
       
   143 <xsl:template name="endgroup"><xsl:param name="text"/>
       
   144 	<xsl:variable name="open" select="substring-before($text,'{')"/>
       
   145 	<xsl:variable name="close" select="substring-before($text,'}')"/>
       
   146 	<xsl:choose>
       
   147 		<xsl:when test="contains($text,'{') and string-length($open) &lt; string-length($close)">
       
   148 			<xsl:value-of select="concat($close,'}')"/>
       
   149 			<xsl:call-template name="endgroup">
       
   150 				<xsl:with-param name="text" select="substring-after($text,'}')"/>
       
   151 			</xsl:call-template>
       
   152 		</xsl:when>
       
   153 		<xsl:when test="contains($text,'}')">
       
   154 			<xsl:value-of select="$close"/>
       
   155 		</xsl:when>
       
   156 		<xsl:otherwise>
       
   157 			<xsl:value-of select="$text"/>
       
   158 		</xsl:otherwise>
       
   159 	</xsl:choose>
       
   160 </xsl:template>
       
   161 
       
   162 <xsl:template name="nocomments"><xsl:param name="text" select="."/>
       
   163 	<xsl:choose>
       
   164 		<xsl:when test="contains($text,'/*')">
       
   165 			<xsl:value-of select="substring-before($text,'/*')"/>
       
   166 			<xsl:call-template name="nocomments">
       
   167 				<xsl:with-param select="substring-after(substring-after($text,'/*'),'*/')" name="text"/>
       
   168 			</xsl:call-template>
       
   169 		</xsl:when>
       
   170 		<xsl:otherwise>
       
   171 			<xsl:value-of select="$text"/>
       
   172 		</xsl:otherwise>
       
   173 	</xsl:choose>
       
   174 </xsl:template>
       
   175 
       
   176 
       
   177 
       
   178 
       
   179 <xsl:template name="parse-list"><xsl:param name="value-text"/><xsl:param name="list"/>
       
   180 	<xsl:choose>
       
   181 		<xsl:when test="contains($list,',')">
       
   182 			<xsl:call-template name="parse-item">
       
   183 				<xsl:with-param select="normalize-space(substring-before($list,','))" name="item"/>
       
   184 				<xsl:with-param select="$value-text" name="value-text"/>
       
   185 			</xsl:call-template>
       
   186 			<xsl:call-template name="parse-list">
       
   187 				<xsl:with-param select="substring-after($list,',')" name="list"/>
       
   188 				<xsl:with-param select="$value-text" name="value-text"/>
       
   189 			</xsl:call-template>
       
   190 		</xsl:when>
       
   191 		<xsl:otherwise>
       
   192 			<xsl:call-template name="parse-item">
       
   193 				<xsl:with-param select="normalize-space($list)" name="item"/>
       
   194 				<xsl:with-param select="$value-text" name="value-text"/>
       
   195 			</xsl:call-template>
       
   196 		</xsl:otherwise>
       
   197 	</xsl:choose>
       
   198 </xsl:template>
       
   199 
       
   200 
       
   201 <xsl:template name="parse-item"><xsl:param name="value-text"/><xsl:param name="item"/>
       
   202 	<xsl:choose>
       
   203 		<xsl:when test="contains($item,' ')">
       
   204 			<xsl:call-template name="parse-term">
       
   205 				<xsl:with-param select="substring-before($item,' ')" name="item"/>
       
   206 				<xsl:with-param select="$value-text" name="value-text"/>
       
   207 				<xsl:with-param select="substring-after($item,' ')" name="descend"/>
       
   208 			</xsl:call-template>
       
   209 		</xsl:when>
       
   210 		<xsl:otherwise>
       
   211 			<xsl:call-template name="parse-term">
       
   212 				<xsl:with-param select="$item" name="item"/>
       
   213 				<xsl:with-param select="$value-text" name="value-text"/>
       
   214 			</xsl:call-template>
       
   215 		</xsl:otherwise>
       
   216 	</xsl:choose>
       
   217 </xsl:template>
       
   218 
       
   219 <xsl:template name="parse-term"><xsl:param name="value-text"/><xsl:param name="item"/><xsl:param name="descend"/>
       
   220 	<xsl:variable name="type">
       
   221 		<xsl:choose>
       
   222 			<xsl:when test="$descend=''">item</xsl:when>
       
   223 			<xsl:otherwise>parent</xsl:otherwise>
       
   224 		</xsl:choose>
       
   225 	</xsl:variable>
       
   226 	<xsl:element name="{$type}">
       
   227 		<xsl:variable name="v0"> <!-- check for pseudo class -->
       
   228 			<xsl:choose>
       
   229 				<xsl:when test="contains($item,':')"><xsl:value-of select="substring-before($item,':')"/></xsl:when>
       
   230 				<xsl:otherwise><xsl:value-of select="$item"/></xsl:otherwise>
       
   231 			</xsl:choose>
       
   232 		</xsl:variable>
       
   233 		<xsl:if test="$v0!=$item">
       
   234 			<xsl:attribute name="pseudo"><xsl:value-of select="substring-after($item,':')"/></xsl:attribute>
       
   235 		</xsl:if>
       
   236 		<xsl:if test="$v0 != '' ">
       
   237 			<xsl:variable name="v1">
       
   238 				<xsl:choose>
       
   239 					<xsl:when test="contains($v0,'.')"><xsl:value-of select="substring-before($v0,'.')"/></xsl:when>
       
   240 					<xsl:otherwise><xsl:value-of select="$v0"/></xsl:otherwise>
       
   241 				</xsl:choose>
       
   242 			</xsl:variable>
       
   243 			<xsl:if test="$v1!=$v0">
       
   244 				<xsl:attribute name="class"><xsl:value-of select="substring-after($v0,'.')"/></xsl:attribute>
       
   245 			</xsl:if>
       
   246 			<xsl:if test="$v1 != '' ">
       
   247 				<xsl:choose>
       
   248 					<xsl:when test="starts-with($v1,'#')">
       
   249 						<xsl:attribute name="id"><xsl:value-of select="substring($v1,2)"/></xsl:attribute>
       
   250 					</xsl:when>
       
   251 					<xsl:otherwise><xsl:attribute name="tag"><xsl:value-of select="$v1"/></xsl:attribute></xsl:otherwise>
       
   252 				</xsl:choose>
       
   253 			</xsl:if>
       
   254 		</xsl:if>
       
   255 		<xsl:choose>
       
   256 			<xsl:when test="$descend!=''">
       
   257 				<xsl:call-template name="parse-item">
       
   258 					<xsl:with-param select="$descend" name="item"/>
       
   259 					<xsl:with-param select="$value-text" name="value-text"/>
       
   260 				</xsl:call-template>
       
   261 			</xsl:when>
       
   262 			<xsl:otherwise>
       
   263 			<xsl:call-template name="parse-properties">
       
   264 				<xsl:with-param select="$value-text" name="text"/>
       
   265 			</xsl:call-template>
       
   266 		</xsl:otherwise>
       
   267 		</xsl:choose>
       
   268 	</xsl:element>
       
   269 </xsl:template>
       
   270 
       
   271 
       
   272 <xsl:template name="parse-properties"><xsl:param name="text"/>
       
   273 	<xsl:choose>
       
   274 		<xsl:when test="contains($text,';')">
       
   275 		<xsl:call-template name="parse-property">
       
   276 			<xsl:with-param select="substring-before($text,';')" name="text"/>
       
   277 		</xsl:call-template>
       
   278 		<xsl:if test="normalize-space(substring-after($text,';'))!=''">
       
   279 			<xsl:call-template name="parse-properties">
       
   280 				<xsl:with-param select="substring-after($text,';')" name="text"/>
       
   281 			</xsl:call-template>
       
   282 		</xsl:if>
       
   283 		</xsl:when>
       
   284 		<xsl:otherwise>
       
   285 			<xsl:call-template name="parse-property">
       
   286 				<xsl:with-param select="$text" name="text"/>
       
   287 			</xsl:call-template>
       
   288 		</xsl:otherwise>
       
   289 	</xsl:choose>
       
   290 </xsl:template>
       
   291 
       
   292 <xsl:template name="parse-property"><xsl:param name="text"/>
       
   293 <xsl:if test="contains($text,':')">
       
   294 	<xsl:variable name="post" select="substring-after($text,':')"/>
       
   295 
       
   296 	<xsl:element name="{normalize-space(substring-before($text,':'))}">
       
   297 		<xsl:choose>
       
   298 			<xsl:when test="contains($post,'!')">
       
   299 				<xsl:attribute name="bang"><xsl:value-of select="substring-after($post,'!')"/></xsl:attribute>
       
   300 				<xsl:value-of select="normalize-space(substring-before($post,'!'))"/>
       
   301 			</xsl:when>
       
   302 			<xsl:otherwise><xsl:value-of select="normalize-space($post)"/></xsl:otherwise>
       
   303 		</xsl:choose>
       
   304 	</xsl:element>
       
   305 </xsl:if>	
       
   306 
       
   307 </xsl:template>
       
   308 
       
   309 
       
   310 </xsl:stylesheet>