Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

XSLT.php

Zuletzt modifiziert: 02.04.2025, 15:04 - Dateigröße: 3.74 KiB


001  <?php
002   
003  /**
004  * @package   s9e\TextFormatter
005  * @copyright Copyright (c) 2010-2022 The s9e authors
006  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
007  */
008  namespace s9e\TextFormatter\Configurator\RendererGenerators;
009   
010  use s9e\TextFormatter\Configurator\Helpers\TemplateHelper;
011  use s9e\TextFormatter\Configurator\RendererGenerator;
012  use s9e\TextFormatter\Configurator\Rendering;
013  use s9e\TextFormatter\Configurator\TemplateNormalizer;
014  use s9e\TextFormatter\Renderers\XSLT as XSLTRenderer;
015   
016  class XSLT implements RendererGenerator
017  {
018      /**
019      * @var TemplateNormalizer
020      */
021      public $normalizer;
022   
023      /**
024      * Constructor
025      */
026      public function __construct()
027      {
028          $this->normalizer = new TemplateNormalizer([
029              'MergeConsecutiveCopyOf',
030              'MergeIdenticalConditionalBranches',
031              'OptimizeNestedConditionals',
032              'RemoveLivePreviewAttributes'
033          ]);
034      }
035   
036      /**
037      * {@inheritdoc}
038      */
039      public function getRenderer(Rendering $rendering)
040      {
041          return new XSLTRenderer($this->getXSL($rendering));
042      }
043   
044      /**
045      * Generate an XSL stylesheet based on given rendering configuration
046      *
047      * @param  Rendering $rendering
048      * @return string
049      */
050      public function getXSL(Rendering $rendering)
051      {
052          $groupedTemplates = [];
053          $templates        = $rendering->getTemplates();
054   
055          // Replace simple templates if there are at least 3 of them
056          TemplateHelper::replaceHomogeneousTemplates($templates, 3);
057   
058          // Group tags with identical templates together
059          foreach ($templates as $tagName => $template)
060          {
061              $template = $this->normalizer->normalizeTemplate($template);
062              $groupedTemplates[$template][] = $tagName;
063          }
064   
065          // Declare all the namespaces in use at the top
066          $xsl = '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"';
067   
068          // Append the namespace declarations to the stylesheet
069          $prefixes = $this->getPrefixes(array_keys($templates));
070          if (!empty($prefixes))
071          {
072              foreach ($prefixes as $prefix)
073              {
074                  $xsl .= ' xmlns:' . $prefix . '="urn:s9e:TextFormatter:' . $prefix . '"';
075              }
076   
077              /**
078              * Exclude those prefixes to keep the HTML neat
079              *
080              * @link http://lenzconsulting.com/namespaces-in-xslt/#exclude-result-prefixes
081              */
082              $xsl .= ' exclude-result-prefixes="' . implode(' ', $prefixes) . '"';
083          }
084   
085          // Start the stylesheet with the boilerplate stuff
086          $xsl .= '><xsl:output method="html" encoding="utf-8" indent="no"/><xsl:decimal-format decimal-separator="."/>';
087   
088          // Add stylesheet parameters
089          foreach ($rendering->getAllParameters() as $paramName => $paramValue)
090          {
091              $xsl .= $this->writeTag('xsl:param', ['name' => $paramName], htmlspecialchars($paramValue, ENT_NOQUOTES));
092          }
093   
094          // Add templates
095          foreach ($groupedTemplates as $template => $tagNames)
096          {
097              $xsl .= $this->writeTag('xsl:template', ['match' => implode('|', $tagNames)], $template);
098          }
099   
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  }