X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=classes%2Fminiwiki.php;h=ab547e0510a2d8507030f3835891f9ccbdf6e242;hb=03264abd4670fadef700cfff7af174df85fd53d8;hp=058607f84ab1fa7309621a4cec8c9c4c832ec9ee;hpb=f18f4920af03295f9036ed69889e5f4e001ca84b;p=platal.git
diff --git a/classes/miniwiki.php b/classes/miniwiki.php
index 058607f..ab547e0 100644
--- a/classes/miniwiki.php
+++ b/classes/miniwiki.php
@@ -7,65 +7,192 @@ class MiniWiki
private static $replacementHTML = array();
private static $replacementText = array();
- public static function Markup($pattern, $replacement, $replacementTxt) {
- MiniWiki::$patternsWiki[] = $pattern;
- MiniWiki::$replacementHTML[] = $replacement;
- MiniWiki::$replacementText[] = $replacementTxt;
+ private static $title_index = -1;
+ private static $info = array();
+
+ public static function Markup($pattern, $replacement, $replacementTxt, $info = null)
+ {
+ $id = count(MiniWiki::$patternsWiki);
+ MiniWiki::$patternsWiki[$id] = $pattern;
+ MiniWiki::$replacementHTML[$id] = $replacement;
+ MiniWiki::$replacementText[$id] = $replacementTxt;
+ if ($info) {
+ MiniWiki::$info[$id] = $info;
+ }
+ return $id;
}
-
- public static function init() {
+
+ public static function init()
+ {
if (isset(MiniWiki::$patternsWiki[0])) {
return;
}
+ MiniWiki::Markup("/(\r\n|\r([^\n]))/", "\n$2", "\n$2");
+
// retours à la ligne avec \\
- MiniWiki::Markup("/\\\\(?".">(\\\\*))\n/e", "str_repeat('
\n',strlen('$1'))", "str_repeat('\\\\n',strlen('$1'))");
-
+ MiniWiki::Markup("/\\\\(?".">(\\\\*))\n/e", "str_repeat('
\n',mb_strlen('$1'))", "str_repeat('\n',mb_strlen('$1'))", "ligne1\\\\\nligne2");
+
+ // * unordered list
+ MiniWiki::Markup("/(^|\n)\*(([^\n]*(\n|$))(\*[^\n]*(\n|$))*)/se",
+ "'
'", + "'$1 -' . str_replace(\"\\n*\", \"\\n -\", '$2')", + "* element1\n* element2\n* element3"); + // # unordered list + MiniWiki::Markup("/(^|\n)#(([^\n]*(\n|$))(#[^\n]*(\n|$))*)/se", "'
', "\n\n"); + MiniWiki::Markup("/\n\n/", '
', "\n\n", "paragraphe1\n\nparagraphe2"); MiniWiki::Markup("/\n/", ' ', "\n"); - MiniWiki::Markup("/^.*<\/p>
.*$/s", "
$0
", "$0"); + MiniWiki::Markup("/^.*<\/p>.*.*$/s", "
$0
", "$0"); + + // french typo rules, unbreakable spaces + MiniWiki::Markup("/ +([!?:;])/", " $1", "$0"); + } + + public static function WikiToHTML($wiki, $title = false) + { + if (!$title) { + $oldrule12 = MiniWiki::$replacementHTML[MiniWiki::$title_index]; + MiniWiki::$replacementHTML[MiniWiki::$title_index] = "'$0'"; + } + $html = preg_replace(MiniWiki::$patternsWiki, + MiniWiki::$replacementHTML, + htmlentities(trim($wiki), ENT_COMPAT, 'UTF-8')); + if (!$title) { + MiniWiki::$replacementHTML[MiniWiki::$title_index] = $oldrule12; + } + return $html; + } + + private static function justify($text, $n) + { + $arr = explode("\n", wordwrap($text, $n)); + $arr = array_map('trim', $arr); + $res = ''; + foreach ($arr as $key => $line) { + $nxl = isset($arr[$key+1]) ? trim($arr[$key+1]) : ''; + $nxl_split = preg_split('! +!u', $nxl); + $nxw_len = count($nxl_split) ? mb_strlen($nxl_split[0]) : 0; + $line = trim($line); + + if (mb_strlen($line)+1+$nxw_len < $n) { + $res .= "$line\n"; + continue; + } + + if (preg_match('![.:;]$!u',$line)) { + $res .= "$line\n"; + continue; + } + + $tmp = preg_split('! +!u', trim($line)); + $words = count($tmp); + if ($words <= 1) { + $res .= "$line\n"; + continue; + } + + $len = array_sum(array_map('mb_strlen', $tmp)); + $empty = $n - $len; + $sw = floatval($empty) / floatval($words-1); + + $cur = 0; + $l = ''; + foreach ($tmp as $word) { + $l .= $word; + $cur += $sw + strlen($word); // Use strlen here instead of mb_strlen because it is used by str_pad + // which is not multibyte compatible + $l = str_pad($l, intval($cur + 0.5)); + } + $res .= trim($l)."\n"; + } + return trim($res); } - public static function WikiToHTML($s) { - return preg_replace(MiniWiki::$patternsWiki, MiniWiki::$replacementHTML, $s); + + public static function WikiToText($wiki, $just=false, $indent=0, $width=68, $title=false) + { + if (!$title) { + $oldrule12 = MiniWiki::$replacementHTML[MiniWiki::$title_index]; + MiniWiki::$replacementHTML[MiniWiki::$title_index] = "'$0'"; + } + //$text = trim($wiki); + //foreach (MiniWiki::$patternsWiki as $key=>$pattern) { + // echo $key . " - " . $pattern . "\n"; + // $text = preg_replace($pattern, MiniWiki::$replacementText[$key], $text); + //} + $text = preg_replace(MiniWiki::$patternsWiki, MiniWiki::$replacementText, trim($wiki)); + if (!$title) { + MiniWiki::$replacementHTML[MiniWiki::$title_index] = $oldrule12; + } + $text = $just ? MiniWiki::justify($text, $width - $indent) : wordwrap($text, $width - $indent); + if($indent) { + $ind = str_pad('',$indent); + $text = $ind.str_replace("\n","\n$ind",$text); + } + return $text; } -}; + + static public function help($with_title = false) + { + if (!$with_title) { + $info12 = MiniWiki::$info[MiniWiki::$title_index]; + unset(MiniWiki::$info[MiniWiki::$title_index]); + } + + $res = array(); + foreach (MiniWiki::$info as $value) { + $res[$value] = MiniWiki::wikiToHtml($value, true); + } + + if (!$with_title) { + MiniWiki::$info[MiniWiki::$title_index] = $info12; + } + return $res; + } +} MiniWiki::init(); + +// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: ?>