summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--code/bbcode.inc.php200
-rw-r--r--module.xml7
2 files changed, 204 insertions, 3 deletions
diff --git a/code/bbcode.inc.php b/code/bbcode.inc.php
index 0941f5c..5d213f4 100644
--- a/code/bbcode.inc.php
+++ b/code/bbcode.inc.php
@@ -1,5 +1,203 @@
<?PHP
class BBCode {
-
+ function Parse($input) {
+ $tags = '';
+ $tl = 0;
+ $tmpok = '';
+ $tmpnok = '';
+ $tmptag = '';
+ $tmpend = '';
+ $tmplist = '';
+ $lists = '';
+
+ $input = strtr($input, array('<' => '&lt;', '>' => '&gt;', '&' => '&amp;', '"' => '&quot;', "\n" => '<br />', "\r" => ''));
+ if($input[0] == '[') {
+ strtok(' ' . $input, '[');
+ $out = '';
+ }
+ else $out = strtok($input, '[');
+
+ while($part = strtok('[')) {
+ if($tmpok) {
+ if(substr($part, 0, strlen($tmpend)) == $tmpend) {
+ $out .= $tmpok . substr($part, strlen($tmpend));
+ $tags = $tmptag . $tags;
+ $lists = $tmplist . $lists;
+ $tl = strcspn($tags, ']');
+ $tmpok = '';
+ $tmpnok = '';
+ $tmptag = '';
+ $tmpend = '';
+ $tmplist = '';
+ continue;
+ }
+ else {
+ $out .= '[' . $tmpnok;
+ $tmpok = '';
+ $tmpnok = '';
+ $tmptag = '';
+ $tmpend = '';
+ $tmplist = '';
+ }
+ }
+
+ if(substr($part, 0, 2) == 'b]') {
+ $out .= '<span style="font-weight:bold">' . substr($part, 2);
+ $tags = '[/b]' . $tags;
+ }
+ elseif(substr($part, 0, 2) == 'i]') {
+ $out .= '<span style="font-style:italic">' . substr($part, 2);
+ $tags = '[/i]' . $tags;
+ }
+ elseif(substr($part, 0, 2) == 'u]') {
+ $out .= '<span style="text-decoration:underline">' . substr($part, 2);
+ $tags = '[/u]' . $tags;
+ }
+ elseif(substr($part, 0, 5) == 'size=') {
+ $size = intval(substr($part, 5));
+ if($size == 9 && $part[6] == ']') {
+ $out .= '<span style="font-size:9pt">' . substr($part, 7);
+ $tags = '[/size]' . $tags;
+ }
+ elseif(($size >= 10 && $size <= 24) && $part[7] == ']') {
+ $out .= '<span style="font-size:' . $size . 'pt">' . substr($part, 8);
+ $tags = '[/size]' . $tags;
+ }
+ else $out .= '[' . $part;
+ }
+ elseif(substr($part, 0, 6) == 'color=') {
+ $l = strspn(substr($part, 6), 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890#(),%');
+ if($part[$l+6] == ']') {
+ $out .= '<span style="color:' . substr($part, 6, $l) . '">' . substr($part, $l+7);
+ $tags = '[/color]' . $tags;
+ }
+ else $out .= '[' . $part;
+ }
+ elseif(substr($part, 0, 5) == 'code]') {
+ $out .= '<div class="bbcode_code"><span class="bbcode_code_caption">Code:</span><div class="bbode_code_text">' . substr($part, 5);
+ $tags = '[/code]' . $tags;
+ }
+ elseif(substr($part, 0, 4) == 'url]') {
+ $tmpok = '<a href="' . substr($part, 4) . '">' . substr($part, 4) . '</a>';
+ $tmpnok = $part;
+ $tmpend = '/url]';
+ }
+ elseif(substr($part, 0, 4) == 'url=') {
+ $l = strcspn(substr($part, 4), '[] ');
+ if($part[$l+4] == ']') {
+ $out .= '<a href="' . substr($part, 4, $l) . '">' . substr($part, $l+5);
+ $tags = '[/url]' . $tags;
+ }
+ else $out .= '[' . $part;
+ }
+ elseif(substr($part, 0, 6) == 'email]') {
+ $tmpok = '<a href="mailto:' . substr($part, 6) . '">' . substr($part, 6) . '</a>';
+ $tmpnok = $part;
+ $tmpend = '/email]';
+ }
+ elseif(substr($part, 0, 6) == 'email=') {
+ $l = strcspn(substr($part, 6), '[] ');
+ if($part[$l+6] == ']') {
+ $out .= '<a href="mailto:' . substr($part, 6, $l) . '">' . substr($part, $l+7);
+ $tags = '[/email]' . $tags;
+ }
+ else $out .= '[' . $part;
+ }
+ elseif(substr($part, 0, 4) == 'img]') {
+ $tmpok = '<img src="images/' . substr($part, 4) . '.jpg" alt="" />';
+ $tmpnok = $part;
+ $tmpend = '/img]';
+ }
+ elseif(substr($part, 0, 5) == 'list]') {
+ $tmpok = substr($part, 5) . '<ul><li>';
+ $tmpnok = $part;
+ $tmptag = '[/list]';
+ $tmpend = '*]';
+ $tmplist = 'u';
+ }
+ elseif(substr($part, 0, 7) == 'list=1]') {
+ $tmpok = substr($part, 7) . '<ol style="list-style-type:decimal"><li>';
+ $tmpnok = $part;
+ $tmptag = '[/list]';
+ $tmpend = '*]';
+ $tmplist = 'o';
+ }
+ elseif(substr($part, 0, 7) == 'list=a]') {
+ $tmpok = substr($part, 7) . '<ol style="list-style-type:lower-latin"><li>';
+ $tmpnok = $part;
+ $tmptag = '[/list]';
+ $tmpend = '*]';
+ $tmplist = 'o';
+ }
+ elseif(substr($part, 0, 2) == '*]') {
+ if(substr($tags, 0, 7) == '[/list]') $out .= '</li><li>' . substr($part, 2);
+ else $out .= '[' . $part;
+ }
+ elseif($tl && substr($part, 0, $tl) == substr($tags, 1, $tl)) {
+ $tags = substr($tags, $tl+1);
+
+ switch(substr($part, 1, $tl-2)) {
+ case 'b':
+ case 'i':
+ case 'u':
+ case 'size':
+ case 'color':
+ $out .= '</span>';
+ break;
+ case 'url':
+ case 'email':
+ $out .= '</a>';
+ break;
+ case 'code':
+ $out .= '</div></div>';
+ break;
+ case 'list':
+ $out .= '</li></' . $lists[0] . 'l>';
+ $lists = substr($lists, 1);
+ }
+
+ $out .= substr($part, $tl);
+ }
+ else $out .= '[' . $part;
+
+ $tl = strcspn($tags, ']');
+ }
+
+ if($tmpok) $out .= '[' . $tmpnok;
+
+ $part = strtok($tags, '[');
+
+ while($part) {
+ $tags = substr($tags, $tl+1);
+
+ switch(substr($part, 1, $tl-2)) {
+ case 'b':
+ case 'i':
+ case 'u':
+ case 'size':
+ case 'color':
+ $out .= '</span>';
+ break;
+ case 'url':
+ case 'email':
+ $out .= '</a>';
+ break;
+ case 'code':
+ $out .= '</blockcode></div>';
+ break;
+ case 'list':
+ $out .= '</li></' . $lists[0] . 'l>';
+ $lists = substr($lists, 1);
+ }
+
+ $tl = strcspn($tags, ']');
+
+ $part = strtok('[');
+ }
+
+ return $out;
+ }
}
+
+ $GLOBALS['bbcode'] = new BBCode;
?>
diff --git a/module.xml b/module.xml
index 085b266..cef3055 100644
--- a/module.xml
+++ b/module.xml
@@ -1,9 +1,12 @@
+<?xml version="1.0" encoding="UTF-8" ?>
<module>
<info>
<name>BBCode</name>
- <desc>A little module that parses BBCode.</desc>
+ <version>0.1</version>
+ <desc>A little module that parses BBCode</desc>
</info>
<files>
- <code>bbcode.inc.php</code>
+ <code>bbcode</code>
+ <template>bbcode</template>
</files>
</module>