Fix code blocks to Diaspora
- Extracts code blocks before BBCode conversion to prevent code highlighting and whitespace meddling - Use the improved HTLM To Markdown library - Use <code> instead of <key> for Diaspora inline code blocks
This commit is contained in:
parent
8149e21add
commit
b0accf4d4c
|
@ -105,8 +105,18 @@ function diaspora_mentions($match) {
|
||||||
return $mention;
|
return $mention;
|
||||||
}
|
}
|
||||||
|
|
||||||
function bb2diaspora($Text,$preserve_nl = false, $fordiaspora = true) {
|
/**
|
||||||
|
* @brief Converts a BBCode text into Markdown
|
||||||
|
*
|
||||||
|
* This function converts a BBCode item body to be sent to Markdown-enabled
|
||||||
|
* systems like Diaspora and Libertree
|
||||||
|
*
|
||||||
|
* @param string $Text
|
||||||
|
* @param bool $preserve_nl Effects unclear, unused in Friendica
|
||||||
|
* @param bool $fordiaspora Diaspora requires more changes than Libertree
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
function bb2diaspora($Text, $preserve_nl = false, $fordiaspora = true) {
|
||||||
$a = get_app();
|
$a = get_app();
|
||||||
|
|
||||||
$OriginalText = $Text;
|
$OriginalText = $Text;
|
||||||
|
@ -129,6 +139,18 @@ function bb2diaspora($Text,$preserve_nl = false, $fordiaspora = true) {
|
||||||
// Converting images with size parameters to simple images. Markdown doesn't know it.
|
// Converting images with size parameters to simple images. Markdown doesn't know it.
|
||||||
$Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $Text);
|
$Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $Text);
|
||||||
|
|
||||||
|
// Extracting multi-line code blocks before the whitespace processing/code highlighter in bbcode()
|
||||||
|
$codeblocks = [];
|
||||||
|
$Text = preg_replace_callback('#\[code(?:=([^\]]*))?\](?=\n)(.*?)\[\/code\]#is',
|
||||||
|
function ($matches) use (&$codeblocks) {
|
||||||
|
$return = '#codeblock-' . count($codeblocks) . '#';
|
||||||
|
|
||||||
|
$prefix = '````' . $matches[1] . PHP_EOL;
|
||||||
|
$codeblocks[] = $prefix . trim($matches[2]) . PHP_EOL . '````';
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
, $Text);
|
||||||
|
|
||||||
// Convert it to HTML - don't try oembed
|
// Convert it to HTML - don't try oembed
|
||||||
if ($fordiaspora) {
|
if ($fordiaspora) {
|
||||||
$Text = bbcode($Text, $preserve_nl, false, 3);
|
$Text = bbcode($Text, $preserve_nl, false, 3);
|
||||||
|
@ -158,7 +180,8 @@ function bb2diaspora($Text,$preserve_nl = false, $fordiaspora = true) {
|
||||||
$stamp1 = microtime(true);
|
$stamp1 = microtime(true);
|
||||||
|
|
||||||
// Now convert HTML to Markdown
|
// Now convert HTML to Markdown
|
||||||
$Text = new HTML_To_Markdown($Text);
|
$converter = new HtmlConverter();
|
||||||
|
$Text = $converter->convert($Text);
|
||||||
|
|
||||||
// unmask the special chars back to HTML
|
// unmask the special chars back to HTML
|
||||||
$Text = str_replace(array('&_lt_;', '&_gt_;', '&_amp_;'), array('<', '>', '&'), $Text);
|
$Text = str_replace(array('&_lt_;', '&_gt_;', '&_amp_;'), array('<', '>', '&'), $Text);
|
||||||
|
@ -177,6 +200,17 @@ function bb2diaspora($Text,$preserve_nl = false, $fordiaspora = true) {
|
||||||
$Text = preg_replace_callback("/([@]\[(.*?)\])\(([$URLSearchString]*?)\)/ism", 'diaspora_mentions', $Text);
|
$Text = preg_replace_callback("/([@]\[(.*?)\])\(([$URLSearchString]*?)\)/ism", 'diaspora_mentions', $Text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Restore code blocks
|
||||||
|
$Text = preg_replace_callback('/#codeblock-([0-9]+)#/iU',
|
||||||
|
function ($matches) use ($codeblocks) {
|
||||||
|
$return = '';
|
||||||
|
if (isset($codeblocks[intval($matches[1])])) {
|
||||||
|
$return = $codeblocks[$matches[1]];
|
||||||
|
}
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
, $Text);
|
||||||
|
|
||||||
call_hooks('bb2diaspora',$Text);
|
call_hooks('bb2diaspora',$Text);
|
||||||
|
|
||||||
return $Text;
|
return $Text;
|
||||||
|
|
|
@ -159,13 +159,6 @@ function stripcode_br_cb($s) {
|
||||||
return '[code]' . str_replace('<br />', '', $s[1]) . '[/code]';
|
return '[code]' . str_replace('<br />', '', $s[1]) . '[/code]';
|
||||||
}
|
}
|
||||||
|
|
||||||
function bb_onelinecode_cb($match) {
|
|
||||||
if (strpos($match[1],"<br>")===false){
|
|
||||||
return "<key>".$match[1]."</key>";
|
|
||||||
}
|
|
||||||
return "<code>".$match[1]."</code>";
|
|
||||||
}
|
|
||||||
|
|
||||||
function tryoembed($match) {
|
function tryoembed($match) {
|
||||||
$url = $match[1];
|
$url = $match[1];
|
||||||
|
|
||||||
|
@ -729,9 +722,31 @@ function bb_highlight($match) {
|
||||||
return $match[0];
|
return $match[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
// BBcode 2 HTML was written by WAY2WEB.net
|
/**
|
||||||
// extended to work with Mistpark/Friendica - Mike Macgirvin
|
* @brief Converts a BBCode message to HTML message
|
||||||
|
*
|
||||||
|
* BBcode 2 HTML was written by WAY2WEB.net
|
||||||
|
* extended to work with Mistpark/Friendica - Mike Macgirvin
|
||||||
|
*
|
||||||
|
* Simple HTML values meaning:
|
||||||
|
* - 0: Friendica display
|
||||||
|
* - 1: Unused
|
||||||
|
* - 2: Used for Facebook, Google+, Windows Phone push, Friendica API
|
||||||
|
* - 3: Used before converting to Markdown in bb2diaspora.php
|
||||||
|
* - 4: Used for WordPress, Libertree (before Markdown), pump.io and tumblr
|
||||||
|
* - 5: Unused
|
||||||
|
* - 6: Used for Appnet
|
||||||
|
* - 7: Used for dfrn, OStatus
|
||||||
|
* - 8: Used for WP backlink text setting
|
||||||
|
*
|
||||||
|
* @staticvar array $allowed_src_protocols
|
||||||
|
* @param string $Text
|
||||||
|
* @param bool $preserve_nl
|
||||||
|
* @param bool $tryoembed
|
||||||
|
* @param int $simplehtml
|
||||||
|
* @param bool $forplaintext
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
function bbcode($Text, $preserve_nl = false, $tryoembed = true, $simplehtml = false, $forplaintext = false) {
|
function bbcode($Text, $preserve_nl = false, $tryoembed = true, $simplehtml = false, $forplaintext = false) {
|
||||||
|
|
||||||
$a = get_app();
|
$a = get_app();
|
||||||
|
@ -1158,8 +1173,17 @@ function bbcode($Text, $preserve_nl = false, $tryoembed = true, $simplehtml = fa
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//replace oneliner <code> with <key>
|
// Replace inline code blocks
|
||||||
$Text = preg_replace_callback("|(?!<br[^>]*>)<code>([^<]*)</code>(?!<br[^>]*>)|ism", 'bb_onelinecode_cb', $Text);
|
$Text = preg_replace_callback("|(?!<br[^>]*>)<code>([^<]*)</code>(?!<br[^>]*>)|ism",
|
||||||
|
function ($match) use ($simplehtml) {
|
||||||
|
$return = '<key>' . $match[1] . '</key>';
|
||||||
|
// Use <code> for Diaspora inline code blocks
|
||||||
|
if ($simplehtml === 3) {
|
||||||
|
$return = '<code>' . $match[1] . '</code>';
|
||||||
|
}
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
, $Text);
|
||||||
|
|
||||||
// Unhide all [noparse] contained bbtags unspacefying them
|
// Unhide all [noparse] contained bbtags unspacefying them
|
||||||
// and triming the [noparse] tag.
|
// and triming the [noparse] tag.
|
||||||
|
|
Loading…
Reference in a new issue