123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- /* SVN FILE: $Id: HamlRenderer.php 93 2010-05-20 17:43:41Z chris.l.yates $ */
- /**
- * HamlRenderer class file.
- * @author Chris Yates <chris.l.yates@gmail.com>
- * @copyright Copyright (c) 2010 PBM Web Development
- * @license http://phamlp.googlecode.com/files/license.txt
- * @package PHamlP
- * @subpackage Haml.renderers
- */
- require_once('HamlCompressedRenderer.php');
- require_once('HamlCompactRenderer.php');
- require_once('HamlExpandedRenderer.php');
- require_once('HamlNestedRenderer.php');
- /**
- * HamlRenderer class.
- * Provides the most common version of each method. Child classs override
- * methods to provide style specific rendering.
- * @package PHamlP
- * @subpackage Haml.renderers
- */
- class HamlRenderer {
- /**#@+
- * Output Styles
- */
- const STYLE_COMPRESSED = 'compressed';
- const STYLE_COMPACT = 'compact';
- const STYLE_EXPANDED = 'expanded';
- const STYLE_NESTED = 'nested';
- /**#@-*/
- const INDENT = ' ';
- private $format;
- private $attrWrapper;
- private $minimizedAttributes;
- /**
- * Returns the renderer for the required render style.
- * @param string render style
- * @return HamlRenderer
- */
- static public function getRenderer($style, $options) {
- switch ($style) {
- case self::STYLE_COMPACT:
- return new HamlCompactRenderer($options);
- case self::STYLE_COMPRESSED:
- return new HamlCompressedRenderer($options);
- case self::STYLE_EXPANDED:
- return new HamlExpandedRenderer($options);
- case self::STYLE_NESTED:
- return new HamlNestedRenderer($options);
- } // switch
- }
- public function __construct($options) {
- foreach ($options as $name => $value) {
- $this->$name = $value;
- } // foreach
- }
- /**
- * Renders element attributes
- */
- private function renderAttributes($attributes) {
- $output = '';
- foreach ($attributes as $name => $value) {
- if (is_integer($name)) { // attribute function
- $output .= " $value";
- }
- elseif ($name == $value &&
- ($this->format === 'html4' || $this->format === 'html5')) {
- $output .= " $name";
- }
- else {
- $output .= " $name={$this->attrWrapper}$value{$this->attrWrapper}";
- }
- }
- return $output;
- }
- /**
- * Renders the opening tag of an element
- */
- public function renderOpeningTag($node) {
- $output = "<{$node->content}";
- $output .= $this->renderAttributes($node->attributes);
- $output .= ($node->isSelfClosing ? ' /' : '') . '>';
- return $output;
- }
- /**
- * Renders the closing tag of an element
- */
- public function renderClosingTag($node) {
- return ($node->isSelfClosing ? '' : "</{$node->content}>");
- }
- /**
- * Renders the opening of a comment
- */
- public function renderOpenComment($node) {
- return ($node->isConditional ? "\n\n" : '') . "<!--{$node->content}" . ($node->isConditional ? ">\n" : ' ');
- }
- /**
- * Renders the closing of a comment
- */
- public function renderCloseComment($node) {
- return ($node->isConditional ? "\n<![endif]" : ' ') . '-->' . ($node->isConditional ? "\n" : '');
- }
- /**
- * Renders the start of a code block
- */
- public function renderStartCodeBlock($node) {
- return $this->renderContent($node);
- }
- /**
- * Renders the end of a code block
- */
- public function renderEndCodeBlock($node) {
- return '<?php }' . (!empty($node->doWhile) ? " {$node->doWhile}" : '') . ' ?>';
- }
- /**
- * Renders content.
- * @param HamlNode the node being rendered
- * @return string the rendered content
- */
- public function renderContent($node) {
- return $node->content;
- }
- }
|