java/Postak/src/cz/frantovo/postak/odchozíEmailText.xsl
author František Kučera <franta-hg@frantovo.cz>
Tue Oct 15 23:16:04 2013 +0200 (2013-10-15)
changeset 21 798e7455da4e
parent 17 9cb46ca7e26c
permissions -rw-r--r--
XSLT převod XHTML na prostý text: mailto odkazy nebudou obsahovat duplicitně adresu
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!--
     3 	Převzato z projektu hellDesk.
     4 -->
     5 <xsl:stylesheet version="1.0"
     6 				xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     7 				xmlns:fn="http://www.w3.org/2005/xpath-functions"
     8 				xmlns:str="http://xsltsl.org/string"
     9 				xmlns:h="http://www.w3.org/1999/xhtml">
    10 	<xsl:output method="text" encoding="UTF-8"/>
    11 	<xsl:strip-space elements="*"/>
    12 
    13 	<xsl:output method="text" encoding="UTF-8"/>
    14 	<xsl:strip-space elements="*"/>
    15 
    16 	<xsl:variable name="urlBase" select="/h:html/h:head/h:base/@href"/>
    17 
    18 
    19 	<!-- Celý dokument -->
    20 	<xsl:template match="/">
    21 		<xsl:apply-templates select="h:html/h:body"/>
    22 	</xsl:template>
    23 
    24 
    25 	<xsl:template match="h:h1">
    26 		<xsl:value-of select="text()"/>
    27 		<xsl:text>&#10;</xsl:text>
    28 		<xsl:call-template name="str:generate-string">
    29 			<xsl:with-param name="text" select="'='"/>
    30 			<xsl:with-param name="count" select="string-length(.)"/>
    31 		</xsl:call-template>
    32 		<xsl:text>&#10;</xsl:text>
    33 		<xsl:text>&#10;</xsl:text>
    34 	</xsl:template>
    35 
    36 
    37 	<xsl:template match="h:h2">
    38 		<xsl:value-of select="text()"/>
    39 		<xsl:text>&#10;</xsl:text>
    40 		<xsl:call-template name="str:generate-string">
    41 			<xsl:with-param name="text" select="'-'"/>
    42 			<xsl:with-param name="count" select="string-length(.)"/>
    43 		</xsl:call-template>
    44 		<xsl:text>&#10;</xsl:text>
    45 		<xsl:text>&#10;</xsl:text>
    46 	</xsl:template>
    47 
    48 
    49 	<xsl:template match="h:h3">
    50 		<xsl:text>### </xsl:text>
    51 		<xsl:value-of select="text()"/>
    52 		<xsl:text>&#10;</xsl:text>
    53 		<xsl:text>&#10;</xsl:text>
    54 	</xsl:template>
    55 
    56 
    57 	<xsl:template match="h:h4">
    58 		<xsl:text>#### </xsl:text>
    59 		<xsl:value-of select="text()"/>
    60 		<xsl:text>&#10;</xsl:text>
    61 		<xsl:text>&#10;</xsl:text>
    62 	</xsl:template>
    63 
    64 
    65 	<xsl:template match="h:h5">
    66 		<xsl:text>##### </xsl:text>
    67 		<xsl:value-of select="text()"/>
    68 		<xsl:text>&#10;</xsl:text>
    69 		<xsl:text>&#10;</xsl:text>
    70 	</xsl:template>
    71 
    72 
    73 	<xsl:template match="h:h6">
    74 		<xsl:text>###### </xsl:text>
    75 		<xsl:value-of select="text()"/>
    76 		<xsl:text>&#10;</xsl:text>
    77 		<xsl:text>&#10;</xsl:text>
    78 	</xsl:template>
    79 
    80 
    81 	<xsl:template match="h:p">
    82 		<xsl:apply-templates/>
    83 		<xsl:text>&#10;</xsl:text>
    84 		<xsl:text>&#10;</xsl:text>
    85 	</xsl:template>
    86 
    87 	
    88 	<xsl:template match="h:blockquote/h:p">
    89 		<xsl:text>&gt; </xsl:text>
    90 		<xsl:apply-templates/>
    91 		<xsl:text>&#10;</xsl:text>
    92 		<xsl:text>&#10;</xsl:text>
    93 	</xsl:template>
    94 
    95 
    96 	<xsl:template match="h:cite[not(starts-with(., '„') or starts-with(., '‚'))]">
    97 		<xsl:text>„</xsl:text>
    98 		<xsl:apply-templates/>
    99 		<xsl:text>“</xsl:text>
   100 	</xsl:template>	
   101 
   102 
   103 	<xsl:template match="h:a">
   104 		<xsl:if test="not(text() = @href or concat('mailto:', text()) = @href)">
   105 			<xsl:text>'</xsl:text>
   106 			<xsl:apply-templates select="node()"/>
   107 			<xsl:text>' </xsl:text>
   108 		</xsl:if>
   109 		<xsl:text>&lt;</xsl:text>
   110 		<xsl:choose>
   111 			<xsl:when test="starts-with(@href, 'http:') or starts-with(@href, 'https:') or starts-with(@href, 'ftp:')">
   112 				<xsl:value-of select="@href"/>
   113 			</xsl:when>
   114 			<xsl:when test="starts-with(@href, 'mailto:')">
   115 				<xsl:value-of select="substring-after(@href, 'mailto:')"/>
   116 			</xsl:when>
   117 			<xsl:otherwise>
   118 				<xsl:choose>
   119 					<!-- TODO: $urlBase by nemělo končit lomítkem -->
   120 					<xsl:when test="starts-with(@href, '/')">
   121 						<xsl:value-of select="concat($urlBase, @href)"/>
   122 					</xsl:when>
   123 					<xsl:otherwise>
   124 						<xsl:value-of select="concat($urlBase, '/', @href)"/>
   125 					</xsl:otherwise>
   126 				</xsl:choose>
   127 			</xsl:otherwise>
   128 		</xsl:choose>
   129 		<xsl:text>&gt;</xsl:text>
   130 		<xsl:if test="normalize-space(@title)">
   131 			<xsl:text> (</xsl:text>
   132 			<xsl:value-of select="@title"/>
   133 			<xsl:text>)</xsl:text>
   134 		</xsl:if>
   135 	</xsl:template>
   136 
   137 
   138 	<xsl:template match="h:img">
   139 		<xsl:text>Obrázek: </xsl:text>
   140 		<xsl:value-of select="@src"/>
   141 		<!-- TODO: relativní odkazy -->
   142 	</xsl:template>
   143 
   144 
   145 	<xsl:template match="h:strong|h:b">
   146 		<xsl:text>**</xsl:text>
   147 		<xsl:apply-templates/>
   148 		<xsl:text>**</xsl:text>
   149 	</xsl:template>
   150 
   151 
   152 	<xsl:template match="h:em|h:i">
   153 		<xsl:text>*</xsl:text>
   154 		<xsl:apply-templates/>
   155 		<xsl:text>*</xsl:text>
   156 	</xsl:template>
   157 
   158 
   159 	<xsl:template match="h:abbr[@title]">
   160 		<xsl:apply-templates/>
   161 		<xsl:text> (</xsl:text>
   162 		<xsl:value-of select="@title"/>
   163 		<xsl:text>)</xsl:text>
   164 	</xsl:template>
   165 
   166 
   167 	<xsl:template match="h:pre">
   168 		<xsl:text>--------------------------------</xsl:text>
   169 		<xsl:text>&#10;</xsl:text>
   170 		<xsl:apply-templates/>
   171 		<xsl:text>&#10;</xsl:text>
   172 		<xsl:text>--------------------------------</xsl:text>
   173 		<xsl:text>&#10;</xsl:text>
   174 		<xsl:text>&#10;</xsl:text>
   175 	</xsl:template>
   176 
   177 
   178 	<xsl:template match="h:code">
   179 		<xsl:text>`</xsl:text>
   180 		<xsl:apply-templates/>
   181 		<xsl:text>`</xsl:text>
   182 	</xsl:template>
   183 
   184 
   185 	<xsl:template match="h:hr">
   186 		<xsl:text>----------------------------------------------------------------</xsl:text>
   187 		<xsl:text>&#10;</xsl:text>
   188 		<xsl:text>&#10;</xsl:text>
   189 	</xsl:template>
   190 
   191 
   192 	<xsl:template match="h:br">
   193 		<xsl:text>&#10;</xsl:text>
   194 	</xsl:template>	
   195 
   196 
   197 	<xsl:template match="h:ol|h:ul">
   198 		<xsl:variable name="úroveň" select="count(ancestor::h:li)"/>
   199 		<xsl:variable name="odsazení">
   200 			<!-- položky na nejvyšší úrovni odsazené jednou mezerou -->
   201 			<xsl:value-of select="' '"/>
   202 			<xsl:call-template name="str:generate-string">
   203 				<xsl:with-param name="text" select="'  '"/>
   204 				<!-- položky na druhé a vyšší úrovni odsazené vždy dvěma dalšími mezerami -->
   205 				<xsl:with-param name="count" select="$úroveň - 1"/>
   206 			</xsl:call-template>
   207 		</xsl:variable>
   208 
   209 		<xsl:if test="$úroveň &gt; 0">
   210 			<xsl:text>&#10;</xsl:text>
   211 		</xsl:if>
   212 
   213 		<xsl:for-each select="h:li">
   214 			<xsl:if test="parent::h:ol">
   215 				<xsl:value-of select="concat($odsazení, position(), ') ')"/>
   216 			</xsl:if>
   217 			<xsl:if test="parent::h:ul">
   218 				<xsl:value-of select="concat($odsazení, '- ')"/>
   219 			</xsl:if>
   220 			<xsl:apply-templates/>
   221 			<xsl:if test="$úroveň = 0 or not(position() = last())">
   222 				<xsl:text>&#10;</xsl:text>
   223 			</xsl:if>
   224 		</xsl:for-each>
   225 		
   226 		<xsl:if test="$úroveň = 0">
   227 			<xsl:text>&#10;</xsl:text>
   228 		</xsl:if>
   229 	</xsl:template>
   230 
   231 
   232 	<xsl:template match="text()[not(parent::h:pre)]">
   233 		<xsl:if test="(starts-with(., ' ') or starts-with(., '&#10;')) and preceding-sibling::node() and not(preceding-sibling::h:br)">
   234 			<xsl:text> </xsl:text>
   235 		</xsl:if>
   236 		<xsl:value-of select="normalize-space(.)"/>
   237 		<xsl:variable name="posledníZnak" select="substring(., string-length(.))"/>
   238 		<!-- TODO: tabulátor? -->
   239 		<xsl:if test="($posledníZnak = ' ' or $posledníZnak = '&#10;') and following-sibling::node()">
   240 			<xsl:text> </xsl:text>
   241 		</xsl:if>
   242 	</xsl:template>
   243 	
   244 	
   245 	<xsl:template match="h:div[@class = 'patička']">
   246 		<xsl:text>-- </xsl:text>
   247 		<xsl:text>&#10;</xsl:text>
   248 		<xsl:apply-templates/>
   249 	</xsl:template>
   250 	
   251 
   252 	<!--
   253 		Převzato z:
   254 		http://xsltsl.sourceforge.net/string.html#template.str:generate-string
   255 	-->
   256 	<xsl:template name="str:generate-string">
   257 		<xsl:param name="text"/>
   258 		<xsl:param name="count"/>
   259 
   260 		<xsl:choose>
   261 			<xsl:when test="string-length($text) = 0 or $count &lt;= 0"/>
   262 			<xsl:otherwise>
   263 				<xsl:value-of select="$text"/>
   264 				<xsl:call-template name="str:generate-string">
   265 					<xsl:with-param name="text" select="$text"/>
   266 					<xsl:with-param name="count" select="$count - 1"/>
   267 				</xsl:call-template>
   268 			</xsl:otherwise>
   269 		</xsl:choose>
   270 	</xsl:template>
   271 
   272 
   273 </xsl:stylesheet>
   274