class.wiki2xhtml.basic.php 32 KB

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