1<?php 2if(!defined('DOKU_INC')) die(); 3if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 4require_once(DOKU_PLUGIN.'syntax.php'); 5class syntax_plugin_chem extends DokuWiki_Syntax_Plugin { 6 function getType(){ return 'formatting'; } 7 function getAllowedTypes() { return array('formatting', 'substition', 'disabled'); } 8 function getSort(){ return 158; } 9 function connectTo($mode) { $this->Lexer->addEntryPattern('<chem>(?=.*?</chem>)',$mode,'plugin_chem'); } 10 function postConnect() { $this->Lexer->addExitPattern('</chem>','plugin_chem'); } 11 12 function handle($match, $state, $pos, Doku_Handler $handler){ 13 switch ($state) { 14 case DOKU_LEXER_ENTER :return array($state, ''); 15 case DOKU_LEXER_UNMATCHED :return array($state, $match); 16 case DOKU_LEXER_EXIT :return array($state, ''); 17 } 18 return array(); 19 } 20 21 function render($mode, Doku_Renderer $renderer, $data) { 22 if($mode == 'xhtml' || $mode=='odt'){ 23 list($state, $match) = $data; 24 switch ($state) { 25 case DOKU_LEXER_ENTER:break; 26 case DOKU_LEXER_UNMATCHED: 27 if($mode=='xhtml'){ 28 // xhtml 29 $renderer->doc .= $this->getChemFormat($match); 30 }else{ 31 // Open document format 32 if (!class_exists('ODTDocument')) { 33 $this->renderOdtChemFormat($renderer, $match); 34 } else { 35 $c = $this->getChemFormat($match); 36 $renderer->generateSpansfromHTMLCode($c); 37 } 38 } 39 break; 40 case DOKU_LEXER_EXIT:break; 41 } 42 return true; 43 } 44 return false; 45 } 46 47 function getChemFormat($raw){ 48 $pattern = array("/([A-Za-z\]\)]+)(0)/", "/[\|]?([0-9]*)[\^]/", "/([^ ][\]\)]?)[\|]?(([\-\+][0-9]*)|([0-9]*[\-\+]))/", "/([A-Z]|[a-z]|\)|\])([1-9][0-9]*)/"); 49 $replace = array("\${1}<sup>\${2}</sup>", "<sup>\${1}</sup>", "\${1}<sup>\${2}</sup>","\${1}<sub>\${2}</sub>"); 50 return preg_replace($pattern,$replace,$raw); 51 } 52 function getOdtChemFormat($raw){ 53 $c = $this->getChemFormat($raw); 54 $pattern = array("/<sup>([^<]+)<\/sup>/","/<sub>([^<]+)<\/sub>/"); 55 $replace = array("<text:span text:style-name=\"sup\">\${1}</text:span>", 56 "<text:span text:style-name=\"sub\">\${1}</text:span>"); 57 return preg_replace($pattern,$replace,$c); 58 } 59 } 60?> 61