owls
diff bbcodeparser.inc.php @ 17:081ba8764994
The wiki-like system became a content-rendering system
This is a single changeset to include all the changes throughout
the last years. Owls no longer supports editing pages but only
renders them. Also, it no longer uses a database to store the
contents but reads the contents from the filesystem. All this
made owls simpler ... anyway, it just reflects my needs.
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Sat, 23 Jul 2016 21:39:17 +0200 |
parents | Bbcodeparser.inc.php@3e3fa7725abb |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/bbcodeparser.inc.php Sat Jul 23 21:39:17 2016 +0200 1.3 @@ -0,0 +1,158 @@ 1.4 +<?php 1.5 +/** 1.6 + * BB-Code-Parser 1.7 + * 1.8 + * @author Meillo r e t u r n s <meillo@marmaro.de> 1.9 + */ 1.10 + 1.11 + 1.12 + /// path to smilies 1.13 + define('SMILIE_DIR', 'smilies/'); 1.14 + 1.15 + 1.16 + 1.17 + /** 1.18 + * parses recursive quotes without a source mentioned 1.19 + * 1.20 + * @param $textinput the bbcode-text 1.21 + * @param $level number of levels to go inside 1.22 + * @return HTML-text 1.23 + */ 1.24 + function parse_quote1($textinput,$level = 1) { 1.25 + $pattern = '#\[quote\](((?R)|(.*))*)\[/quote\]#isUe'; 1.26 + $replacement = "'<br />[nl]<span class=\"quote0\">Zitat:</span>[nl]<div class=\"quote1\">[nl]'.parse_quote1('$1', 1.27 + ". ($level + 1) ." 1.28 + ).'[nl]</div>[nl]'"; 1.29 + return preg_replace($pattern, $replacement, $textinput); 1.30 + } 1.31 + 1.32 + /** 1.33 + * parses recursive quotes with a source mentioned 1.34 + * 1.35 + * @param $textinput the bbcode-text 1.36 + * @param $level number of levels to go inside 1.37 + * @return HTML-text 1.38 + */ 1.39 + function parse_quote2($textinput,$level = 1) { 1.40 + $pattern = '#\[quote\=(.*)\](((?R)|(.*))*)\[/quote\]#isUe'; 1.41 + $replacement = "'<br />[nl]<span class=\"quote0\">Zitat: ($1)</span>[nl]<div class=\"quote2\">[nl]'.parse_quote2('$2', 1.42 + ". ($level + 1) ." 1.43 + ).'[nl]</div>[nl]'"; 1.44 + return preg_replace($pattern, $replacement, $textinput); 1.45 + } 1.46 + 1.47 + /** 1.48 + * replaces smilies 1.49 + * 1.50 + * @param $text text with ASCII-smilies 1.51 + * @return text with [img]-smilies 1.52 + */ 1.53 + function smilies($text) { 1.54 + $smilies = array( ':-?\)' => 'Smile.gif', 1.55 + ';-?\)' => 'Wink.gif', 1.56 + ':-?D' => 'Biggrin.gif', 1.57 + ':-?\(' => 'Sad.gif', 1.58 + ':-?P' => 'Lick.gif', 1.59 + ':o' => 'Talk.gif', 1.60 + ':-S' => 'Dontknow.gif', 1.61 + ':dontknow:' => 'Dontknow.gif', 1.62 + ':-@' => 'Angry.gif', 1.63 + ':cool:' => 'Cool.gif', 1.64 + 'B-\)' => 'Cool.gif', 1.65 + '%-\)' => 'Crosseyed.gif', 1.66 + '%-\(' => 'Crosseyed.gif', 1.67 + ':rolleyes:' => 'Rolleyes.gif', 1.68 + ':eek:' => 'Shocked.gif'); 1.69 + while(list($key, $val) = each($smilies)) { 1.70 + $key = '/(\s|^)'.$key.'(\s|$)/'; 1.71 + $text = preg_replace($key, '$1[img]'. SMILIE_DIR . $val .'[/img]$2', $text); 1.72 + } 1.73 + return $text; 1.74 + } 1.75 + 1.76 + 1.77 + /** 1.78 + * turns bbcode in HTML 1.79 + * 1.80 + * @param $text the text with bbcode inside 1.81 + * @param $smilies set to 1 causes smilies to be replaced with pics 1.82 + * @param $images set to 1 causes images to be displayed ([img]-tag) 1.83 + * @return text with HTML-code 1.84 + */ 1.85 + function bbcode($text, $smilies = 0, $images = 0) { 1.86 + 1.87 + // line breaks and special chars 1.88 + $text = preg_replace("#(\r\n)|(\r)#", "\n", htmlentities($text, ENT_COMPAT, 'UTF-8')); 1.89 + $text = preg_replace("#(\n){3,}#", "\n\n", $text); 1.90 + 1.91 + // smilies 1.92 + if ($smilies == 1) { 1.93 + $text = smilies($text); 1.94 + } 1.95 + 1.96 + /* 1.97 + // new-lines 1.98 + $text = preg_replace("#(\r\n)|(\r)#", "\n", $text); 1.99 + $text = str_replace("\n", '<br />[nl]', htmlentities($text, ENT_COMPAT, 'UTF-8')); 1.100 + 1.101 + // bold 1.102 + $text = preg_replace("#\[b\](.*?)\[/b\]#i", "<strong>$1</strong>", $text); 1.103 + // italic 1.104 + $text = preg_replace("#\[i\](.*?)\[/i\]#i", "<i>$1</i>", $text); 1.105 + // links 1.106 + $text = preg_replace("#\[url\](.*)\[/url\]#iU", "<a href=\"$1\">$1</a>", $text); 1.107 + $text = preg_replace("#\[url=(.*)\](.*)\[/url\]#iU", "<a href=\"$1\">$2</a>", $text); 1.108 + // lists 1.109 + //$text = preg_replace("#\[list\]\<br /\>(.*)\[/list\]#iU", "<ul>$1</ul>", $text); 1.110 + //$text = preg_replace("#\[\*\](.*)\<br \/\>#iU", "<li>$1</li>", $text); 1.111 + // quotes 1.112 + */ 1.113 + $text = parse_quote1($text); 1.114 + $text = parse_quote2($text); 1.115 + // images 1.116 + if ($images == 1) { 1.117 + $text = preg_replace("#\[img\](.*?)\[/img\]#i", "<img src=\"$1\" alt=\"<[Bild]>\" />", $text); 1.118 + } 1.119 + 1.120 + /* 1.121 + // remove backslashes 1.122 + $text = preg_replace("#\\\#is", "", $text); 1.123 + // new-lines 1.124 + $text = str_replace('[nl]', "\n", $text); 1.125 + */ 1.126 + 1.127 + 1.128 + // inline 1.129 + $text = preg_replace('#\[b\](.*)\[/b\]#iU', '<strong>$1</strong>', $text); 1.130 + $text = preg_replace('#\*(.*)\*#iU', '<strong>$1</strong>', $text); 1.131 + $text = preg_replace('#\[i\](.*)\[/i\]#iU', '<i>$1</i>', $text); 1.132 + $text = preg_replace('#[^A-Za-z0-9]_(.*)_[^A-Za-z0-9]#iU', '<em>$1</em>', $text); 1.133 + $text = preg_replace('#{{{(.*)}}}#iUs', '<tt>$1</tt>', $text); 1.134 + $text = preg_replace("#\[url\](.*)\[/url\]#iU","<a href=\"$1\">$1</a>",$text); 1.135 + $text = preg_replace("#\[url=(.*)\](.*)\[/url\]#iU","<a href=\"$1\">$2</a>",$text); 1.136 + $text = preg_replace("#\[img\](.*)\[/img\]#iU","<img src=\"$1\" alt=\"[Bild]\" />", $text); 1.137 + $text = preg_replace("#^-{3,}$#iUm","<hr />", $text); 1.138 + 1.139 + // Listen 1.140 +# $text = preg_replace("#\[list\](.*)\[/list\]#i","<ul>$1</ul>", $text); 1.141 + $text = preg_replace("#^\s*\*(.*)\n#imU", "<li>$1</li>", $text); 1.142 + $text = preg_replace("#^((\s*\<li\>(.*)\</li\>\s*)+)$#im", "<ul>$1</ul>", $text); 1.143 + 1.144 + 1.145 + 1.146 + 1.147 + // boxes 1.148 + 1.149 + 1.150 + $text = preg_replace('#^(.+)$#mU', "<p>\n $1\n</p>", $text); 1.151 + 1.152 + # nicht wenn <h_> oder <ul> enthalten sind 1.153 + $text = preg_replace("#\<p\>\n \[h([1-6]{1})\](.*)\[/h[1-6]{1}\]\n\</p\>#iU", "\n<h$1>$2</h$1>", $text); 1.154 + $text = preg_replace("#\<p\>\n ===(.*)\n\</p\>#iU", "\n<h3>$1</h3>", $text); 1.155 + $text = preg_replace("#\<p\>\n ==(.*)\n\</p\>#iU", "\n<h2>$1</h2>", $text); 1.156 + 1.157 + $text = preg_replace("#\<p\>\n \<ul\>(.*)\</ul\>\n\</p\>#iU", "<ul>\n $1\n</ul>", $text); 1.158 + return $text; 1.159 + } 1.160 + 1.161 +?>