* @copyright 2009 Kevin van Zonneveld (http://kevin.vanzonneveld.net) * @license http://www.opensource.org/licenses/bsd-license.php New BSD Licence * @link http://kevin.vanzonneveld.net/code/ */ Class KvzHtml { /** * Can track TOC if you use H1, H2 tags * * @var array */ protected $_toc = array(); protected $_tocLevelPrev = false; protected $_tocIdPrev = false; protected $_idCnt = array(); protected $_ids = array(); protected $_buffer = array(); protected $_cache = array(); protected $_options = array(); public $loremIpsum = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum'; public function __construct ($options = array()) { $this->_options = $options; if (!isset($this->_options['xhtml'])) $this->_options['xhtml'] = true; if (!isset($this->_options['track_toc'])) $this->_options['track_toc'] = false; if (!isset($this->_options['link_toc'])) $this->_options['link_toc'] = true; if (!isset($this->_options['indentation'])) $this->_options['indentation'] = 4; if (!isset($this->_options['newlines'])) $this->_options['newlines'] = true; if (!isset($this->_options['buffer'])) $this->_options['buffer'] = false; if (!isset($this->_options['xml'])) $this->_options['xml'] = false; if (!isset($this->_options['tidy'])) $this->_options['tidy'] = false; // Not recommended cause you cannot nest tags with echo: if (!isset($this->_options['echo'])) $this->_options['echo'] = false; } public function setOption ($key, $val) { if (!array_key_exists($key, $this->_options)) { trigger_error(sprintf( '%s is not a valid options', $key ), E_USER_ERROR); return false; } $this->_options[$key] = $val; } protected function _trackToc ($tag, $body) { if (preg_match('/h(\d)/i', $tag, $m)) { $tocId = count($this->_toc); $tocLevel = $m[1]; $tocLine = ''; $suffix = ''; $prefix = ''; if ($this->_tocIdPrev === false) { // root element $prefix .= $this->_tag('a', '', array( 'name' => 'toc_root', '__trimbody' => true, '__echo' => false, )); } if ($this->_tocIdPrev === false || $tocLevel > $this->_tocLevelPrev) { // Indent $prefix .= str_repeat( str_repeat(' ', $tocLevel-1) . '
'."\n"; foreach($args as $arg) { print_r($arg); } echo ''."\n"; } public function prd () { $args = func_get_args(); call_user_func_array(array($this, 'pr'), $args); die(); } public function reset () { $this->_idCnt = array(); $this->_ids = array(); $this->_buffer = array(); $this->_toc = array(); $this->_tocLevelPrev = false; $this->_tocIdPrev = false; } protected function _createId ($tag) { if (!isset($this->_idCnt[$tag])) { $this->_idCnt[$tag] = 0; } $this->_idCnt[$tag]++; $id = $tag . '-' . $this->_idCnt[$tag]; $this->_ids[] = $id; return $id; } public function getLastId () { return end($this->_ids); } protected function _args ($args = array()) { $argumentsT = ''; if (is_array($args) && count($args)) { foreach ($args as $k => $v) { if (substr($k, 0, 2) === '__') { continue; } if (is_array($v)) { if ($k === 'style') { $v2 = ''; foreach ($v as $stylek => $stylev) { $v2 .= sprintf(' %s: %s;', $stylek, $stylev); } $v = trim($v2); } else { $v = implode(' ', $v); } } if (is_numeric($k)) { $argumentsT .= sprintf(' %s', $v); } else { if (!empty($v)) { $argumentsT .= sprintf(' %s=\'%s\'', $k, trim($v)); } } } } else { $argumentsT = ''; } return str_replace('%', '%%', $argumentsT); } protected function _tag ($tag, $body = true, $tagOptions = array()) { if (is_string($tagOptions)) { $tagOptions = array('class' => $tagOptions); } if (!is_array($tagOptions)) $tagOptions = array(); if (is_array($body)) { $body = implode("\n", $body); } // Cast class-options if not present in $tagOptions foreach($this->_options as $key => $val) { if (!array_key_exists('__' . $key, $tagOptions)) { $tagOptions['__' . $key] = $val; } } // Use a default for every Tag specific option that // remains if (!array_key_exists('__newlineAfterOpeningTag', $tagOptions)) $tagOptions['__newlineAfterOpeningTag'] = true; if (!array_key_exists('__newlineAfterClosingTag', $tagOptions)) $tagOptions['__newlineAfterClosingTag'] = true; if (!array_key_exists('__trimbody', $tagOptions)) $tagOptions['__trimbody'] = false; if (!array_key_exists('__gt', $tagOptions)) $tagOptions['__gt'] = '>'; if (!array_key_exists('__lt', $tagOptions)) $tagOptions['__lt'] = '<'; if (!array_key_exists('__skip', $tagOptions)) $tagOptions['__skip'] = false; if (!array_key_exists('__onlybody', $tagOptions)) $tagOptions['__onlybody'] = false; if (!array_key_exists('__cdata', $tagOptions)) $tagOptions['__cdata'] = false; if (!array_key_exists('__closetag', $tagOptions)) $tagOptions['__closetag'] = null; if (!array_key_exists('__opentag', $tagOptions)) $tagOptions['__opentag'] = null; if (!array_key_exists('__sclose', $tagOptions)) $tagOptions['__sclose'] = null; // TOC? if ($this->_options['track_toc']) { if (!is_bool($body) && !is_null($body)) { $body .= $this->_trackToc($tag, $body); } } // Indent body $bodyIndented = $this->indent($body, $tagOptions['__indentation'])."\n"; // Other defaults for XML if ($tagOptions['__xml']) { if (!$tagOptions['__trimbody'] && strpos($body, '<') === false) { $bodyIndented = trim($bodyIndented); if ($body !== true) { $tagOptions['__newlineAfterOpeningTag'] = false; } } } if ($tagOptions['__trimbody']) { $bodyIndented = trim($bodyIndented); $tagOptions['__newlineAfterOpeningTag'] = false; } if (isset($tagOptions['id']) && $tagOptions['id'] === true) { // auto id $tagOptions['id'] = $this->_createId($tag); } if ($tagOptions['__skip']) { return ''; } if ($tagOptions['__cdata'] && !is_bool($body) && !is_null($body)) { $bodyIndented = ''; } if ($tagOptions['__onlybody']) { return $bodyIndented; } $tagArguments = $this->_args($tagOptions); if ($tagOptions['__opentag'] === null) { $tagOptions['__opentag'] = $tagOptions['__lt'] . '%s' . $tagArguments . $tagOptions['__gt'] . $this->_linesep($tagOptions['__newlineAfterOpeningTag']); } if ($tagOptions['__closetag'] === null) { $tagOptions['__closetag'] = $tagOptions['__lt'] . '/%s' . $tagOptions['__gt'] . $this->_linesep($tagOptions['__newlineAfterClosingTag']); } if ($tagOptions['__sclose'] === null) { $tagOptions['__sclose'] = $tagOptions['__lt'] . '%s' . $tagArguments . ' ' . ($tagOptions['__xhtml'] ? '/' : '') . $tagOptions['__gt'] . $this->_linesep($tagOptions['__newlineAfterClosingTag'] || $tagOptions['__newlineAfterOpeningTag']); } $tagOptions['__opentag'] = sprintf($tagOptions['__opentag'], $tag); $tagOptions['__closetag'] = sprintf($tagOptions['__closetag'], $tag); $tagOptions['__sclose'] = sprintf($tagOptions['__sclose'], $tag); if (true === $body) { // Opening tag $result = $tagOptions['__opentag']; } else if (false === $body) { // Close tag $result = $tagOptions['__closetag']; } else if (null === $body) { // Self-closing tag $result = $tagOptions['__sclose']; } else { // Full tag $result = $tagOptions['__opentag'] . $bodyIndented . $tagOptions['__closetag']; } return $this->out($result, array( 'echo' => $tagOptions['__echo'], 'buffer' => $tagOptions['__buffer'], )); } /** * Taken from CakePHP's Set Class * array_merge & $this->_merge is just never what you need * * @param