author | Dan |
Tue, 13 Nov 2007 22:28:30 -0500 | |
changeset 7 | 37387f84fe25 |
parent 1 | 6f8b7c6fac02 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/* |
|
3 |
* Decir |
|
4 |
* Version 0.1 |
|
5 |
* Copyright (C) 2007 Dan Fuhry |
|
6 |
* bbcode.php - BBcode-to-HTML renderer |
|
7 |
* |
|
8 |
* This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License |
|
9 |
* as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
|
12 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
|
13 |
*/ |
|
14 |
||
1
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
15 |
function render_bbcode($text, $bbcode_uid = false) |
0 | 16 |
{ |
17 |
// First things first, strip out all [code] sections |
|
18 |
$text = decir_bbcode_strip_code($text, $bbcode_uid, $_code); |
|
19 |
||
1
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
20 |
if ( $bbcode_uid ) |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
21 |
$bbcode_uid = ':' . $bbcode_uid; |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
22 |
|
0 | 23 |
// Bold text |
1
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
24 |
$text = preg_replace("/\[b$bbcode_uid\](.*?)\[\/b$bbcode_uid\]/is", '<b>\\1</b>', $text); |
0 | 25 |
|
26 |
// Italicized text |
|
1
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
27 |
$text = preg_replace("/\[i$bbcode_uid\](.*?)\[\/i$bbcode_uid\]/is", '<i>\\1</i>', $text); |
0 | 28 |
|
1
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
29 |
// Underlined text |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
30 |
$text = preg_replace("/\[u$bbcode_uid\](.*?)\[\/u$bbcode_uid\]/is", '<u>\\1</u>', $text); |
0 | 31 |
|
32 |
// Colored text |
|
1
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
33 |
$text = preg_replace("/\[color$bbcode_uid=#([A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]([A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9])?)\](.*?)\[\/color$bbcode_uid\]/is", '<span style="color: #\\1">\\3</span>', $text); |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
34 |
|
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
35 |
// Size |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
36 |
$text = preg_replace("/\[size$bbcode_uid=([0-4]+(\.[0-9]+)?)\](.*?)\[\/size$bbcode_uid\]/is", '<span style="font-size: \\1em;">\\3</span>', $text); |
0 | 37 |
|
38 |
// Quotes |
|
1
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
39 |
$text = preg_replace("/\[quote$bbcode_uid=\"?([^]]+?)\"?\](.*?)\[\/quote$bbcode_uid\]/is", '<span class="decir-quoteheader">\\1 wrote:</span><blockquote>\\2</blockquote>', $text); |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
40 |
$text = preg_replace("/\[quote$bbcode_uid\](.*?)\[\/quote$bbcode_uid\]/is", '<blockquote class="decir-quotebody">\\1</blockquote>', $text); |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
41 |
|
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
42 |
// https?:\/\/((([a-z0-9-]+\.)*)[a-z0-9-]+)(\/[A-z0-9_%\|~`!\!@#\$\^&\*\(\):;\.,\/-]*(\?(([a-z0-9_-]+)(=[A-z0-9_%\|~`\!@#\$\^&\*\(\):;\.,\/-\[\]]+)?((&([a-z0-9_-]+)(=[A-z0-9_%\|~`!\!@#\$\^&\*\(\):;\.,\/-]+)?)*))?)?)? |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
43 |
|
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
44 |
// Links |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
45 |
// Trial and error. |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
46 |
$regexp = "/\[url$bbcode_uid(=(https?:\/\/((([a-z0-9-]+\.)*)[a-z0-9-]+)(\/[A-z0-9_%\|~`!\!@#\$\^&\*\(\):;\.,\/-]*(\?(([a-z0-9_-]+)(=[A-z0-9_%\|~`\!@#\$\^&\*\(\):;\.,\/-\[\]]+)?((&([a-z0-9_-]+)(=[A-z0-9_%\|~`!\!@#\$\^&\*\(\):;\.,\/-]+)?)*))?)?)?))?\](.*?)\[\/url$bbcode_uid\]/is"; |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
47 |
$text = preg_replace($regexp, '<a href="\\2">\\15</a>', $text); |
0 | 48 |
|
49 |
// Newlines |
|
50 |
$text = str_replace("\n", "<br />\n", $text); |
|
51 |
||
7
37387f84fe25
Add edit functionality to forum management and implemented a sick drag-and-drop reordering system for forums
Dan
parents:
1
diff
changeset
|
52 |
// Wikilinks |
37387f84fe25
Add edit functionality to forum management and implemented a sick drag-and-drop reordering system for forums
Dan
parents:
1
diff
changeset
|
53 |
$text = RenderMan::parse_internal_links($text); |
37387f84fe25
Add edit functionality to forum management and implemented a sick drag-and-drop reordering system for forums
Dan
parents:
1
diff
changeset
|
54 |
|
0 | 55 |
// Restore [code] blocks |
56 |
$text = decir_bbcode_restore_code($text, $bbcode_uid, $_code); |
|
57 |
||
58 |
// Code |
|
1
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
59 |
$text = preg_replace("/\[code$bbcode_uid\](.*?)\[\/code$bbcode_uid\]/is", '<pre>\\1</pre>', $text); |
0 | 60 |
|
61 |
return $text; |
|
62 |
} |
|
63 |
||
64 |
function decir_bbcode_strip_code($text, $uid, &$code_secs) |
|
65 |
{ |
|
66 |
preg_match_all("/\[code:$uid\](.*?)\[\/code:$uid\]/is", $text, $matches); |
|
67 |
foreach ( $matches[1] as $i => $m ) |
|
68 |
{ |
|
69 |
$text = str_replace_once($m, "{CODE_SECTION|$i:$uid}", $text); |
|
70 |
$code_secs[$i] = $m; |
|
71 |
} |
|
72 |
return $text; |
|
73 |
} |
|
74 |
||
75 |
function decir_bbcode_restore_code($text, $uid, $code_secs) |
|
76 |
{ |
|
77 |
foreach ( $code_secs as $i => $code ) |
|
78 |
{ |
|
79 |
$text = str_replace("{CODE_SECTION|$i:$uid}", $code, $text); |
|
80 |
} |
|
81 |
return $text; |
|
82 |
} |
|
83 |
||
84 |
function bbcode_strip_uid($bbcode, $uid) |
|
85 |
{ |
|
86 |
// BBcode tags with attributes |
|
87 |
$bbcode = preg_replace("/\[([a-z]+?):{$uid}=([^\]]+?)\](.*?)\[\/\\1:{$uid}\]/is", '[\\1=\\2]\\3[/\\1]', $bbcode); |
|
88 |
||
89 |
// BBcode tags without attributes |
|
90 |
$bbcode = preg_replace("/\[([a-z]+?):{$uid}\](.*?)\[\/\\1:{$uid}\]/is", '[\\1]\\2[/\\1]', $bbcode); |
|
91 |
||
92 |
return $bbcode; |
|
93 |
} |
|
94 |
||
1
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
95 |
function bbcode_inject_uid($text, &$uid) |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
96 |
{ |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
97 |
$seed = md5( implode('.', explode(' ', microtime)) ); |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
98 |
$uid = substr($seed, 0, 10); |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
99 |
// Bold text |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
100 |
$text = preg_replace("/\[b\](.*?)\[\/b\]/is", "[b:$uid]\\1[/b:$uid]", $text); |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
101 |
|
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
102 |
// Italicized text |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
103 |
$text = preg_replace("/\[i\](.*?)\[\/i\]/is", "[i:$uid]\\1[/i:$uid]", $text); |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
104 |
|
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
105 |
// Uunderlined text |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
106 |
$text = preg_replace("/\[u\](.*?)\[\/u\]/is", "[u:$uid]\\1[/u:$uid]", $text); |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
107 |
|
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
108 |
// Colored text |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
109 |
$text = preg_replace("/\[color=\#([A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]([A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9])?)\](.*?)\[\/color\]/is", "[color:$uid=#\\1]\\3[/color:$uid]", $text); |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
110 |
|
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
111 |
// Size |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
112 |
$text = preg_replace('/\[size=([0-4]+(\.[0-9]+)?)\](.*?)\[\/size\]/is', "[size:$uid=\\1]\\3[/size:$uid]", $text); |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
113 |
|
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
114 |
// Quotes |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
115 |
$text = preg_replace("/\[quote\](.*?)\[\/quote\]/is", "[quote:$uid]\\1[/quote:$uid]", $text); |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
116 |
$text = preg_replace("/\[quote=\"?([^]]+)\"?\](.*?)\[\/quote\]/is", "[quote:$uid=\\1]\\2[/quote:$uid]", $text); |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
117 |
|
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
118 |
// Code |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
119 |
$text = preg_replace("/\[code\](.*?)\[\/code\]/is", "[code:$uid]\\1[/code:$uid]", $text); |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
120 |
|
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
121 |
// URLs |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
122 |
$text = preg_replace('/\[url(=https?:\/\/([^ ]+))?\](.*?)\[\/url\]/is', "[url:$uid\\1]\\3[/url:$uid]", $text); |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
123 |
|
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
124 |
return $text; |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
125 |
} |
6f8b7c6fac02
Let's just say: major progress and still only 20% complete. So many changes I forgot to commit.
Dan
parents:
0
diff
changeset
|
126 |