helpers/mimeXhtmlPart.xsl
author František Kučera <franta-hg@frantovo.cz>
Fri Oct 14 12:41:14 2011 +0200 (2011-10-14)
changeset 77 623025c704c5
parent 76 b5690fc25af6
child 78 969f001a0f5f
permissions -rwxr-xr-x
Drupal: lámání textu na odstavce – XSLT 2.0, TODO:…
     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:apply-templates select="h:html/h:body/node()"/>
    83 				
    84 				<xsl:if test="$wwwRead or $wwwPost">
    85 					<div class="wwwLinks">
    86 						<p>Přístup přes síť www:</p>
    87 						<ul>
    88 							<xsl:if test="$wwwRead"><li><a href="{$wwwRead}">číst tento příspěvek</a></li></xsl:if>
    89 							<xsl:if test="$wwwPost"><li><a href="{$wwwPost}">odpovědět na tento příspěvek</a></li></xsl:if>
    90 						</ul>
    91 					</div>
    92 				</xsl:if>
    93 			</body>
    94 		</html>
    95 	</xsl:template>
    96 	
    97 	
    98 	<!-- Odkazy -->
    99 	<xsl:template match="h:a">
   100 		<a href="{@href}" title="{@title}"><xsl:apply-templates/></a>
   101 	</xsl:template>
   102 	
   103 	
   104 	<!-- Obrázky -->
   105 	<xsl:template match="h:img">
   106 		<img src="{@src}" alt="{@alt}" title="{@title}"><xsl:apply-templates/></img>
   107 	</xsl:template>
   108 	
   109 	<!-- Zkratky -->
   110 	<xsl:template match="h:abbr">
   111 		<abbr title="{@title}"><xsl:apply-templates/></abbr>
   112 	</xsl:template>
   113 	
   114 	
   115 	<!-- Další povolené značky – ostatní odfiltrujeme (zbude z nich jen text) -->
   116 	<xsl:template match="*">
   117 		<xsl:choose>		
   118 			<xsl:when test="
   119 				name() = 'h1' or
   120 				name() = 'h2' or
   121 				name() = 'h3' or
   122 				name() = 'h4' or
   123 				name() = 'h5' or
   124 				name() = 'h6' or
   125 				name() = 'p' or
   126 				name() = 'div' or
   127 				name() = 'br' or
   128 				name() = 'strong' or
   129 				name() = 'em' or
   130 				name() = 'sub' or
   131 				name() = 'sup' or
   132 				name() = 'del' or
   133 				name() = 'ul' or
   134 				name() = 'ol' or
   135 				name() = 'li' or
   136 				name() = 'pre' or
   137 				name() = 'code' or
   138 				name() = 'blockquote'">
   139 				<xsl:element name="{name()}">
   140 					<xsl:apply-templates/>
   141 				</xsl:element>
   142 			</xsl:when>
   143 			<xsl:otherwise>
   144 				<xsl:value-of select="."/>
   145 			</xsl:otherwise>
   146 		</xsl:choose>		
   147     </xsl:template>
   148     
   149     
   150     <!-- 
   151     	Z neuzavřeného (nevalidně se vyskytujícího v body) textu uděláme odstavce.
   152     -->
   153 	<xsl:template match="h:body/text()">
   154 		<xsl:call-template name="dělejOdstavce">
   155 			<xsl:with-param name="blokTextu" select="."/>
   156 		</xsl:call-template>		
   157 	</xsl:template>
   158 	<xsl:template name="dělejOdstavce">
   159 		<xsl:param name="blokTextu"/>
   160 		<xsl:variable name="oddělovač" select="'\n\s+\n\s+'"/>
   161 		<xsl:for-each select="fn:tokenize(., $oddělovač)">
   162 			<xsl:if test="normalize-space(.)">
   163 				<p class="mešuge">
   164 					<xsl:value-of select="."/>
   165 					<!-- 
   166 						Toto je poslední odstavec bloku textu 
   167 						a blok nekončí dvěma konci řádku → 
   168 						může za ním následovat značka (např. odkaz nebo tučné písmo)
   169 						vnořená do téhož odstavce
   170 					-->
   171 					<xsl:if test="
   172 						position() = last() and
   173 						not(fn:matches($blokTextu, concat('.*', $oddělovač ,'$')))
   174 						">
   175 						
   176 						<xsl:variable name="n" select="$blokTextu/following-sibling::*[position() = 1]"/>
   177 						<xsl:variable name="nn" select="$n/name()"/>
   178 						<!--
   179 							Za blokem textu nenásleduje značka, která nemůže být uvnitř odstavce.
   180 						-->
   181 						<xsl:if test="not(
   182 								$nn = 'p' or
   183 								$nn = 'div' or								
   184 								$nn = 'h1' or								
   185 								$nn = 'h2' or								
   186 								$nn = 'h3' or								
   187 								$nn = 'h4' or								
   188 								$nn = 'h5' or								
   189 								$nn = 'h6' or								
   190 								$nn = 'pre' or								
   191 								$nn = 'table' or								
   192 								$nn = 'blockquote' or								
   193 								$nn = 'hr'								
   194 							)">
   195 							<xsl:apply-templates select="$n"/>
   196 							<xsl:apply-templates select="$n/following-sibling::text()[position() = 1]"/>
   197 						</xsl:if>						
   198 					</xsl:if>
   199 				</p>
   200 			</xsl:if>
   201 		</xsl:for-each>
   202 	</xsl:template>
   203     
   204 	
   205 </xsl:stylesheet>