Ejemplo n.º 1
0
 /**
  * Generate an XSL stylesheet based on given rendering configuration
  *
  * @param  Rendering $rendering
  * @return string
  */
 public function getXSL(Rendering $rendering)
 {
     $groupedTemplates = [];
     $prefixes = [];
     $templates = $rendering->getTemplates();
     // Replace simple templates if there are at least 3 of them
     TemplateHelper::replaceHomogeneousTemplates($templates, 3);
     // Group tags with identical templates together
     foreach ($templates as $tagName => $template) {
         $template = $this->optimizer->optimizeTemplate($template);
         $groupedTemplates[$template][] = $tagName;
         // Record the tag's prefix if applicable
         $pos = strpos($tagName, ':');
         if ($pos !== false) {
             $prefixes[substr($tagName, 0, $pos)] = 1;
         }
     }
     // Declare all the namespaces in use at the top
     $xsl = '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"';
     // Append the namespace declarations to the stylesheet
     $prefixes = array_keys($prefixes);
     sort($prefixes);
     foreach ($prefixes as $prefix) {
         $xsl .= ' xmlns:' . $prefix . '="urn:s9e:TextFormatter:' . $prefix . '"';
     }
     /**
      * Exclude those prefixes to keep the HTML neat
      *
      * @link http://lenzconsulting.com/namespaces-in-xslt/#exclude-result-prefixes
      */
     if (!empty($prefixes)) {
         $xsl .= ' exclude-result-prefixes="' . implode(' ', $prefixes) . '"';
     }
     // Start the stylesheet with the boilerplate stuff
     $xsl .= '><xsl:output method="html" encoding="utf-8" indent="no"';
     $xsl .= '/>';
     // Add stylesheet parameters
     foreach ($rendering->getAllParameters() as $paramName => $paramValue) {
         $xsl .= '<xsl:param name="' . htmlspecialchars($paramName) . '"';
         if ($paramValue === '') {
             $xsl .= '/>';
         } else {
             $xsl .= '>' . htmlspecialchars($paramValue) . '</xsl:param>';
         }
     }
     // Add templates
     foreach ($groupedTemplates as $template => $tagNames) {
         // Open the template element
         $xsl .= '<xsl:template match="' . implode('|', $tagNames) . '"';
         // Make it a self-closing element if the template is empty
         if ($template === '') {
             $xsl .= '/>';
         } else {
             $xsl .= '>' . $template . '</xsl:template>';
         }
     }
     $xsl .= '</xsl:stylesheet>';
     return $xsl;
 }
Ejemplo n.º 2
0
 public function getXSL(Rendering $rendering)
 {
     $groupedTemplates = array();
     $prefixes = array();
     $templates = $rendering->getTemplates();
     TemplateHelper::replaceHomogeneousTemplates($templates, 3);
     foreach ($templates as $tagName => $template) {
         $template = $this->optimizer->optimizeTemplate($template);
         $groupedTemplates[$template][] = $tagName;
         $pos = \strpos($tagName, ':');
         if ($pos !== \false) {
             $prefixes[\substr($tagName, 0, $pos)] = 1;
         }
     }
     $xsl = '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"';
     $prefixes = \array_keys($prefixes);
     \sort($prefixes);
     foreach ($prefixes as $prefix) {
         $xsl .= ' xmlns:' . $prefix . '="urn:s9e:TextFormatter:' . $prefix . '"';
     }
     if (!empty($prefixes)) {
         $xsl .= ' exclude-result-prefixes="' . \implode(' ', $prefixes) . '"';
     }
     $xsl .= '><xsl:output method="html" encoding="utf-8" indent="no"';
     $xsl .= '/>';
     foreach ($rendering->getAllParameters() as $paramName => $paramValue) {
         $xsl .= '<xsl:param name="' . \htmlspecialchars($paramName) . '"';
         if ($paramValue === '') {
             $xsl .= '/>';
         } else {
             $xsl .= '>' . \htmlspecialchars($paramValue) . '</xsl:param>';
         }
     }
     foreach ($groupedTemplates as $template => $tagNames) {
         $xsl .= '<xsl:template match="' . \implode('|', $tagNames) . '"';
         if ($template === '') {
             $xsl .= '/>';
         } else {
             $xsl .= '>' . $template . '</xsl:template>';
         }
     }
     $xsl .= '</xsl:stylesheet>';
     return $xsl;
 }
Ejemplo n.º 3
0
 /**
  * @testdox replaceHomogeneousTemplates() tests
  * @dataProvider getReplaceHomogeneousTemplatesTests
  */
 public function testReplaceHomogeneousTemplates($templates, $expected)
 {
     TemplateHelper::replaceHomogeneousTemplates($templates);
     $this->assertSame($expected, $templates);
 }