helpers/mimeXhtmlPart.xsl
author František Kučera <franta-hg@frantovo.cz>
Fri Oct 14 00:45:06 2011 +0200 (2011-10-14)
changeset 76 b5690fc25af6
child 77 623025c704c5
permissions -rwxr-xr-x
Drupal: XSL šablona pro formátování XHTML části zprávy.
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <xsl:stylesheet version="1.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 				<xsl:if test="$urlBase">
    31 					<base href="{$urlBase}"/>
    32 				</xsl:if>
    33 				<xsl:if test="$title">
    34 					<title><xsl:value-of select="$title"/></title>
    35 				</xsl:if>
    36 				<style type="text/css">
    37 					body {
    38 						font-family: sans-serif;
    39 						font-size: 16px;
    40 					}
    41 					
    42 					.wwwLinks {
    43 						border-top: 2px solid grey;
    44 						margin-top: 16px;
    45 						font-size: 66%;
    46 					}
    47 					
    48 					blockquote {
    49 						background-color: #eee;
    50 						font-style: italic;
    51 						padding: 2px 20px;
    52 						margin: 10px 32px;
    53 						border-left: 3px solid silver;
    54 					}
    55 					
    56 					pre {
    57 						background-color: #eee;
    58 						padding: 2px 20px;
    59 						margin: 10px 32px;
    60 						border-left: 3px solid #a00;
    61 					}
    62 					
    63 					img {
    64 						margin: 8px;
    65 					}
    66 					
    67 					a img {
    68 						border: none;
    69 					}
    70 				</style>
    71 			</head>
    72 			<body>
    73 				<xsl:if test="$title and $isRoot">
    74 					<h1><xsl:value-of select="$title"/></h1>			
    75 				</xsl:if>
    76 				<xsl:apply-templates select="h:html/h:body/node()"/>
    77 				
    78 				<xsl:if test="$wwwRead or $wwwPost">
    79 					<div class="wwwLinks">
    80 						<p>Přístup přes síť www:</p>
    81 						<ul>
    82 							<xsl:if test="$wwwRead"><li><a href="{$wwwRead}">číst tento příspěvek</a></li></xsl:if>
    83 							<xsl:if test="$wwwPost"><li><a href="{$wwwPost}">odpovědět na tento příspěvek</a></li></xsl:if>
    84 						</ul>
    85 					</div>
    86 				</xsl:if>
    87 			</body>
    88 		</html>
    89 	</xsl:template>
    90 	
    91 	
    92 	<!-- Odkazy -->
    93 	<xsl:template match="h:a">
    94 		<a href="{@href}" title="{@title}"><xsl:apply-templates/></a>
    95 	</xsl:template>
    96 	
    97 	
    98 	<!-- Obrázky -->
    99 	<xsl:template match="h:img">
   100 		<img src="{@src}" alt="{@alt}" title="{@title}"><xsl:apply-templates/></img>
   101 	</xsl:template>
   102 	
   103 	<!-- Zkratky -->
   104 	<xsl:template match="h:abbr">
   105 		<abbr title="{@title}"><xsl:apply-templates/></abbr>
   106 	</xsl:template>
   107 	
   108 	
   109 	<!-- Další povolené značky – ostatní odfiltrujeme (zbude z nich jen text) -->
   110 	<xsl:template match="*">
   111 		<xsl:choose>		
   112 			<xsl:when test="
   113 				name() = 'h1' or
   114 				name() = 'h2' or
   115 				name() = 'h3' or
   116 				name() = 'h4' or
   117 				name() = 'h5' or
   118 				name() = 'h6' or
   119 				name() = 'p' or
   120 				name() = 'div' or
   121 				name() = 'br' or
   122 				name() = 'strong' or
   123 				name() = 'em' or
   124 				name() = 'sub' or
   125 				name() = 'sup' or
   126 				name() = 'del' or
   127 				name() = 'ul' or
   128 				name() = 'ol' or
   129 				name() = 'li' or
   130 				name() = 'pre' or
   131 				name() = 'code' or
   132 				name() = 'blockquote'">
   133 				<xsl:element name="{name()}">
   134 					<xsl:apply-templates/>
   135 				</xsl:element>
   136 			</xsl:when>
   137 			<xsl:otherwise>
   138 				<xsl:value-of select="."/>
   139 			</xsl:otherwise>
   140 		</xsl:choose>		
   141     </xsl:template>
   142     
   143     
   144     <!-- 
   145     	Z neuzavřeného (nevalidně se vyskytujícího v body) textu uděláme odstavce.
   146     	Bohužel nefunguje, když neuzavřený text obsahuje jiné značky (odkazy atd.),
   147     	pak se totiž jedná o dva textové uzly (před značkou a za ní) a vzniknou dva odstavce.
   148     -->
   149 	<xsl:template match="h:body/text()">
   150 		<xsl:call-template name="makeParagraphs">
   151 			<xsl:with-param name="string" select="."/>
   152 		</xsl:call-template>
   153 	</xsl:template>
   154     <xsl:variable name="newLinePlaceholder" select="'ߜ'"/>
   155     <xsl:template name="makeParagraphs">
   156     	<xsl:param name="string" />
   157     	<xsl:call-template name="makeParagraphsInternal">
   158     		<!--
   159     			Konec řádku převedeme na obskurní znak, 
   160     			normalizací pročistíme bílé znaky → 
   161     			dva obskurní znaky oddělené mezerou pak značí předěl mezi odstavci
   162     		-->
   163     		<xsl:with-param name="string" select="normalize-space(translate(., '&#10;', $newLinePlaceholder))"/>
   164     	</xsl:call-template>
   165     </xsl:template>
   166 	<xsl:template name="makeParagraphsInternal">
   167 		<xsl:param name="string" />
   168 		<xsl:if test="ends-with($string, $newLinePlaceholder)"><xsl:comment>končí na $newLinePlaceholder</xsl:comment> </xsl:if>
   169 		<!-- V XSLT 2.0 můžeme použít fn:tokenize -->
   170 		<xsl:param name="delimiter" select="concat($newLinePlaceholder, ' ', $newLinePlaceholder)"/>
   171 		<xsl:choose>
   172 			<xsl:when test="$delimiter and contains($string, $delimiter)">
   173 				<xsl:call-template name="makeParagraph">
   174 					<xsl:with-param name="string" select="substring-before($string, $delimiter)"></xsl:with-param>
   175 				</xsl:call-template>
   176 				<xsl:call-template name="makeParagraphsInternal">
   177 					<xsl:with-param name="string" select="substring-after($string, $delimiter)" />
   178 					<xsl:with-param name="delimiter" select="$delimiter" />
   179 				</xsl:call-template>
   180 			</xsl:when>
   181 			<xsl:otherwise>
   182 				<!--
   183 					Poslední odstavec → zkontrolovat, zda je za ním  
   184 				-->
   185 				<xsl:call-template name="makeParagraph">
   186 					<xsl:with-param name="string" select="$string"></xsl:with-param>
   187 				</xsl:call-template>
   188 			</xsl:otherwise>
   189 		</xsl:choose>
   190 	</xsl:template>
   191 	<xsl:template name="makeParagraph">
   192 		<xsl:param name="string" />
   193 			<!-- prázdné textové uzly nás nezajímají – nechceme prázdné odstavce -->
   194 			<xsl:if test="normalize-space($string)">
   195 				<p class="mešuge"><xsl:value-of select="translate($string, $newLinePlaceholder, '&#10;')" /></p>				
   196 			</xsl:if>
   197 	</xsl:template>
   198 
   199 </xsl:stylesheet>