[ Index ] |
PHP Cross Reference of phpBB-3.3.14-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package s9e\TextFormatter 5 * @copyright Copyright (c) 2010-2022 The s9e authors 6 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 7 */ 8 namespace s9e\TextFormatter\Configurator\RendererGenerators; 9 10 use s9e\TextFormatter\Configurator\Helpers\TemplateHelper; 11 use s9e\TextFormatter\Configurator\RendererGenerator; 12 use s9e\TextFormatter\Configurator\Rendering; 13 use s9e\TextFormatter\Configurator\TemplateNormalizer; 14 use s9e\TextFormatter\Renderers\XSLT as XSLTRenderer; 15 16 class XSLT implements RendererGenerator 17 { 18 /** 19 * @var TemplateNormalizer 20 */ 21 public $normalizer; 22 23 /** 24 * Constructor 25 */ 26 public function __construct() 27 { 28 $this->normalizer = new TemplateNormalizer([ 29 'MergeConsecutiveCopyOf', 30 'MergeIdenticalConditionalBranches', 31 'OptimizeNestedConditionals', 32 'RemoveLivePreviewAttributes' 33 ]); 34 } 35 36 /** 37 * {@inheritdoc} 38 */ 39 public function getRenderer(Rendering $rendering) 40 { 41 return new XSLTRenderer($this->getXSL($rendering)); 42 } 43 44 /** 45 * Generate an XSL stylesheet based on given rendering configuration 46 * 47 * @param Rendering $rendering 48 * @return string 49 */ 50 public function getXSL(Rendering $rendering) 51 { 52 $groupedTemplates = []; 53 $templates = $rendering->getTemplates(); 54 55 // Replace simple templates if there are at least 3 of them 56 TemplateHelper::replaceHomogeneousTemplates($templates, 3); 57 58 // Group tags with identical templates together 59 foreach ($templates as $tagName => $template) 60 { 61 $template = $this->normalizer->normalizeTemplate($template); 62 $groupedTemplates[$template][] = $tagName; 63 } 64 65 // Declare all the namespaces in use at the top 66 $xsl = '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"'; 67 68 // Append the namespace declarations to the stylesheet 69 $prefixes = $this->getPrefixes(array_keys($templates)); 70 if (!empty($prefixes)) 71 { 72 foreach ($prefixes as $prefix) 73 { 74 $xsl .= ' xmlns:' . $prefix . '="urn:s9e:TextFormatter:' . $prefix . '"'; 75 } 76 77 /** 78 * Exclude those prefixes to keep the HTML neat 79 * 80 * @link http://lenzconsulting.com/namespaces-in-xslt/#exclude-result-prefixes 81 */ 82 $xsl .= ' exclude-result-prefixes="' . implode(' ', $prefixes) . '"'; 83 } 84 85 // Start the stylesheet with the boilerplate stuff 86 $xsl .= '><xsl:output method="html" encoding="utf-8" indent="no"/><xsl:decimal-format decimal-separator="."/>'; 87 88 // Add stylesheet parameters 89 foreach ($rendering->getAllParameters() as $paramName => $paramValue) 90 { 91 $xsl .= $this->writeTag('xsl:param', ['name' => $paramName], htmlspecialchars($paramValue, ENT_NOQUOTES)); 92 } 93 94 // Add templates 95 foreach ($groupedTemplates as $template => $tagNames) 96 { 97 $xsl .= $this->writeTag('xsl:template', ['match' => implode('|', $tagNames)], $template); 98 } 99 100 $xsl .= '</xsl:stylesheet>'; 101 102 return $xsl; 103 } 104 105 /** 106 * Extract and return the sorted list of prefixes used in given list of tag names 107 * 108 * @param string[] $tagNames 109 * @return string[] 110 */ 111 protected function getPrefixes(array $tagNames) 112 { 113 $prefixes = []; 114 foreach ($tagNames as $tagName) 115 { 116 $pos = strpos($tagName, ':'); 117 if ($pos !== false) 118 { 119 $prefixes[substr($tagName, 0, $pos)] = 1; 120 } 121 } 122 $prefixes = array_keys($prefixes); 123 sort($prefixes); 124 125 return $prefixes; 126 } 127 128 /** 129 * Serialize given tag to XML 130 * 131 * @param string $tagName 132 * @param array $attributes 133 * @param string $content 134 * @return string 135 */ 136 protected function writeTag($tagName, array $attributes, $content) 137 { 138 $xml = '<' . $tagName; 139 foreach ($attributes as $attrName => $attrValue) 140 { 141 $xml .= ' ' . $attrName . '="' . htmlspecialchars($attrValue, ENT_COMPAT, 'utf-8') . '"'; 142 } 143 if ($content === '') 144 { 145 $xml .= '/>'; 146 } 147 else 148 { 149 $xml .= '>' . $content . '</' . $tagName . '>'; 150 } 151 152 return $xml; 153 } 154 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Nov 25 19:05:08 2024 | Cross-referenced by PHPXref 0.7.1 |