1<?php
2/**
3 * DokuWiki Plugin duoshuo (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Matt <caijiamx@gmail.com>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12class syntax_plugin_duoshuo extends DokuWiki_Syntax_Plugin {
13    const DUOSHUO_SYNTAX = "~~DUOSHUO~~";
14    const NODUOSHUO_SYNTAX = "~~NODUOSHUO~~";
15    /**
16     * @return string Syntax mode type
17     */
18    public function getType() {
19        return 'substition';
20    }
21    /**
22     * @return string Paragraph type
23     */
24    public function getPType() {
25        return 'block';
26    }
27    /**
28     * @return int Sort order - Low numbers go before high numbers
29     */
30    public function getSort() {
31        return 160;
32    }
33
34    /**
35     * Connect lookup pattern to lexer.
36     *
37     * @param string $mode Parser mode
38     */
39    public function connectTo($mode) {
40        $this->Lexer->addSpecialPattern(self::DUOSHUO_SYNTAX,$mode,'plugin_duoshuo');
41        $this->Lexer->addSpecialPattern(self::NODUOSHUO_SYNTAX,$mode,'plugin_duoshuo');
42    }
43
44    /**
45     * Handle matches of the duoshuo syntax
46     *
47     * @param string $match The match of the syntax
48     * @param int    $state The state of the handler
49     * @param int    $pos The position in the document
50     * @param Doku_Handler    $handler The handler
51     * @return array Data for the renderer
52     */
53    public function handle($match, $state, $pos, &$handler){
54        $match = preg_replace( '/~~/' , '' , $match );
55        return array(strtolower($match));;
56    }
57
58    /**
59     * Render xhtml output or metadata
60     *
61     * @param string         $mode      Renderer mode (supported modes: xhtml)
62     * @param Doku_Renderer  $renderer  The renderer
63     * @param array          $data      The data from the handler() function
64     * @return bool If rendering was successful.
65     */
66    public function render($mode, &$renderer, $data) {
67        if($mode != 'xhtml') return false;
68        if($data[0] == "duoshuo"){
69            $renderer->doc .= $this->getDuoshuoScript();
70        }
71        return true;
72    }
73
74    public function getDuoshuoScript(){
75        $short_name = $this->getConf('shortname');
76        $wiki_id  = getID();
77        $wiki_title = tpl_pagetitle($wiki_id , true);
78        $host = $_SERVER['HTTPS']?"https":"http";
79        $host = $host . "://" . $_SERVER['SERVER_NAME'];
80        $wiki_url = $host . wl($wiki_id);
81        $doc = '
82        <!-- 多说评论框 start -->
83    <div class="ds-thread" data-thread-key="" data-title="' . $wiki_title . '" data-url="' . $wiki_url . '"></div>
84<!-- 多说评论框 end -->
85<!-- 多说公共JS代码 start (一个网页只需插入一次) -->
86<script type="text/javascript">
87var duoshuoQuery = {short_name:"' . $short_name . '"};
88    (function() {
89        var ds = document.createElement("script");
90        ds.type = "text/javascript";ds.async = true;
91        ds.src = (document.location.protocol == "https:" ? "https:" : "http:") + "//static.duoshuo.com/embed.js";
92        ds.charset = "UTF-8";
93        (document.getElementsByTagName("head")[0]
94         || document.getElementsByTagName("body")[0]).appendChild(ds);
95    })();
96    </script>
97<!-- 多说公共JS代码 end -->';
98        return $doc;
99    }
100}
101
102// vim:ts=4:sw=4:et:
103