1<?php
2/**
3 * TeXit-Plugin: Parses TeXit-blocks in xhtml mode
4 * Copyright (C) 2007 Danjer
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 * --------------------------------------------------------------------
20 *
21 * @author     Danjer <danjer@doudouke.org>
22 * @date       2007-02-11
23 */
24
25
26if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
27if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
28require_once(DOKU_PLUGIN.'syntax.php');
29require_once(DOKU_PLUGIN.'texit/texitrender.php');
30
31/**
32 * All DokuWiki plugins to extend the parser/rendering mechanism
33 * need to inherit from this class
34 */
35class syntax_plugin_texit extends DokuWiki_Syntax_Plugin {
36
37    /**
38     * What kind of syntax are we?
39     */
40    public function getType(){
41      return 'protected';
42    }
43
44    /**
45     * @return string Paragraph type
46     */
47//    public function getPType() {
48//        return 'normal'; // let's keep the default value
49//    }
50
51    /**
52     * Where to sort in?
53     */
54    public function getSort(){
55      return 100;
56    }
57
58  /**
59   * Connect pattern to lexer
60   */
61  function connectTo($mode) {
62    $this->Lexer->addEntryPattern('<texit(?=.*\x3C/texit\x3E)', $mode,
63				  'plugin_texit');
64  }
65
66  function postConnect() {
67    $this->Lexer->addExitPattern('</texit>','plugin_texit');
68  }
69
70  /**
71   * Handle the match
72   */
73  function handle($match, $state, $pos, &$handler){
74    //print_r(array('match' => $match, 'state' => $state, "pos" => $pos, "handler" => $handler));
75    //print "<br>";
76    if ($state == DOKU_LEXER_UNMATCHED) {
77
78      $matches = preg_split('/>/u',$match,2);
79      $matches[0] = trim($matches[0]);
80      if ( trim($matches[0]) == '' ) {
81	$matches[0] = NULL;
82      }
83      return array($state,$matches[0], $matches[1],$pos);
84    }
85
86    return array($state,'',$match,$pos);
87  }
88
89  /**
90   * Create output
91   */
92  function render($mode, &$renderer, $data) {
93    global $ID;
94    list($state, $substate, $match, $pos) = $data;
95    if (!isset($this->_texit)) {
96      if (!$this->configloaded) {
97	$this->loadConfig();
98      }
99      $this->_texit = new texitrender_plugin_texit($ID);
100    }
101    if($mode == 'xhtml'){
102      $renderer->info['cache'] = $this->_texit->docache();
103      if ($state == DOKU_LEXER_EXIT) {
104	return TRUE;
105      }
106      if ($state != DOKU_LEXER_UNMATCHED) {
107	return FALSE;
108      }
109      switch ($substate) {
110      case 'info':
111	if ($this->_texit->add_data($substate, $match)) {
112	  $renderer->doc .= $this->_texit->render() . '<p>';
113	}
114	break;
115      case 'footer':
116      case 'begin':
117      case 'document':
118      case 'command':
119      default:
120	break;
121      }
122      return TRUE;
123    }
124    if($mode == 'latex'){
125      if ($state == DOKU_LEXER_EXIT) {
126	return TRUE;
127      }
128      if ($state != DOKU_LEXER_UNMATCHED) {
129	return FALSE;
130      }
131      if (!isset($substate)) {
132	$renderer->put($match);
133      }
134      return TRUE;
135    }
136    return FALSE;
137  }
138}
139?>
140