owls

annotate 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
rev   line source
meillo@0 1 <?php
meillo@0 2 /**
meillo@0 3 * BB-Code-Parser
meillo@0 4 *
meillo@8 5 * @author Meillo r e t u r n s <meillo@marmaro.de>
meillo@0 6 */
meillo@0 7
meillo@0 8
meillo@8 9 /// path to smilies
meillo@17 10 define('SMILIE_DIR', 'smilies/');
meillo@2 11
meillo@2 12
meillo@2 13
meillo@8 14 /**
meillo@8 15 * parses recursive quotes without a source mentioned
meillo@8 16 *
meillo@8 17 * @param $textinput the bbcode-text
meillo@8 18 * @param $level number of levels to go inside
meillo@8 19 * @return HTML-text
meillo@8 20 */
meillo@8 21 function parse_quote1($textinput,$level = 1) {
meillo@0 22 $pattern = '#\[quote\](((?R)|(.*))*)\[/quote\]#isUe';
meillo@0 23 $replacement = "'<br />[nl]<span class=\"quote0\">Zitat:</span>[nl]<div class=\"quote1\">[nl]'.parse_quote1('$1',
meillo@0 24 ". ($level + 1) ."
meillo@0 25 ).'[nl]</div>[nl]'";
meillo@0 26 return preg_replace($pattern, $replacement, $textinput);
meillo@0 27 }
meillo@8 28
meillo@8 29 /**
meillo@8 30 * parses recursive quotes with a source mentioned
meillo@8 31 *
meillo@8 32 * @param $textinput the bbcode-text
meillo@8 33 * @param $level number of levels to go inside
meillo@8 34 * @return HTML-text
meillo@8 35 */
meillo@8 36 function parse_quote2($textinput,$level = 1) {
meillo@0 37 $pattern = '#\[quote\=(.*)\](((?R)|(.*))*)\[/quote\]#isUe';
meillo@0 38 $replacement = "'<br />[nl]<span class=\"quote0\">Zitat: ($1)</span>[nl]<div class=\"quote2\">[nl]'.parse_quote2('$2',
meillo@0 39 ". ($level + 1) ."
meillo@0 40 ).'[nl]</div>[nl]'";
meillo@0 41 return preg_replace($pattern, $replacement, $textinput);
meillo@0 42 }
meillo@0 43
meillo@8 44 /**
meillo@8 45 * replaces smilies
meillo@8 46 *
meillo@8 47 * @param $text text with ASCII-smilies
meillo@8 48 * @return text with [img]-smilies
meillo@8 49 */
meillo@0 50 function smilies($text) {
meillo@17 51 $smilies = array( ':-?\)' => 'Smile.gif',
meillo@17 52 ';-?\)' => 'Wink.gif',
meillo@17 53 ':-?D' => 'Biggrin.gif',
meillo@17 54 ':-?\(' => 'Sad.gif',
meillo@17 55 ':-?P' => 'Lick.gif',
meillo@0 56 ':o' => 'Talk.gif',
meillo@0 57 ':-S' => 'Dontknow.gif',
meillo@0 58 ':dontknow:' => 'Dontknow.gif',
meillo@0 59 ':-@' => 'Angry.gif',
meillo@2 60 ':cool:' => 'Cool.gif',
meillo@17 61 'B-\)' => 'Cool.gif',
meillo@17 62 '%-\)' => 'Crosseyed.gif',
meillo@17 63 '%-\(' => 'Crosseyed.gif',
meillo@0 64 ':rolleyes:' => 'Rolleyes.gif',
meillo@0 65 ':eek:' => 'Shocked.gif');
meillo@0 66 while(list($key, $val) = each($smilies)) {
meillo@17 67 $key = '/(\s|^)'.$key.'(\s|$)/';
meillo@17 68 $text = preg_replace($key, '$1[img]'. SMILIE_DIR . $val .'[/img]$2', $text);
meillo@0 69 }
meillo@0 70 return $text;
meillo@0 71 }
meillo@0 72
meillo@0 73
meillo@8 74 /**
meillo@8 75 * turns bbcode in HTML
meillo@8 76 *
meillo@8 77 * @param $text the text with bbcode inside
meillo@8 78 * @param $smilies set to 1 causes smilies to be replaced with pics
meillo@8 79 * @param $images set to 1 causes images to be displayed ([img]-tag)
meillo@8 80 * @return text with HTML-code
meillo@8 81 */
meillo@0 82 function bbcode($text, $smilies = 0, $images = 0) {
meillo@0 83
meillo@17 84 // line breaks and special chars
meillo@17 85 $text = preg_replace("#(\r\n)|(\r)#", "\n", htmlentities($text, ENT_COMPAT, 'UTF-8'));
meillo@17 86 $text = preg_replace("#(\n){3,}#", "\n\n", $text);
meillo@17 87
meillo@0 88 // smilies
meillo@0 89 if ($smilies == 1) {
meillo@0 90 $text = smilies($text);
meillo@0 91 }
meillo@0 92
meillo@17 93 /*
meillo@0 94 // new-lines
meillo@0 95 $text = preg_replace("#(\r\n)|(\r)#", "\n", $text);
meillo@17 96 $text = str_replace("\n", '<br />[nl]', htmlentities($text, ENT_COMPAT, 'UTF-8'));
meillo@0 97
meillo@0 98 // bold
meillo@0 99 $text = preg_replace("#\[b\](.*?)\[/b\]#i", "<strong>$1</strong>", $text);
meillo@0 100 // italic
meillo@0 101 $text = preg_replace("#\[i\](.*?)\[/i\]#i", "<i>$1</i>", $text);
meillo@0 102 // links
meillo@0 103 $text = preg_replace("#\[url\](.*)\[/url\]#iU", "<a href=\"$1\">$1</a>", $text);
meillo@0 104 $text = preg_replace("#\[url=(.*)\](.*)\[/url\]#iU", "<a href=\"$1\">$2</a>", $text);
meillo@0 105 // lists
meillo@0 106 //$text = preg_replace("#\[list\]\<br /\>(.*)\[/list\]#iU", "<ul>$1</ul>", $text);
meillo@0 107 //$text = preg_replace("#\[\*\](.*)\<br \/\>#iU", "<li>$1</li>", $text);
meillo@0 108 // quotes
meillo@17 109 */
meillo@0 110 $text = parse_quote1($text);
meillo@0 111 $text = parse_quote2($text);
meillo@0 112 // images
meillo@0 113 if ($images == 1) {
meillo@0 114 $text = preg_replace("#\[img\](.*?)\[/img\]#i", "<img src=\"$1\" alt=\"&lt;[Bild]&gt;\" />", $text);
meillo@0 115 }
meillo@0 116
meillo@17 117 /*
meillo@0 118 // remove backslashes
meillo@0 119 $text = preg_replace("#\\\#is", "", $text);
meillo@0 120 // new-lines
meillo@0 121 $text = str_replace('[nl]', "\n", $text);
meillo@17 122 */
meillo@0 123
meillo@17 124
meillo@17 125 // inline
meillo@17 126 $text = preg_replace('#\[b\](.*)\[/b\]#iU', '<strong>$1</strong>', $text);
meillo@17 127 $text = preg_replace('#\*(.*)\*#iU', '<strong>$1</strong>', $text);
meillo@17 128 $text = preg_replace('#\[i\](.*)\[/i\]#iU', '<i>$1</i>', $text);
meillo@17 129 $text = preg_replace('#[^A-Za-z0-9]_(.*)_[^A-Za-z0-9]#iU', '<em>$1</em>', $text);
meillo@17 130 $text = preg_replace('#{{{(.*)}}}#iUs', '<tt>$1</tt>', $text);
meillo@17 131 $text = preg_replace("#\[url\](.*)\[/url\]#iU","<a href=\"$1\">$1</a>",$text);
meillo@17 132 $text = preg_replace("#\[url=(.*)\](.*)\[/url\]#iU","<a href=\"$1\">$2</a>",$text);
meillo@17 133 $text = preg_replace("#\[img\](.*)\[/img\]#iU","<img src=\"$1\" alt=\"[Bild]\" />", $text);
meillo@17 134 $text = preg_replace("#^-{3,}$#iUm","<hr />", $text);
meillo@17 135
meillo@17 136 // Listen
meillo@17 137 # $text = preg_replace("#\[list\](.*)\[/list\]#i","<ul>$1</ul>", $text);
meillo@17 138 $text = preg_replace("#^\s*\*(.*)\n#imU", "<li>$1</li>", $text);
meillo@17 139 $text = preg_replace("#^((\s*\<li\>(.*)\</li\>\s*)+)$#im", "<ul>$1</ul>", $text);
meillo@17 140
meillo@17 141
meillo@17 142
meillo@17 143
meillo@17 144 // boxes
meillo@17 145
meillo@17 146
meillo@17 147 $text = preg_replace('#^(.+)$#mU', "<p>\n $1\n</p>", $text);
meillo@17 148
meillo@17 149 # nicht wenn <h_> oder <ul> enthalten sind
meillo@17 150 $text = preg_replace("#\<p\>\n \[h([1-6]{1})\](.*)\[/h[1-6]{1}\]\n\</p\>#iU", "\n<h$1>$2</h$1>", $text);
meillo@17 151 $text = preg_replace("#\<p\>\n ===(.*)\n\</p\>#iU", "\n<h3>$1</h3>", $text);
meillo@17 152 $text = preg_replace("#\<p\>\n ==(.*)\n\</p\>#iU", "\n<h2>$1</h2>", $text);
meillo@17 153
meillo@17 154 $text = preg_replace("#\<p\>\n \<ul\>(.*)\</ul\>\n\</p\>#iU", "<ul>\n $1\n</ul>", $text);
meillo@0 155 return $text;
meillo@0 156 }
meillo@0 157
meillo@8 158 ?>