f18f4920 |
1 | <?php |
2 | |
3 | class MiniWiki |
4 | { |
5 | |
6 | private static $patternsWiki = array(); |
7 | private static $replacementHTML = array(); |
8 | private static $replacementText = array(); |
9 | |
47ea918c |
10 | private static $title_index = -1; |
72b2f8bb |
11 | private static $info = array(); |
12 | |
47ea918c |
13 | public static function Markup($pattern, $replacement, $replacementTxt, $info = null) |
72b2f8bb |
14 | { |
47ea918c |
15 | $id = count(MiniWiki::$patternsWiki); |
5d1fffc0 |
16 | MiniWiki::$patternsWiki[$id] = $pattern; |
17 | MiniWiki::$replacementHTML[$id] = $replacement; |
18 | MiniWiki::$replacementText[$id] = $replacementTxt; |
72b2f8bb |
19 | if ($info) { |
20 | MiniWiki::$info[$id] = $info; |
21 | } |
47ea918c |
22 | return $id; |
f18f4920 |
23 | } |
24 | |
72b2f8bb |
25 | public static function init() |
26 | { |
f18f4920 |
27 | if (isset(MiniWiki::$patternsWiki[0])) { |
28 | return; |
29 | } |
47ea918c |
30 | MiniWiki::Markup("/(\r\n|\r([^\n]))/", "\n$2", "\n$2"); |
5d1fffc0 |
31 | |
f18f4920 |
32 | // retours à la ligne avec \\ |
dcdcd18a |
33 | MiniWiki::Markup("/\\\\(?".">(\\\\*))\n/e", "str_repeat('<br />\n',mb_strlen('$1'))", "str_repeat('\n',mb_strlen('$1'))", "ligne1\\\\\nligne2"); |
f18f4920 |
34 | |
e219710a |
35 | // * unordered list |
7373df1e |
36 | MiniWiki::Markup("/(^|\n)\*(([^\n]*(\n|$))(\*[^\n]*(\n|$))*)/se", "'<ul><li>'.str_replace(\"\\n*\",'</li><li>','$2').'</li></ul>'", "'$0'", "* element1\n* element2\n* element3"); |
e219710a |
37 | // # unordered list |
7373df1e |
38 | MiniWiki::Markup("/(^|\n)#(([^\n]*(\n|$))(#[^\n]*(\n|$))*)/se", "'<ol><li>'.str_replace(\"\\n#\",'</li><li>','$2').'</li></ol>'", "'$0'", "# element1\n# element2\n# element3"); |
e219710a |
39 | |
f18f4920 |
40 | // bold, italic and others |
41 | // ''' bold ''' |
47ea918c |
42 | MiniWiki::Markup("/'''(.*?)'''/",'<strong>$1</strong>','*$1*', "'''gras'''"); |
f18f4920 |
43 | // '' italic '' |
47ea918c |
44 | MiniWiki::Markup("/''(.*?)''/",'<em>$1</em>','/$1/', "''italique''"); |
f18f4920 |
45 | // '+ big +' |
47ea918c |
46 | MiniWiki::Markup("/'\\+(.*?)\\+'/",'<big>$1</big>','*$1*', "'+grand+'"); |
f18f4920 |
47 | // '- small -' |
47ea918c |
48 | MiniWiki::Markup("/'\\-(.*?)\\-'/",'<small>$1</small>','$1', "'-petit-'"); |
f18f4920 |
49 | // '^superscript^' |
47ea918c |
50 | MiniWiki::Markup("/'\\^(.*?)\\^'/",'<sup>$1</sup>','$1', "'^exposant^'"); |
f18f4920 |
51 | // '_subscript_' |
47ea918c |
52 | MiniWiki::Markup("/'_(.*?)_'/",'<sub>$1</sub>','$1', "'_indice_'"); |
f18f4920 |
53 | // {+ underline +} |
47ea918c |
54 | MiniWiki::Markup("/\\{\\+(.*?)\\+\\}/",'<ins>$1</ins>','_$1_', "{+insertion+}"); |
f18f4920 |
55 | // {- strikeout -} |
47ea918c |
56 | MiniWiki::Markup("/\\{-(.*?)-\\}/",'<del>$1</del>','-$1-', "{-suppression-}"); |
57 | // {color| colored text |} |
80714a6a |
58 | MiniWiki::Markup("/%([a-z]+|\#[0-9a-f]{3,6})%(.*?)%%/i", "<span style='color: $1;'>$2</span>", "$2", |
59 | "%red% texte en rouge %%\\\\\n%#ff0% texte en jaune %%\\\\\n%#0000ff% texte en bleu %%"); |
f18f4920 |
60 | // [+ big +] [++ bigger ++] [+++ even bigger +++] ... |
dcdcd18a |
61 | MiniWiki::Markup("/\\[(([-+])+)(.*?)\\1\\]/e","'<span style=\'font-size:'.(round(pow(6/5,$2mb_strlen('$1'))*100,0)).'%\'>$3</span>'", "'$3'", "[+ grand +]\n\n[++ plus grand ++]\n\n[+++ encore plus grand +++]"); |
f18f4920 |
62 | |
63 | // ----- <hr/> |
47ea918c |
64 | MiniWiki::Markup("/(\n|^)--(--+| \n)/s", '$1<hr/>', '$1-- '."\n", "----\n"); |
f18f4920 |
65 | // titles |
dcdcd18a |
66 | MiniWiki::$title_index = MiniWiki::Markup('/(\n|^)(!+)([^\n]*)/se', "'$1<h'.mb_strlen('$2').'>$3</h'.mb_strlen('$2').'>'", |
47ea918c |
67 | "'$1$3'", "!titre1\n\n!!titre2\n\n!!!titre3"); |
f18f4920 |
68 | |
f18f4920 |
69 | // links |
9cd89110 |
70 | MiniWiki::Markup('/((?:https?|ftp):\/\/(?:[\.\,\;\!\:]*[\w@~%$£µ&i#\-+=_\/\?])*)/ui', '<a href="\\0">\\0</a>', '<\\0>'); |
71 | MiniWiki::Markup('/(\s|^|\\[\\[)www\.((?:[\.\,\;\!\:]*[\w@~%$£µ&i#\-+=_\/\?])*)/iu', '\\1<a href="http://www.\\2">www.\\2</a>', '<http://www.\\2>'); |
72 | MiniWiki::Markup('/(?:mailto:)?([a-z0-9.\-+_]+@([\-.+_]?[a-z0-9])+)/i', '<a href="mailto:\\0">\\0</a>', '<\\0>'); |
73 | MiniWiki::Markup('/\\[\\[\\s*<a href="([^>]*)">.*<\/a>\\s*\|([^\\]]+)\\]\\]/i', '<a href="\\1">\\2</a>', '\\2 <\\1>', "[[http://www.example.com|Mon site web]]\n\nhttp://www.example.com\n\ntest@example.com"); |
f18f4920 |
74 | |
75 | // paragraphs and empty lines |
47ea918c |
76 | MiniWiki::Markup("/\n\n/", '</p><p>', "\n\n", "paragraphe1\n\nparagraphe2"); |
77 | MiniWiki::Markup("/\n/", ' ', "\n"); |
78 | MiniWiki::Markup("/^.*<\/p><p>.*$/s", "<p>$0</p>", "$0"); |
f18f4920 |
79 | } |
80 | |
72b2f8bb |
81 | public static function WikiToHTML($wiki, $title = false) |
82 | { |
02fdd1c8 |
83 | if (!$title) { |
47ea918c |
84 | $oldrule12 = MiniWiki::$replacementHTML[MiniWiki::$title_index]; |
85 | MiniWiki::$replacementHTML[MiniWiki::$title_index] = "'$0'"; |
5d1fffc0 |
86 | } |
61664f8b |
87 | $html = preg_replace(MiniWiki::$patternsWiki, |
88 | MiniWiki::$replacementHTML, |
89 | htmlentities(trim($wiki), ENT_COMPAT, 'UTF-8')); |
02fdd1c8 |
90 | if (!$title) { |
47ea918c |
91 | MiniWiki::$replacementHTML[MiniWiki::$title_index] = $oldrule12; |
5d1fffc0 |
92 | } |
93 | return $html; |
f18f4920 |
94 | } |
02fdd1c8 |
95 | |
dcdcd18a |
96 | private static function justify($text, $n) |
02fdd1c8 |
97 | { |
dcdcd18a |
98 | $arr = explode("\n", wordwrap($text, $n)); |
99 | $arr = array_map('trim', $arr); |
02fdd1c8 |
100 | $res = ''; |
101 | foreach ($arr as $key => $line) { |
102 | $nxl = isset($arr[$key+1]) ? trim($arr[$key+1]) : ''; |
dcdcd18a |
103 | $nxl_split = preg_split('! +!u', $nxl); |
104 | $nxw_len = count($nxl_split) ? mb_strlen($nxl_split[0]) : 0; |
02fdd1c8 |
105 | $line = trim($line); |
106 | |
dcdcd18a |
107 | if (mb_strlen($line)+1+$nxw_len < $n) { |
02fdd1c8 |
108 | $res .= "$line\n"; |
109 | continue; |
110 | } |
111 | |
dcdcd18a |
112 | if (preg_match('![.:;]$!u',$line)) { |
02fdd1c8 |
113 | $res .= "$line\n"; |
114 | continue; |
115 | } |
116 | |
dcdcd18a |
117 | $tmp = preg_split('! +!u', trim($line)); |
02fdd1c8 |
118 | $words = count($tmp); |
119 | if ($words <= 1) { |
120 | $res .= "$line\n"; |
121 | continue; |
122 | } |
123 | |
dcdcd18a |
124 | $len = array_sum(array_map('mb_strlen', $tmp)); |
02fdd1c8 |
125 | $empty = $n - $len; |
126 | $sw = floatval($empty) / floatval($words-1); |
127 | |
128 | $cur = 0; |
129 | $l = ''; |
130 | foreach ($tmp as $word) { |
131 | $l .= $word; |
dcdcd18a |
132 | $cur += $sw + strlen($word); // Use strlen here instead of mb_strlen because it is used by str_pad |
133 | // which is not multibyte compatible |
134 | $l = str_pad($l, intval($cur + 0.5)); |
02fdd1c8 |
135 | } |
136 | $res .= trim($l)."\n"; |
137 | } |
138 | return trim($res); |
139 | } |
140 | |
141 | |
72b2f8bb |
142 | public static function WikiToText($wiki, $just=false, $indent=0, $width=68, $title=false) |
143 | { |
02fdd1c8 |
144 | if (!$title) { |
47ea918c |
145 | $oldrule12 = MiniWiki::$replacementHTML[MiniWiki::$title_index]; |
146 | MiniWiki::$replacementHTML[MiniWiki::$title_index] = "'$0'"; |
02fdd1c8 |
147 | } |
7373df1e |
148 | //$text = trim($wiki); |
149 | //foreach (MiniWiki::$patternsWiki as $key=>$pattern) { |
150 | // echo $key . " - " . $pattern . "\n"; |
151 | // $text = preg_replace($pattern, MiniWiki::$replacementText[$key], $text); |
152 | //} |
02fdd1c8 |
153 | $text = preg_replace(MiniWiki::$patternsWiki, MiniWiki::$replacementText, trim($wiki)); |
154 | if (!$title) { |
47ea918c |
155 | MiniWiki::$replacementHTML[MiniWiki::$title_index] = $oldrule12; |
02fdd1c8 |
156 | } |
dcdcd18a |
157 | $text = $just ? MiniWiki::justify($text, $width - $indent) : wordwrap($text, $width - $indent); |
02fdd1c8 |
158 | if($indent) { |
159 | $ind = str_pad('',$indent); |
160 | $text = $ind.str_replace("\n","\n$ind",$text); |
161 | } |
162 | return $text; |
163 | } |
72b2f8bb |
164 | |
165 | static public function help($with_title = false) |
166 | { |
167 | if (!$with_title) { |
47ea918c |
168 | $info12 = MiniWiki::$info[MiniWiki::$title_index]; |
169 | unset(MiniWiki::$info[MiniWiki::$title_index]); |
72b2f8bb |
170 | } |
171 | |
fdbeba4f |
172 | $res = array(); |
72b2f8bb |
173 | foreach (MiniWiki::$info as $value) { |
fdbeba4f |
174 | $res[$value] = MiniWiki::wikiToHtml($value, true); |
72b2f8bb |
175 | } |
72b2f8bb |
176 | |
177 | if (!$with_title) { |
47ea918c |
178 | MiniWiki::$info[MiniWiki::$title_index] = $info12; |
72b2f8bb |
179 | } |
180 | return $res; |
181 | } |
182 | } |
f18f4920 |
183 | |
184 | MiniWiki::init(); |
5b21237d |
185 | |
186 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
f18f4920 |
187 | ?> |