HamlRenderer.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /* SVN FILE: $Id: HamlRenderer.php 93 2010-05-20 17:43:41Z chris.l.yates $ */
  3. /**
  4. * HamlRenderer class file.
  5. * @author Chris Yates <chris.l.yates@gmail.com>
  6. * @copyright Copyright (c) 2010 PBM Web Development
  7. * @license http://phamlp.googlecode.com/files/license.txt
  8. * @package PHamlP
  9. * @subpackage Haml.renderers
  10. */
  11. require_once('HamlCompressedRenderer.php');
  12. require_once('HamlCompactRenderer.php');
  13. require_once('HamlExpandedRenderer.php');
  14. require_once('HamlNestedRenderer.php');
  15. /**
  16. * HamlRenderer class.
  17. * Provides the most common version of each method. Child classs override
  18. * methods to provide style specific rendering.
  19. * @package PHamlP
  20. * @subpackage Haml.renderers
  21. */
  22. class HamlRenderer {
  23. /**#@+
  24. * Output Styles
  25. */
  26. const STYLE_COMPRESSED = 'compressed';
  27. const STYLE_COMPACT = 'compact';
  28. const STYLE_EXPANDED = 'expanded';
  29. const STYLE_NESTED = 'nested';
  30. /**#@-*/
  31. const INDENT = ' ';
  32. private $format;
  33. private $attrWrapper;
  34. private $minimizedAttributes;
  35. /**
  36. * Returns the renderer for the required render style.
  37. * @param string render style
  38. * @return HamlRenderer
  39. */
  40. static public function getRenderer($style, $options) {
  41. switch ($style) {
  42. case self::STYLE_COMPACT:
  43. return new HamlCompactRenderer($options);
  44. case self::STYLE_COMPRESSED:
  45. return new HamlCompressedRenderer($options);
  46. case self::STYLE_EXPANDED:
  47. return new HamlExpandedRenderer($options);
  48. case self::STYLE_NESTED:
  49. return new HamlNestedRenderer($options);
  50. } // switch
  51. }
  52. public function __construct($options) {
  53. foreach ($options as $name => $value) {
  54. $this->$name = $value;
  55. } // foreach
  56. }
  57. /**
  58. * Renders element attributes
  59. */
  60. private function renderAttributes($attributes) {
  61. $output = '';
  62. foreach ($attributes as $name => $value) {
  63. if (is_integer($name)) { // attribute function
  64. $output .= " $value";
  65. }
  66. elseif ($name == $value &&
  67. ($this->format === 'html4' || $this->format === 'html5')) {
  68. $output .= " $name";
  69. }
  70. else {
  71. $output .= " $name={$this->attrWrapper}$value{$this->attrWrapper}";
  72. }
  73. }
  74. return $output;
  75. }
  76. /**
  77. * Renders the opening tag of an element
  78. */
  79. public function renderOpeningTag($node) {
  80. $output = "<{$node->content}";
  81. $output .= $this->renderAttributes($node->attributes);
  82. $output .= ($node->isSelfClosing ? ' /' : '') . '>';
  83. return $output;
  84. }
  85. /**
  86. * Renders the closing tag of an element
  87. */
  88. public function renderClosingTag($node) {
  89. return ($node->isSelfClosing ? '' : "</{$node->content}>");
  90. }
  91. /**
  92. * Renders the opening of a comment
  93. */
  94. public function renderOpenComment($node) {
  95. return ($node->isConditional ? "\n\n" : '') . "<!--{$node->content}" . ($node->isConditional ? ">\n" : ' ');
  96. }
  97. /**
  98. * Renders the closing of a comment
  99. */
  100. public function renderCloseComment($node) {
  101. return ($node->isConditional ? "\n<![endif]" : ' ') . '-->' . ($node->isConditional ? "\n" : '');
  102. }
  103. /**
  104. * Renders the start of a code block
  105. */
  106. public function renderStartCodeBlock($node) {
  107. return $this->renderContent($node);
  108. }
  109. /**
  110. * Renders the end of a code block
  111. */
  112. public function renderEndCodeBlock($node) {
  113. return '<?php }' . (!empty($node->doWhile) ? " {$node->doWhile}" : '') . ' ?>';
  114. }
  115. /**
  116. * Renders content.
  117. * @param HamlNode the node being rendered
  118. * @return string the rendered content
  119. */
  120. public function renderContent($node) {
  121. return $node->content;
  122. }
  123. }