summaryrefslogtreecommitdiff
path: root/platform/www/lib/plugins/textinsert/syntax.php
blob: 82a60b604daa44068054c8fe2d73b1f230b9ee26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
 * 
 * 
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Myron Turner <turnermm02@shaw.ca>
 * 
 */
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
define('REPLACE_DIR', DOKU_INC . 'data/meta/macros/');
define('MACROS_FILE', REPLACE_DIR . 'macros.ser');


/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_textinsert extends DokuWiki_Syntax_Plugin {
   var $macros;
   var $translations;
   var $ns;
    /**
     * return some info
     */

    /**
     * What kind of syntax are we?
     */
    function getType(){
        return 'substition';
    }

    /**
     * Where to sort in?
     */ 
    function getSort(){
        return 155;
    }


    /**
     * Connect pattern to lexer
     */
    function connectTo($mode) {
        $this->Lexer->addSpecialPattern('#@\!?[\w\-\._]+\!?@#',$mode,'plugin_textinsert');
        $this->Lexer->addSpecialPattern('#@\!\![\w\-\._]+@#',$mode,'plugin_textinsert');
		$this->Lexer->addSpecialPattern('#@[\w\-\._]+~.*?~@#',$mode,'plugin_textinsert');
        $this->Lexer->addSpecialPattern('#@[\w\-\._]+[\r\n]+~[^\r\n]+~@#',$mode,'plugin_textinsert');
    }


    /**
     * Handle the match
     */
    function handle($match, $state, $pos, Doku_Handler $handler){
		
        $html=false;
		$translation = false;
        $match = substr($match,2,-2); 
        $match = trim($match);   
        if(strpos($match, 'HTML')) $html=true;
        if(strpos($match, 'LANG_') !== false) {
		    $translation=true;
			list($prefix,$trans) = explode('_',$match,2);
			}
		
			global $ID;
			list($ns,$rest) = explode(':',$ID,2);			 
				if(@file_exists($filename = DOKU_PLUGIN . "textinsert/lang/$ns/lang.php")) {
					include $filename;
					$this->translations = $lang;
               
            }
 			
			if(@file_exists($filename = DOKU_PLUGIN . "textinsert/lang/$ns/macros.php")) {
					include $filename;   
                    $ar = 'lang_' .$ns;           
                    $tr = $$ar;	            				
                    if($this->translations)  {
                          $this->translations = array_merge($lang,$tr);                       
                    }
				    else $this->translations = $tr;
           }
    
           if(!empty($ns)) {
               $this->ns = $ns;
           }
        $this->macros = $this->get_macros();
		
       
       
        while(preg_match('#(\*\*|//|__|\'\').*?\1#m',$match )) { 
            $match = preg_replace_callback(
            '#(\*\*|//|__|\'\')(.*?)(\1)#',
                function($matches) {
                    $matches[1] = str_replace(array('**','//','__','\'\'',),array('<b>','<em>','<u>','<code>'),$matches[1]);
                   $matches[3] = str_replace(array('**','//','__','\'\''),array('</b>','</em>','</u>','</code>'),$matches[3]);    
                    return $matches[1] . $matches[2] . $matches[3];
                },$match );
        }		
        
		if(preg_match('/(.*?)~([\s\S]+)~$/',$match,$subtitution)) {
             $match=$subtitution[1];
             $subtitution[2] = str_replace('\\,','&#44;',$subtitution[2]);
             $substitutions=explode(',',$subtitution[2]);	
             $substitutions = preg_replace('#\/\/.+#',"",$substitutions);        
             $substitutions = preg_replace('#\\\n#',"<br />",$substitutions);    
		}
   
        if(!array_key_exists($match, $this->macros) ) {
            $err = $this->getLang('not_found');
           msg("$match $err", -1);  
           $match = "";              
        }
        else {
			if($translation && isset($this->translations[$trans])){
				$match = $this->translations[$trans];
			}
			else {
				$match =$this->macros[$match];                
			}
		   }
		  
		if(!is_array($substitutions)) $substitutions = array();
		for($i=0; $i<count($substitutions); $i++) {
	            $search = '%' . ($i+1);
	            $match = str_replace ($search ,  trim($substitutions[$i]), $match);
        }	
        
        $match = $this->get_inserts($match,$translation); 

        if($html) {
          $match =  str_replace('&lt;','<',$match);
          $match =  str_replace('&gt;','>',$match);
        }

        return array($state,$match);
    }

    /**
     * Create output
     */
    function render($mode, Doku_Renderer $renderer, $data) {
        global $INFO;
        if($mode == 'xhtml'){           
            list($state, $word) = $data;          
            If(strpos($word,'_ID_') !== false ) {               
                $word = str_replace('_ID_',$INFO['id'], $word);      
            }
            $renderer->doc .= $word;
            return true;
        }
        return false;
    }
    
    function get_macros() {
        $a = array();
        if(file_exists(MACROS_FILE)) {
            $a = unserialize(file_get_contents(MACROS_FILE));
       }
       else if($this->getConf('farm')) { 
           $a = unserialize(file_get_contents(metaFN('macros','.ser')));
       }
       $r =  $this->get_std_replacements() ;
       $result = array_merge($r,$a);   
       return array_merge($r,$a);        
    }

   function get_inserts($match,$translation) {
      $inserts = array();    
	  
	  // replace embedded macros
      if(preg_match_all('/#@(.*?)@#/',$match,$inserts)) {        
		$keys = $inserts[1]; 
		$pats = $inserts[0];        

		for($i=0; $i<count($keys); $i++) {
		   $insert = $this->macros[$keys[$i]];
			if($translation ||strpos($keys[$i], 'LANG_') !== false)  {
					list($prefix,$trans) = explode('_',$keys[$i],2);
					$_insert = $this->translations[$trans];
					if($_insert) $insert =$_insert;
			}
			$match = str_replace($pats[$i],$insert,$match);
          }
		  
      }  // end replace embedded macros
    
     
      $entities =  getEntities();
      $e_keys = array_keys($entities);
      $e_values =  array_values($entities);
      $match = str_replace($e_keys,$e_values,$match);    
      
      return  $match;
   }
  
  function get_std_replacements() {
        if(!$this->getConf('stdreplace')) return array();
        global $conf;
        global $INFO;
        global $ID;

        $file = noNS($ID);       
        $page = cleanID($file) ;
	
        $names =array(
                              'ID',
                              'NS',
                              'FILE',
                              '!FILE',
                              '!FILE!',
                              'PAGE',
                              '!PAGE',
                              '!!PAGE',
                              '!PAGE!',
                              'USER',
                              'DATE',
                              '_ID_'
                              );
                              
            $values = array(
                              $ID,
                              getNS($ID),
                              $file,
                              utf8_ucfirst($file),
                              utf8_strtoupper($file),
                              $page,
                              utf8_ucfirst($page),
                              utf8_ucwords($page),
                              utf8_strtoupper($page),
                              $_SERVER['REMOTE_USER'],                              
                              strftime($conf['dformat'], time()),
							  '_ID_'
                           );
     $std_replacements = array();
     for($i=0; $i<count($names) ; $i++) {
           $std_replacements[$names[$i]] = $values[$i];
     }     
   
     return $std_replacements;
}
  
  function write_debug($what, $screen = false) {
	  return;
	  $what=print_r($what,true);
       if($screen) {
       msg('<pre>' . $what . '</pre>');     
           return;
       }
	   $handle=fopen("textinsert.txt",'a');
	   fwrite($handle,"$what\n");
	   fclose($handle);
  }
}