PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/libs/clearbricks/text.wiki2xhtml/class.wiki2xhtml.php

https://bitbucket.org/dotclear/dotclear/
PHP | 1305 lines | 923 code | 179 blank | 203 comment | 208 complexity | cd276c4686d956fea1fd9a56096726ba MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0
  1. <?php
  2. ###########################################################
  3. # /!\ /!\ /!\ EDIT THIS FILE IN UTF8 /!\ /!\ /!\ #
  4. ###########################################################
  5. # ***** BEGIN LICENSE BLOCK *****
  6. # This file is part of Clearbricks.
  7. # Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear
  8. # All rights reserved.
  9. #
  10. # Clearbricks is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # Clearbricks is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with Clearbricks; if not, write to the Free Software
  22. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. #
  24. # ***** END LICENSE BLOCK *****
  25. #
  26. # Contributor(s):
  27. # Stephanie Booth
  28. # Mathieu Pillard
  29. # Christophe Bonijol
  30. # Jean-Charles Bagneris
  31. # Nicolas Chachereau
  32. # Jérôme Lipowicz
  33. #
  34. # Version : 3.2.6
  35. # Release date : 2008-07-16
  36. # History :
  37. #
  38. # 3.2.6
  39. # => Added ``inline html`` support
  40. #
  41. # 3.2.5
  42. # => Changed longdesc by title in images
  43. #
  44. # 3.2.4
  45. # => Auto links
  46. # => Code cleanup
  47. #
  48. # 3.2.3
  49. # Olivier
  50. # => PHP5 Strict
  51. #
  52. # 3.2.2
  53. # Olivier
  54. # => Changement de la gestion des URL spéciales
  55. #
  56. # 3.2.1
  57. # Olivier
  58. # => Changement syntaxe des macros
  59. #
  60. # 3.2
  61. # Olivier
  62. # => Changement de fonctionnement des macros
  63. # => Passage de fonctions externes pour les macros et les mots wiki
  64. #
  65. # 3.1d
  66. # Jérôme Lipowicz
  67. # => antispam
  68. # Olivier
  69. # => centrage d'image
  70. #
  71. # 3.1c
  72. # Olivier
  73. # => Possibilité d'échaper les | dans les marqueurs avec \
  74. #
  75. # 3.1b
  76. # Nicolas Chachereau
  77. # => Changement de regexp pour la correction syntaxique
  78. #
  79. # 3.1a
  80. # Olivier
  81. # => Bug du Call-time pass-by-reference
  82. #
  83. # 3.1
  84. # Olivier
  85. # => Ajout des macros ŤŤŤ..ťťť
  86. # => Ajout des blocs vides řřř
  87. # => Ajout du niveau de titre paramétrable
  88. # => Option de blocage du parseur dans les <pre>
  89. # => Titres au format setext (experimental, désactivé)
  90. #
  91. # 3.0
  92. # Olivier => Récriture du parseur inline, plus d'erreur XHTML
  93. # => Ajout d'une vérification d'intégrité pour les listes
  94. # => Les acronymes sont maintenant dans un fichier texte
  95. # => Ajout d'un tag images ((..)), del --..-- et ins ++..++
  96. # => Plus possible de faire des liens JS [lien|javascript:...]
  97. # => Ajout des notes de bas de page §§...§§
  98. # => Ajout des mots wiki
  99. #
  100. # 2.5
  101. # Olivier => Récriture du code, plus besoin du saut de ligne entre blocs !=
  102. #
  103. # 2.0
  104. # Stephanie => correction des PCRE et ajout de fonctionnalités
  105. # Mathieu => ajout du strip-tags, implementation des options, reconnaissance automatique d'url, etc.
  106. # Olivier => chagement de active_link en active_urls
  107. # => ajout des options pour les blocs
  108. # => intégration de l'aide dans le code, avec les options
  109. # => début de quelque chose pour la reconnaissance auto d'url (avec Mat)
  110. # TODO :
  111. # Mathieu => active_wiki_urls (modifier wikiParseUrl ?)
  112. # => active_auto_urls
  113. #
  114. # * => ajouter des options.
  115. # => trouver un meilleur nom pour active_link ? (pour le jour ou ca sera tellement une usine
  116. # a gaz que on generera des tags <link> :)
  117. #
  118. # Wiki2xhtml
  119. class wiki2xhtml
  120. {
  121. public $__version__ = '3.2.6';
  122. public $T;
  123. public $opt;
  124. public $line;
  125. public $acro_table;
  126. public $foot_notes;
  127. public $macros;
  128. public $functions;
  129. public $tags;
  130. public $open_tags;
  131. public $close_tags;
  132. public $custom_tags = array();
  133. public $all_tags;
  134. public $tag_pattern;
  135. public $escape_table;
  136. public $allowed_inline = array();
  137. function __construct()
  138. {
  139. # Mise en place des options
  140. $this->setOpt('active_title',1); # Activation des titres !!!
  141. $this->setOpt('active_setext_title',0); # Activation des titres setext (EXPERIMENTAL)
  142. $this->setOpt('active_hr',1); # Activation des <hr />
  143. $this->setOpt('active_lists',1); # Activation des listes
  144. $this->setOpt('active_quote',1); # Activation du <blockquote>
  145. $this->setOpt('active_pre',1); # Activation du <pre>
  146. $this->setOpt('active_empty',1); # Activation du bloc vide řřř
  147. $this->setOpt('active_auto_urls',0); # Activation de la reconnaissance d'url
  148. $this->setOpt('active_auto_br',0); # Activation du saut de ligne automatique (dans les paragraphes)
  149. $this->setOpt('active_antispam',1); # Activation de l'antispam pour les emails
  150. $this->setOpt('active_urls',1); # Activation des liens []
  151. $this->setOpt('active_auto_img',1); # Activation des images automatiques dans les liens []
  152. $this->setOpt('active_img',1); # Activation des images (())
  153. $this->setOpt('active_anchor',1); # Activation des ancres ~...~
  154. $this->setOpt('active_em',1); # Activation du <em> ''...''
  155. $this->setOpt('active_strong',1); # Activation du <strong> __...__
  156. $this->setOpt('active_br',1); # Activation du <br /> %%%
  157. $this->setOpt('active_q',1); # Activation du <q> {{...}}
  158. $this->setOpt('active_code',1); # Activation du <code> @@...@@
  159. $this->setOpt('active_acronym',1); # Activation des acronymes
  160. $this->setOpt('active_ins',1); # Activation des ins ++..++
  161. $this->setOpt('active_del',1); # Activation des del --..--
  162. $this->setOpt('active_inline_html',1); # Activation du HTML inline ``...``
  163. $this->setOpt('active_footnotes',1); # Activation des notes de bas de page
  164. $this->setOpt('active_wikiwords',0); # Activation des mots wiki
  165. $this->setOpt('active_macros',1); # Activation des macros /// ///
  166. $this->setOpt('parse_pre',1); # Parser l'intérieur de blocs <pre> ?
  167. $this->setOpt('active_fr_syntax',1); # Corrections syntaxe FR
  168. $this->setOpt('first_title_level',3); # Premier niveau de titre <h..>
  169. $this->setOpt('note_prefix','wiki-footnote');
  170. $this->setOpt('note_str','<div class="footnotes"><h4>Notes</h4>%s</div>');
  171. $this->setOpt('note_str_single','<div class="footnotes"><h4>Note</h4>%s</div>');
  172. $this->setOpt('words_pattern',
  173. '((?<![A-Za-z0-9])([A-Z][a-z]+){2,}(?![A-Za-z0-9]))');
  174. $this->setOpt('auto_url_pattern',
  175. '%(?<![\[\|])(http://|https://|ftp://|news:)([^"\s\)!]+)%msu');
  176. $this->setOpt('acronyms_file',dirname(__FILE__).'/acronyms.txt');
  177. $this->acro_table = $this->__getAcronyms();
  178. $this->foot_notes = array();
  179. $this->functions = array();
  180. $this->macros = array();
  181. $this->registerFunction('macro:html',array($this,'__macroHTML'));
  182. }
  183. function setOpt($option, $value)
  184. {
  185. $this->opt[$option] = $value;
  186. }
  187. function setOpts($options)
  188. {
  189. if (!is_array($options)) {
  190. return;
  191. }
  192. foreach ($options as $k => $v) {
  193. $this->opt[$k] = $v;
  194. }
  195. }
  196. function getOpt($option)
  197. {
  198. return (!empty($this->opt[$option])) ? $this->opt[$option] : false;
  199. }
  200. function registerFunction($type,$name)
  201. {
  202. if (is_callable($name)) {
  203. $this->functions[$type] = $name;
  204. }
  205. }
  206. function transform($in)
  207. {
  208. # Initialisation des tags
  209. $this->__initTags();
  210. $this->foot_notes = array();
  211. # Récupération des macros
  212. if ($this->getOpt('active_macros')) {
  213. $in = preg_replace('#^///(.*?)///($|\r)#mse',"\\\$this->__getMacro('\\1')",$in);
  214. }
  215. # Vérification du niveau de titre
  216. if ($this->getOpt('first_title_level') > 4) {
  217. $this->setOpt('first_title_level',4);
  218. }
  219. $res = str_replace("\r", '', $in);
  220. $escape_pattern = array();
  221. # traitement des titres ŕ la setext
  222. if ($this->getOpt('active_setext_title') && $this->getOpt('active_title')) {
  223. $res = preg_replace('/^(.*)\n[=]{5,}$/m','!!!$1',$res);
  224. $res = preg_replace('/^(.*)\n[-]{5,}$/m','!!$1',$res);
  225. }
  226. # Transformation des mots Wiki
  227. if ($this->getOpt('active_wikiwords') && $this->getOpt('words_pattern')) {
  228. $res = preg_replace('/'.$this->getOpt('words_pattern').'/msu','śśś$1śśś',$res);
  229. }
  230. # Transformation des URLs automatiques
  231. if ($this->getOpt('active_auto_urls'))
  232. {
  233. $active_urls = $this->getOpt('active_urls');
  234. $this->setOpt('active_urls',1);
  235. $this->__initTags();
  236. # If urls are not active, escape URLs tags
  237. if (!$active_urls)
  238. {
  239. $res = preg_replace(
  240. '%(?<!\\\\)(['.preg_quote(implode('',$this->tags['a'])).'])%msU',
  241. '\\\$1',$res
  242. );
  243. }
  244. # Transforms urls while preserving tags.
  245. $tree = preg_split($this->tag_pattern,$res,-1,PREG_SPLIT_DELIM_CAPTURE);
  246. foreach ($tree as &$leaf) {
  247. $leaf = preg_replace($this->getOpt('auto_url_pattern'),'[$1$2]',$leaf);
  248. }
  249. unset($leaf);
  250. $res = implode($tree);
  251. }
  252. $this->T = explode("\n",$res);
  253. $this->T[] = '';
  254. # Parse les blocs
  255. $res = $this->__parseBlocks();
  256. # Line break
  257. if ($this->getOpt('active_br')) {
  258. $res = preg_replace('/(?<!\\\)%%%/', '<br />', $res);
  259. $escape_pattern[] = '%%%';
  260. }
  261. # Nettoyage des \s en trop
  262. $res = preg_replace('/([\s]+)(<\/p>|<\/li>|<\/pre>)/u', '$2', $res);
  263. $res = preg_replace('/(<li>)([\s]+)/u', '$1', $res);
  264. # On vire les escapes
  265. if (!empty($escape_pattern)) {
  266. $res = preg_replace('/\\\('.implode('|',$escape_pattern).')/','$1',$res);
  267. }
  268. # On vire les śśśMotWikiśśś qui sont resté (dans les url...)
  269. if ($this->getOpt('active_wikiwords') && $this->getOpt('words_pattern')) {
  270. $res = preg_replace('/śśś'.$this->getOpt('words_pattern').'śśś/msu','$1',$res);
  271. }
  272. # On remet les macros
  273. if ($this->getOpt('active_macros')) {
  274. $res = preg_replace('/^##########MACRO#([0-9]+)#$/mse','\$this->__putMacro("$1")',$res);
  275. }
  276. # Auto line break dans les paragraphes
  277. if ($this->getOpt('active_auto_br')) {
  278. $res = preg_replace_callback('%(<p>)(.*?)(</p>)%msu',array($this,'__autoBR'),$res);
  279. }
  280. # On ajoute les notes
  281. if (count($this->foot_notes) > 0)
  282. {
  283. $res_notes = '';
  284. $i = 1;
  285. foreach ($this->foot_notes as $k => $v) {
  286. $res_notes .= "\n".'<p>[<a href="#rev-'.$k.'" id="'.$k.'">'.$i.'</a>] '.$v.'</p>';
  287. $i++;
  288. }
  289. $res .= sprintf("\n".(count($this->foot_notes) > 1 ? $this->getOpt('note_str') : $this->getOpt('note_str_single'))."\n",$res_notes);
  290. }
  291. return $res;
  292. }
  293. /* PRIVATE
  294. --------------------------------------------------- */
  295. function __initTags()
  296. {
  297. $tags = array(
  298. 'em' => array("''","''"),
  299. 'strong' => array('__','__'),
  300. 'acronym' => array('??','??'),
  301. 'a' => array('[',']'),
  302. 'img' => array('((','))'),
  303. 'q' => array('{{','}}'),
  304. 'code' => array('@@','@@'),
  305. 'anchor' => array('~','~'),
  306. 'del' => array('--','--'),
  307. 'ins' => array('++','++'),
  308. 'inline' => array('``','``'),
  309. 'note' => array('$$','$$'),
  310. 'word' => array('śśś','śśś')
  311. );
  312. $this->linetags = array(
  313. 'empty' => 'řřř',
  314. 'title' => '([!]{1,4})',
  315. 'hr' => '[-]{4}[- ]',
  316. 'quote' => '(&gt;|;:)',
  317. 'lists' => '([*#]+)',
  318. 'pre' => '[ ]{1}'
  319. );
  320. $this->tags = array_merge($tags,$this->custom_tags);
  321. # Suppression des tags selon les options
  322. if (!$this->getOpt('active_urls')) {
  323. unset($this->tags['a']);
  324. }
  325. if (!$this->getOpt('active_img')) {
  326. unset($this->tags['img']);
  327. }
  328. if (!$this->getOpt('active_anchor')) {
  329. unset($this->tags['anchor']);
  330. }
  331. if (!$this->getOpt('active_em')) {
  332. unset($this->tags['em']);
  333. }
  334. if (!$this->getOpt('active_strong')) {
  335. unset($this->tags['strong']);
  336. }
  337. if (!$this->getOpt('active_q')) {
  338. unset($this->tags['q']);
  339. }
  340. if (!$this->getOpt('active_code')) {
  341. unset($this->tags['code']);
  342. }
  343. if (!$this->getOpt('active_acronym')) {
  344. unset($this->tags['acronym']);
  345. }
  346. if (!$this->getOpt('active_ins')) {
  347. unset($this->tags['ins']);
  348. }
  349. if (!$this->getOpt('active_del')) {
  350. unset($this->tags['del']);
  351. }
  352. if (!$this->getOpt('active_inline_html')) {
  353. unset($this->tags['inline']);
  354. }
  355. if (!$this->getOpt('active_footnotes')) {
  356. unset($this->tags['note']);
  357. }
  358. if (!$this->getOpt('active_wikiwords')) {
  359. unset($this->tags['word']);
  360. }
  361. # Suppression des tags de début de ligne selon les options
  362. if (!$this->getOpt('active_empty')) {
  363. unset($this->linetags['empty']);
  364. }
  365. if (!$this->getOpt('active_title')) {
  366. unset($this->linetags['title']);
  367. }
  368. if (!$this->getOpt('active_hr')) {
  369. unset($this->linetags['hr']);
  370. }
  371. if (!$this->getOpt('active_quote')) {
  372. unset($this->linetags['quote']);
  373. }
  374. if (!$this->getOpt('active_lists')) {
  375. unset($this->linetags['lists']);
  376. }
  377. if (!$this->getOpt('active_pre')) {
  378. unset($this->linetags['pre']);
  379. }
  380. $this->open_tags = $this->__getTags();
  381. $this->close_tags = $this->__getTags(false);
  382. $this->all_tags = $this->__getAllTags();
  383. $this->tag_pattern = $this->__getTagsPattern();
  384. $this->escape_table = $this->all_tags;
  385. array_walk($this->escape_table,create_function('&$a','$a = \'\\\\\'.$a;'));
  386. }
  387. function __getTags($open=true)
  388. {
  389. $res = array();
  390. foreach ($this->tags as $k => $v) {
  391. $res[$k] = ($open) ? $v[0] : $v[1];
  392. }
  393. return $res;
  394. }
  395. function __getAllTags()
  396. {
  397. $res = array();
  398. foreach ($this->tags as $v) {
  399. $res[] = $v[0];
  400. $res[] = $v[1];
  401. }
  402. return array_values(array_unique($res));
  403. }
  404. function __getTagsPattern($escape=false)
  405. {
  406. $res = $this->all_tags;
  407. array_walk($res,create_function('&$a','$a = preg_quote($a,"/");'));
  408. if (!$escape) {
  409. return '/(?<!\\\)('.implode('|',$res).')/';
  410. } else {
  411. return '('.implode('|',$res).')';
  412. }
  413. }
  414. /* Blocs
  415. --------------------------------------------------- */
  416. function __parseBlocks()
  417. {
  418. $mode = $type = null;
  419. $res = '';
  420. $max = count($this->T);
  421. for ($i=0; $i<$max; $i++)
  422. {
  423. $pre_mode = $mode;
  424. $pre_type = $type;
  425. $end = ($i+1 == $max);
  426. $line = $this->__getLine($i,$type,$mode);
  427. if ($type != 'pre' || $this->getOpt('parse_pre')) {
  428. $line = $this->__inlineWalk($line);
  429. }
  430. $res .= $this->__closeLine($type,$mode,$pre_type,$pre_mode);
  431. $res .= $this->__openLine($type,$mode,$pre_type,$pre_mode);
  432. # P dans les blockquotes
  433. if ($type == 'blockquote' && trim($line) == '' && $pre_type == $type) {
  434. $res .= "</p>\n<p>";
  435. }
  436. # Correction de la syntaxe FR dans tous sauf pre et hr
  437. # Sur idée de Christophe Bonijol
  438. # Changement de regex (Nicolas Chachereau)
  439. if ($this->getOpt('active_fr_syntax') && $type != null && $type != 'pre' && $type != 'hr') {
  440. $line = preg_replace('%[ ]+([:?!;\x{00BB}](\s|$))%u','&nbsp;$1',$line);
  441. $line = preg_replace('%(\x{00AB})[ ]+%u','$1&nbsp;',$line);
  442. }
  443. $res .= $line;
  444. }
  445. return trim($res);
  446. }
  447. function __getLine($i,&$type,&$mode)
  448. {
  449. $pre_type = $type;
  450. $pre_mode = $mode;
  451. $type = $mode = null;
  452. if (empty($this->T[$i])) {
  453. return false;
  454. }
  455. $line = htmlspecialchars($this->T[$i],ENT_NOQUOTES);
  456. # Ligne vide
  457. if (empty($line))
  458. {
  459. $type = null;
  460. }
  461. elseif ($this->getOpt('active_empty') && preg_match('/^řřř(.*)$/',$line,$cap))
  462. {
  463. $type = null;
  464. $line = trim($cap[1]);
  465. }
  466. # Titre
  467. elseif ($this->getOpt('active_title') && preg_match('/^([!]{1,4})(.*)$/',$line,$cap))
  468. {
  469. $type = 'title';
  470. $mode = strlen($cap[1]);
  471. $line = trim($cap[2]);
  472. }
  473. # Ligne HR
  474. elseif ($this->getOpt('active_hr') && preg_match('/^[-]{4}[- ]*$/',$line))
  475. {
  476. $type = 'hr';
  477. $line = null;
  478. }
  479. # Blockquote
  480. elseif ($this->getOpt('active_quote') && preg_match('/^(&gt;|;:)(.*)$/',$line,$cap))
  481. {
  482. $type = 'blockquote';
  483. $line = trim($cap[2]);
  484. }
  485. # Liste
  486. elseif ($this->getOpt('active_lists') && preg_match('/^([*#]+)(.*)$/',$line,$cap))
  487. {
  488. $type = 'list';
  489. $mode = $cap[1];
  490. $valid = true;
  491. # Vérification d'intégrité
  492. $dl = ($type != $pre_type) ? 0 : strlen($pre_mode);
  493. $d = strlen($mode);
  494. $delta = $d-$dl;
  495. if ($delta < 0 && strpos($pre_mode,$mode) !== 0) {
  496. $valid = false;
  497. }
  498. if ($delta > 0 && $type == $pre_type && strpos($mode,$pre_mode) !== 0) {
  499. $valid = false;
  500. }
  501. if ($delta == 0 && $mode != $pre_mode) {
  502. $valid = false;
  503. }
  504. if ($delta > 1) {
  505. $valid = false;
  506. }
  507. if (!$valid) {
  508. $type = 'p';
  509. $mode = null;
  510. $line = '<br />'.$line;
  511. } else {
  512. $line = trim($cap[2]);
  513. }
  514. }
  515. # Préformaté
  516. elseif ($this->getOpt('active_pre') && preg_match('/^[ ]{1}(.*)$/',$line,$cap))
  517. {
  518. $type = 'pre';
  519. $line = $cap[1];
  520. }
  521. # Paragraphe
  522. else {
  523. $type = 'p';
  524. if (preg_match('/^\\\((?:('.implode('|',$this->linetags).')).*)$/',$line,$cap)) {
  525. $line = $cap[1];
  526. }
  527. $line = trim($line);
  528. }
  529. return $line;
  530. }
  531. function __openLine($type,$mode,$pre_type,$pre_mode)
  532. {
  533. $open = ($type != $pre_type);
  534. if ($open && $type == 'p')
  535. {
  536. return "\n<p>";
  537. }
  538. elseif ($open && $type == 'blockquote')
  539. {
  540. return "\n<blockquote><p>";
  541. }
  542. elseif (($open || $mode != $pre_mode) && $type == 'title')
  543. {
  544. $fl = $this->getOpt('first_title_level');
  545. $fl = $fl+3;
  546. $l = $fl-$mode;
  547. return "\n<h".($l).'>';
  548. }
  549. elseif ($open && $type == 'pre')
  550. {
  551. return "\n<pre>";
  552. }
  553. elseif ($open && $type == 'hr')
  554. {
  555. return "\n<hr />";
  556. }
  557. elseif ($type == 'list')
  558. {
  559. $dl = ($open) ? 0 : strlen($pre_mode);
  560. $d = strlen($mode);
  561. $delta = $d-$dl;
  562. $res = '';
  563. if($delta > 0) {
  564. if(substr($mode, -1, 1) == '*') {
  565. $res .= "<ul>\n";
  566. } else {
  567. $res .= "<ol>\n";
  568. }
  569. } elseif ($delta < 0) {
  570. $res .= "</li>\n";
  571. for($j = 0; $j < abs($delta); $j++) {
  572. if (substr($pre_mode,(0 - $j - 1), 1) == '*') {
  573. $res .= "</ul>\n</li>\n";
  574. } else {
  575. $res .= "</ol>\n</li>\n";
  576. }
  577. }
  578. } else {
  579. $res .= "</li>\n";
  580. }
  581. return $res."<li>";
  582. }
  583. else
  584. {
  585. return null;
  586. }
  587. }
  588. function __closeLine($type,$mode,$pre_type,$pre_mode)
  589. {
  590. $close = ($type != $pre_type);
  591. if ($close && $pre_type == 'p')
  592. {
  593. return "</p>\n";
  594. }
  595. elseif ($close && $pre_type == 'blockquote')
  596. {
  597. return "</p></blockquote>\n";
  598. }
  599. elseif (($close || $mode != $pre_mode) && $pre_type == 'title')
  600. {
  601. $fl = $this->getOpt('first_title_level');
  602. $fl = $fl+3;
  603. $l = $fl-$pre_mode;
  604. return '</h'.($l).">\n";
  605. }
  606. elseif ($close && $pre_type == 'pre')
  607. {
  608. return "</pre>\n";
  609. }
  610. elseif ($close && $pre_type == 'list')
  611. {
  612. $res = '';
  613. for($j = 0; $j < strlen($pre_mode); $j++) {
  614. if(substr($pre_mode,(0 - $j - 1), 1) == '*') {
  615. $res .= "</li>\n</ul>";
  616. } else {
  617. $res .= "</li>\n</ol>";
  618. }
  619. }
  620. return $res;
  621. }
  622. else
  623. {
  624. return "\n";
  625. }
  626. }
  627. /* Inline
  628. --------------------------------------------------- */
  629. function __inlineWalk($str,$allow_only=null)
  630. {
  631. $tree = preg_split($this->tag_pattern,$str,-1,PREG_SPLIT_DELIM_CAPTURE);
  632. $res = '';
  633. for ($i=0; $i<count($tree); $i++)
  634. {
  635. $attr = '';
  636. if (in_array($tree[$i],array_values($this->open_tags)) &&
  637. ($allow_only == null || in_array(array_search($tree[$i],$this->open_tags),$allow_only)))
  638. {
  639. $tag = array_search($tree[$i],$this->open_tags);
  640. $tag_type = 'open';
  641. if (($tidy = $this->__makeTag($tree,$tag,$i,$i,$attr,$tag_type)) !== false)
  642. {
  643. if ($tag != '') {
  644. $res .= '<'.$tag.$attr;
  645. $res .= ($tag_type == 'open') ? '>' : ' />';
  646. }
  647. $res .= $tidy;
  648. }
  649. else
  650. {
  651. $res .= $tree[$i];
  652. }
  653. }
  654. else
  655. {
  656. $res .= $tree[$i];
  657. }
  658. }
  659. # Suppression des echappements
  660. $res = str_replace($this->escape_table,$this->all_tags,$res);
  661. return $res;
  662. }
  663. function __makeTag(&$tree,&$tag,$position,&$j,&$attr,&$type)
  664. {
  665. $res = '';
  666. $closed = false;
  667. $itag = $this->close_tags[$tag];
  668. # Recherche fermeture
  669. for ($i=$position+1;$i<count($tree);$i++)
  670. {
  671. if ($tree[$i] == $itag)
  672. {
  673. $closed = true;
  674. break;
  675. }
  676. }
  677. # Résultat
  678. if ($closed)
  679. {
  680. for ($i=$position+1;$i<count($tree);$i++)
  681. {
  682. if ($tree[$i] != $itag)
  683. {
  684. $res .= $tree[$i];
  685. }
  686. else
  687. {
  688. switch ($tag)
  689. {
  690. case 'a':
  691. $res = $this->__parseLink($res,$tag,$attr,$type);
  692. break;
  693. case 'img':
  694. $type = 'close';
  695. $res = $this->__parseImg($res,$attr);
  696. break;
  697. case 'acronym':
  698. $res = $this->__parseAcronym($res,$attr);
  699. break;
  700. case 'q':
  701. $res = $this->__parseQ($res,$attr);
  702. break;
  703. case 'anchor':
  704. $tag = 'a';
  705. $res = $this->__parseAnchor($res,$attr);
  706. break;
  707. case 'note':
  708. $tag = '';
  709. $res = $this->__parseNote($res);
  710. break;
  711. case 'inline':
  712. $tag = '';
  713. $res = $this->__parseInlineHTML($res);
  714. break;
  715. case 'word':
  716. $res = $this->parseWikiWord($res,$tag,$attr,$type);
  717. break;
  718. default :
  719. $res = $this->__inlineWalk($res);
  720. break;
  721. }
  722. if ($type == 'open' && $tag != '') {
  723. $res .= '</'.$tag.'>';
  724. }
  725. $j = $i;
  726. break;
  727. }
  728. }
  729. return $res;
  730. }
  731. else
  732. {
  733. return false;
  734. }
  735. }
  736. function __splitTagsAttr($str)
  737. {
  738. $res = preg_split('/(?<!\\\)\|/',$str);
  739. foreach ($res as $k => $v) {
  740. $res[$k] = str_replace("\|",'|',$v);
  741. }
  742. return $res;
  743. }
  744. # Antispam (Jérôme Lipowicz)
  745. function __antiSpam($str)
  746. {
  747. $encoded = bin2hex($str);
  748. $encoded = chunk_split($encoded, 2, '%');
  749. $encoded = '%'.substr($encoded, 0, strlen($encoded) - 1);
  750. return $encoded;
  751. }
  752. function __parseLink($str,&$tag,&$attr,&$type)
  753. {
  754. $n_str = $this->__inlineWalk($str,array('acronym','img'));
  755. $data = $this->__splitTagsAttr($n_str);
  756. $no_image = false;
  757. # Only URL in data
  758. if (count($data) == 1)
  759. {
  760. $url = trim($str);
  761. $content = strlen($url) > 35 ? substr($url,0,35).'...' : $url;
  762. $lang = '';
  763. $title = $url;
  764. }
  765. elseif (count($data) > 1)
  766. {
  767. $url = trim($data[1]);
  768. $content = $data[0];
  769. $lang = (!empty($data[2])) ? $this->protectAttr($data[2],true) : '';
  770. $title = (!empty($data[3])) ? $data[3] : '';
  771. $no_image = (!empty($data[4])) ? (boolean) $data[4] : false;
  772. }
  773. # Remplacement si URL spéciale
  774. $this->__specialUrls($url,$content,$lang,$title);
  775. # On vire les &nbsp; dans l'url
  776. $url = str_replace('&nbsp;',' ',$url);
  777. if (preg_match('/^(.+)[.](gif|jpg|jpeg|png)$/', $url) && !$no_image && $this->getOpt('active_auto_img'))
  778. {
  779. # On ajoute les dimensions de l'image si locale
  780. # Idée de Stephanie
  781. $img_size = null;
  782. if (!preg_match('#[a-zA-Z]+://#', $url)) {
  783. if (preg_match('#^/#',$url)) {
  784. $path_img = $_SERVER['DOCUMENT_ROOT'] . $url;
  785. } else {
  786. $path_img = $url;
  787. }
  788. $img_size = @getimagesize($path_img);
  789. }
  790. $attr = ' src="'.$this->protectAttr($this->protectUrls($url)).'"'.
  791. $attr .= (count($data) > 1) ? ' alt="'.$this->protectAttr($content).'"' : ' alt=""';
  792. $attr .= ($lang) ? ' lang="'.$lang.'"' : '';
  793. $attr .= ($title) ? ' title="'.$this->protectAttr($title).'"' : '';
  794. $attr .= (is_array($img_size)) ? ' '.$img_size[3] : '';
  795. $tag = 'img';
  796. $type = 'close';
  797. return null;
  798. }
  799. else
  800. {
  801. if ($this->getOpt('active_antispam') && preg_match('/^mailto:/',$url)) {
  802. $content = $content == $url ? preg_replace('%^mailto:%','',$content) : $content;
  803. $url = 'mailto:'.$this->__antiSpam(substr($url,7));
  804. }
  805. $attr = ' href="'.$this->protectAttr($this->protectUrls($url)).'"';
  806. $attr .= ($lang) ? ' hreflang="'.$lang.'"' : '';
  807. $attr .= ($title) ? ' title="'.$this->protectAttr($title).'"' : '';
  808. return $content;
  809. }
  810. }
  811. function __specialUrls(&$url,&$content,&$lang,&$title)
  812. {
  813. foreach ($this->functions as $k => $v)
  814. {
  815. if (strpos($k,'url:') === 0 && strpos($url,substr($k,4)) === 0)
  816. {
  817. $res = call_user_func($v,$url,$content);
  818. $url = isset($res['url']) ? $res['url'] : $url;
  819. $content = isset($res['content']) ? $res['content'] : $content;
  820. $lang = isset($res['lang']) ? $res['lang'] : $lang;
  821. $title = isset($res['title']) ? $res['title'] : $title;
  822. break;
  823. }
  824. }
  825. }
  826. function __parseImg($str,&$attr)
  827. {
  828. $data = $this->__splitTagsAttr($str);
  829. $alt = '';
  830. $url = $data[0];
  831. if (!empty($data[1])) {
  832. $alt = $data[1];
  833. }
  834. $attr = ' src="'.$this->protectAttr($this->protectUrls($url)).'"';
  835. $attr .= ' alt="'.$this->protectAttr($alt).'"';
  836. if (!empty($data[2])) {
  837. $data[2] = strtoupper($data[2]);
  838. if ($data[2] == 'G' || $data[2] == 'L') {
  839. $attr .= ' style="float:left; margin: 0 1em 1em 0;"';
  840. } elseif ($data[2] == 'D' || $data[2] == 'R') {
  841. $attr .= ' style="float:right; margin: 0 0 1em 1em;"';
  842. } elseif ($data[2] == 'C') {
  843. $attr .= ' style="display:block; margin:0 auto;"';
  844. }
  845. }
  846. if (!empty($data[3])) {
  847. $attr .= ' title="'.$this->protectAttr($data[3]).'"';
  848. }
  849. return null;
  850. }
  851. function __parseQ($str,&$attr)
  852. {
  853. $str = $this->__inlineWalk($str);
  854. $data = $this->__splitTagsAttr($str);
  855. $content = $data[0];
  856. $lang = (!empty($data[1])) ? $this->protectAttr($data[1],true) : '';
  857. $attr .= (!empty($lang)) ? ' lang="'.$lang.'"' : '';
  858. $attr .= (!empty($data[2])) ? ' cite="'.$this->protectAttr($this->protectUrls($data[2])).'"' : '';
  859. return $content;
  860. }
  861. function __parseAnchor($str,&$attr)
  862. {
  863. $name = $this->protectAttr($str,true);
  864. if ($name != '') {
  865. $attr = ' name="'.$name.'"';
  866. }
  867. return null;
  868. }
  869. function __parseNote($str)
  870. {
  871. $i = count($this->foot_notes)+1;
  872. $id = $this->getOpt('note_prefix').'-'.$i;
  873. $this->foot_notes[$id] = $this->__inlineWalk($str);
  874. return '<sup>\[<a href="#'.$id.'" id="rev-'.$id.'">'.$i.'</a>\]</sup>';
  875. }
  876. function __parseInlineHTML($str)
  877. {
  878. return str_replace(array('&gt;','&lt;'),array('>','<'),$str);
  879. }
  880. # Obtenir un acronyme
  881. function __parseAcronym($str,&$attr)
  882. {
  883. $data = $this->__splitTagsAttr($str);
  884. $acronym = $data[0];
  885. $title = $lang = '';
  886. if (count($data) > 1)
  887. {
  888. $title = $data[1];
  889. $lang = (!empty($data[2])) ? $this->protectAttr($data[2],true) : '';
  890. }
  891. if ($title == '' && !empty($this->acro_table[$acronym])) {
  892. $title = $this->acro_table[$acronym];
  893. }
  894. $attr = ($title) ? ' title="'.$this->protectAttr($title).'"' : '';
  895. $attr .= ($lang) ? ' lang="'.$lang.'"' : '';
  896. return $acronym;
  897. }
  898. # Définition des acronymes, dans le fichier acronyms.txt
  899. function __getAcronyms()
  900. {
  901. $file = $this->getOpt('acronyms_file');
  902. $res = array();
  903. if (file_exists($file))
  904. {
  905. if (($fc = @file($file)) !== false)
  906. {
  907. foreach ($fc as $v)
  908. {
  909. $v = trim($v);
  910. if ($v != '')
  911. {
  912. $p = strpos($v,':');
  913. $K = (string) trim(substr($v,0,$p));
  914. $V = (string) trim(substr($v,($p+1)));
  915. if ($K) {
  916. $res[$K] = $V;
  917. }
  918. }
  919. }
  920. }
  921. }
  922. return $res;
  923. }
  924. # Mots wiki (pour héritage)
  925. function parseWikiWord($str,&$tag,&$attr,&$type)
  926. {
  927. $tag = $attr = '';
  928. if (isset($this->functions['wikiword'])) {
  929. return call_user_func($this->functions['wikiword'],$str);
  930. }
  931. return $str;
  932. }
  933. /* Protection des attributs */
  934. function protectAttr($str,$name=false)
  935. {
  936. if ($name && !preg_match('/^[A-Za-z][A-Za-z0-9_:.-]*$/',$str)) {
  937. return '';
  938. }
  939. return str_replace(array("'",'"'),array('&#039;','&quot;'),$str);
  940. }
  941. /* Protection des urls */
  942. function protectUrls($str)
  943. {
  944. if (preg_match('/^javascript:/',$str)) {
  945. $str = '#';
  946. }
  947. return $str;
  948. }
  949. /* Auto BR */
  950. function __autoBR($m)
  951. {
  952. return $m[1].str_replace("\n","<br />\n",$m[2]).$m[3];
  953. }
  954. /* Macro
  955. --------------------------------------------------- */
  956. function __getMacro($s)
  957. {
  958. $this->macros[] = str_replace('\"','"',$s);
  959. return 'řřř##########MACRO#'.(count($this->macros)-1).'#';
  960. }
  961. function __putMacro($id)
  962. {
  963. $id = (integer) $id;
  964. if (isset($this->macros[$id]))
  965. {
  966. $content = str_replace("\r",'',$this->macros[$id]);
  967. $c = explode("\n",$content);
  968. # premičre ligne, premier mot
  969. $fl = trim($c[0]);
  970. $fw = $fl;
  971. if ($fl) {
  972. if (strpos($fl,' ') !== false) {
  973. $fw = substr($fl,0,strpos($fl,' '));
  974. }
  975. $content = implode("\n",array_slice($c,1));
  976. }
  977. if ($c[0] == "\n") {
  978. $content = implode("\n",array_slice($c,1));
  979. }
  980. if ($fw)
  981. {
  982. if (isset($this->functions['macro:'.$fw]))
  983. {
  984. return call_user_func($this->functions['macro:'.$fw],$content,$fl);
  985. }
  986. }
  987. # Si on n'a rien pu faire, on retourne le tout sous
  988. # forme de <pre>
  989. return '<pre>'.htmlspecialchars($this->macros[$id]).'</pre>';
  990. }
  991. return null;
  992. }
  993. function __macroHTML($s)
  994. {
  995. return $s;
  996. }
  997. /* Aide et debug
  998. --------------------------------------------------- */
  999. function help()
  1000. {
  1001. $help['b'] = array();
  1002. $help['i'] = array();
  1003. $help['b'][] = 'Laisser une ligne vide entre chaque bloc <em>de męme nature</em>.';
  1004. $help['b'][] = '<strong>Paragraphe</strong> : du texte et une ligne vide';
  1005. if ($this->getOpt('active_title')) {
  1006. $help['b'][] = '<strong>Titre</strong> : <code>!!!</code>, <code>!!</code>, '.
  1007. '<code>!</code> pour des titres plus ou moins importants';
  1008. }
  1009. if ($this->getOpt('active_hr')) {
  1010. $help['b'][] = '<strong>Trait horizontal</strong> : <code>----</code>';
  1011. }
  1012. if ($this->getOpt('active_lists')) {
  1013. $help['b'][] = '<strong>Liste</strong> : ligne débutant par <code>*</code> ou '.
  1014. '<code>#</code>. Il est possible de mélanger les listes '.
  1015. '(<code>*#*</code>) pour faire des listes de plusieurs niveaux. '.
  1016. 'Respecter le style de chaque niveau';
  1017. }
  1018. if ($this->getOpt('active_pre')) {
  1019. $help['b'][] = '<strong>Texte préformaté</strong> : espace devant chaque ligne de texte';
  1020. }
  1021. if ($this->getOpt('active_quote')) {
  1022. $help['b'][] = '<strong>Bloc de citation</strong> : <code>&gt;</code> ou '.
  1023. '<code>;:</code> devant chaque ligne de texte';
  1024. }
  1025. if ($this->getOpt('active_fr_syntax')) {
  1026. $help['i'][] = 'La correction de ponctuation est active. Un espace '.
  1027. 'insécable remplacera automatiquement tout espace '.
  1028. 'précédant les marques ";","?",":" et "!".';
  1029. }
  1030. if ($this->getOpt('active_em')) {
  1031. $help['i'][] = '<strong>Emphase</strong> : deux apostrophes <code>\'\'texte\'\'</code>';
  1032. }
  1033. if ($this->getOpt('active_strong')) {
  1034. $help['i'][] = '<strong>Forte emphase</strong> : deux soulignés <code>__texte__</code>';
  1035. }
  1036. if ($this->getOpt('active_br')) {
  1037. $help['i'][] = '<strong>Retour forcé ŕ la ligne</strong> : <code>%%%</code>';
  1038. }
  1039. if ($this->getOpt('active_ins')) {
  1040. $help['i'][] = '<strong>Insertion</strong> : deux plus <code>++texte++</code>';
  1041. }
  1042. if ($this->getOpt('active_del')) {
  1043. $help['i'][] = '<strong>Suppression</strong> : deux moins <code>--texte--</code>';
  1044. }
  1045. if ($this->getOpt('active_urls')) {
  1046. $help['i'][] = '<strong>Lien</strong> : <code>[url]</code>, <code>[nom|url]</code>, '.
  1047. '<code>[nom|url|langue]</code> ou <code>[nom|url|langue|titre]</code>.';
  1048. $help['i'][] = '<strong>Image</strong> : comme un lien mais avec une extension d\'image.'.
  1049. '<br />Pour désactiver la reconnaissance d\'image mettez 0 dans un dernier '.
  1050. 'argument. Par exemple <code>[image|image.gif||0]</code> fera un lien vers l\'image au '.
  1051. 'lieu de l\'afficher.'.
  1052. '<br />Il est conseillé d\'utiliser la nouvelle syntaxe.';
  1053. }
  1054. if ($this->getOpt('active_img')) {
  1055. $help['i'][] = '<strong>Image</strong> (nouvelle syntaxe) : '.
  1056. '<code>((url|texte alternatif))</code>, '.
  1057. '<code>((url|texte alternatif|position))</code> ou '.
  1058. '<code>((url|texte alternatif|position|description longue))</code>. '.
  1059. '<br />La position peut prendre les valeur L ou G (gauche), R ou D (droite) ou C (centré).';
  1060. }
  1061. if ($this->getOpt('active_anchor')) {
  1062. $help['i'][] = '<strong>Ancre</strong> : <code>~ancre~</code>';
  1063. }
  1064. if ($this->getOpt('active_acronym')) {
  1065. $help['i'][] = '<strong>Acronyme</strong> : <code>??acronyme??</code> ou '.
  1066. '<code>??acronyme|titre??</code>';
  1067. }
  1068. if ($this->getOpt('active_q')) {
  1069. $help['i'][] = '<strong>Citation</strong> : <code>{{citation}}</code>, '.
  1070. '<code>{{citation|langue}}</code> ou <code>{{citation|langue|url}}</code>';
  1071. }
  1072. if ($this->getOpt('active_code')) {
  1073. $help['i'][] = '<strong>Code</strong> : <code>@@code ici@@</code>';
  1074. }
  1075. if ($this->getOpt('active_footnotes')) {
  1076. $help['i'][] = '<strong>Note de bas de page</strong> : <code>$$Corps de la note$$</code>';
  1077. }
  1078. $res = '<dl class="wikiHelp">';
  1079. $res .= '<dt>Blocs</dt><dd>';
  1080. if (count($help['b']) > 0)
  1081. {
  1082. $res .= '<ul><li>';
  1083. $res .= implode('&nbsp;;</li><li>', $help['b']);
  1084. $res .= '.</li></ul>';
  1085. }
  1086. $res .= '</dd>';
  1087. $res .= '<dt>Éléments en ligne</dt><dd>';
  1088. if (count($help['i']) > 0)
  1089. {
  1090. $res .= '<ul><li>';
  1091. $res .= implode('&nbsp;;</li><li>', $help['i']);
  1092. $res .= '.</li></ul>';
  1093. }
  1094. $res .= '</dd>';
  1095. $res .= '</dl>';
  1096. return $res;
  1097. }
  1098. /*
  1099. function debug()
  1100. {
  1101. $mode = $type = null;
  1102. $max = count($this->T);
  1103. $res =
  1104. '<table border="1">'.
  1105. '<tr><th>p-mode</th><th>p-type</th><th>mode</th><th>type</th><th>chaine</th></tr>';
  1106. for ($i=0; $i<$max; $i++)
  1107. {
  1108. $pre_mode = $mode;
  1109. $pre_type = $type;
  1110. $line = $this->__getLine($i,$type,$mode);
  1111. $res .=
  1112. '<tr><td>'.$pre_mode.'</td><td>'.$pre_type.'</td>'.
  1113. '<td>'.$mode.'</td><td>'.$type.'</td><td>'.$line.'</td></tr>';
  1114. }
  1115. $res .= '</table>';
  1116. return $res;
  1117. }
  1118. //*/
  1119. }
  1120. ?>