1<?php
2/**
3 * Plugin Weiqi: Displays Weiqi (also named Go game or Baduk) diagrams.
4 * http://wiki.splitbrain.org/plugin:weiqi
5 * http://en.wikipedia.org/wiki/Go_(board_game)
6 *
7 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author     Christophe Gragnic <christophegragnic@yahoo.fr>
9 */
10
11// must be run within Dokuwiki
12if(!defined('DOKU_INC')) die();
13
14if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15require_once(DOKU_PLUGIN.'syntax.php');
16
17/**
18 * All DokuWiki plugins to extend the parser/rendering mechanism
19 * need to inherit from this class
20 */
21class syntax_plugin_weiqi extends DokuWiki_Syntax_Plugin {
22
23    // these class vars are like conf vars (will be in the admin page one day)
24    // they are initialized in the plugin constructor syntax_plugin_weiqi()
25
26    // syntax starts at <xxx>, ends at </xxx> where xxx is $syntax_tag
27    var $syntax_tag;
28
29    /**
30     * Plugin Constructor.
31     */
32    function syntax_plugin_weiqi() {
33      require_once(DOKU_INC.'lib/plugins/weiqi/weiqi_parser.php');
34      // enable direct access to language strings
35      $this->setupLocale();
36      // enable direct access to configuration
37      $this->loadConfig();
38
39      $this->syntax_tag = 'weiqi';
40
41      // have a look at weiqi_parser.php
42      // for some explanations about those config items
43      $this->conf = array(
44                'attributes_key' => 'attributes:',
45                'caption_key' => 'caption:',
46                'img_path_web' => DOKU_BASE.'lib/plugins/weiqi/img/',
47                'img_path_fs' => 'lib/plugins/weiqi/img/',
48                'letter_sequence' => 'abcdefghjklmnopqrstuvwxyz',
49                'plain_text_coeff' => 0.7,
50                'allowed_attribute_keys' => array(
51                    'demo', 'goban', 'coords', 'grid_size',
52                    'reverse_numbers', 'reverse_letters',
53                    'start_number', 'start_letter',
54                    'advanced'),
55                'default_attributes' => array(
56                    'demo' => false,
57                    'goban' => 1,
58                    'grid_size' => 25,
59                    'edges_width' => 10,
60                    'coords' => false,
61                    'reverse_numbers' => false,
62                    'reverse_letters' => false,
63                    'start_number' => 1,
64                    'start_letter' => 1,
65                    'advanced' => false,
66                    )
67                );
68    }
69
70    function getInfo() {
71      return array(
72        'author' => 'Grahack',
73        'email'  => 'christophegragnic@yahoo.fr',
74        'date'   => '2008-06-19',
75        'name'   => 'Weiqi Plugin',
76        'desc'   => 'Displays Weiqi (also named Go game or Baduk) diagrams',
77        'url'    => 'http://wiki.splitbrain.org/plugin:weiqi',
78      );
79    }
80
81    function getType() { return 'protected'; }
82    function getAllowedTypes() { return array('formatting', 'disabled'); }
83    function getPType(){ return 'block';}
84    function getSort() { return 999; }
85    function connectTo($mode) {
86        $pattern = '<'.$this->syntax_tag.'.*?>(?=.*?</'.$this->syntax_tag.'>)';
87        $this->Lexer->addEntryPattern($pattern,$mode,'plugin_weiqi');
88    }
89    function postConnect() {
90        $this->Lexer->addExitPattern('</'.$this->syntax_tag.'>','plugin_weiqi');
91    }
92    function handle($match, $state, $pos, &$handler) {
93        if ($state == DOKU_LEXER_UNMATCHED) {
94            $parser = new weiqi_parser();
95            $parser->set_conf($this->conf);
96            $parsed = $parser->parse($match);
97            if (!$parser->error) return array($state, $parsed);
98            else {
99                switch ($parser->error_code) {
100                    case WEIQI_ERROR_BAD_ATTRIBUTES :
101                        $message = $this->lang['weiqi bad attributes']
102                            .' ('.trim($parser->error_data['attributes_str']).')<br />';
103                        $bad_attributes = '';
104                        foreach( $parser->error_data['errors'] as $key => $value) {
105                            if ($value == '') {
106                                $bad_attributes.= ', '.$key
107                                .' '.$this->lang['weiqi not allowed'];
108                            } else {
109                                $bad_attributes.= ', '.$value
110                                .' '.$this->lang['weiqi not allowed for'].' '.$key;
111                            }
112                        }
113                        $message.= trim(trim($bad_attributes, ','));
114                        break;
115                    case WEIQI_ERROR_ONE_CAPTION :
116                        $message = $this->lang['weiqi one caption'];
117                        break;
118                    case WEIQI_ERROR_RECTANGULAR :
119                        $message = $this->lang['weiqi rectangular'];
120                        break;
121                    default : $message = $this->lang['weiqi unknown error'];
122                }
123                return array($state, array(), $message);
124            }
125        } else return array($state);
126    }
127    function render($mode, &$renderer, $data) {
128        if ($mode == 'xhtml') {
129            list($state, $goban_data, $message) = $data;
130            switch ($state) {
131                case DOKU_LEXER_ENTER :
132                    $renderer->doc .= '<div class="weiqi">';
133                    return true;
134                case DOKU_LEXER_UNMATCHED :
135                    if ($message!='') {
136                        $renderer->doc.= '<div class="error">';
137                        $renderer->doc.= $this->lang['weiqi error'].': '.$message;
138                        $renderer->doc.= '</div>';
139                        // no need to display the goban if something is wrong
140                        // so we stop here
141                        return true;
142                    }
143                    $parser = new weiqi_parser();
144                    $parser->set_conf($this->conf);
145                    $renderer->doc .= $parser->render($goban_data);
146                    return true;
147                case DOKU_LEXER_EXIT :
148                    $renderer->doc .= "</div>";
149                    return true;
150            }
151        }
152        return false;
153    }
154}
155