* @license PHP License * @package wb * @subpackage Markup */ /** * Tag converter * * Convert node to node * * @version 0.1.3 * @package wb * @subpackage Markup */ abstract class WBMarkup_Converter extends WBStdClass { /** * associative array to configure converter * @param array */ protected $config = array(); /** * document's md5 sum * @var string */ protected $md5 = 'x'; /** * template engine * @var patTemplate */ protected $tmpl; /** * template dir format */ protected $tmplDirPrototype = ''; /** * template folder * @var string */ protected $tmplDir = ''; /** * constructor * * start up end set temüplate dir */ public function __construct() { $clazz = explode('_', get_class($this)); array_shift($clazz); array_shift($clazz); $this->tmplDirPrototype = sprintf('Wxml/converter/%s', strtolower(implode('/', $clazz))); $this->tmplDir = $this->tmplDirPrototype; $configFile = sprintf('wxml/converter/%s', strtolower(implode('/', $clazz))); $config = WBClass::create('WBConfig'); if ($config->load( $configFile, true )) { $this->config = array_merge($this->config, $config->get()); } } /** * set template folder * * load template from dir... * * @var string $dir */ public function setTmplDir( $dir = null ) { $this->tmplDir = $this->tmplDirPrototype; if (!empty($dir)) { $this->tmplDir .= '/' . trim($dir, '/'); } } /** * setup convert when started * * Inform convert whenever created or new scan was started * * @param WBMarkup_Handler $handler * @param string $content */ public function preset($handler, &$content) { $this->md5 = md5($content); } /** * reset * * @return bool true on success */ public function reset() { return true; } /** * transform Xinha Tags to XML * * @param array $node to be modified document node * @return bool true on success */ abstract public function toXml(&$node); /** * transform Xml to Xinha (html) tags * * @param array $node to be modified document node * @return bool true on success */ abstract public function toXinha(&$node); /** * transform XML to Html * * @param array $node to be modified document node * @return bool true on success */ abstract public function toHtml(&$node); /** * create tag attribute style * * implode style array to string * * @param array $style * @return string */ protected function implodeStyle($style) { $tmp = array(); foreach ($style as $k => $v) { $tmp[] = $k . ':' . $v; } return implode(';', $tmp) . ';'; } /** * extract all stype attribute to array * * * @param array $node * @return array */ protected function explodeStyle($node) { if (!isset($node['attributes']['style'])) { return array(); } $tmp = explode(';', trim($node['attributes']['style'])); $tmp = array_map('trim', $tmp); $style = array(); foreach ($tmp as $t) { $t = trim($t); if (empty($t)) { continue; } $s = array_map('trim', explode(':', $t)); if (count($s == 2)) { // remove "px" from style if (substr($s[1], -2) == 'px') { $s[1] = substr($s[1], 0, -2); } $style[$s[0]] = $s[1]; } } return $style; } /** * load templates from file * * @param string $tmpl * @return bool true on success */ protected function loadTemplates($tmpl, $local = true) { if (!$this->tmpl) { $this->tmpl = WBClass::create('patTemplate'); $this->tmpl->addGlobalVars($this->config, 'config_'); } if ($local) { $tmpl = $this->tmplDir . '/' . $tmpl; } // false on error patErrorManager::pushExpect(6000); $ret = $this->tmpl->readTemplatesFromInput($tmpl . '.tmpl'); if (patErrorManager::isError($ret)) { return false; } patErrorManager::popExpect(); return true; } /** * convert template to node * * Add vars and parse temlate and replace node's CData * * @param array $node * @param array $data */ protected function template2HtmlNode(&$node, $data) { $html = array( 'ns' => null, 'tag' => '', 'attributes' => array(), 'isEmpty' => false, 'cData' => array() ); $this->tmpl->addGlobalVars($data); $html['cData'][] = $this->tmpl->getParsedTemplate('snippet'); $this->tmpl->clearAllTemplates(); $node = $html; } } ?>