1<?php 2/** 3 * DokuWiki Syntax Plugin InlineJS Embedder 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Satoshi Sahara <sahara.satoshi@gmail.com> 7 * 8 * @see also: https://www.dokuwiki.org/devel:javascript 9 * 10 * Allow inline JavaScript in DokuWiki page. 11 * This plugin ensures that your script embedded inside of CDATA section. 12 * 13 * SYNTAX: 14 * <javascript> 15 * ... 16 * </javascript> 17 */ 18 19// must be run within Dokuwiki 20if (!defined('DOKU_INC')) die(); 21 22class syntax_plugin_inlinejs_embedder extends DokuWiki_Syntax_Plugin 23{ 24 public function getType() 25 { // Syntax Type 26 return 'protected'; 27 } 28 29 public function getPType() 30 { // Paragraph Type 31 return 'block'; 32 } 33 34 /** 35 * Connect pattern to lexer 36 */ 37 protected $mode, $pattern; 38 39 public function preConnect() 40 { 41 // drop 'syntax_' from class name 42 $this->mode = substr(get_class($this), 7); 43 44 // syntax pattern 45 $this->pattern[1] = '<javascript>(?=.*?</javascript>)'; 46 $this->pattern[4] = '</javascript>'; 47 } 48 49 public function connectTo($mode) 50 { 51 $this->Lexer->addEntryPattern($this->pattern[1], $mode, $this->mode); 52 } 53 54 public function postConnect() 55 { 56 $this->Lexer->addExitPattern($this->pattern[4], $this->mode); 57 } 58 59 public function getSort() 60 { // sort number used to determine priority of this mode 61 return 305; 62 } 63 64 /** 65 * Plugin features 66 */ 67 protected $code = null; 68 69 70 /** 71 * handle the match 72 */ 73 public function handle($match, $state, $pos, Doku_Handler $handler) 74 { 75 global $conf; 76 77 if ($this->getConf('follow_htmlok') && !$conf['htmlok']) { 78 msg($this->getPluginName().': '.$this->getPluginComponent().' is disabled.',-1); 79 return false; 80 } 81 82 switch ($state) { 83 case DOKU_LEXER_ENTER: 84 return false; 85 86 case DOKU_LEXER_UNMATCHED: 87 $this->code = $match; 88 return false; 89 90 case DOKU_LEXER_EXIT: 91 $data = array($state, $this->code); 92 $this->code = null; 93 94 if ($this->getConf('follow_htmlok') && !$conf['htmlok']) { 95 $msg = $this->getPluginComponent().' is disabled.'; 96 msg($this->getPluginName().': '.$msg, -1); 97 return false; 98 } 99 return $data; 100 } 101 return false; 102 } 103 104 /** 105 * Create output 106 */ 107 public function render($format, Doku_Renderer $renderer, $data) 108 { 109 list($state, $code) = $data; 110 if ($format != 'xhtml') return false; 111 112 $html = '<script type="text/javascript">'.DOKU_LF.'/*<![CDATA[*/'; 113 $html.= $code; // raw write 114 $html.= '/*!]]>*/'.DOKU_LF.'</script>'.DOKU_LF; 115 $renderer->doc .= $html; 116 117 return true; 118 } 119} 120