diff --git a/composer.json b/composer.json index 04e4b655d..c71777550 100644 --- a/composer.json +++ b/composer.json @@ -18,13 +18,12 @@ "asika/simple-console": "^1.0", "divineomega/password_exposed": "^2.4", "ezyang/htmlpurifier": "~4.7.0", - "league/html-to-markdown": "~4.4.1", + "league/html-to-markdown": "~4.8.0", "lightopenid/lightopenid": "dev-master", "michelf/php-markdown": "^1.7", "mobiledetect/mobiledetectlib": "2.8.*", "paragonie/random_compat": "^2.0", "pear/Text_LanguageDetect": "1.*", - "pear/Text_Highlighter": "dev-master", "seld/cli-prompt": "^1.0", "smarty/smarty": "^3.1", "fxp/composer-asset-plugin": "~1.3", diff --git a/composer.lock b/composer.lock index 76c20a1b9..b0ff745c6 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "d62c3e3d6971ee63a862a22ff3cd3768", + "content-hash": "5f6a43237dc52758484cd21cd76e8ce6", "packages": [ { "name": "asika/simple-console", @@ -483,16 +483,16 @@ }, { "name": "league/html-to-markdown", - "version": "4.4.1", + "version": "4.8.0", "source": { "type": "git", "url": "https://github.com/thephpleague/html-to-markdown.git", - "reference": "82ea375b5b2b1da1da222644c0565c695bf88186" + "reference": "f9a879a068c68ff47b722de63f58bec79e448f9d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/thephpleague/html-to-markdown/zipball/82ea375b5b2b1da1da222644c0565c695bf88186", - "reference": "82ea375b5b2b1da1da222644c0565c695bf88186", + "url": "https://api.github.com/repos/thephpleague/html-to-markdown/zipball/f9a879a068c68ff47b722de63f58bec79e448f9d", + "reference": "f9a879a068c68ff47b722de63f58bec79e448f9d", "shasum": "" }, "require": { @@ -511,7 +511,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "4.5-dev" + "dev-master": "4.9-dev" } }, "autoload": { @@ -524,17 +524,17 @@ "MIT" ], "authors": [ - { - "name": "Colin O'Dell", - "email": "colinodell@gmail.com", - "homepage": "http://www.colinodell.com", - "role": "Lead Developer" - }, { "name": "Nick Cernis", "email": "nick@cern.is", "homepage": "http://modernnerd.net", "role": "Original Author" + }, + { + "name": "Colin O'Dell", + "email": "colinodell@gmail.com", + "homepage": "https://www.colinodell.com", + "role": "Lead Developer" } ], "description": "An HTML-to-markdown conversion helper for PHP", @@ -543,7 +543,7 @@ "html", "markdown" ], - "time": "2017-03-16T00:45:59+00:00" + "time": "2018-09-18T12:18:08+00:00" }, { "name": "lightopenid/lightopenid", @@ -1669,204 +1669,6 @@ ], "time": "2018-02-15T05:50:20+00:00" }, - { - "name": "pear/console_getopt", - "version": "v1.4.1", - "source": { - "type": "git", - "url": "https://github.com/pear/Console_Getopt.git", - "reference": "82f05cd1aa3edf34e19aa7c8ca312ce13a6a577f" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/pear/Console_Getopt/zipball/82f05cd1aa3edf34e19aa7c8ca312ce13a6a577f", - "reference": "82f05cd1aa3edf34e19aa7c8ca312ce13a6a577f", - "shasum": "" - }, - "type": "library", - "autoload": { - "psr-0": { - "Console": "./" - } - }, - "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "./" - ], - "license": [ - "BSD-2-Clause" - ], - "authors": [ - { - "name": "Greg Beaver", - "email": "cellog@php.net", - "role": "Helper" - }, - { - "name": "Andrei Zmievski", - "email": "andrei@php.net", - "role": "Lead" - }, - { - "name": "Stig Bakken", - "email": "stig@php.net", - "role": "Developer" - } - ], - "description": "More info available on: http://pear.php.net/package/Console_Getopt", - "time": "2015-07-20T20:28:12+00:00" - }, - { - "name": "pear/pear-core-minimal", - "version": "v1.10.3", - "source": { - "type": "git", - "url": "https://github.com/pear/pear-core-minimal.git", - "reference": "070f0b600b2caca2501e2c9b7e553016e4b0d115" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/pear/pear-core-minimal/zipball/070f0b600b2caca2501e2c9b7e553016e4b0d115", - "reference": "070f0b600b2caca2501e2c9b7e553016e4b0d115", - "shasum": "" - }, - "require": { - "pear/console_getopt": "~1.4", - "pear/pear_exception": "~1.0" - }, - "replace": { - "rsky/pear-core-min": "self.version" - }, - "type": "library", - "autoload": { - "psr-0": { - "": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "src/" - ], - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Christian Weiske", - "email": "cweiske@php.net", - "role": "Lead" - } - ], - "description": "Minimal set of PEAR core files to be used as composer dependency", - "time": "2017-02-28T16:46:11+00:00" - }, - { - "name": "pear/pear_exception", - "version": "v1.0.0", - "source": { - "type": "git", - "url": "https://github.com/pear/PEAR_Exception.git", - "reference": "8c18719fdae000b690e3912be401c76e406dd13b" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/pear/PEAR_Exception/zipball/8c18719fdae000b690e3912be401c76e406dd13b", - "reference": "8c18719fdae000b690e3912be401c76e406dd13b", - "shasum": "" - }, - "require": { - "php": ">=4.4.0" - }, - "require-dev": { - "phpunit/phpunit": "*" - }, - "type": "class", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "psr-0": { - "PEAR": "" - } - }, - "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "." - ], - "license": [ - "BSD-2-Clause" - ], - "authors": [ - { - "name": "Helgi Thormar", - "email": "dufuz@php.net" - }, - { - "name": "Greg Beaver", - "email": "cellog@php.net" - } - ], - "description": "The PEAR Exception base class.", - "homepage": "https://github.com/pear/PEAR_Exception", - "keywords": [ - "exception" - ], - "time": "2015-02-10T20:07:52+00:00" - }, - { - "name": "pear/text_highlighter", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/pear/Text_Highlighter.git", - "reference": "2ccac2d9eaf55dc08bbbdb7136c93fb399d0f855" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/pear/Text_Highlighter/zipball/2ccac2d9eaf55dc08bbbdb7136c93fb399d0f855", - "reference": "2ccac2d9eaf55dc08bbbdb7136c93fb399d0f855", - "shasum": "" - }, - "require": { - "pear/pear-core-minimal": "~1.10.0", - "php": ">=5.4.0" - }, - "require-dev": { - "phpunit/phpunit": "@stable" - }, - "type": "library", - "autoload": { - "psr-0": { - "Text": "./" - } - }, - "include-path": [ - "./" - ], - "license": [ - "PHP-3.01" - ], - "authors": [ - { - "email": "ssttoo@gmail.com", - "name": "Stoyan Stefanov", - "role": "Lead" - }, - { - "email": "demenev@gmail.com", - "name": "Andrey Demenev", - "role": "Lead" - } - ], - "description": "More info available on: http://pear.php.net/package/Text_Highlighter", - "support": { - "issues": "http://pear.php.net/bugs/search.php?cmd=display&package_name[]=Text_Highlighter", - "source": "https://github.com/pear/Text_Highlighter" - }, - "time": "2018-01-27T08:24:15+00:00" - }, { "name": "pear/text_languagedetect", "version": "v1.0.0", @@ -3145,7 +2947,7 @@ } ], "description": "Provides the functionality to compare PHP values for equality", - "homepage": "https://github.com/sebastianbergmann/comparator", + "homepage": "http://www.github.com/sebastianbergmann/comparator", "keywords": [ "comparator", "compare", @@ -3247,7 +3049,7 @@ } ], "description": "Provides functionality to handle HHVM/PHP environments", - "homepage": "https://github.com/sebastianbergmann/environment", + "homepage": "http://www.github.com/sebastianbergmann/environment", "keywords": [ "Xdebug", "environment", @@ -3315,7 +3117,7 @@ } ], "description": "Provides the functionality to export PHP variables for visualization", - "homepage": "https://github.com/sebastianbergmann/exporter", + "homepage": "http://www.github.com/sebastianbergmann/exporter", "keywords": [ "export", "exporter" @@ -3367,7 +3169,7 @@ } ], "description": "Snapshotting of global state", - "homepage": "https://github.com/sebastianbergmann/global-state", + "homepage": "http://www.github.com/sebastianbergmann/global-state", "keywords": [ "global state" ], @@ -3469,7 +3271,7 @@ } ], "description": "Provides functionality to recursively process PHP variables", - "homepage": "https://github.com/sebastianbergmann/recursion-context", + "homepage": "http://www.github.com/sebastianbergmann/recursion-context", "time": "2016-11-19T07:33:16+00:00" }, { @@ -3669,8 +3471,7 @@ "aliases": [], "minimum-stability": "stable", "stability-flags": { - "lightopenid/lightopenid": 20, - "pear/text_highlighter": 20 + "lightopenid/lightopenid": 20 }, "prefer-stable": false, "prefer-lowest": false, diff --git a/include/text.php b/include/text.php index d251824e2..97baee7f6 100644 --- a/include/text.php +++ b/include/text.php @@ -1911,58 +1911,3 @@ function format_network_name($network, $url = 0) { return $network_name; } } - -/** - * @brief Syntax based code highlighting for popular languages. - * @param string $s Code block - * @param string $lang Programming language - * @return string Formated html - */ -function text_highlight($s, $lang) { - if ($lang === 'js') { - $lang = 'javascript'; - } - - if ($lang === 'bash') { - $lang = 'sh'; - } - - // @TODO: Replace Text_Highlighter_Renderer_Html by scrivo/highlight.php - - // Autoload the library to make constants available - class_exists('Text_Highlighter_Renderer_Html'); - - $options = [ - 'numbers' => HL_NUMBERS_LI, - 'tabsize' => 4, - ]; - - $tag_added = false; - $s = trim(html_entity_decode($s, ENT_COMPAT)); - $s = str_replace(' ', "\t", $s); - - /* - * The highlighter library insists on an opening php tag for php code blocks. If - * it isn't present, nothing is highlighted. So we're going to see if it's present. - * If not, we'll add it, and then quietly remove it after we get the processed output back. - */ - if ($lang === 'php' && strpos($s, 'factory($lang); - $hl->setRenderer($renderer); - $o = $hl->highlight($s); - $o = str_replace("\n", '', $o); - - if ($tag_added) { - $b = substr($o, 0, strpos($o, '
' . $o . '
';
-}
diff --git a/mod/babel.php b/mod/babel.php
index 3352366bd..e5ae96be7 100644
--- a/mod/babel.php
+++ b/mod/babel.php
@@ -6,9 +6,11 @@
use Friendica\Content\Text;
use Friendica\Core\L10n;
-function visible_lf($s)
+function visible_whitespace($s)
{
- return str_replace("\n", '' . $markdown . '' ]; + $html = Text\Markdown::convert($markdown); + $results[] = [ + 'title' => L10n::t('Markdown::convert (raw HTML)'), + 'content' => htmlspecialchars($html) + ]; + + $results[] = [ + 'title' => L10n::t('Markdown::convert'), + 'content' => $html + ]; + $bbcode = Text\Markdown::toBBCode($markdown); $results[] = [ 'title' => L10n::t('Markdown::toBBCode'), @@ -86,7 +99,7 @@ function babel_content() case 'html' : $html = trim($_REQUEST['text']); $results[] = [ - 'title' => L10n::t("Raw HTML input"), + 'title' => L10n::t('Raw HTML input'), 'content' => htmlspecialchars($html) ]; @@ -98,7 +111,13 @@ function babel_content() $bbcode = Text\HTML::toBBCode($html); $results[] = [ 'title' => L10n::t('HTML::toBBCode'), - 'content' => visible_lf($bbcode) + 'content' => visible_whitespace($bbcode) + ]; + + $markdown = Text\HTML::toMarkdown($html); + $results[] = [ + 'title' => L10n::t('HTML::toMarkdown'), + 'content' => visible_whitespace($markdown) ]; $text = Text\HTML::toPlaintext($html); @@ -111,7 +130,7 @@ function babel_content() $tpl = get_markup_template('babel.tpl'); $o = replace_macros($tpl, [ - '$text' => ['text', L10n::t('Source text'), defaults($_REQUEST, 'text', ''), ''], + '$text' => ['text', L10n::t('Source text'), htmlentities(defaults($_REQUEST, 'text', '')), ''], '$type_bbcode' => ['type', L10n::t('BBCode'), 'bbcode', '', defaults($_REQUEST, 'type', 'bbcode') == 'bbcode'], '$type_markdown' => ['type', L10n::t('Markdown'), 'markdown', '', defaults($_REQUEST, 'type', 'bbcode') == 'markdown'], '$type_html' => ['type', L10n::t('HTML'), 'html', '', defaults($_REQUEST, 'type', 'bbcode') == 'html'], diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index 2dc13ae5e..c3453bcf7 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -25,7 +25,6 @@ use Friendica\Util\Map; use Friendica\Util\Network; use Friendica\Util\ParseUrl; use Friendica\Util\Proxy as ProxyUtils; -use League\HTMLToMarkdown\HtmlConverter; class BBCode extends BaseObject { @@ -348,7 +347,7 @@ class BBCode extends BaseObject */ public static function toPlaintext($text, $keep_urls = true) { - $naked_text = preg_replace('/\[(.+?)\]/','', $text); + $naked_text = preg_replace('/\[(.+?)\]\s*/','', $text); if (!$keep_urls) { $naked_text = preg_replace('#https?\://[^\s<]+[^\s\.\)]#i', '', $naked_text); } @@ -1164,21 +1163,6 @@ class BBCode extends BaseObject return $return; } - private static function textHighlightCallback($match) - { - // Fallback in case the language doesn't exist - $return = '[code]' . $match[2] . '[/code]'; - - if (in_array(strtolower($match[1]), - ['php', 'css', 'mysql', 'sql', 'abap', 'diff', 'html', 'perl', 'ruby', - 'vbscript', 'avrc', 'dtd', 'java', 'xml', 'cpp', 'python', 'javascript', 'js', 'sh', 'bash']) - ) { - $return = text_highlight($match[2], strtolower($match[1])); - } - - return $return; - } - /** * @brief Converts a BBCode message to HTML message * @@ -1227,6 +1211,22 @@ class BBCode extends BaseObject return $return; }; + // Extracting multi-line code blocks before the whitespace processing + $codeblocks = []; + + $text = preg_replace_callback("#\[code(?:=([^\]]*))?\](.*?)\[\/code\]#is", + function ($matches) use (&$codeblocks) { + $return = $matches[0]; + if (strpos($matches[2], "\n") !== false) { + $return = '#codeblock-' . count($codeblocks) . '#'; + + $codeblocks[] = '
' . trim($matches[2], "\n\r") . '
';
+ }
+ return $return;
+ },
+ $text
+ );
+
// Hide all [noparse] contained bbtags by spacefying them
// POSSIBLE BUG --> Will the 'preg' functions crash if there's an embedded image?
@@ -1273,11 +1273,6 @@ class BBCode extends BaseObject
$text = preg_replace("/\[share(.*?)avatar\s?=\s?'.*?'\s?(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism", "\n[share$1$2]$3[/share]", $text);
}
- // Check for [code] text here, before the linefeeds are messed with.
- // The highlighter will unescape and re-escape the content.
- if (strpos($text, '[code=') !== false) {
- $text = preg_replace_callback("/\[code=(.*?)\](.*?)\[\/code\]/ism", 'self::textHighlightCallback', $text);
- }
// Convert new line chars to html (.*)
#iUs',
+ '#(.*)
#iUs',
function ($matches) use (&$codeblocks) {
$return = '[codeblock-' . count($codeblocks) . ']';
@@ -131,7 +132,7 @@ class HTML
$prefix = '[code=' . $matches[1] . ']';
}
- $codeblocks[] = $prefix . trim($matches[2]) . '[/code]';
+ $codeblocks[] = $prefix . PHP_EOL . trim($matches[2]) . PHP_EOL . '[/code]';
return $return;
},
$message
@@ -672,4 +673,19 @@ class HTML
return trim($message);
}
+
+ /**
+ * Converts provided HTML code to Markdown. The hardwrap parameter maximizes
+ * compatibility with Diaspora in spite of the Markdown standards.
+ *
+ * @param string $html
+ * @return string
+ */
+ public static function toMarkdown($html)
+ {
+ $converter = new HtmlConverter(['hard_break' => true]);
+ $markdown = $converter->convert($html);
+
+ return $markdown;
+ }
}
diff --git a/src/Content/Text/Markdown.php b/src/Content/Text/Markdown.php
index c50319e4c..0687b5377 100644
--- a/src/Content/Text/Markdown.php
+++ b/src/Content/Text/Markdown.php
@@ -32,6 +32,7 @@ class Markdown extends BaseObject
$MarkdownParser = new MarkdownExtra();
$MarkdownParser->hard_wrap = $hardwrap;
+ $MarkdownParser->code_class_prefix = 'language-';
$html = $MarkdownParser->transform($text);
self::getApp()->save_timestamp($stamp1, "parser");