Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

92 lines
2.3KB

  1. <?php
  2. /**
  3. * Decorator/extender XSLT processor specifically for HTML documents.
  4. */
  5. class ConfigDoc_HTMLXSLTProcessor
  6. {
  7. /**
  8. * Instance of XSLTProcessor
  9. */
  10. protected $xsltProcessor;
  11. public function __construct($proc = false)
  12. {
  13. if ($proc === false) $proc = new XSLTProcessor();
  14. $this->xsltProcessor = $proc;
  15. }
  16. /**
  17. * @note Allows a string $xsl filename to be passed
  18. */
  19. public function importStylesheet($xsl)
  20. {
  21. if (is_string($xsl)) {
  22. $xsl_file = $xsl;
  23. $xsl = new DOMDocument();
  24. $xsl->load($xsl_file);
  25. }
  26. return $this->xsltProcessor->importStylesheet($xsl);
  27. }
  28. /**
  29. * Transforms an XML file into compatible XHTML based on the stylesheet
  30. * @param $xml XML DOM tree, or string filename
  31. * @return string HTML output
  32. * @todo Rename to transformToXHTML, as transformToHTML is misleading
  33. */
  34. public function transformToHTML($xml)
  35. {
  36. if (is_string($xml)) {
  37. $dom = new DOMDocument();
  38. $dom->load($xml);
  39. } else {
  40. $dom = $xml;
  41. }
  42. $out = $this->xsltProcessor->transformToXML($dom);
  43. // fudges for HTML backwards compatibility
  44. // assumes that document is XHTML
  45. $out = str_replace('/>', ' />', $out); // <br /> not <br/>
  46. $out = str_replace(' xmlns=""', '', $out); // rm unnecessary xmlns
  47. if (class_exists('Tidy')) {
  48. // cleanup output
  49. $config = array(
  50. 'indent' => true,
  51. 'output-xhtml' => true,
  52. 'wrap' => 80
  53. );
  54. $tidy = new Tidy;
  55. $tidy->parseString($out, $config, 'utf8');
  56. $tidy->cleanRepair();
  57. $out = (string) $tidy;
  58. }
  59. return $out;
  60. }
  61. /**
  62. * Bulk sets parameters for the XSL stylesheet
  63. * @param array $options Associative array of options to set
  64. */
  65. public function setParameters($options)
  66. {
  67. foreach ($options as $name => $value) {
  68. $this->xsltProcessor->setParameter('', $name, $value);
  69. }
  70. }
  71. /**
  72. * Forward any other calls to the XSLT processor
  73. */
  74. public function __call($name, $arguments)
  75. {
  76. call_user_func_array(array($this->xsltProcessor, $name), $arguments);
  77. }
  78. }
  79. // vim: et sw=4 sts=4