1<?php 2/** 3 * DokuWiki Plugin Code Prettifier 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Satoshi Sahara <sahara.satoshi@gmail.com> 7 */ 8class action_plugin_codeprettify extends DokuWiki_Action_Plugin 9{ 10 // register hook 11 public function register(Doku_Event_Handler $controller) 12 { 13 $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'load_code_prettify'); 14 } 15 16 17 /** 18 * register google code prettifier script and css 19 */ 20 public function load_code_prettify(Doku_Event $event, $param) 21 { 22 // Base URL for prettify.js and optional language handler scripts 23 // ex: https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/ 24 if ($this->getConf('url_prettify_handlers')) { 25 $urlPrettifyHandlers = $this->getConf('url_prettify_handlers'); 26 } else { 27 $urlPrettifyHandlers = 28 DOKU_BASE.'lib/plugins/codeprettify/code-prettify/src/'; 29 } 30 31 // Base URL for color theme for code-prettify (css) 32 // ex: https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/skins/ 33 if ($this->getConf('url_prettify_skins')) { 34 $urlPrettifySkins = $this->getConf('url_prettify_skins'); 35 } else { 36 $urlPrettifySkins = 37 DOKU_BASE.'lib/plugins/codeprettify/code-prettify/styles/'; 38 } 39 40 // load prettify.js and optional language handler scripts 41 $handlers = 'prettify'; 42 if ($this->getConf('lang_handlers')) { 43 $handlers .= ',' . trim($this->getConf('lang_handlers'), ','); 44 $handlers = str_replace(' ', '', $handlers); 45 $handlers = str_replace(',',',lang-', $handlers); 46 } 47 $scripts = explode(',', $handlers); 48 49 foreach ($scripts as $script) { 50 $event->data['script'][] = [ 51 'type' => 'text/javascript', 52 'charset' => 'utf-8', 53 'src' => $urlPrettifyHandlers. $script.'.js', 54 'defer' => 'defer', 55 '_data' => '', 56 ]; 57 } 58 59 // load convenient language handler which enables prettyprinting 60 // as plain text, ie. not any kind of language code. 61 // use <Code:none>..</Code> to show code as plain text. 62 $event->data['script'][] = [ 63 'type' => 'text/javascript', 64 'charset' => 'utf-8', 65 'src' => DOKU_BASE.'lib/plugins/codeprettify/code-prettify/src/lang-none.js', 66 'defer' => 'defer', 67 '_data' => '', 68 ]; 69 70 // load color theme for code-prettify (css file) 71 if ($this->getConf('skin')) { 72 $skin = $urlPrettifySkins . $this->getConf('skin'); 73 } else { 74 $skin = $urlPrettifyHandlers .'prettify.css'; 75 } 76 $event->data['link'][] = array ( 77 'rel' => 'stylesheet', 78 'type' => 'text/css', 79 'href' => $skin, 80 ); 81 } 82 83} 84