* @license PHP License * @package wb * @subpackage Markup */ WBClass::load('WBString' , 'WBMarkup_Handler_Xml2Html'); /** * Markup Scanner Handler: XML2Tex * * Convert Wombat XML data to TeX * * @version 0.1.3 * @package wb * @subpackage Markup */ class WBMarkup_Handler_Xml2Tex extends WBMarkup_Handler_Xml2Html { /** * Search and Replace Special Characters * * @see initSepcialReplace() * @var array */ private $specialReplace = array(); /** * convert node of default namespace * * @param string $tag * @param array $node */ protected function convertDefault($tag, &$node) { $html = array( 'ns' => null, 'tag' => '', 'attributes' => array(), 'isEmpty' => false, 'cData' => array() ); $cData = implode('', $node['cData']); // strip any unknown tags if (!$this->tmpl || !$this->tmpl->exists('snippet_' . $tag)) { $html['cData'][] = $cData; $node = $html; return; } $this->tmpl->clearTemplate('snippet_' . $tag); $this->tmpl->addVars('snippet_' . $tag, $node['attributes'], 'ATT_'); $this->tmpl->addVar('snippet_' . $tag, 'CDATA', $cData); $html['cData'][] = $this->tmpl->getParsedTemplate('snippet_' . $tag); $node = $html; } public function getVfsFileList() { $con = $this->getConverter('wb', 'vfsfile'); return $con->getFileList(); } /** * handler fo cData * * Simply add cDate to document * * @param string $cData character data * @return bool usually true, false to stop the scanner */ public function onCharacterData($cData) { $this->initSepcialReplace(); //$cData = str_replace('\\', '\\textbackslash ', $cData); $cData = str_replace($this->specialReplace['search'], $this->specialReplace['replace'], $cData); if (strstr($cData, '®')) { if (0 == $this->depth) { $cData = str_replace('®', '{\\textsuperscript{®}}', $cData); } } return parent::onCharacterData($cData); } /** * handler for entities * * Add entities to document's cData * * @param string $entity the entity element, e.g. nbsp for   * @param boolean $isUnicode true = the element is a unicode string */ public function onEntityElement($entity, $isUnicode) { $search = array( 'gt', 'lt', 'amp', 'quot', 'apos' ); $replace = array( '>', '<', '&', "''", '\'' ); $cData = str_replace($search, $replace, $entity); $this->doc[$this->depth]['cData'][] = $cData; return true; } /** * called right after scan is complete * * @return bool usually true, false to stop the scanner */ public function onScanComplete() { return parent::onScanComplete(); } /** * Get patTemplate object * * @return patTemplate */ protected function createPatTemplate() { $obj = WBClass::create('patTemplate'); $obj->setDefaultAttribute('whitespace', 'keep'); $obj->setType('tex'); return $obj; } /** * Get File Name Suffix * * @return string */ protected function getPatTemplateSuffix() { return '.tex.tmpl'; } /** * Get Instance of Matching Converter * * Use parent implementation and set template mode to "TeX"; * * @param $ns * @param $tag * @return WBMarkup_Converter */ protected function getConverter($ns, $tag) { $con = parent::getConverter($ns, $tag); if (!$con) { return $con; } $con->setTmplType('tex'); return $con; } /** * replace URL string with propper node * * @see preg_replace_callback() * @param array $match * @return string */ public function replaceUrlString($match) { $text = $match[2] . $match[4]; $text = str_replace('%', '\\%', $text); $text = str_replace('_', '\\_', $text); return sprintf('\href{%s}{%s}', $match[0], $text); } /** * Initialize Search and Replace * * Prepare list of well known characters to be replaced. * @see str_replace() */ private function initSepcialReplace() { if (!empty($this->specialReplace)) { return; } $search = array( '_', '%', '®', '®', '(R)', ' ', '<', '>', '⇒', '≙', chr(11), '~', '≥', '&', '"', //'--', //'­' ); $replace = array( '\\_', '\\%', '®', '®', '®', ' ', '<', '>', '$\\Rightarrow$', '$\\stackrel{\\wedge}{=}$', //'$\\symbol{"2259}$', '', '$\\sim$', '$\\ge$', '\\&', "''", //'\-', //'\-' ); $this->addSpecialReplaceGreek($search, $replace); $this->specialReplace['search'] = $search; $this->specialReplace['replace'] = $replace; } /** * Add Greek Alphabet * * Add letters to search and replace lists * * @param array $search * @param array $replace */ private function addSpecialReplaceGreek(&$search, &$replace) { // lower case $search[] = 'α'; $replace[] = '$\alpha$'; $search[] = 'β'; $replace[] = '$\beta$'; $search[] = 'γ'; $replace[] = '$\gamma$'; $search[] = 'δ'; $replace[] = '$\delta$'; $search[] = 'ε'; $replace[] = '$\epsilon$'; $search[] = 'ζ'; $replace[] = '$\ceta$'; $search[] = 'η'; $replace[] = '$\eta$'; $search[] = 'θ'; $replace[] = '$\theta$'; $search[] = 'ι'; $replace[] = '$\iota$'; $search[] = 'κ'; $replace[] = '$\kappa$'; $search[] = 'λ'; $replace[] = '$\lambda$'; $search[] = 'µ'; // different kind of µ character $replace[] = '$\mu$'; $search[] = 'µ'; // see above $replace[] = '$\mu$'; $search[] = 'μ'; // more µ character $replace[] = '$\mu$'; $search[] = 'ν'; $replace[] = '$\nu$'; $search[] = 'ξ'; $replace[] = '$\xi'; $search[] = 'ο'; $replace[] = '$\omicron$'; $search[] = 'π'; $replace[] = '$\pi$'; $search[] = 'ρ'; $replace[] = '$\rho$'; $search[] = 'σ'; $replace[] = '$\sigma$'; $search[] = 'τ'; $replace[] = '$\tau$'; $search[] = 'υ'; $replace[] = '$\upsilon$'; $search[] = 'φ'; $replace[] = '$\phi$'; $search[] = 'χ'; $replace[] = '$\chi$'; $search[] = 'ψ'; $replace[] = '$\psi$'; $search[] = 'ω'; $replace[] = '$\omega$'; } }