1<?php
2/**
3 * jokuwiki syntax plugin
4 * markup example:
5 * ex1:
6 * <jw name="jwHelloWorld">{ "helloTo" : "World" }</jw>
7 *
8 * ex2:
9 * <jw name='jwHelloWorld'
10 *     style='width:100px;height=40px'
11 *     noscript='Javascript is disabled'>
12 *      { "helloTo" : "World" }
13 * </jw>
14 *--
15 * ex1 translates to
16 * <div id='jw-jwHelloWorld-1'
17 *      class='jwHelloWold'
18 *      data-jw='{ "jokuwiki" : "jwHelloWold", "id" : "jw-jwHelloWorld-1", "data" : { "helloTo" : "World" }}'>
19 * </div>
20 *
21 * ex2 translates to
22 * <div id='jw-jwHelloWorld-2'
23 *      class='jwHelloWold'
24 *      style='width:100px;height=40px'
25 *      data-jw='{ "jokuwiki" : "jwHelloWold", "id" : "jw-jwHelloWorld-2", "data" : { "helloTo" : "World" }}'>
26 *    <noscript>Javascript is disabled</noscript>
27 * </div>
28 */
29
30file_put_contents('/tmp/jokuwiki_loaded', 'yes');
31
32if(!defined('DOKU_INC')) die();
33
34if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
35require_once DOKU_PLUGIN.'syntax.php';
36
37/**
38 * All DokuWiki plugins to extend the parser/rendering mechanism
39 * need to inherit from this class
40 */
41class syntax_plugin_jokuwiki extends DokuWiki_Syntax_Plugin {
42
43    function getInfo(){
44        return array(
45            'author' => 'SyMcBean',
46            'email'  => 'colin.mckinnon@ntlworld.com',
47            'date'   => '2013-06-17',
48            'name'   => 'JokuWiki',
49            'desc'   => 'Framework for Javascript tools',
50            'url'    => 'http://www.dokuwiki.org/plugin:jokuwiki',
51        );
52    }
53
54    function getType(){
55        return 'substitution';
56    }
57    function getPType(){
58        return 'block';
59    }
60
61/**
62 * must be injected before jwPlugins which do something
63 */
64    function getSort() {
65        return 901;
66    }
67    function connectTo($mode) {
68            file_put_contents('/tmp/jokuwiki_0','started');
69            // $p='<jw\b?[^>]*>.*?</jw>'; // fail
70            // $p='<jw\b[^>]*>.*?<\/jw>'; // fail
71            // $p='<jw .*\?>(?=.*?</jw>'; // fail <?php
72            $p='<jw.*?>(?=.*?</jw>'; // <?php
73            $this->Lexer->addSpecialPattern($p,$mode,'plugin_jokuwiki');
74            file_put_contents('/tmp/jokuwiki_a', 'running '. $p);
75    }
76
77    function handle($match, $state, $pos, &$handler) {
78        list($attributes, $data)=explode('>', substr($match,2,4),2);
79        $attrs=$this->parse_attributes($attributes);
80        $attrs['data-jw']=$this->cleanupData($data);
81        file_put_contents('/tmp/jokuwiki_b', var_export($attrs, true));
82        return $attrs;
83    }
84    function parse_attributes($attrs)
85    {
86          $name='';
87          $style='';
88          $id='';
89          $noscript='';
90          if (preg_match_all('/(\w*)="(.*?)"/us',$attrs, $matches, PREG_SET_ORDER)) {
91                foreach($matches as $key=>$val) {
92                    switch ($key) {
93                        case 'name':
94                        case 'style':
95                        case 'id':
96                        case 'noscript':
97                                $$key=trim(str_replace('"', '', $val));
98                                break;
99                        default:
100                                break;
101                    }
102                }
103          }
104          return array($name, $id, $style, $noscript);
105    }
106    function render($mode, &$renderer, $attrs) {
107        global $jokuwiki_ids;
108    // $data is what the function handle return'ed.
109        if($mode == 'xhtml'){
110            if (!$attrs['id']) {
111                $jokuwiki_ids++;
112                $attrs['id']="jw-" . $attrs['name'] . "-" . $jokuwiki_ids;
113            }
114            $noscript=$attrs['noscript'];
115            unset($attrs['noscript']);
116            $append="<div ";
117            foreach ($attrs as $k=>$val) {
118                $append.=' ' . $k . "='" . $val . "'";
119            }
120            $append.=">";
121            if (trim($noscript)) $append.="<noscript>$noscript</noscript>\n";
122            $append.="</div>\n";
123            $renderer->doc.=$append;
124            return true;
125        }
126        return false;
127    }
128
129   }
130/* EOF */
131