1<?php
2/**
3 *
4 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
5 * @author     Andreas Gohr <andi@splitbrain.org>
6 */
7
8if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
9if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
10require_once(DOKU_PLUGIN.'syntax.php');
11
12
13/**
14 * All DokuWiki plugins to extend the parser/rendering mechanism
15 * need to inherit from this class
16 */
17class syntax_plugin_disqus extends DokuWiki_Syntax_Plugin {
18
19    /**
20     * What kind of syntax are we?
21     */
22    function getType(){
23        return 'substition';
24    }
25
26    function getPType(){
27        return 'block';
28    }
29
30    /**
31     * Where to sort in?
32     */
33    function getSort(){
34        return 160;
35    }
36
37    /**
38     * Connect pattern to lexer
39     */
40    function connectTo($mode) {
41      $this->Lexer->addSpecialPattern('~~DISQUS\b.*?~~',$mode,'plugin_disqus');
42    }
43
44    /**
45     * Handle the match
46     */
47    function handle($match, $state, $pos, Doku_Handler $handler){
48
49        $match = substr($match, 8, -2);         //strip ~~DISQUS from start and ~~ from end
50        $shortname = strtolower(trim($match));  //strip spaces
51
52        if (!$shortname) $shortname = $this->getConf('shortname');
53        return $shortname;
54    }
55
56    /**
57     * Create output
58     */
59    function render($mode, Doku_Renderer $R, $data) {
60        if($mode != 'xhtml') return false;
61        $R->doc .= $this->_disqus($data);
62        return true;
63    }
64
65    function _disqus($shortname = ''){
66        global $ID;
67        global $INFO;
68
69        if (!$shortname === '') $shortname = $this->getConf('shortname');
70
71        $doc = '';
72        $doc .= '<script charset="utf-8" type="text/javascript">
73                    <!--//--><![CDATA[//><!--'."\n";
74        if($this->getConf('devel'))
75            $doc .= 'var disqus_developer = '.$this->getConf('devel').";\n";
76        $doc .= "var disqus_url     = '".wl($ID,'',true)."';\n";
77        $doc .= "var disqus_title   = '".addslashes($INFO['meta']['title'])."';\n";
78        $doc .= "var disqus_message = '".addslashes($INFO['meta']['abstract'])."';\n";
79        $doc .= 'var disqus_container_id = \'disqus__thread\';
80                    //--><!]]>
81                    </script>';
82        $doc .= '<div id="disqus__thread"></div>';
83        $doc .= '<script type="text/javascript" src="//disqus.com/forums/'.hsc($shortname).'/embed.js"></script>';
84        $doc .= '<noscript><a href="//'.hsc($shortname).'.disqus.com/?url=ref">View the discussion thread.</a></noscript>';
85
86        return $doc;
87    }
88
89}
90
91//Setup VIM: ex: et ts=4 enc=utf-8 :
92