1<?php
2/**
3* DokuWiki Plugin syntaxhighlighter3 (Action Component)
4*
5* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6* @author  Daniel Lindgren <bd.dali@gmail.com>, based on syntaxhighlighter plugin by David Shin.
7*/
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14if (!defined('DOKU_LF')) {
15    define('DOKU_LF', "\n");
16}
17if (!defined('DOKU_TAB')) {
18    define('DOKU_TAB', "\t");
19}
20if (!defined('DOKU_PLUGIN')) {
21    define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
22}
23
24require_once DOKU_PLUGIN.'action.php';
25
26class action_plugin_syntaxhighlighter3_action extends DokuWiki_Action_Plugin
27{
28    public function register(Doku_Event_Handler &$controller)
29    {
30        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, '_hooksh');
31        $controller->register_hook('TPL_ACT_RENDER', 'AFTER', $this, '_hookjsprocessing');
32    }
33
34    public function _hooksh(Doku_Event &$event, $param)
35    {
36        // Add SyntaxHighlighter stylesheets. At least two, shCore.css and a theme.
37        $event->data['link'][] = array( 'rel'   => 'stylesheet',
38            'type'  => 'text/css',
39            'href'  => DOKU_BASE.'lib/plugins/syntaxhighlighter3/sxh3/pkg/styles/shCore.css',
40            );
41        $event->data['link'][] = array( 'rel'   => 'stylesheet',
42            'type'  => 'text/css',
43            'href'  => DOKU_BASE.'lib/plugins/syntaxhighlighter3/sxh3/pkg/styles/'.$this->getConf('theme'),
44            );
45
46        // Register core brush and autoloader.
47        $event->data["script"][] = array("type"   => "text/javascript",
48            "src"   => DOKU_BASE."lib/plugins/syntaxhighlighter3/sxh3/pkg/scripts/shCore.min.js",
49            "_data" => ""
50            );
51        $event->data["script"][] = array("type"   => "text/javascript",
52            "src"   => DOKU_BASE."lib/plugins/syntaxhighlighter3/sxh3/pkg/scripts/shAutoloader.js",
53            "_data" => ""
54            );
55        // Always load XML brush, needed for the option html-script.
56        $event->data["script"][] = array("type"   => "text/javascript",
57            "src"   => DOKU_BASE."lib/plugins/syntaxhighlighter3/sxh3/pkg/scripts/shBrushXml.js",
58            "_data" => ""
59            );
60    }
61
62    public function _hookjsprocessing(Doku_Event &$event, $param)
63    {
64        global $ID;
65        global $INFO;
66
67        //this ensures that code will be written only on base page
68        //not on other inlined wiki pages (e.g. when using monobook template)
69        if ($ID != $INFO["id"]) {
70            return;
71        }
72
73        ptln("");
74        ptln("<script type='text/javascript'>");
75        ptln("  SyntaxHighlighter.autoloader(");
76
77        // Get brushes.
78        $brushes = $this->getConf('brushes');
79        $brushes_split = explode(',', $brushes);
80        $lastbrush = array_pop($brushes_split);
81        foreach ($brushes_split as $brush) {
82            //ptln("<! DEBUG: ".$brush.">");
83            $brush_split = explode(' ', $brush);
84            $brush_script = array_pop($brush_split);
85            $brush_alias = strtolower(implode(' ', $brush_split));
86            ptln("    '".$brush_alias." ".DOKU_BASE."lib/plugins/syntaxhighlighter3/sxh3/pkg/scripts/".$brush_script."',");
87        }
88
89        // Last brush, no comma at the end of the line.
90        $brush_split = explode(' ', $lastbrush);
91        $brush_script = array_pop($brush_split);
92        $brush_alias = strtolower(implode(' ', $brush_split));
93        ptln("    '".$brush_alias." ".DOKU_BASE."lib/plugins/syntaxhighlighter3/sxh3/pkg/scripts/".$brush_script."'");
94        ptln("  );");
95        ptln("  SyntaxHighlighter.defaults['auto-links'] = " . ($this->getConf('auto-links') == 1 ? 'true' : 'false') . ";");
96        ptln("  SyntaxHighlighter.defaults['collapse'] = " . ($this->getConf('collapse') == 1 ? 'true' : 'false') . ";");
97        $firstLine = $this->getConf('first-line');
98        if ($firstLine > 0) {
99            ptln("  SyntaxHighlighter.defaults['first-line'] = " . $this->getConf('first-line') . ";");
100        }
101        ptln("  SyntaxHighlighter.defaults['gutter'] = " . ($this->getConf('gutter') == 1 ? 'true' : 'false') . ";");
102        ptln("  SyntaxHighlighter.defaults['html-script'] = " . ($this->getConf('html-script') == 1 ? 'true' : 'false') . ";");
103        ptln("  SyntaxHighlighter.defaults['smart-tabs'] = " . ($this->getConf('smart-tabs') == 1 ? 'true' : 'false') . ";");
104        $tabSize = $this->getConf('tab-size');
105        if ($tabSize > 0) {
106            ptln("  SyntaxHighlighter.defaults['tab-size'] = " . $this->getConf('tab-size') . ";");
107        }
108        ptln("  SyntaxHighlighter.defaults['toolbar'] = " . ($this->getConf('toolbar') == 1 ? 'true' : 'false') . ";");
109        ptln("  SyntaxHighlighter.all();");
110        ptln("</script>");
111    }
112}
113
114// vim:ts=4:sw=4:et:
115