helpers/mimeXhtmlPart.xsl
author František Kučera <franta-hg@frantovo.cz>
Tue Oct 18 19:10:29 2011 +0200 (2011-10-18)
changeset 94 8c5867ac69df
parent 90 2ed2497d4559
child 97 7520a9246ff5
permissions -rwxr-xr-x
Drupal: lepší ošetření atributů u obrázků a odkazů, konverze i/b → em/strong, kontrola (apply-templates) vnořených značek.
     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="headComment"/>
    20 	<xsl:param name="isRoot"/>
    21 	<xsl:param name="urlBase"/>
    22 	<xsl:param name="wwwRead"/>
    23 	<xsl:param name="wwwPost"/>
    24 	
    25 	
    26 	<!-- Celý dokument -->
    27 	<xsl:template match="/">
    28 		<html>
    29 			<head>
    30 				<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
    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 				<xsl:if test="$headComment">
    38 					<xsl:comment><xsl:value-of select="$headComment"/></xsl:comment>
    39 				</xsl:if>
    40 				<style type="text/css">
    41 					body {
    42 						font-family: sans-serif;
    43 						font-size: 16px;
    44 					}
    45 					
    46 					.wwwLinks {
    47 						border-top: 2px solid grey;
    48 						margin-top: 16px;
    49 						font-size: 66%;
    50 					}
    51 					
    52 					blockquote {
    53 						background-color: #eee;
    54 						font-style: italic;
    55 						padding: 2px 20px;
    56 						margin: 10px 32px;
    57 						border-left: 3px solid silver;
    58 					}
    59 					
    60 					pre {
    61 						background-color: #eee;
    62 						padding: 2px 20px;
    63 						margin: 10px 32px;
    64 						border-left: 3px solid #a00;
    65 					}
    66 					
    67 					img {
    68 						margin: 8px;
    69 					}
    70 					
    71 					a img {
    72 						border: none;
    73 					}
    74 				</style>
    75 			</head>
    76 			<body>
    77 				<xsl:if test="$title and $isRoot">
    78 					<h1><xsl:value-of select="$title"/></h1>			
    79 				</xsl:if>
    80 								
    81 				<xsl:apply-templates select="h:html/h:body"/>
    82 				
    83 				<xsl:if test="$wwwRead or $wwwPost">
    84 					<div class="wwwLinks">
    85 						<p>Přístup přes síť www:</p>
    86 						<ul>
    87 							<xsl:if test="$wwwRead"><li><a href="{$wwwRead}">číst tento příspěvek</a></li></xsl:if>
    88 							<xsl:if test="$wwwPost"><li><a href="{$wwwPost}">odpovědět na tento příspěvek</a></li></xsl:if>
    89 						</ul>
    90 					</div>
    91 				</xsl:if>
    92 			</body>
    93 		</html>
    94 	</xsl:template>
    95 	
    96 	
    97 	<!-- Odkazy -->
    98 	<xsl:template match="h:a">
    99 		<xsl:element name="a">
   100 			<xsl:if test="@href"><xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute></xsl:if>
   101 			<xsl:if test="@title"><xsl:attribute name="title"><xsl:value-of select="@title"/></xsl:attribute></xsl:if>
   102 			<xsl:if test="@id"><xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute></xsl:if>
   103 			<xsl:apply-templates select="node()"/>
   104 		</xsl:element>
   105 	</xsl:template>
   106 	
   107 	
   108 	<!-- Obrázky -->
   109 	<xsl:template match="h:img">
   110 		<xsl:element name="img">
   111 			<xsl:if test="@src"><xsl:attribute name="src"><xsl:value-of select="@src"/></xsl:attribute></xsl:if>
   112 			<xsl:if test="@title"><xsl:attribute name="title"><xsl:value-of select="@title"/></xsl:attribute></xsl:if>
   113 			<xsl:if test="@alt"><xsl:attribute name="alt"><xsl:value-of select="@alt"/></xsl:attribute></xsl:if>
   114 		</xsl:element>
   115 	</xsl:template>
   116 	
   117 	
   118 	<!-- Zkratky -->
   119 	<xsl:template match="h:abbr">
   120 		<abbr title="{@title}"><xsl:apply-templates/></abbr>
   121 	</xsl:template>
   122 	
   123 	
   124 	<!-- Citace -->
   125 	<xsl:template match="h:blockquote[not(h:p)]">
   126 		<blockquote>
   127 			<p>
   128 				<xsl:copy-of select="node()"/>
   129 			</p>
   130 		</blockquote>
   131 	</xsl:template>
   132 	
   133 	<!-- Tučné písmo -->
   134 	<xsl:template match="h:b">
   135 		<strong>
   136 			<xsl:apply-templates select="node()"/>
   137 		</strong>
   138 	</xsl:template>
   139 	
   140 	<!-- Skloněné písmo -->
   141 	<xsl:template match="h:i">
   142 		<em>
   143 			<xsl:apply-templates select="node()"/>
   144 		</em>
   145 	</xsl:template>
   146 	
   147 	
   148 	<!--
   149 		Další povolené značky (jejich případné atributy zahodíme).
   150 		Ostatní elementy odfiltrujeme (zbude z nich jen text). 
   151 	-->
   152 	<xsl:template match="*">
   153 		<xsl:choose>		
   154 			<xsl:when test="
   155 				name() = 'h1' or
   156 				name() = 'h2' or
   157 				name() = 'h3' or
   158 				name() = 'h4' or
   159 				name() = 'h5' or
   160 				name() = 'h6' or
   161 				name() = 'p' or
   162 				name() = 'div' or
   163 				name() = 'br' or
   164 				name() = 'hr' or
   165 				name() = 'strong' or
   166 				name() = 'em' or
   167 				name() = 'sub' or
   168 				name() = 'sup' or
   169 				name() = 'del' or
   170 				name() = 'ul' or
   171 				name() = 'ol' or
   172 				name() = 'li' or
   173 				name() = 'pre' or
   174 				name() = 'code' or
   175 				name() = 'blockquote'">
   176 				<xsl:element name="{name()}">
   177 					<xsl:apply-templates select="node()"/>
   178 				</xsl:element>
   179 			</xsl:when>
   180 			<xsl:otherwise>
   181 				<xsl:apply-templates/>
   182 			</xsl:otherwise>
   183 		</xsl:choose>		
   184     </xsl:template>
   185 	
   186 </xsl:stylesheet>