1<?php
2/**
3 * DokuWiki Plugin codebender (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Christian Moll <christian@chrmoll.de>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12class syntax_plugin_codebender_syntax extends DokuWiki_Syntax_Plugin {
13    /**
14     * @return string Syntax mode type
15     */
16    public function getType() {
17        return 'substition';
18    }
19    /**
20     * @return string Paragraph type
21     */
22    public function getPType() {
23        return 'block';
24    }
25    function getAllowedTypes() {
26	return array('container','substition','protected','disabled','formatting','paragraphs');
27    }
28    /**
29     * @return int Sort order - Low numbers go before high numbers
30     */
31    public function getSort() {
32        return 200;
33    }
34
35    /**
36     * Connect lookup pattern to lexer.
37     *
38     * @param string $mode Parser mode
39     */
40    public function connectTo($mode) {
41        $this->Lexer->addEntryPattern('<codebender>(?=.*?</codebender.*?>)',$mode,'plugin_codebender_syntax');
42    }
43
44    public function postConnect() {
45        $this->Lexer->addExitPattern('</codebender>','plugin_codebender_syntax');
46    }
47
48    /**
49     * Handle matches of the codebender syntax
50     *
51     * @param string $match The match of the syntax
52     * @param int    $state The state of the handler
53     * @param int    $pos The position in the document
54     * @param Doku_Handler    $handler The handler
55     * @return array Data for the renderer
56     */
57    public function handle($match, $state, $pos, Doku_Handler &$handler){
58        switch($state) {
59           case DOKU_LEXER_ENTER: return array($state, '');
60           case DOKU_LEXER_UNMATCHED: return array($state, $match);
61           case DOKU_LEXER_EXIT: return array($state, '');
62        }
63    }
64
65    /**
66     * Render xhtml output or metadata
67     *
68     * @param string         $mode      Renderer mode (supported modes: xhtml)
69     * @param Doku_Renderer  $renderer  The renderer
70     * @param array          $data      The data from the handler() function
71     * @return bool If rendering was successful.
72     */
73    public function render($mode, Doku_Renderer &$renderer, $data) {
74        if($mode == 'xhtml') {
75	  list($state, $match) = $data;
76
77	  switch($state) {
78	    case DOKU_LEXER_ENTER:
79	      $renderer->doc .= "<div class='codebender'>";
80	      break;
81	    case DOKU_LEXER_UNMATCHED:
82	      $renderer->doc .=  '<iframe style="height: 510px; width: 100%; margin: 10px 0 10px;" allowTransparency="true" src="https://codebender.cc/embed/sketch:'.$match.'" frameborder="0"></iframe>';
83	      break;
84	    case DOKU_LEXER_EXIT:
85	      $renderer->doc .= "</div>";
86	      break;
87	  }
88
89	  return true;
90	  }
91        return false;
92    }
93}
94
95// vim:ts=4:sw=4:et:
96