I work on a fluxbb to flarum converter in php, I would like to know how to properly convert bbcode posts to flarum lang. I thought I understood that flarum core use your s9e/TextFormatter, that's true ?
<?php
...
$postData = array(
':id' => $post['id'],
':discussion_id' => $post['topic_id'],
':number' => $currentPostNumber,
':time' => ConvertTimestampToDatetime(intval($post['posted'])),
':user_id' => $userId,
':type' => 'comment',
':content' => FormatText($post['message']),
':edit_time' => ($post['edited']) ? ConvertTimestampToDatetime(intval($post['edited'])) : null,
':edit_user_id' => ($post['edited_by']) ? GetUserID($dbFlarum, $post['edited_by']) : null,
':is_approved' => 1
);
$query = RunPreparedQuery($dbFlarum, $postData, "INSERT INTO posts(id,discussion_id,number,time,user_id,type,content,edit_time,edit_user_id,is_approved) VALUES(:id,:discussion_id,:number,:time,:user_id,:type,:content,:edit_time,:edit_user_id,:is_approved)");
$postsMigrated += $query->rowCount();
...
function FormatText($text) {
$text = preg_replace('#\:\w+#', '', $text);
$text = ConvertBBCodeToFlarumLang($text);
$text = str_replace(""","\"",$text);
$text = preg_replace('|[[\/\!]*?[^\[\]]*?]|si', '', $text);
// Wrap text lines with paragraph tags
$explodedText = explode("\n", $text);
foreach ($explodedText as $key => $value)
{
// Only wrap in a paragraph tag if the line has actual text
if(strlen($value) > 1)
$explodedText[$key] = '<p>' . $value . '</p>';
}
$text = implode("\n", $explodedText);
return $text;
}
function ConvertBBCodeToFlarumLang($text) {
$text = preg_replace('#\[b](.+)\[\/b]#', "<B><s>[b]</s>$1<e>[/b]</e></B>", $text);
$text = preg_replace('#\[i](.+)\[\/i]#', "<I><s>[i]</s>$1<e>[/i]</e></I>", $text);
$text = preg_replace('#\[u](.+)\[\/u]#', "<U><s>[u]</s>$1<e>[/u]</e></U>", $text);
$text = preg_replace('#\[s](.+)\[\/s]#', "<S><s>[s]</s>$1<e>[/s]</e></S>", $text);
$text = preg_replace('#\[h](.+)\[\/h]#', "<H1><s># </s>$1</H1>", $text);
$text = preg_replace('#\[center](.+)\[\/center]#', "<CENTER><s>[center]</s>$1<e>[/center]</e></CENTER>", $text);
$text = preg_replace('#\[color=\#(\w+)](.+)\[\/color]#', "<COLOR color=\"#$1\"><s>[color=]</s>$2<e>[/color]</e></COLOR>", $text);
$text = preg_replace('#\[quote](.+)\[\/quote]#', "<QUOTE><i>></i>$1</QUOTE>", $text);
$text = preg_replace('#\[quote=(.+)](.+)\[\/quote]#', "<QUOTE author=\"$1\"><s>[quote=$1]</s><p>$2</p><e>[/quote]</e></QUOTE>", $text);
$text = preg_replace('#\[url](.+)\[\/url]#', "<URL url=\"$1\"><s>[url=$1]</s>$1<e>[/url]</e></URL>", $text);
$text = preg_replace('#\[url=(.+)](.+)\[\/url]#', "<URL url=\"$1\"><s>[url=$1]</s>$2<e>[/url]</e></URL>", $text);
$text = preg_replace('#\[img](.+)\[\/img]#', "<IMG src=\"$1\"><s>[img]</s>$1<e>[/img]</e></IMG>", $text);
$text = preg_replace('#\[img=(.+)](.+)\[\/img]#', "<IMG alt=\"$1\" src=\"$2\"><s>![</s>$1<e>]($2)</e></IMG>", $text);
$text = preg_replace('#\[code](.+)\[\/code]#is', "<CODE><s>```</s>$1<e>```</e></CODE>", $text);
$text = preg_replace('#\[pre](.+)\[\/pre]#is', "<CODE><s>```</s>$1<e>```</e></CODE>", $text);
$text = preg_replace('#\[list=\*](.+)\[\/list]#is', "<LIST><s>[list=*]</s>$1<e>[/list]</e></LIST>", $text);
$text = preg_replace('#\[\*](.+)\[\/\*]#is', "<LI><s>[*]</s><p>$1</p><e>[/*]</e></LI>", $text);
return $text;
}
Can you give me some hints/suggestions for improving my converter ? I intend to make it open-source when it will be stable.