1<?php
2
3/**
4 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
5   * class        syntax_plugin_annotate_anno
6   * @author     Myron Turner <turnermm02@shaw.ca>
7*/
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13
14require_once(DOKU_PLUGIN.'syntax.php');
15define("BULLET_PNG", DOKU_REL ."lib/plugins/annotate/bullet.png");
16//msg(BULLET_PNG);
17class syntax_plugin_annotate_anno extends DokuWiki_Syntax_Plugin {
18
19    var $test_str;
20    /**
21     * What kind of syntax are we?
22     */
23    function getType(){
24        return 'substition';
25    }
26
27    /**
28     * What about paragraphs?
29     */
30
31    function getPType(){
32       //return 'block';
33    }
34
35    /**
36     * Where to sort in?
37     */
38    function getSort(){
39        return 155;
40    }
41    public function  __construct() {
42      $this->test_str = '';
43    }
44
45    /**
46     * Connect pattern to lexer
47     */
48     public function connectTo($mode) {
49		 $this->Lexer->addEntryPattern(
50		 '<@anno:\d\d?>(?=.*?</@anno>)',$mode,
51		 'plugin_annotate_anno');
52
53		 $this->Lexer->addEntryPattern(
54		 '<@anno:\[\d\d?;;\w+\]>(?=.*?</@anno>)',$mode,
55		 'plugin_annotate_anno');
56
57		 $this->Lexer->addSpecialPattern(
58		 '<anno:\d\d?>.*?</anno>',$mode,'plugin_annotate_anno');
59
60	 }
61
62
63 public function postConnect() {
64       $this->Lexer->addExitPattern('</@anno>','plugin_annotate_anno');
65 }
66
67    /**
68     * Handle the match
69     */
70    function handle($match, $state, $pos, Doku_Handler $handler){
71
72        switch($state) {
73          case DOKU_LEXER_ENTER :
74		      $match = str_replace(array(':', ';',  '[' , ']'),   array('_'," ","",""),substr($match,2,-1));
75			   return array($state, $match);
76          case DOKU_LEXER_UNMATCHED :
77              if(preg_match("/<top>([\w\:]+)\<\/top\>/m",$match,$matches)) {
78                   $id = $matches[1];
79                   $text = io_readWikiPage(wikiFN($id, $rev), $id, false);
80                   if($text) {
81                      $match = preg_replace("/<top>.*?<\/top>/ms", "\n$text\n",$match);
82                  }
83              }
84              if(preg_match("/<bottom>([\w\:]+)\<\/bottom\>/m",$match,$matches)) {
85                   $id = $matches[1];
86                   $text = io_readWikiPage(wikiFN($id, $rev), $id, false);
87                   if($text) {
88                      $match = preg_replace("/<bottom>.*?<\/bottom>/ms", "\\\\\\ $text",$match);
89                  }
90              }
91              return array($state, $match);
92          case DOKU_LEXER_EXIT :   return array($state, '');
93          case DOKU_LEXER_SPECIAL:
94		      $inner = substr($match,6,-7);
95			  return array($state, $inner);
96       }
97         return array($state, "" );
98
99    }
100
101   /**
102     * Create output
103     */
104    function render($mode, Doku_Renderer $renderer, $data) {
105		static $last;
106        if($mode == 'xhtml'){
107            list($state, $xhtml) = $data;
108            switch ($state) {
109                case DOKU_LEXER_ENTER :
110                   $classes = preg_split("/\s+/", $xhtml);
111                   $xhtml	 = rtrim(implode(' ',$classes));
112				   $last = $classes[0];
113                   if (count($classes) > 1) {
114                       $tip = '<span class="' . $xhtml . ' anno-dclk-over ui-widget-content">';
115                   }
116                    else {
117				    $tip = '<span class="annotation ui-widget-content '. $xhtml . '">';
118                    }
119				    $renderer->doc .= $tip;
120				break;
121                case DOKU_LEXER_UNMATCHED :
122                $renderer->doc .= '<span id="anno_close_' . $last . '"><span class="anno_exit">close</span> </span>';
123       		    $xhtml = trim($xhtml);
124                if(preg_match('/^\{\{([\w\:]+)\}\}$/',$xhtml,$matches)) {
125				  	   $html = p_wiki_xhtml($matches[1]);
126			        }
127			       else {
128                      $secedit = false;
129				      $html = html_secedit(p_render('xhtml',p_get_instructions($xhtml),$info),$secedit);				 //$info is reference, $secedit is handled by html_secedit
130 				}
131				$html = $this->html_filter($html);
132                $renderer->doc .= $html;  break;
133                case DOKU_LEXER_EXIT :
134				    $renderer->doc .= "</span>"; break;
135				case DOKU_LEXER_SPECIAL:
136					list($which,$text)= explode('>',$xhtml);
137                    $title = 'anno_' .$which;
138				    $renderer->doc .= '<span class="anno" title="' .$title.'">' .htmlentities($text).'</span>'; break;
139
140        }
141           return true;
142        }
143        return false;
144    }
145
146	function html_filter($html){
147       $html = str_replace(array('[List]','[tsiL]'),"",$html);
148	   $html = preg_replace('/<\/?p>/ms',"",$html);
149	   $html = preg_replace_callback('/<(\/)?h(\d).*?>/ms',
150		function($matches){
151			if($matches[1] == '/'){
152				$style = '</b>';
153				if($matches[2] <= 3){
154					$style .= '<br />';
155				}
156			}
157			else {
158				if($matches[2] <= 4){
159                    if($matches[2] == 1) {
160                        $style = '<br /><b class="h1_bold">';
161                    }
162                    else if($matches[2] == 2) {
163                        $style = '<br /><b class="h2_bold">';
164                    }
165                    else if($matches[2] == 3) {
166                        $style = '<br /><b class="h3_bold">';
167                    }
168                    else if($matches[2] == 4) {
169                        $style = '<br /><b class="h4_bold">';
170                    }
171				  //  else $style = '<br /><b class="extra_bold">';
172				}
173				else $style = '<br /><b>';
174			}
175			return $style;
176		}, $html);
177
178		$html = preg_replace_callback(
179        '/<table[^>]+>(.*?)<\/table>/ms',
180        function($matches) {
181            $replace = array('td','th','</tr>');
182            $replacements = array('span','span_h','<br/>');
183            $matches[1] = str_replace($replace, $replacements,$matches[1]);
184            $matches[1] = preg_replace("/\<tr\s+class=\"row\d\">/","",$matches[1]);
185            $matches[1] = preg_replace(array("/\<tr\s+class=\"row\d\">/","/col\d+/"),array("",'anno_col'),$matches[1]);
186            $matches[1] = preg_replace('/\"anno_col\"\s+colspan=\"(\d)\"/',"anno_colspan_$1",$matches[1]);
187            $matches[1] = preg_replace("/span_h\s+class=\"/","span class=\"theader ",$matches[1]);
188            $matches[1] = str_replace('/span_h','/span',$matches[1]);
189            return'<br>' . $matches[1] .'<br />';
190        },$html);
191
192        $html = preg_replace_callback(
193         '/\<(o|u)l\>([\s\S]+)\<\/(o|u)l\>/ms',
194        function($matches) {
195            $matches[0] = preg_replace("/\<li\s+class=\"level(\d).*?\"\>/","<span class='anno_li_$1'>&nbsp; </span>", $matches[0]);
196            $matches[0] = preg_replace('/\<div\s+class.*?li\">/',"",$matches[0]);
197            $matches[0] = str_replace('</div>','<br>',$matches[0]);
198            return  $matches[0] ."\nEOL";
199        },$html);
200        //$html = preg_replace("/\<\/?(ul|ol|li)\>/","", $html);
201
202       $html = preg_replace("/\<\/(ul|ol|li)\>/","", $html);
203
204        $html = preg_replace_callback(
205          '/(\<ol>|\<ul\>)(.*?)EOL/ms',
206            function($matches) {
207             $type = "";
208    /*
209ol             { list-style: decimal outside; }
210ol ol          { list-style-type: lower-alpha; }
211ol ol ol       { list-style-type: upper-roman; }
212ol ol ol ol    { list-style-type: upper-alpha; }
213ol ol ol ol ol { list-style-type: lower-roman; }
214*/
215     $anno_li_1 = 0;
216     $anno_li_3 = 'abcdefghijklmnopqrstuvwxyz';
217     $anno_li_2 = $anno_li_8=$anno_li_7=$anno_li_6 = array('i','ii','iii','iv','v','vi','vii','viii','ix','x','xi','xii','xiii','xiv','xv','xvi','xvii','xviii','xix','xx','xxi','xxii','xxiii','xxiv','xxv','xxvi');
218     $anno_li_4 = array('I','II','III','IV','V','VI','VII','VIII','IX','X','XI','XII','XIII','XIV','XV','XVI','XVII','XVIII','XIX','XX','XXI','XXII','XXIII','XXIV','XXV','XXVI');
219     $anno_li_5 = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
220     $current = array('anno_li_0' => 0,'anno_li_1' => 0,'anno_li_2' => 0,'anno_li_3'=>0,'anno_li_4'=>0,'anno_li_5'=>0,'anno_li_6'=>0,
221         'anno_li_7'=>0,'anno_li_8'=>0);
222
223     $_list =  explode("\n", $matches[2]);
224     $retv = "<br />";
225
226     for($i=0; $i<count($_list); $i++) {
227         $_list[$i] = trim($_list[$i]);
228
229         if(!empty($_list[$i])) {
230             if(preg_match("/\<ol>/",$_list[$i])) {
231                 $type = 'o';
232                 continue;
233             }
234             else if(preg_match("/\<ul\>/",$_list[$i])) {
235                 $type = 'u';
236                 continue;
237             }
238
239             if($type == 'u') {
240                 $_list[$i] = str_replace("</span>", "<img src=\"" . BULLET_PNG ."\"></span>",$_list[$i] );
241             }
242             else {
243                if(preg_match("/(anno_li_(\d))/",$_list[$i],$match)) {
244                    if($match[1] == 'anno_li_1') {
245                        $anno_li_1++;
246                        $_list[$i] = str_replace('&nbsp;',$anno_li_1,$_list[$i]);  // Insert current number
247                    }
248                    else {
249                        $b = $match[1]; // anno_li_<n>
250                        $a = ${$b};  //   $anno_li_<n>
251                        $marker = $a[$current[$match[1]]];
252                        $_list[$i] = str_replace('&nbsp;',$marker,$_list[$i]);
253                        $current[$match[1]]++;       // $current[ 'anno_li_<n>']
254                    }
255                }
256             }
257             $retv .= $_list[$i] ."\n";
258         }
259     }
260        return $retv;
261    },$html);
262$html  = str_replace('<hr />','<br /><span class="anno_hr"> </span><br />',$html);
263$html = str_replace('<blockquote>', '<br /><span class = "anno_blockquote">',$html);
264$html = str_replace('</blockquote>', '</span>',$html);
265
266		$html = preg_replace('/<\/?div.*?>/ms',"",$html);
267		$html = preg_replace('/<!--.*?-->/ms',"",$html);
268
269		return $html;
270	}
271
272}
273
274
275