]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/InlineParser.php
Allow "#[[" syntax for anchors
[SourceForge/phpwiki.git] / lib / InlineParser.php
1 <?php 
2 rcs_id('$Id$');
3 /* Copyright (C) 2002 Geoffrey T. Dairiki <dairiki@dairiki.org>
4  * Copyright (C) 2004-2008 Reini Urban
5  * Copyright (C) 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
6  *
7  * This file is part of PhpWiki.
8  * 
9  * PhpWiki is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * PhpWiki is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  * 
19  * You should have received a copy of the GNU General Public License
20  * along with PhpWiki; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23 /**
24  * This is the code which deals with the inline part of the (new-style)
25  * wiki-markup.
26  *
27  * @package Markup
28  * @author Geoffrey T. Dairiki, Reini Urban
29  */
30
31 /**
32  * This is the character used in wiki markup to escape characters with
33  * special meaning.
34  */
35 define('ESCAPE_CHAR', '~');
36
37 require_once(dirname(__FILE__).'/HtmlElement.php');
38 require_once('lib/CachedMarkup.php');
39 require_once(dirname(__FILE__).'/stdlib.php');
40
41
42 function WikiEscape($text) {
43     return str_replace('#', ESCAPE_CHAR . '#', $text);
44 }
45
46 function UnWikiEscape($text) {
47     return preg_replace('/' . ESCAPE_CHAR . '(.)/', '\1', $text);
48 }
49
50 /**
51  * Return type from RegexpSet::match and RegexpSet::nextMatch.
52  *
53  * @see RegexpSet
54  */
55 class RegexpSet_match {
56     /**
57      * The text leading up the the next match.
58      */
59     var $prematch;
60     /**
61      * The matched text.
62      */
63     var $match;
64     /**
65      * The text following the matched text.
66      */
67     var $postmatch;
68     /**
69      * Index of the regular expression which matched.
70      */
71     var $regexp_ind;
72 }
73
74 /**
75  * A set of regular expressions.
76  *
77  * This class is probably only useful for InlineTransformer.
78  */
79 class RegexpSet
80 {
81     /** Constructor
82      *
83      * @param array $regexps A list of regular expressions.  The
84      * regular expressions should not include any sub-pattern groups
85      * "(...)".  (Anonymous groups, like "(?:...)", as well as
86      * look-ahead and look-behind assertions are okay.)
87      */
88     function RegexpSet ($regexps) {
89         assert($regexps);
90         $this->_regexps = array_unique($regexps);
91         if (!defined('_INLINE_OPTIMIZATION')) define('_INLINE_OPTIMIZATION',0);
92     }
93
94     /**
95      * Search text for the next matching regexp from the Regexp Set.
96      *
97      * @param string $text The text to search.
98      *
99      * @return RegexpSet_match  A RegexpSet_match object, or false if no match.
100      */
101     function match ($text) {
102         return $this->_match($text, $this->_regexps, '*?');
103     }
104
105     /**
106      * Search for next matching regexp.
107      *
108      * Here, 'next' has two meanings:
109      *
110      * Match the next regexp(s) in the set, at the same position as the last match.
111      *
112      * If that fails, match the whole RegexpSet, starting after the position of the
113      * previous match.
114      *
115      * @param string $text Text to search.
116      *
117      * @param RegexpSet_match $prevMatch A RegexpSet_match object.
118      * $prevMatch should be a match object obtained by a previous
119      * match upon the same value of $text.
120      *
121      * @return RegexpSet_match A RegexpSet_match object, or false if no match.
122      */
123     function nextMatch ($text, $prevMatch) {
124         // Try to find match at same position.
125         $pos = strlen($prevMatch->prematch);
126         $regexps = array_slice($this->_regexps, $prevMatch->regexp_ind + 1);
127         if ($regexps) {
128             $repeat = sprintf('{%d}', $pos);
129             if ( ($match = $this->_match($text, $regexps, $repeat)) ) {
130                 $match->regexp_ind += $prevMatch->regexp_ind + 1;
131                 return $match;
132             }
133             
134         }
135         
136         // Failed.  Look for match after current position.
137         $repeat = sprintf('{%d,}?', $pos + 1);
138         return $this->_match($text, $this->_regexps, $repeat);
139     }
140
141     // Syntax: http://www.pcre.org/pcre.txt
142     //   x - EXTENDED, ignore whitespace
143     //   s - DOTALL
144     //   A - ANCHORED
145     //   S - STUDY
146     function _match ($text, $regexps, $repeat) {
147         // If one of the regexps is an empty string, php will crash here: 
148         // sf.net: Fatal error: Allowed memory size of 8388608 bytes exhausted 
149         //         (tried to allocate 634 bytes)
150         if (_INLINE_OPTIMIZATION) { // disabled, wrong
151             // So we try to minize memory usage, by looping explicitly,
152             // and storing only those regexp which actually match. 
153             // There may be more than one, so we have to find the longest, 
154             // and match inside until the shortest is empty.
155             $matched = array(); $matched_ind = array();
156             for ($i=0; $i<count($regexps); $i++) {
157                 if (!trim($regexps[$i])) {
158                     trigger_error("empty regexp $i", E_USER_WARNING);
159                     continue;
160                 }
161                 $pat= "/ ( . $repeat ) ( " . $regexps[$i] . " ) /x";
162                 if (preg_match($pat, $text, $_m)) {
163                     $m = $_m; // FIXME: prematch, postmatch is wrong
164                     $matched[] = $regexps[$i];
165                     $matched_ind[] = $i;
166                     $regexp_ind = $i;
167                 }
168             }
169             // To overcome ANCHORED:
170             // We could sort by longest match and iterate over these.
171             if (empty($matched)) return false;
172         }
173         $match = new RegexpSet_match;
174         
175         // Optimization: if the matches are only "$" and another, then omit "$"
176         if (! _INLINE_OPTIMIZATION or count($matched) > 2) {
177             assert(!empty($repeat));
178             assert(!empty($regexps));
179             // We could do much better, if we would know the matching markup for the 
180             // longest regexp match:
181             $hugepat= "/ ( . $repeat ) ( (" . join(')|(', $regexps) . ") ) /Asx";
182             // Proposed premature optimization 1:
183             //$hugepat= "/ ( . $repeat ) ( (" . join(')|(', array_values($matched)) . ") ) /Asx";
184             if (! preg_match($hugepat, $text, $m)) {
185                 return false;
186             }
187             // Proposed premature optimization 1:
188             //$match->regexp_ind = $matched_ind[count($m) - 4];
189             $match->regexp_ind = count($m) - 4;
190         } else {
191             $match->regexp_ind = $regexp_ind;
192         }
193         
194         $match->postmatch = substr($text, strlen($m[0]));
195         $match->prematch = $m[1];
196         $match->match = $m[2];
197
198         /* DEBUGGING */
199         if (DEBUG & _DEBUG_PARSER) {
200           static $_already_dumped = 0;
201           if (!$_already_dumped) {
202             var_dump($regexps); 
203             if (_INLINE_OPTIMIZATION)
204                 var_dump($matched);
205             var_dump($matched_inc); 
206           }
207           $_already_dumped = 1;
208           PrintXML(HTML::dl(HTML::dt("input"),
209                           HTML::dd(HTML::pre($text)),
210                           HTML::dt("regexp"),
211                           HTML::dd(HTML::pre($match->regexp_ind, ":", $regexps[$match->regexp_ind])),
212                           HTML::dt("prematch"),
213                           HTML::dd(HTML::pre($match->prematch)),
214                           HTML::dt("match"),
215                           HTML::dd(HTML::pre($match->match)),
216                           HTML::dt("postmatch"),
217                           HTML::dd(HTML::pre($match->postmatch))
218                           ));
219         }
220         return $match;
221     }
222 }
223
224
225
226 /**
227  * A simple markup rule (i.e. terminal token).
228  *
229  * These are defined by a regexp.
230  *
231  * When a match is found for the regexp, the matching text is replaced.
232  * The replacement content is obtained by calling the SimpleMarkup::markup method.
233  */ 
234 class SimpleMarkup
235 {
236     var $_match_regexp;
237
238     /** Get regexp.
239      *
240      * @return string Regexp which matches this token.
241      */
242     function getMatchRegexp () {
243         return $this->_match_regexp;
244     }
245
246     /** Markup matching text.
247      *
248      * @param string $match The text which matched the regexp
249      * (obtained from getMatchRegexp).
250      *
251      * @return mixed The expansion of the matched text.
252      */
253     function markup ($match /*, $body */) {
254         trigger_error("pure virtual", E_USER_ERROR);
255     }
256 }
257
258 /**
259  * A balanced markup rule.
260  *
261  * These are defined by a start regexp, and an end regexp.
262  */ 
263 class BalancedMarkup
264 {
265     var $_start_regexp;
266
267     /** Get the starting regexp for this rule.
268      *
269      * @return string The starting regexp.
270      */
271     function getStartRegexp () {
272         return $this->_start_regexp;
273     }
274     
275     /** Get the ending regexp for this rule.
276      *
277      * @param string $match The text which matched the starting regexp.
278      *
279      * @return string The ending regexp.
280      */
281     function getEndRegexp ($match) {
282         return $this->_end_regexp;
283     }
284
285     /** Get expansion for matching input.
286      *
287      * @param string $match The text which matched the starting regexp.
288      *
289      * @param mixed $body Transformed text found between the starting
290      * and ending regexps.
291      *
292      * @return mixed The expansion of the matched text.
293      */
294     function markup ($match, $body) {
295         trigger_error("pure virtual", E_USER_ERROR);
296     }
297 }
298
299 class Markup_escape  extends SimpleMarkup
300 {
301     function getMatchRegexp () {
302         return ESCAPE_CHAR . '(?: [[:alnum:]]+ | .)';
303     }
304     
305     function markup ($match) {
306         assert(strlen($match) >= 2);
307         return substr($match, 1);
308     }
309 }
310
311 /**
312  * [image.jpg size=50% border=5], [image.jpg size=50x30]
313  * Support for the following attributes: see stdlib.php:LinkImage()
314  *   size=<precent>%, size=<width>x<height>
315  *   border=n, align=\w+, hspace=n, vspace=n
316  *   width=n, height=n
317  *   title, lang, id, alt
318  */
319 function isImageLink($link) {
320     if (!$link) return false;
321     assert(defined('INLINE_IMAGES'));
322     return preg_match("/\\.(" . INLINE_IMAGES . ")$/i", $link)
323         or preg_match("/\\.(" . INLINE_IMAGES . ")\s+(size|border|align|hspace|vspace|type|data|width|height|title|lang|id|alt)=/i", $link);
324 }
325
326 function LinkBracketLink($bracketlink) {
327
328     // $bracketlink will start and end with brackets; in between will
329     // be either a page name, a URL or both separated by a pipe.
330     
331    $wikicreolesyntax = false;
332
333    if (string_starts_with($bracketlink, "[[") or string_starts_with($bracketlink, "#[[")) {
334        $wikicreolesyntax = true;
335        $bracketlink = str_replace("[[", "[", $bracketlink);
336        $bracketlink = str_replace("]]", "]", $bracketlink);
337    }
338   
339     // Strip brackets and leading space
340     // bug#1904088  Some brackets links on 2 lines cause the parser to crash
341     preg_match('/(\#?) \[\s* (?: (.*?) \s* (?<!' . ESCAPE_CHAR . ')(\|) )? \s* (.+?) \s*\]/x',
342                str_replace("\n", " ", $bracketlink), $matches);
343     if (count($matches) < 4) {
344         // "[ personal\ninformation manager | PhpWiki:PersonalWiki ]"
345         trigger_error(_("Invalid [] syntax ignored").": ".$bracketlink, E_USER_WARNING);
346         return new Cached_Link;
347     }
348     list (, $hash, $label, $bar, $rawlink) = $matches;
349
350     if ($wikicreolesyntax and $label) {
351         $temp = $label;
352         $label = $rawlink;
353         $rawlink = $temp;
354     }
355
356     // Mediawiki compatibility: allow "Image:" and "File:"
357     // as synonyms of "Upload:"
358     if (string_starts_with($rawlink, "Image:")) {
359         $rawlink = str_replace("Image:", "Upload:", $rawlink);
360     }
361     if (string_starts_with($rawlink, "File:")) {
362         $rawlink = str_replace("File:", "Upload:", $rawlink);
363     }
364
365     $label = UnWikiEscape($label);
366     /*
367      * Check if the user has typed a explicit URL. This solves the
368      * problem where the URLs have a ~ character, which would be stripped away.
369      *   "[http:/server/~name/]" will work as expected
370      *   "http:/server/~name/"   will NOT work as expected, will remove the ~
371      */
372     if (   string_starts_with ($rawlink, "http://")
373         or string_starts_with ($rawlink, "https://") ) 
374     {
375         $link = $rawlink;
376         // Mozilla Browser URI Obfuscation Weakness 2004-06-14
377         //   http://www.securityfocus.com/bid/10532/
378         //   goodurl+"%2F%20%20%20."+badurl
379         if (preg_match("/%2F(%20)+\./i", $rawlink)) {
380             $rawlink = preg_replace("/%2F(%20)+\./i","%2F.",$rawlink);
381         }
382     } else
383         $link  = UnWikiEscape($rawlink);
384
385     /* Relatives links by Joel Schaubert.
386      * Recognize [../bla] or [/bla] as relative links, without needing http://
387      * but [ /link ] only if SUBPAGE_SEPERATOR is not "/". 
388      * Normally /Page links to the subpage /Page.
389      */
390     if (SUBPAGE_SEPARATOR == '/') {
391         if (preg_match('/^\.\.\//', $link)) {
392             return new Cached_ExternalLink($link, $label);
393         }
394     } else if (preg_match('/^(\.\.\/|\/)/', $link)) {
395         return new Cached_ExternalLink($link, $label);
396     }
397
398     // Handle "[[SandBox|{{image.jpg}}]]" and "[[SandBox|{{image.jpg|alt text}}]]"
399     if (string_starts_with($label, "{{")) {
400         $imgurl = substr($label, 2, -2); // Remove "{{" and "}}"
401         $pipe = strpos($imgurl, '|');
402         if ($pipe === false) {
403             $label = LinkImage(getUploadDataPath() . $imgurl, $link);
404         } else {
405             list($img, $alt) = explode("|", $imgurl);
406             $label = LinkImage(getUploadDataPath() . $img, $alt);
407         }
408     } else
409     
410     // [label|link]
411     // If label looks like a url to an image or object, we want an image link.
412     if (isImageLink($label)) {
413         $imgurl = $label;
414         $intermap = getInterwikiMap();
415         if (preg_match("/^" . $intermap->getRegexp() . ":/", $label)) {
416             $imgurl = $intermap->link($label);
417             $imgurl = $imgurl->getAttr('href');
418         } elseif (! preg_match("#^(" . ALLOWED_PROTOCOLS . "):#", $imgurl)) {
419             // local theme linkname like 'images/next.gif'.
420             global $WikiTheme;
421             $imgurl = $WikiTheme->getImageURL($imgurl);
422         }
423         // for objects (non-images) the link is taken as alt tag, 
424         // which is in return taken as alternative img
425         $label = LinkImage($imgurl, $link);
426     }
427
428     if ($hash) {
429         // It's an anchor, not a link...
430         $id = MangleXmlIdentifier($link);
431         return HTML::a(array('name' => $id, 'id' => $id),
432                        $bar ? $label : $link);
433     }
434
435     if (preg_match("#^(" . ALLOWED_PROTOCOLS . "):#", $link)) {
436         // if it's an image, embed it; otherwise, it's a regular link
437         if (isImageLink($link) and empty($label)) // patch #1348996 by Robert Litwiniec
438             return LinkImage($link, $label);
439         else
440             return new Cached_ExternalLink($link, $label);
441     }
442     elseif (substr($link,0,8) == 'phpwiki:')
443         return new Cached_PhpwikiURL($link, $label);
444
445     /* Semantic relations and attributes. 
446      * Relation and attribute names must be word chars only, no space.
447      * Links and Attributes may contain everything. word, nums, units, space, groupsep, numsep, ...
448      */
449     elseif (preg_match("/^ (\w+) (:[:=]) (.*) $/x", $link) and !isImageLink($link))
450         return new Cached_SemanticLink($link, $label);
451
452     /* Do not store the link */    
453     elseif (substr($link,0,1) == ':')
454         return new Cached_WikiLink($link, $label);
455
456     /*
457      * Inline images in Interwiki urls's:
458      * [File:my_image.gif] inlines the image,
459      * File:my_image.gif shows a plain inter-wiki link,
460      * [what a pic|File:my_image.gif] shows a named inter-wiki link to the gif
461      * [File:my_image.gif|what a pic] shows a inlimed image linked to the page "what a pic"
462      *
463      * Note that for simplicity we will accept embedded object tags (non-images) 
464      * here also, and seperate them later in LinkImage()
465      */
466     elseif (strstr($link,':')
467             and ($intermap = getInterwikiMap()) 
468             and preg_match("/^" . $intermap->getRegexp() . ":/", $link)) 
469     {
470         // trigger_error("label: $label link: $link", E_USER_WARNING);
471         if (empty($label) and isImageLink($link)) {
472             // if without label => inlined image [File:xx.gif]
473             $imgurl = $intermap->link($link);
474             return LinkImage($imgurl->getAttr('href'), $link);
475         }
476         return new Cached_InterwikiLink($link, $label);
477     } else {
478         // Split anchor off end of pagename.
479         if (preg_match('/\A(.*)(?<!'.ESCAPE_CHAR.')#(.*?)\Z/', $rawlink, $m)) {
480             list(,$rawlink,$anchor) = $m;
481             $pagename = UnWikiEscape($rawlink);
482             $anchor = UnWikiEscape($anchor);
483             if (!$label)
484                 $label = $link;
485         }
486         else {
487             $pagename = $link;
488             $anchor = false;
489         }
490         return new Cached_WikiLink($pagename, $label, $anchor);
491     }
492 }
493
494 class Markup_wikicreolebracketlink  extends SimpleMarkup
495 {
496     var $_match_regexp = "\\#? \\[\\[ .*? [^]\\s] .*? \\]\\]";
497
498     function markup ($match) {
499         $link = LinkBracketLink($match);
500         assert($link->isInlineElement());
501         return $link;
502     }
503 }
504
505 class Markup_bracketlink  extends SimpleMarkup
506 {
507     var $_match_regexp = "\\#? \\[ .*? [^]\\s] .*? \\]";
508     
509     function markup ($match) {
510         $link = LinkBracketLink($match);
511         assert($link->isInlineElement());
512         return $link;
513     }
514 }
515
516 class Markup_spellcheck extends SimpleMarkup
517 {
518     function Markup_spellcheck () {
519         $this->suggestions = $GLOBALS['request']->getArg('suggestions');
520     }
521     function getMatchRegexp () {
522         if (empty($this->suggestions))
523             return "(?# false )";
524         $words = array_keys($this->suggestions);
525         return "(?<= \W ) (?:" . join('|', $words) . ") (?= \W )";
526     }
527     
528     function markup ($match) {
529         if (empty($this->suggestions) or empty($this->suggestions[$match]))
530             return $match;
531         return new Cached_SpellCheck(UnWikiEscape($match), $this->suggestions[$match]);
532     }
533 }
534
535 class Markup_searchhighlight extends SimpleMarkup
536 {
537     function Markup_searchhighlight () {
538         $result = $GLOBALS['request']->_searchhighlight;
539         require_once("lib/TextSearchQuery.php");
540         $query = new TextSearchQuery($result['query']);
541         $this->hilight_re = $query->getHighlightRegexp();
542         $this->engine = $result['engine'];
543     }
544     function getMatchRegexp () {
545         return $this->hilight_re;
546     }
547     function markup ($match) {
548         return new Cached_SearchHighlight(UnWikiEscape($match), $this->engine);
549     }
550 }
551
552 class Markup_url extends SimpleMarkup
553 {
554     function getMatchRegexp () {
555         return "(?<![[:alnum:]]) (?:" . ALLOWED_PROTOCOLS . ") : [^\s<>\"']+ (?<![ ,.?; \] \) ])";
556     }
557     
558     function markup ($match) {
559         return new Cached_ExternalLink(UnWikiEscape($match));
560     }
561 }
562
563 class Markup_interwiki extends SimpleMarkup
564 {
565     function getMatchRegexp () {
566         $map = getInterwikiMap();
567         return "(?<! [[:alnum:]])" . $map->getRegexp(). ": [^:=]\S+ (?<![ ,.?;! \] \) \" \' ])";
568     }
569
570     function markup ($match) {
571         return new Cached_InterwikiLink(UnWikiEscape($match));
572     }
573 }
574
575 class Markup_semanticlink extends SimpleMarkup
576 {
577     // No units seperated by space allowed here
578     // For :: (relations) only words, no comma,
579     // but for := (attributes) comma and dots are allowed. Units with groupsep.
580     // Ending dots or comma are not part of the link.
581     var $_match_regexp = "(?: \w+:=\S+(?<![\.,]))|(?: \w+::[\w\.]+(?<!\.))"; 
582
583     function markup ($match) {
584         return new Cached_SemanticLink(UnWikiEscape($match));
585     }
586 }
587
588 class Markup_wikiword extends SimpleMarkup
589 {
590     function getMatchRegexp () {
591         global $WikiNameRegexp;
592         if (!trim($WikiNameRegexp)) return " " . WIKI_NAME_REGEXP;
593         return " $WikiNameRegexp";
594     }
595
596     function markup ($match) {
597         if (!$match) return false;
598         if ($this->_isWikiUserPage($match))
599             return new Cached_UserLink($match); //$this->_UserLink($match);
600         else
601             return new Cached_WikiLink($match);
602     }
603
604     // FIXME: there's probably a more useful place to put these two functions    
605     function _isWikiUserPage ($page) {
606         global $request;
607         $dbi = $request->getDbh();
608         $page_handle = $dbi->getPage($page);
609         if ($page_handle and $page_handle->get('pref'))
610             return true;
611         else
612             return false;
613     }
614
615     function _UserLink($PageName) {
616         $link = HTML::a(array('href' => $PageName));
617         $link->pushContent(PossiblyGlueIconToText('wikiuser', $PageName));
618         $link->setAttr('class', 'wikiuser');
619         return $link;
620     }
621 }
622
623 class Markup_linebreak extends SimpleMarkup
624 {
625     //var $_match_regexp = "(?: (?<! %) %%% (?! %) | <(?:br|BR)> | <(?:br|BR) \/> )";
626     var $_match_regexp = "(?: (?<! %) %%% (?! %) | \\\\\\\\ | <(?:br|BR)> )";
627
628     function markup ($match) {
629         return HTML::br();
630     }
631 }
632
633 class Markup_wikicreole_italics extends BalancedMarkup
634 {
635     var $_start_regexp = "\\/\\/";
636  
637     function getEndRegexp ($match) {
638         return "\\/\\/"; 
639     }
640    
641     function markup ($match, $body) {
642         $tag = 'em';
643         return new HtmlElement($tag, $body);
644     }
645 }
646
647 class Markup_wikicreole_bold extends BalancedMarkup
648 {
649     var $_start_regexp = "\\*\\*";
650  
651     function getEndRegexp ($match) {
652         return "\\*\\*"; 
653     }
654    
655     function markup ($match, $body) {
656         $tag = 'strong';
657         return new HtmlElement($tag, $body);
658     }
659 }
660
661 class Markup_wikicreole_monospace extends BalancedMarkup
662 {
663     var $_start_regexp = "\\#\\#";
664  
665     function getEndRegexp ($match) {
666         return "\\#\\#"; 
667     }
668    
669     function markup ($match, $body) {
670         $tag = 'tt';
671         return new HtmlElement($tag, $body);
672     }
673 }
674
675 class Markup_wikicreole_superscript extends BalancedMarkup
676 {
677     var $_start_regexp = "\\^\\^";
678  
679     function getEndRegexp ($match) {
680         return "\\^\\^"; 
681     }
682    
683     function markup ($match, $body) {
684         $tag = 'sup';
685         return new HtmlElement($tag, $body);
686     }
687 }
688  
689 class Markup_wikicreole_subscript extends BalancedMarkup
690 {
691     var $_start_regexp = ",,";
692  
693     function getEndRegexp ($match) {
694         return $match; 
695     }
696    
697     function markup ($match, $body) {
698         $tag = 'sub';
699         return new HtmlElement($tag, $body);
700     }
701 }
702
703 class Markup_old_emphasis  extends BalancedMarkup
704 {
705     var $_start_regexp = "''|__";
706
707     function getEndRegexp ($match) {
708         return $match;
709     }
710     
711     function markup ($match, $body) {
712         $tag = $match == "''" ? 'em' : 'strong';
713         return new HtmlElement($tag, $body);
714     }
715 }
716
717 class Markup_nestled_emphasis extends BalancedMarkup
718 {
719     function getStartRegexp() {
720         static $start_regexp = false;
721
722         if (!$start_regexp) {
723             // The three possible delimiters
724             // (none of which can be followed by itself.)
725             $i = "_ (?! _)";
726             $b = "\\* (?! \\*)";
727             $tt = "= (?! =)";
728
729             $any = "(?: ${i}|${b}|${tt})"; // any of the three.
730
731             // Any of [_*=] is okay if preceded by space or one of [-"'/:]
732             $start[] = "(?<= \\s|^|[-\"'\\/:]) ${any}";
733
734             // _ or * is okay after = as long as not immediately followed by =
735             $start[] = "(?<= =) (?: ${i}|${b}) (?! =)";
736             // etc...
737             $start[] = "(?<= _) (?: ${b}|${tt}) (?! _)";
738             $start[] = "(?<= \\*) (?: ${i}|${tt}) (?! \\*)";
739
740
741             // any delimiter okay after an opening brace ( [{<(] )
742             // as long as it's not immediately followed by the matching closing
743             // brace.
744             $start[] = "(?<= { ) ${any} (?! } )";
745             $start[] = "(?<= < ) ${any} (?! > )";
746             $start[] = "(?<= \\( ) ${any} (?! \\) )";
747             
748             $start = "(?:" . join('|', $start) . ")";
749             
750             // Any of the above must be immediately followed by non-whitespace.
751             $start_regexp = $start . "(?= \S)";
752         }
753
754         return $start_regexp;
755     }
756
757     function getEndRegexp ($match) {
758         $chr = preg_quote($match);
759         return "(?<= \S | ^ ) (?<! $chr) $chr (?! $chr) (?= \s | [-)}>\"'\\/:.,;!? _*=] | $)";
760     }
761     
762     function markup ($match, $body) {
763         switch ($match) {
764         case '*': return new HtmlElement('b', $body);
765         case '=': return new HtmlElement('tt', $body);
766         case '_': return new HtmlElement('i', $body);
767         }
768     }
769 }
770
771 class Markup_html_emphasis extends BalancedMarkup
772 {
773     var $_start_regexp = 
774         "<(?: b|big|i|small|tt|em|strong|cite|code|dfn|kbd|samp|s|strike|del|var|sup|sub )>";
775
776     function getEndRegexp ($match) {
777         return "<\\/" . substr($match, 1);
778     }
779     
780     function markup ($match, $body) {
781         $tag = substr($match, 1, -1);
782         return new HtmlElement($tag, $body);
783     }
784 }
785
786 class Markup_html_divspan extends BalancedMarkup
787 {
788     var $_start_regexp = 
789         "<(?: div|span )(?: \s[^>]*)?>";
790
791     function getEndRegexp ($match) {
792         if (substr($match,1,4) == 'span')
793             $tag = 'span';
794         else
795             $tag = 'div';
796         return "<\\/" . $tag . '>';
797     }
798     
799     function markup ($match, $body) {
800         if (substr($match,1,4) == 'span')
801             $tag = 'span';
802         else
803             $tag = 'div';
804         $rest = substr($match,1+strlen($tag),-1);
805         if (!empty($rest)) {
806             $args = parse_attributes($rest);
807         } else {
808             $args = array();
809         }
810         return new HtmlElement($tag, $args, $body);
811     }
812 }
813
814
815 class Markup_html_abbr extends BalancedMarkup
816 {
817     //rurban: abbr|acronym need an optional title tag.
818     //sf.net bug #728595
819     // allowed attributes: title and lang
820     var $_start_regexp = "<(?: abbr|acronym )(?: [^>]*)?>"; 
821
822     function getEndRegexp ($match) {
823         if (substr($match,1,4) == 'abbr')
824             $tag = 'abbr';
825         else
826             $tag = 'acronym';
827         return "<\\/" . $tag . '>';
828     }
829     
830     function markup ($match, $body) {
831         if (substr($match,1,4) == 'abbr')
832             $tag = 'abbr';
833         else
834             $tag = 'acronym';
835         $rest = substr($match,1+strlen($tag),-1);
836         $attrs = parse_attributes($rest);
837         // Remove attributes other than title and lang
838         $allowedargs = array();
839         foreach ($attrs as $key => $value) {
840             if (in_array ($key, array("title", "lang"))) {
841                 $allowedargs[$key] = $value;
842             }
843         }
844         return new HtmlElement($tag, $allowedargs, $body);
845     }
846 }
847
848 /** ENABLE_MARKUP_COLOR
849  *  See http://www.pmwiki.org/wiki/PmWiki/WikiStyles and
850  *      http://www.flexwiki.com/default.aspx/FlexWiki/FormattingRules.html
851  */
852 class Markup_color extends BalancedMarkup {
853     // %color=blue% blue text %% and back to normal
854     var $_start_regexp = "%color=(?: [^%]*)%";
855     var $_end_regexp = "%%";
856     
857     function markup ($match, $body) {
858         $color = strtolower(substr($match, 7, -1));
859
860         $morecolors = array('beige' => '#f5f5dc',
861                             'brown' => '#a52a2a',
862                             'chocolate' => '#d2691e',
863                             'cyan' => '#00ffff',
864                             'gold' => '#ffd700',
865                             'ivory' => '#fffff0',
866                             'indigo' => '#4b0082',
867                             'magenta' => '#ff00ff',
868                             'orange' => '#ffa500',
869                             'pink' => '#ffc0cb',
870                             'salmon' => '#fa8072',
871                             'snow' => '#fffafa',
872                             'turquoise' => '#40e0d0',
873                             'violet' => '#ee82ee',
874                            );
875
876         if (isset($morecolors[$color])) {
877             $color = $morecolors[$color];
878         }
879
880         // HTML 4 defines the following 16 colors
881         if (in_array($color, array('aqua', 'black', 'blue', 'fuchsia', 
882                                    'gray', 'green', 'lime', 'maroon',
883                                    'navy', 'olive', 'purple', 'red',
884                                    'silver', 'teal', 'white', 'yellow'))
885               or ((substr($color,0,1) == '#') 
886                   and ((strlen($color) == 4) or (strlen($color) == 7))
887                   and (strspn(substr($color,1),'0123456789abcdef') == strlen($color)-1))) {
888             return new HtmlElement('span', array('style' => "color: $color"), $body);
889         } else {
890             return new HtmlElement('span', array('class' => 'error'), 
891                                    sprintf(_("unknown color %s ignored"), substr($match, 7, -1)));
892         }
893     }
894 }
895
896 // Wikicreole placeholder
897 // <<<placeholder>>>
898 class Markup_placeholder extends SimpleMarkup
899 {
900     var $_match_regexp = '<<<.*?>>>';
901
902     function markup ($match) {
903         return HTML::span($match);
904     }
905 }
906
907 // Single-line HTML comment
908 // <!-- This is a comment -->
909 class Markup_html_comment extends SimpleMarkup
910 {
911     var $_match_regexp = '<!--.*?-->';
912
913     function markup ($match) {
914         return HTML::raw('');
915     }
916 }
917
918 // Special version for single-line plugins formatting, 
919 //  like: '<small>< ?plugin PopularNearby ? ></small>'
920 class Markup_plugin extends SimpleMarkup
921 {
922     var $_match_regexp = '<\?plugin(?:-form)?\s[^\n]+?\?>';
923
924     function markup ($match) {
925         return new Cached_PluginInvocation($match);
926     }
927 }
928
929 // Special version for single-line Wikicreole plugins formatting.
930 class Markup_plugin_wikicreole extends SimpleMarkup
931 {
932     var $_match_regexp = '<<[^\n]+?>>';
933
934     function markup ($match) {
935         $pi = str_replace("<<", "<?plugin ", $match);
936         $pi = str_replace(">>", " ?>", $pi);
937         return new Cached_PluginInvocation($pi);
938     }
939 }
940
941 // Special version for plugins in xml syntax 
942 // <name arg=value>body</name> or <name /> => < ? plugin pluginname arg=value body ? >
943 // PLUGIN_MARKUP_MAP = "html:RawHtml dot:GraphViz toc:CreateToc amath:AsciiMath richtable:RichTable include:IncludePage tex:TexToPng"
944 class Markup_xml_plugin extends BalancedMarkup
945 {
946     //var $_start_regexp = "<(?: ".join('|',PLUGIN_MARKUP_MAP)." )(?: \s[^>]*)>";
947
948     function getStartRegexp () {
949         global $PLUGIN_MARKUP_MAP;
950         static $_start_regexp;
951         if ($_start_regexp) return $_start_regexp;
952         if (empty($PLUGIN_MARKUP_MAP))
953             return '';
954         //"<(?: html|dot|toc|amath|richtable|include|tex )(?: \s[^>]*)>"
955         $_start_regexp = "<(?: ".join('|',array_keys($PLUGIN_MARKUP_MAP))." )(?: \s[^>]* | / )>";
956         return $_start_regexp;
957     }
958     function getEndRegexp ($match) {
959         return "<\\/" . $match . '>';
960     }
961     function markup ($match, $body) {
962         global $PLUGIN_MARKUP_MAP;
963         $name = substr($match,2,-2); 
964         $vars = '';
965         if (preg_match('/^(\S+)\|(.*)$/', $name, $_m)) {
966             $name = $_m[1];
967             $vars = $_m[2]; //str_replace(' ', '&', $_m[2]);
968         }
969         if (!isset($PLUGIN_MARKUP_MAP[$name])) {
970             trigger_error("No plugin for $name $vars defined.", E_USER_WARNING);
971             return "";
972         }
973         $plugin = $PLUGIN_MARKUP_MAP[$name];
974         return new Cached_PluginInvocation("<"."?plugin $plugin $vars $body ?".">");
975     }
976 }
977
978 /**
979  *  Mediawiki <nowiki>
980  *  <nowiki>...</nowiki>
981  */
982 class Markup_nowiki extends SimpleMarkup
983 {
984     var $_match_regexp = '<nowiki>.*?<\/nowiki>';
985
986     function markup ($match) {
987         // Remove <nowiki> and </nowiki>
988         return HTML::raw(substr($match, 8, -9));
989     }
990 }
991
992 /**
993  *  Wikicreole preformatted
994  *  {{{
995  *  }}}
996  */
997 class Markup_wikicreole_preformatted extends SimpleMarkup
998 {
999     var $_match_regexp = '\{\{\{.*?\}\}\}';
1000
1001     function markup ($match) {
1002         // Remove {{{ and }}}
1003         return new HtmlElement('tt', substr($match, 3, -3));
1004     }
1005 }
1006
1007 /** ENABLE_MARKUP_TEMPLATE
1008  *  Template syntax similar to Mediawiki
1009  *  {{template}}
1010  * => < ? plugin Template page=template ? >
1011  *  {{template|var1=value1|var2=value|...}}
1012  * => < ? plugin Template page=template var=value ... ? >
1013  */
1014 class Markup_template_plugin  extends SimpleMarkup
1015 {
1016     // patch #1732793: allow \n, mult. {{ }} in one line, and single letters
1017     var $_match_regexp = '\{\{.*?\}\}';
1018     
1019     function markup ($match) {
1020
1021         $page = substr($match,2,-2);
1022
1023         // Check for predefined icons.
1024         $predefinedicons = array(":)" => "ic_smile.png",
1025                                  ":(" => "ic_sad.png",
1026                                  ":P" => "ic_tongue.png",
1027                                  ":D" => "ic_biggrin.png",
1028                                  ";)" => "ic_wink.png",
1029                                  "(y)" => "ic_handyes.png",
1030                                  "(n)" => "ic_handno.png",
1031                                  "(i)" => "ic_info.png",
1032                                  "(/)" => "ic_check.png",
1033                                  "(x)" => "ic_cross.png",
1034                                  "(!)" => "ic_danger.png",
1035                                  "(+)" => "ic_plus.png",
1036                                  "(-)" => "ic_minus.png",
1037                                  "(?)" => "ic_help.png",
1038                                  "(on)" => "ic_lighton.png",
1039                                  "(off)" => "ic_lightoff.png",
1040                                  "(*)" => "ic_yellowstar.png",
1041                                  "(*r)" => "ic_redstar.png",
1042                                  "(*g)" => "ic_greenstar.png",
1043                                  "(*b)" => "ic_bluestar.png",
1044                                  "(*y)" => "ic_yellowstar.png",
1045                                 );
1046         foreach ($predefinedicons as $ascii => $icon) {
1047             if (trim($page) == $ascii) {
1048                 return LinkImage("/wiki/themes/default/images/$icon", $page);
1049             }
1050         }
1051
1052         if (strpos($page, "|") === false) {
1053             $imagename = $page;
1054             $alt = "";
1055         } else {
1056             $imagename = substr($page, 0, strpos($page, "|"));
1057             $alt = ltrim(strstr($page, "|"), "|");
1058         }
1059
1060         // It's not a Mediawiki template, it's a Wikicreole image
1061         if (is_image($imagename)) {
1062             if ($imagename[0] == '/') {
1063                 // We should not hardcode "/phpwiki"
1064                 return LinkImage(SERVER_URL . "/phpwiki" . $imagename, $alt);
1065             } else {
1066                 return LinkImage(getUploadDataPath() . $imagename, $alt);
1067             }
1068         }
1069
1070         $page = str_replace("\n", "", $page); 
1071         $vars = '';
1072
1073         if (preg_match('/^(\S+?)\|(.*)$/', $page, $_m)) {
1074             $page = $_m[1];
1075             $vars = '"' . preg_replace('/\|/', '" "', $_m[2]) . '"'; 
1076             $vars = preg_replace('/"(\S+)=([^"]*)"/', '\\1="\\2"', $vars);
1077         }
1078  
1079         // page may contain a version number
1080         // {{foo?version=5}}
1081         // in that case, output is "page=foo rev=5"
1082         if (strstr($page, "?")) {
1083             $page = str_replace("?version=", "\" rev=\"", $page);
1084         }
1085
1086         if ($vars)
1087             $s = '<'.'?plugin Template page="'.$page.'" '.$vars.' ?'.'>';
1088         else
1089             $s = '<'.'?plugin Template page="' . $page . '" ?'.'>';
1090         return new Cached_PluginInvocation($s);
1091     }
1092 }
1093
1094 // "..." => "&#133;"  browser specific display (not cached?)
1095 // Support some HTML::Entities: (C) for copy, --- for mdash, -- for ndash
1096 // TODO: "--" => "&emdash;" browser specific display (not cached?)
1097
1098 class Markup_html_entities  extends SimpleMarkup {
1099     //var $_match_regexp = '(: \.\.\.|\-\-|\-\-\-|\(C\) )';
1100
1101     function Markup_html_entities() {
1102         $this->_entities = array('...'  => '&#133;',
1103                                  '--'   => '&ndash;',
1104                                  '---'  => '&mdash;',
1105                                  '(C)'  => '&copy;',
1106                                  '&copy;' => '&copy;',
1107                                  '&trade;'  => '&trade;',
1108                                  );
1109         $this->_match_regexp = 
1110             '(: ' . 
1111             join('|', array_map('preg_quote', array_keys($this->_entities))) . 
1112             ' )';
1113     }
1114    
1115     function markup ($match) {
1116         return HTML::Raw($this->_entities[$match]);
1117     }
1118 }
1119
1120 class Markup_isonumchars  extends SimpleMarkup {
1121     var $_match_regexp = '\&\#\d{2,5};';
1122     
1123     function markup ($match) {
1124         return HTML::Raw($match);
1125     }
1126 }
1127
1128 class Markup_isohexchars extends SimpleMarkup {
1129     // hexnums, like &#x00A4; <=> &curren;
1130     var $_match_regexp = '\&\#x[0-9a-fA-F]{2,4};';
1131     
1132     function markup ($match) {
1133         return HTML::Raw($match);
1134     }
1135 }
1136
1137 // FIXME: Do away with magic phpwiki forms.  (Maybe phpwiki: links too?)
1138 // FIXME: Do away with plugin-links.  They seem not to be used.
1139 //Plugin link
1140
1141 class InlineTransformer
1142 {
1143     var $_regexps = array();
1144     var $_markup = array();
1145     
1146     function InlineTransformer ($markup_types = false) {
1147         global $request;
1148         // We need to extend the inline parsers by certain actions, like SearchHighlight, 
1149         // SpellCheck and maybe CreateToc.
1150         if (!$markup_types) {
1151             $non_default = false;
1152             $markup_types = array
1153                 ('escape', 'wikicreolebracketlink', 'bracketlink', 'url',
1154                  'html_comment', 'placeholder',
1155                  'interwiki',  'semanticlink', 'wikiword', 'linebreak',
1156                  'wikicreole_superscript',
1157                  'wikicreole_subscript',
1158                  'wikicreole_italics', 'wikicreole_bold',
1159                  'wikicreole_monospace', 
1160                  'old_emphasis', 'nestled_emphasis',
1161                  'html_emphasis', 'html_abbr', 'plugin', 'plugin_wikicreole',
1162                  'isonumchars', 'isohexchars', /*'html_entities'*/
1163                  );
1164             if (DISABLE_MARKUP_WIKIWORD)
1165                 $markup_types = array_remove($markup_types, 'wikiword');
1166
1167             $action = $request->getArg('action');
1168             if ($action == 'SpellCheck' and $request->getArg('suggestions'))
1169             {   // insert it after url
1170                 array_splice($markup_types, 2, 1, array('url','spellcheck'));
1171             }
1172             if (isset($request->_searchhighlight))
1173             {   // insert it after url
1174                 array_splice($markup_types, 2, 1, array('url','searchhighlight'));
1175                 //$request->setArg('searchhighlight', false);
1176             }
1177         } else {
1178             $non_default = true;
1179         }
1180         foreach ($markup_types as $mtype) {
1181             $class = "Markup_$mtype";
1182             $this->_addMarkup(new $class);
1183         }
1184         $this->_addMarkup(new Markup_nowiki);
1185         if (ENABLE_MARKUP_DIVSPAN and !$non_default)
1186             $this->_addMarkup(new Markup_html_divspan);
1187         if (ENABLE_MARKUP_COLOR and !$non_default)
1188             $this->_addMarkup(new Markup_color);
1189         // Markup_wikicreole_preformatted must be before Markup_template_plugin
1190         $this->_addMarkup(new Markup_wikicreole_preformatted);
1191         if (ENABLE_MARKUP_TEMPLATE and !$non_default)
1192             $this->_addMarkup(new Markup_template_plugin);
1193         // This does not work yet
1194         if (0 and PLUGIN_MARKUP_MAP and !$non_default)
1195             $this->_addMarkup(new Markup_xml_plugin);
1196     }
1197
1198     function _addMarkup ($markup) {
1199         if (isa($markup, 'SimpleMarkup'))
1200             $regexp = $markup->getMatchRegexp();
1201         else
1202             $regexp = $markup->getStartRegexp();
1203
1204         assert( !isset($this->_markup[$regexp]) );
1205         assert( strlen(trim($regexp)) > 0 );
1206         $this->_regexps[] = $regexp;
1207         $this->_markup[] = $markup;
1208     }
1209         
1210     function parse (&$text, $end_regexps = array('$')) {
1211         $regexps = $this->_regexps;
1212
1213         // $end_re takes precedence: "favor reduce over shift"
1214         array_unshift($regexps, $end_regexps[0]);
1215         //array_push($regexps, $end_regexps[0]);
1216         $regexps = new RegexpSet($regexps);
1217         
1218         $input = $text;
1219         $output = new XmlContent;
1220
1221         $match = $regexps->match($input);
1222         
1223         while ($match) {
1224             if ($match->regexp_ind == 0) {
1225                 // No start pattern found before end pattern.
1226                 // We're all done!
1227                 if (isset($markup) and is_object($markup) 
1228                     and isa($markup,'Markup_plugin')) 
1229                 {
1230                     $current =& $output->_content[count($output->_content)-1];
1231                     $current->setTightness(true,true);
1232                 }
1233                 $output->pushContent($match->prematch);
1234                 $text = $match->postmatch;
1235                 return $output;
1236             }
1237
1238             $markup = $this->_markup[$match->regexp_ind - 1];
1239             $body = $this->_parse_markup_body($markup, $match->match, 
1240                                               $match->postmatch, $end_regexps);
1241             if (!$body) {
1242                 // Couldn't match balanced expression.
1243                 // Ignore and look for next matching start regexp.
1244                 $match = $regexps->nextMatch($input, $match);
1245                 continue;
1246             }
1247
1248             // Matched markup.  Eat input, push output.
1249             // FIXME: combine adjacent strings.
1250             if (isa($markup, 'SimpleMarkup'))
1251                 $current = $markup->markup($match->match);
1252             else
1253                 $current = $markup->markup($match->match, $body);
1254             $input = $match->postmatch;
1255             if (isset($markup) and is_object($markup) 
1256                 and isa($markup,'Markup_plugin')) 
1257             {
1258                 $current->setTightness(true,true);
1259             }
1260             $output->pushContent($match->prematch, $current);
1261
1262             $match = $regexps->match($input);
1263         }
1264
1265         // No pattern matched, not even the end pattern.
1266         // Parse fails.
1267         return false;
1268     }
1269
1270     function _parse_markup_body ($markup, $match, &$text, $end_regexps) {
1271         if (isa($markup, 'SimpleMarkup'))
1272             return true;        // Done. SimpleMarkup is simple.
1273
1274         if (!is_object($markup)) return false; // Some error: Should assert
1275         array_unshift($end_regexps, $markup->getEndRegexp($match));
1276
1277         // Optimization: if no end pattern in text, we know the
1278         // parse will fail.  This is an important optimization,
1279         // e.g. when text is "*lots *of *start *delims *with
1280         // *no *matching *end *delims".
1281         $ends_pat = "/(?:" . join(").*(?:", $end_regexps) . ")/xs";
1282         if (!preg_match($ends_pat, $text))
1283             return false;
1284         return $this->parse($text, $end_regexps);
1285     }
1286 }
1287
1288 class LinkTransformer extends InlineTransformer
1289 {
1290     function LinkTransformer () {
1291         $this->InlineTransformer(array('escape', 'wikicreolebracketlink', 'bracketlink', 'url',
1292                                        'semanticlink', 'interwiki', 'wikiword', 
1293                                        ));
1294     }
1295 }
1296
1297 class NowikiTransformer extends InlineTransformer
1298 {
1299     function NowikiTransformer () {
1300         $this->InlineTransformer
1301             (array('linebreak',
1302                    'html_emphasis', 'html_abbr', 'plugin', 'plugin_wikicreole',
1303                    'isonumchars', 'isohexchars', /*'html_entities',*/
1304                    ));
1305     }
1306 }
1307
1308 function TransformInline($text, $markup = 2.0, $basepage=false) {
1309     static $trfm;
1310     $action = $GLOBALS['request']->getArg('action');
1311     if (empty($trfm) or $action == 'SpellCheck') {
1312         $trfm = new InlineTransformer;
1313     }
1314     
1315     if ($markup < 2.0) {
1316         $text = ConvertOldMarkup($text, 'inline');
1317     }
1318
1319     if ($basepage) {
1320         return new CacheableMarkup($trfm->parse($text), $basepage);
1321     }
1322     return $trfm->parse($text);
1323 }
1324
1325 function TransformLinks($text, $markup = 2.0, $basepage = false) {
1326     static $trfm;
1327     
1328     if (empty($trfm)) {
1329         $trfm = new LinkTransformer;
1330     }
1331
1332     if ($markup < 2.0) {
1333         $text = ConvertOldMarkup($text, 'links');
1334     }
1335     
1336     if ($basepage) {
1337         return new CacheableMarkup($trfm->parse($text), $basepage);
1338     }
1339     return $trfm->parse($text);
1340 }
1341
1342 /**
1343  * Transform only html markup and entities.
1344  */
1345 function TransformInlineNowiki($text, $markup = 2.0, $basepage=false) {
1346     static $trfm;
1347     
1348     if (empty($trfm)) {
1349         $trfm = new NowikiTransformer;
1350     }
1351     if ($basepage) {
1352         return new CacheableMarkup($trfm->parse($text), $basepage);
1353     }
1354     return $trfm->parse($text);
1355 }
1356
1357 // (c-file-style: "gnu")
1358 // Local Variables:
1359 // mode: php
1360 // tab-width: 8
1361 // c-basic-offset: 4
1362 // c-hanging-comment-ender-p: nil
1363 // indent-tabs-mode: nil
1364 // End:   
1365 ?>