X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Fhtml2bbcode.php;h=1deab951958ac3a6750ac956a2289af6be3581e5;hb=71a5de40f105234aa8082844d7839c3e0e49e905;hp=0dafecc71fcca9f017d9a62413782aba5781872d;hpb=d653e27e2dfc172fd66a0987312952d736aca3e7;p=friendica.git
diff --git a/include/html2bbcode.php b/include/html2bbcode.php
old mode 100755
new mode 100644
index 0dafecc71f..1deab95195
--- a/include/html2bbcode.php
+++ b/include/html2bbcode.php
@@ -1,11 +1,14 @@
$value) {
- $startbb = str_replace('%'.++$i, '$1', $startbb);
+ $startbb = str_replace('\x01'.++$i, '$1', $startbb);
if (strpos('*'.$startbb, '$1') > 0) {
@@ -76,28 +79,34 @@ function node2bbcodesub(&$doc, $oldnode, $attributes, $startbb, $endbb)
return($replace);
}
-function deletenode(&$doc, $node)
-{
- $xpath = new DomXPath($doc);
- $list = $xpath->query("//".$node);
- foreach ($list as $child)
- $child->parentNode->removeChild($child);
-}
-
function html2bbcode($message)
{
- //$file = tempnam("/tmp/", "html");
- //file_put_contents($file, $message);
-
$message = str_replace("\r", "", $message);
+ // Removing code blocks before the whitespace removal processing below
+ $codeblocks = [];
+ $message = preg_replace_callback('#
(.*)
#iUs',
+ function ($matches) use (&$codeblocks) {
+ $return = '[codeblock-' . count($codeblocks) . ']';
+
+ $prefix = '[code]';
+ if ($matches[1] != '') {
+ $prefix = '[code=' . $matches[1] . ']';
+ }
+ $codeblocks[] = $prefix . $matches[2] . '[/code]';
+ return $return;
+ }
+ , $message);
+
$message = str_replace(array(
"",
- "
"),
+ "",
+ ),
array(
"",
- ""),
+ "",
+ ),
$message);
// remove namespaces
@@ -111,12 +120,12 @@ function html2bbcode($message)
@$doc->loadHTML($message);
- deletenode($doc, 'style');
- deletenode($doc, 'head');
- deletenode($doc, 'title');
- deletenode($doc, 'meta');
- deletenode($doc, 'xml');
- deletenode($doc, 'removeme');
+ xml::deleteNode($doc, 'style');
+ xml::deleteNode($doc, 'head');
+ xml::deleteNode($doc, 'title');
+ xml::deleteNode($doc, 'meta');
+ xml::deleteNode($doc, 'xml');
+ xml::deleteNode($doc, 'removeme');
$xpath = new DomXPath($doc);
$list = $xpath->query("//pre");
@@ -124,7 +133,7 @@ function html2bbcode($message)
$node->nodeValue = str_replace("\n", "\r", $node->nodeValue);
$message = $doc->saveHTML();
- $message = str_replace(array("\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"), array("<", ">", "
", " ", ""), $message);
+ $message = str_replace(array("\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"), array("<", ">", "
", " ", ""), $message);
$message = preg_replace('= [\s]*=i', " ", $message);
@$doc->loadHTML($message);
@@ -142,26 +151,33 @@ function html2bbcode($message)
node2bbcode($doc, 'span', array('style'=>'font-style: italic;'), '[i]', '[/i]');
node2bbcode($doc, 'span', array('style'=>'font-weight: bold;'), '[b]', '[/b]');
- node2bbcode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[font=$1][size=$2][color=$3]', '[/color][/size][/font]');
+ /*node2bbcode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[font=$1][size=$2][color=$3]', '[/color][/size][/font]');
node2bbcode($doc, 'font', array('size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[size=$1][color=$2]', '[/color][/size]');
node2bbcode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(.+)/'), '[font=$1][size=$2]', '[/size][/font]');
node2bbcode($doc, 'font', array('face'=>'/([\w ]+)/', 'color'=>'/(.+)/'), '[font=$1][color=$3]', '[/color][/font]');
node2bbcode($doc, 'font', array('face'=>'/([\w ]+)/'), '[font=$1]', '[/font]');
node2bbcode($doc, 'font', array('size'=>'/(\d+)/'), '[size=$1]', '[/size]');
node2bbcode($doc, 'font', array('color'=>'/(.+)/'), '[color=$1]', '[/color]');
-
+*/
// Untested
//node2bbcode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*font-family:\s*(.+?)[,;].*color:\s*(.+?)[,;].*/'), '[size=$1][font=$2][color=$3]', '[/color][/font][/size]');
//node2bbcode($doc, 'span', array('style'=>'/.*font-size:\s*(\d+)[,;].*/'), '[size=$1]', '[/size]');
//node2bbcode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*/'), '[size=$1]', '[/size]');
node2bbcode($doc, 'span', array('style'=>'/.*color:\s*(.+?)[,;].*/'), '[color="$1"]', '[/color]');
+
//node2bbcode($doc, 'span', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
//node2bbcode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)pt.*/'), '[font=$1][size=$2]', '[/size][/font]');
//node2bbcode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)px.*/'), '[font=$1][size=$2]', '[/size][/font]');
//node2bbcode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
+ // Importing the classes - interesting for importing of posts from third party networks that were exported from friendica
+ // Test
+ //node2bbcode($doc, 'span', array('class'=>'/([\w ]+)/'), '[class=$1]', '[/class]');
+ node2bbcode($doc, 'span', array('class'=>'type-link'), '[class=type-link]', '[/class]');
+ node2bbcode($doc, 'span', array('class'=>'type-video'), '[class=type-video]', '[/class]');
+
node2bbcode($doc, 'strong', array(), '[b]', '[/b]');
node2bbcode($doc, 'em', array(), '[i]', '[/i]');
node2bbcode($doc, 'b', array(), '[b]', '[/b]');
@@ -182,6 +198,7 @@ function html2bbcode($message)
node2bbcode($doc, 'span', array(), "", "");
node2bbcode($doc, 'pre', array(), "", "");
+
node2bbcode($doc, 'div', array(), "\r", "\r");
node2bbcode($doc, 'p', array(), "\n", "\n");
@@ -191,21 +208,29 @@ function html2bbcode($message)
node2bbcode($doc, 'hr', array(), "[hr]", "");
- //node2bbcode($doc, 'table', array(), "", "");
- //node2bbcode($doc, 'tr', array(), "\n", "");
- //node2bbcode($doc, 'td', array(), "\t", "");
- node2bbcode($doc, 'table', array(), "[table]", "[/table]");
- node2bbcode($doc, 'th', array(), "[th]", "[/th]");
- node2bbcode($doc, 'tr', array(), "[tr]", "[/tr]");
- node2bbcode($doc, 'td', array(), "[td]", "[/td]");
-
- node2bbcode($doc, 'h1', array(), "\n\n[size=xx-large][b]", "[/b][/size]\n");
- node2bbcode($doc, 'h2', array(), "\n\n[size=x-large][b]", "[/b][/size]\n");
- node2bbcode($doc, 'h3', array(), "\n\n[size=large][b]", "[/b][/size]\n");
- node2bbcode($doc, 'h4', array(), "\n\n[size=medium][b]", "[/b][/size]\n");
- node2bbcode($doc, 'h5', array(), "\n\n[size=small][b]", "[/b][/size]\n");
- node2bbcode($doc, 'h6', array(), "\n\n[size=x-small][b]", "[/b][/size]\n");
-
+ node2bbcode($doc, 'table', array(), "", "");
+ node2bbcode($doc, 'tr', array(), "\n", "");
+ node2bbcode($doc, 'td', array(), "\t", "");
+ //node2bbcode($doc, 'table', array(), "[table]", "[/table]");
+ //node2bbcode($doc, 'th', array(), "[th]", "[/th]");
+ //node2bbcode($doc, 'tr', array(), "[tr]", "[/tr]");
+ //node2bbcode($doc, 'td', array(), "[td]", "[/td]");
+
+ //node2bbcode($doc, 'h1', array(), "\n\n[size=xx-large][b]", "[/b][/size]\n");
+ //node2bbcode($doc, 'h2', array(), "\n\n[size=x-large][b]", "[/b][/size]\n");
+ //node2bbcode($doc, 'h3', array(), "\n\n[size=large][b]", "[/b][/size]\n");
+ //node2bbcode($doc, 'h4', array(), "\n\n[size=medium][b]", "[/b][/size]\n");
+ //node2bbcode($doc, 'h5', array(), "\n\n[size=small][b]", "[/b][/size]\n");
+ //node2bbcode($doc, 'h6', array(), "\n\n[size=x-small][b]", "[/b][/size]\n");
+
+ node2bbcode($doc, 'h1', array(), "\n\n[h1]", "[/h1]\n");
+ node2bbcode($doc, 'h2', array(), "\n\n[h2]", "[/h2]\n");
+ node2bbcode($doc, 'h3', array(), "\n\n[h3]", "[/h3]\n");
+ node2bbcode($doc, 'h4', array(), "\n\n[h4]", "[/h4]\n");
+ node2bbcode($doc, 'h5', array(), "\n\n[h5]", "[/h5]\n");
+ node2bbcode($doc, 'h6', array(), "\n\n[h6]", "[/h6]\n");
+
+ node2bbcode($doc, 'a', array('href'=>'/mailto:(.+)/'), '[mail=$1]', '[/mail]');
node2bbcode($doc, 'a', array('href'=>'/(.+)/'), '[url=$1]', '[/url]');
node2bbcode($doc, 'img', array('src'=>'/(.+)/', 'width'=>'/(\d+)/', 'height'=>'/(\d+)/'), '[img=$2x$3]$1', '[/img]');
@@ -216,7 +241,7 @@ function html2bbcode($message)
node2bbcode($doc, 'audio', array('src'=>'/(.+)/'), '[audio]$1', '[/audio]');
node2bbcode($doc, 'iframe', array('src'=>'/(.+)/'), '[iframe]$1', '[/iframe]');
- node2bbcode($doc, 'code', array(), '[code]', '[/code]');
+ node2bbcode($doc, 'key', array(), '[code]', '[/code]');
$message = $doc->saveHTML();
@@ -285,6 +310,18 @@ function html2bbcode($message)
// Handling Yahoo style of mails
$message = str_replace('[hr][b]From:[/b]', '[quote][b]From:[/b]', $message);
- return(trim($message));
+ // Restore code blocks
+ $message = preg_replace_callback('#\[codeblock-([0-9]+)\]#iU',
+ function ($matches) use ($codeblocks) {
+ $return = '';
+ if (isset($codeblocks[intval($matches[1])])) {
+ $return = $codeblocks[$matches[1]];
+ }
+ return $return;
+ }
+ , $message);
+
+ $message = trim($message);
+
+ return $message;
}
-?>