PageRenderTime 100ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/text.wiki2xhtml/class.wiki2xhtml.php

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