Skip to content

Commit 4a04c89

Browse files
Implements abstract SMF\Parser class & sub-classes
Signed-off-by: Jon Stovell <jonstovell@gmail.com>
1 parent 5c8fe7e commit 4a04c89

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1426
-1543
lines changed

Sources/Actions/Admin/ACP.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use SMF\Actions\MessageIndex;
2020
use SMF\Actions\Notify;
2121
use SMF\ActionTrait;
22-
use SMF\BBCodeParser;
2322
use SMF\Cache\CacheApi;
2423
use SMF\Config;
2524
use SMF\Db\DatabaseApi as Db;
@@ -28,6 +27,7 @@
2827
use SMF\Lang;
2928
use SMF\Mail;
3029
use SMF\Menu;
30+
use SMF\Parser;
3131
use SMF\SecurityToken;
3232
use SMF\Theme;
3333
use SMF\Url;
@@ -976,7 +976,7 @@ public static function prepareDBSettingContext(array &$config_vars): void
976976
// What about any BBC selection boxes?
977977
if (!empty($bbcChoice)) {
978978
// What are the options, eh?
979-
$temp = BBCodeParser::getCodes();
979+
$temp = Parser::getBBCodes();
980980
$bbcTags = [];
981981

982982
foreach ($temp as $tag) {
@@ -1351,7 +1351,7 @@ public static function saveDBSettings(array &$config_vars): void
13511351
elseif ($var[0] == 'bbc') {
13521352
$bbcTags = [];
13531353

1354-
foreach (BBCodeParser::getCodes() as $tag) {
1354+
foreach (Parser::getBBCodes() as $tag) {
13551355
$bbcTags[] = $tag['tag'];
13561356
}
13571357

Sources/Actions/Admin/Boards.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use SMF\ActionInterface;
1919
use SMF\Actions\BackwardCompatibility;
2020
use SMF\ActionTrait;
21-
use SMF\BBCodeParser;
2221
use SMF\Board;
2322
use SMF\Category;
2423
use SMF\Config;
@@ -28,6 +27,7 @@
2827
use SMF\IntegrationHook;
2928
use SMF\Lang;
3029
use SMF\Menu;
30+
use SMF\Parser;
3131
use SMF\SecurityToken;
3232
use SMF\Theme;
3333
use SMF\Url;
@@ -366,7 +366,7 @@ public function editCategory2(): void
366366

367367
// Try to get any valid HTML to BBC first, add a naive attempt to strip it off, htmlspecialchars for the rest
368368
$catOptions['cat_name'] = Utils::htmlspecialchars(strip_tags($_POST['cat_name']));
369-
$catOptions['cat_desc'] = Utils::htmlspecialchars(strip_tags(BBCodeParser::load()->unparse($_POST['cat_desc'])));
369+
$catOptions['cat_desc'] = Utils::htmlspecialchars(strip_tags(Parser::transform($_POST['cat_desc'], Parser::OUTPUT_BBC)));
370370
$catOptions['is_collapsible'] = isset($_POST['collapse']);
371371

372372
if (isset($_POST['add'])) {
@@ -677,7 +677,7 @@ public function editBoard2(): void
677677

678678
// Try to get any valid HTML to BBC first, add a naive attempt to strip it off, htmlspecialchars for the rest
679679
$boardOptions['board_name'] = Utils::htmlspecialchars(strip_tags($_POST['board_name']));
680-
$boardOptions['board_description'] = Utils::htmlspecialchars(strip_tags(BBCodeParser::load()->unparse($_POST['desc'])));
680+
$boardOptions['board_description'] = Utils::htmlspecialchars(strip_tags(Parser::transform($_POST['desc'], Parser::OUTPUT_BBC)));
681681

682682
$boardOptions['moderator_string'] = $_POST['moderators'];
683683

Sources/Actions/Admin/ErrorLog.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
use SMF\ActionInterface;
1919
use SMF\ActionTrait;
20-
use SMF\BBCodeParser;
2120
use SMF\Config;
2221
use SMF\Db\DatabaseApi as Db;
2322
use SMF\ErrorHandler;
2423
use SMF\IP;
2524
use SMF\Lang;
2625
use SMF\PageIndex;
26+
use SMF\Parser;
2727
use SMF\SecurityToken;
2828
use SMF\Theme;
2929
use SMF\Time;
@@ -430,7 +430,7 @@ public function viewFile(): void
430430
ErrorHandler::fatalLang('error_bad_line');
431431
}
432432

433-
$file_data = explode('<br />', BBCodeParser::highlightPhpCode(Utils::htmlspecialchars(file_get_contents($file))));
433+
$file_data = explode('<br />', Parser::highlightPhpCode(Utils::htmlspecialchars(file_get_contents($file))));
434434

435435
// We don't want to slice off too many so lets make sure we stop at the last one
436436
$max = min($max, max(array_keys($file_data)));

Sources/Actions/Admin/Features.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
use SMF\Actions\BackwardCompatibility;
2020
use SMF\Actions\Profile\Notification;
2121
use SMF\ActionTrait;
22-
use SMF\BBCodeParser;
2322
use SMF\Config;
2423
use SMF\Db\DatabaseApi as Db;
2524
use SMF\ErrorHandler;
2625
use SMF\Graphics\Image;
2726
use SMF\IntegrationHook;
2827
use SMF\ItemList;
2928
use SMF\Lang;
30-
use SMF\MarkdownParser;
3129
use SMF\Menu;
30+
use SMF\Parser;
31+
use SMF\Parsers\MarkdownParser;
3232
use SMF\Profile;
3333
use SMF\Sapi;
3434
use SMF\SecurityToken;
@@ -185,7 +185,7 @@ public function bbc(): void
185185
$bbcTags = [];
186186
$bbcTagsChildren = [];
187187

188-
foreach (BBCodeParser::getCodes() as $tag) {
188+
foreach (Parser::getBBCodes() as $tag) {
189189
$bbcTags[] = $tag['tag'];
190190

191191
if (isset($tag['require_children'])) {
@@ -194,8 +194,8 @@ public function bbc(): void
194194
}
195195

196196
// Clean up tags with children
197-
foreach($bbcTagsChildren as $parent_tag => $children) {
198-
foreach($children as $index => $child_tag) {
197+
foreach ($bbcTagsChildren as $parent_tag => $children) {
198+
foreach ($children as $index => $child_tag) {
199199
// Remove entries where parent and child tag is the same
200200
if ($child_tag == $parent_tag) {
201201
unset($bbcTagsChildren[$parent_tag][$index]);
@@ -592,7 +592,7 @@ public function signature(): void
592592
// Clean up the tag stuff!
593593
$bbcTags = [];
594594

595-
foreach (BBCodeParser::getCodes() as $tag) {
595+
foreach (Parser::getBBCodes() as $tag) {
596596
$bbcTags[] = $tag['tag'];
597597
}
598598

@@ -1035,7 +1035,7 @@ public function profileEdit(): void
10351035
[],
10361036
);
10371037

1038-
while($row = Db::$db->fetch_assoc($request)) {
1038+
while ($row = Db::$db->fetch_assoc($request)) {
10391039
$fields[] = $row['id_field'];
10401040
}
10411041
Db::$db->free_result($request);

Sources/Actions/Admin/News.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use SMF\Actions\BackwardCompatibility;
2020
use SMF\Actions\Notify;
2121
use SMF\ActionTrait;
22-
use SMF\BBCodeParser;
2322
use SMF\Config;
2423
use SMF\Db\DatabaseApi as Db;
2524
use SMF\Editor;
@@ -31,6 +30,7 @@
3130
use SMF\Mail;
3231
use SMF\Menu;
3332
use SMF\Msg;
33+
use SMF\Parser;
3434
use SMF\PersonalMessage\PM;
3535
use SMF\SecurityToken;
3636
use SMF\Theme;
@@ -1106,7 +1106,7 @@ public static function list_getNews(): array
11061106
$admin_current_news[$id] = [
11071107
'id' => $id,
11081108
'unparsed' => Msg::un_preparsecode($line),
1109-
'parsed' => preg_replace('~<([/]?)form[^>]*?[>]*>~i', '<em class="smalltext">&lt;$1form&gt;</em>', BBCodeParser::load()->parse($line)),
1109+
'parsed' => preg_replace('~<([/]?)form[^>]*?[>]*>~i', '<em class="smalltext">&lt;$1form&gt;</em>', Parser::transform($line)),
11101110
];
11111111
}
11121112

@@ -1193,7 +1193,7 @@ public static function prepareMailingForPreview(): void
11931193
if (!empty(Utils::$context['send_html'])) {
11941194
$enablePostHTML = Config::$modSettings['enablePostHTML'];
11951195
Config::$modSettings['enablePostHTML'] = Utils::$context['send_html'];
1196-
Utils::$context[$key] = BBCodeParser::load()->parse(Utils::$context[$key]);
1196+
Utils::$context[$key] = Parser::transform(Utils::$context[$key]);
11971197
Config::$modSettings['enablePostHTML'] = $enablePostHTML;
11981198
}
11991199

Sources/Actions/Admin/Smileys.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use SMF\Actions\BackwardCompatibility;
2222
use SMF\Actions\MessageIndex;
2323
use SMF\ActionTrait;
24-
use SMF\BBCodeParser;
2524
use SMF\Cache\CacheApi;
2625
use SMF\Config;
2726
use SMF\Db\DatabaseApi as Db;
@@ -33,6 +32,7 @@
3332
use SMF\Menu;
3433
use SMF\Msg;
3534
use SMF\PackageManager\SubsPackage;
35+
use SMF\Parser;
3636
use SMF\SecurityToken;
3737
use SMF\Theme;
3838
use SMF\User;
@@ -1622,7 +1622,7 @@ public function install(): void
16221622

16231623
if (!empty($action['parse_bbc'])) {
16241624
Msg::preparsecode(Utils::$context[$type]);
1625-
Utils::$context[$type] = BBCodeParser::load()->parse(Utils::$context[$type]);
1625+
Utils::$context[$type] = Parser::transform(Utils::$context[$type]);
16261626
} else {
16271627
Utils::$context[$type] = nl2br(Utils::$context[$type]);
16281628
}

Sources/Actions/Agreement.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717

1818
use SMF\ActionInterface;
1919
use SMF\ActionTrait;
20-
use SMF\BBCodeParser;
2120
use SMF\Config;
2221
use SMF\ErrorHandler;
2322
use SMF\Lang;
24-
use SMF\MarkdownParser;
23+
use SMF\Parser;
2524
use SMF\Theme;
2625
use SMF\User;
2726
use SMF\Utils;
@@ -149,31 +148,32 @@ protected function prepareAgreementContext(): void
149148

150149
if (!empty(Utils::$context['agreement_file'])) {
151150
$cache_id = strtr(Utils::$context['agreement_file'], [Config::$languagesdir => '', '.txt' => '', '.' => '_']);
152-
Utils::$context['agreement'] = BBCodeParser::load()->parse(file_get_contents(Utils::$context['agreement_file']), true, $cache_id);
151+
Utils::$context['agreement'] = Parser::transform(
152+
string: file_get_contents(Utils::$context['agreement_file']),
153+
options: ['cache_id' => $cache_id, 'hard_breaks' => 0],
154+
);
153155
} elseif (Utils::$context['can_accept_agreement']) {
154156
ErrorHandler::fatalLang('error_no_agreement', false);
155157
}
156-
157-
if (!empty(Config::$modSettings['enableMarkdown'])) {
158-
Utils::$context['agreement'] = MarkdownParser::load(MarkdownParser::OUTPUT_HTML, 0)->parse(Utils::$context['agreement'], true);
159-
}
160158
}
161159

162160
if (!Utils::$context['accept_doc'] || Utils::$context['can_accept_privacy_policy']) {
163161
// Have we got a localized policy?
164162
if (!empty(Config::$modSettings['policy_' . User::$me->language])) {
165-
Utils::$context['privacy_policy'] = BBCodeParser::load()->parse(Config::$modSettings['policy_' . User::$me->language]);
163+
Utils::$context['privacy_policy'] = Parser::transform(
164+
string: Config::$modSettings['policy_' . User::$me->language],
165+
options: ['hard_breaks' => 0],
166+
);
166167
} elseif (!empty(Config::$modSettings['policy_' . Lang::$default])) {
167-
Utils::$context['privacy_policy'] = BBCodeParser::load()->parse(Config::$modSettings['policy_' . Lang::$default]);
168+
Utils::$context['privacy_policy'] = Parser::transform(
169+
string: Config::$modSettings['policy_' . Lang::$default],
170+
options: ['hard_breaks' => 0],
171+
);
168172
}
169173
// Then I guess we've got nothing
170174
elseif (Utils::$context['can_accept_privacy_policy']) {
171175
ErrorHandler::fatalLang('error_no_privacy_policy', false);
172176
}
173-
174-
if (!empty(Config::$modSettings['enableMarkdown'])) {
175-
Utils::$context['privacy_policy'] = MarkdownParser::load(MarkdownParser::OUTPUT_HTML, 0)->parse(Utils::$context['privacy_policy'], true);
176-
}
177177
}
178178
}
179179
}

Sources/Actions/Announce.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
use SMF\ActionInterface;
1919
use SMF\ActionTrait;
20-
use SMF\BBCodeParser;
2120
use SMF\Board;
2221
use SMF\BrowserDetector;
2322
use SMF\Config;
@@ -27,7 +26,7 @@
2726
use SMF\Lang;
2827
use SMF\Logging;
2928
use SMF\Mail;
30-
use SMF\MarkdownParser;
29+
use SMF\Parser;
3130
use SMF\Theme;
3231
use SMF\Topic;
3332
use SMF\User;
@@ -168,11 +167,7 @@ public function send(): void
168167
Lang::censorText(Utils::$context['topic_subject']);
169168
Lang::censorText($message);
170169

171-
$message = BBCodeParser::load()->parse($message, false, $id_msg);
172-
173-
if (!empty(Config::$modSettings['enableMarkdown'])) {
174-
$message = MarkdownParser::load()->parse($message, true);
175-
}
170+
$message = Parser::transform(string: $message, options: ['cache_id' => $id_msg]);
176171

177172
$message = trim(Utils::htmlspecialcharsDecode(strip_tags(strtr($message, ['<br>' => "\n", '</div>' => "\n", '</li>' => "\n", '<p>' => '', '</p>' => "\n\n", '&#91;' => '[', '&#93;' => ']']))));
178173

Sources/Actions/Feed.php

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use SMF\ActionTrait;
2020
use SMF\Attachment;
2121
use SMF\Autolinker;
22-
use SMF\BBCodeParser;
2322
use SMF\Board;
2423
use SMF\BrowserDetector;
2524
use SMF\Cache\CacheApi;
@@ -29,7 +28,7 @@
2928
use SMF\IntegrationHook;
3029
use SMF\IP;
3130
use SMF\Lang;
32-
use SMF\MarkdownParser;
31+
use SMF\Parser;
3332
use SMF\Sapi;
3433
use SMF\Theme;
3534
use SMF\Time;
@@ -779,11 +778,11 @@ public function getXmlNews(): array
779778
$row['body'] = strtr(Utils::entitySubstr(str_replace('<br>', "\n", $row['body']), 0, Config::$modSettings['xmlnews_maxlen'] - 3), ["\n" => '<br>']) . '...';
780779
}
781780

782-
$row['body'] = BBCodeParser::load()->parse($row['body'], (bool) $row['smileys_enabled'], (int) $row['id_msg']);
783-
784-
if (!empty(Config::$modSettings['enableMarkdown'])) {
785-
$row['body'] = MarkdownParser::load()->parse($row['body'], true);
786-
}
781+
$row['body'] = Parser::transform(
782+
string: $row['body'],
783+
input_types: Parser::INPUT_BBC | Parser::INPUT_MARKDOWN | ((bool) $row['smileys_enabled'] ? Parser::INPUT_SMILEYS : 0),
784+
options: ['cache_id' => (int) $row['id_msg']],
785+
);
787786

788787
Lang::censorText($row['body']);
789788
Lang::censorText($row['subject']);
@@ -1223,11 +1222,11 @@ public function getXmlRecent(): array
12231222
$row['body'] = strtr(Utils::entitySubstr(str_replace('<br>', "\n", $row['body']), 0, Config::$modSettings['xmlnews_maxlen'] - 3), ["\n" => '<br>']) . '...';
12241223
}
12251224

1226-
$row['body'] = BBCodeParser::load()->parse($row['body'], (bool) $row['smileys_enabled'], (int) $row['id_msg']);
1227-
1228-
if (!empty(Config::$modSettings['enableMarkdown'])) {
1229-
$row['body'] = MarkdownParser::load()->parse($row['body'], true);
1230-
}
1225+
$row['body'] = Parser::transform(
1226+
string: $row['body'],
1227+
input_types: Parser::INPUT_BBC | Parser::INPUT_MARKDOWN | ((bool) $row['smileys_enabled'] ? Parser::INPUT_SMILEYS : 0),
1228+
options: ['cache_id' => (int) $row['id_msg']],
1229+
);
12311230

12321231
Lang::censorText($row['body']);
12331232
Lang::censorText($row['subject']);
@@ -1991,11 +1990,11 @@ public function getXmlPosts(): array
19911990
}
19921991

19931992
// If using our own format, we want both the raw and the parsed content.
1994-
$row[$this->format === 'smf' ? 'body_html' : 'body'] = BBCodeParser::load()->parse($row['body'], (bool) $row['smileys_enabled'], (int) $row['id_msg']);
1995-
1996-
if (!empty(Config::$modSettings['enableMarkdown'])) {
1997-
$row[$this->format === 'smf' ? 'body_html' : 'body'] = MarkdownParser::load()->parse($row[$this->format === 'smf' ? 'body_html' : 'body'], true);
1998-
}
1993+
$row[$this->format === 'smf' ? 'body_html' : 'body'] = Parser::transform(
1994+
string: $row['body'],
1995+
input_types: Parser::INPUT_BBC | Parser::INPUT_MARKDOWN | ((bool) $row['smileys_enabled'] ? Parser::INPUT_SMILEYS : 0),
1996+
options: ['cache_id' => (int) $row['id_msg']],
1997+
);
19991998

20001999
// Do we want to include any attachments?
20012000
if (!empty(Config::$modSettings['attachmentEnable']) && !empty(Config::$modSettings['xmlnews_attachments'])) {
@@ -2446,11 +2445,7 @@ public function getXmlPMs(): array
24462445
}
24472446

24482447
// If using our own format, we want both the raw and the parsed content.
2449-
$row[$this->format === 'smf' ? 'body_html' : 'body'] = BBCodeParser::load()->parse($row['body']);
2450-
2451-
if (!empty(Config::$modSettings['enableMarkdown'])) {
2452-
$row[$this->format === 'smf' ? 'body_html' : 'body'] = MarkdownParser::load()->parse($row[$this->format === 'smf' ? 'body_html' : 'body'], true);
2453-
}
2448+
$row[$this->format === 'smf' ? 'body_html' : 'body'] = Parser::transform($row['body']);
24542449

24552450
$recipients = array_combine(explode(',', $row['id_members_to']), explode($separator, $row['to_names']));
24562451

0 commit comments

Comments
 (0)