helpers/mimeXhtmlPart.xsl
author František Kučera <franta-hg@frantovo.cz>
Fri Oct 14 13:32:34 2011 +0200 (2011-10-14)
changeset 78 969f001a0f5f
parent 77 623025c704c5
child 79 245311d10726
permissions -rwxr-xr-x
Drupal: XSLT odstavce – textové vs. netextové uzly.
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <xsl:stylesheet version="2.0"
     3 	xmlns="http://www.w3.org/1999/xhtml"
     4 	xmlns:h="http://www.w3.org/1999/xhtml"
     5 	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     6 	xmlns:fn="http://www.w3.org/2005/xpath-functions"
     7 	xmlns:svg="http://www.w3.org/2000/svg"
     8 	xmlns:xs="http://www.w3.org/2001/XMLSchema"
     9 	exclude-result-prefixes="fn h xs">
    10 	<xsl:output 
    11 		method="xml" 
    12 		indent="yes" 
    13 		encoding="UTF-8"		
    14 		doctype-public="-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" 
    15 		doctype-system="http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd"/>
    16 	
    17 		
    18 	<xsl:param name="title"/>
    19 	<xsl:param name="isRoot"/>
    20 	<xsl:param name="urlBase"/>
    21 	<xsl:param name="wwwRead"/>
    22 	<xsl:param name="wwwPost"/>
    23 	
    24 	
    25 	<!-- Celý dokument -->
    26 	<xsl:template match="/">
    27 		<html>
    28 			<head>
    29 				<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
    30 				<meta http-equiv="X-NNTP-Generated" content="{fn:current-dateTime()}" />
    31 				<xsl:if test="$urlBase">
    32 					<base href="{$urlBase}"/>
    33 				</xsl:if>
    34 				<xsl:if test="$title">
    35 					<title><xsl:value-of select="$title"/></title>
    36 				</xsl:if>
    37 				<style type="text/css">
    38 					body {
    39 						font-family: sans-serif;
    40 						font-size: 16px;
    41 					}
    42 					
    43 					.wwwLinks {
    44 						border-top: 2px solid grey;
    45 						margin-top: 16px;
    46 						font-size: 66%;
    47 					}
    48 					
    49 					blockquote {
    50 						background-color: #eee;
    51 						font-style: italic;
    52 						padding: 2px 20px;
    53 						margin: 10px 32px;
    54 						border-left: 3px solid silver;
    55 					}
    56 					
    57 					pre {
    58 						background-color: #eee;
    59 						padding: 2px 20px;
    60 						margin: 10px 32px;
    61 						border-left: 3px solid #a00;
    62 					}
    63 					
    64 					img {
    65 						margin: 8px;
    66 					}
    67 					
    68 					a img {
    69 						border: none;
    70 					}
    71 					
    72 					/** TODO: smazat */
    73 					.mešuge {
    74 						background-color: #afa;
    75 					}
    76 				</style>
    77 			</head>
    78 			<body>
    79 				<xsl:if test="$title and $isRoot">
    80 					<h1><xsl:value-of select="$title"/></h1>			
    81 				</xsl:if>
    82 				<xsl:call-template name="zpracujTělo">
    83 					<xsl:with-param name="prvek" select="h:html/h:body/node()[1]"/>
    84 				</xsl:call-template>
    85 				
    86 				<xsl:if test="$wwwRead or $wwwPost">
    87 					<div class="wwwLinks">
    88 						<p>Přístup přes síť www:</p>
    89 						<ul>
    90 							<xsl:if test="$wwwRead"><li><a href="{$wwwRead}">číst tento příspěvek</a></li></xsl:if>
    91 							<xsl:if test="$wwwPost"><li><a href="{$wwwPost}">odpovědět na tento příspěvek</a></li></xsl:if>
    92 						</ul>
    93 					</div>
    94 				</xsl:if>
    95 			</body>
    96 		</html>
    97 	</xsl:template>
    98 	
    99 	
   100 	<!-- Odkazy -->
   101 	<xsl:template match="h:a">
   102 		<a href="{@href}" title="{@title}"><xsl:apply-templates/></a>
   103 	</xsl:template>
   104 	
   105 	
   106 	<!-- Obrázky -->
   107 	<xsl:template match="h:img">
   108 		<img src="{@src}" alt="{@alt}" title="{@title}"><xsl:apply-templates/></img>
   109 	</xsl:template>
   110 	
   111 	<!-- Zkratky -->
   112 	<xsl:template match="h:abbr">
   113 		<abbr title="{@title}"><xsl:apply-templates/></abbr>
   114 	</xsl:template>
   115 	
   116 	
   117 	<!-- Další povolené značky – ostatní odfiltrujeme (zbude z nich jen text) -->
   118 	<xsl:template match="*">
   119 		<xsl:choose>		
   120 			<xsl:when test="
   121 				name() = 'h1' or
   122 				name() = 'h2' or
   123 				name() = 'h3' or
   124 				name() = 'h4' or
   125 				name() = 'h5' or
   126 				name() = 'h6' or
   127 				name() = 'p' or
   128 				name() = 'div' or
   129 				name() = 'br' or
   130 				name() = 'strong' or
   131 				name() = 'em' or
   132 				name() = 'sub' or
   133 				name() = 'sup' or
   134 				name() = 'del' or
   135 				name() = 'ul' or
   136 				name() = 'ol' or
   137 				name() = 'li' or
   138 				name() = 'pre' or
   139 				name() = 'code' or
   140 				name() = 'blockquote'">
   141 				<xsl:element name="{name()}">
   142 					<xsl:apply-templates/>
   143 				</xsl:element>
   144 			</xsl:when>
   145 			<xsl:otherwise>
   146 				<xsl:value-of select="."/>
   147 			</xsl:otherwise>
   148 		</xsl:choose>		
   149     </xsl:template>
   150     
   151     
   152     <!-- 
   153     	Z neuzavřeného (nevalidně se vyskytujícího v body) textu uděláme odstavce.
   154     -->
   155 	<xsl:template name="zpracujTělo">
   156 		<xsl:param name="prvek"/>
   157 		<xsl:if test="$prvek">
   158 			<li>
   159 				&lt;
   160 				<xsl:choose>
   161 					<xsl:when test="$prvek/self::text()">
   162 						<!--
   163 							Textový uzel → budeme dělat odstavce
   164 							(rekurzivně se opět zavolá šablona zpracujTělo)
   165 						<xsl:call-template name="dělejOdstavce">
   166 							<xsl:with-param name="blokTextu" select="."/>
   167 						</xsl:call-template>
   168 						-->
   169 						[T]
   170 					</xsl:when>
   171 					<xsl:otherwise>
   172 						<!--
   173 							Jiný uzel → aplikujeme obecné šablony
   174 							a zpracujeme následující uzel.
   175 						<xsl:apply-templates select="."/>
   176 						-->
   177 						[N]
   178 					</xsl:otherwise>					
   179 				</xsl:choose>
   180 				[<xsl:value-of select="$prvek/name()"/>]
   181 				[<xsl:value-of select="$prvek/text()"/>]
   182 				[<xsl:value-of select="$prvek/self::text()"/>]
   183 				&gt;
   184 			</li>	
   185 			<xsl:call-template name="zpracujTělo">
   186 					<xsl:with-param name="prvek" select="$prvek/following-sibling::node()[1]"/>
   187 			</xsl:call-template>
   188 				
   189 		</xsl:if>
   190 	</xsl:template>
   191 	
   192 	<xsl:template name="dělejOdstavce">
   193 		<xsl:param name="blokTextu"/>
   194 		<xsl:variable name="oddělovač" select="'\n\s+\n\s+'"/>
   195 		<xsl:for-each select="fn:tokenize(., $oddělovač)">
   196 			<xsl:if test="normalize-space(.)">
   197 				<p class="mešuge">
   198 					<xsl:value-of select="."/>
   199 					<!-- 
   200 						Toto je poslední odstavec bloku textu 
   201 						a blok nekončí dvěma konci řádku → 
   202 						může za ním následovat značka (např. odkaz nebo tučné písmo)
   203 						vnořená do téhož odstavce
   204 					-->
   205 					<xsl:if test="
   206 						position() = last() and
   207 						not(fn:matches($blokTextu, concat('.*', $oddělovač ,'$')))
   208 						">
   209 						
   210 						<xsl:variable name="n" select="$blokTextu/following-sibling::*[position() = 1]"/>
   211 						<xsl:variable name="nn" select="$n/name()"/>
   212 						<!--
   213 							Za blokem textu nenásleduje značka, která nemůže být uvnitř odstavce.
   214 						-->
   215 						<xsl:if test="not(
   216 								$nn = 'p' or
   217 								$nn = 'div' or								
   218 								$nn = 'h1' or								
   219 								$nn = 'h2' or								
   220 								$nn = 'h3' or								
   221 								$nn = 'h4' or								
   222 								$nn = 'h5' or								
   223 								$nn = 'h6' or								
   224 								$nn = 'pre' or								
   225 								$nn = 'table' or								
   226 								$nn = 'blockquote' or								
   227 								$nn = 'hr'								
   228 							)">
   229 							<xsl:apply-templates select="$n"/>
   230 							<xsl:apply-templates select="$n/following-sibling::text()[position() = 1]"/>
   231 						</xsl:if>						
   232 					</xsl:if>
   233 				</p>
   234 			</xsl:if>
   235 		</xsl:for-each>
   236 	</xsl:template>
   237     
   238 	
   239 </xsl:stylesheet>