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