From d6985baaed150b1dc1e8ace1d86b097c2c8861fb Mon Sep 17 00:00:00 2001
From: Hypolite Petovan <mrpetovan@gmail.com>
Date: Mon, 17 Sep 2018 23:18:34 -0400
Subject: [PATCH] Improve mod/babel

- Handle multiple line endings
- Conserve HTML special chars in input box on submit
- Add new result panel with HTML::toMarkdown result
---
 mod/babel.php | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/mod/babel.php b/mod/babel.php
index fcd63113e8..e5ae96be70 100644
--- a/mod/babel.php
+++ b/mod/babel.php
@@ -10,7 +10,7 @@ function visible_whitespace($s)
 {
 	$s = str_replace(' ', '&nbsp;', $s);
 
-	return str_replace("\n", '<br />', $s);
+	return str_replace(["\r\n", "\n", "\r"], '<br />', $s);
 }
 
 function babel_content()
@@ -114,6 +114,12 @@ function babel_content()
 					'content' => visible_whitespace($bbcode)
 				];
 
+				$markdown = Text\HTML::toMarkdown($html);
+				$results[] = [
+					'title' => L10n::t('HTML::toMarkdown'),
+					'content' => visible_whitespace($markdown)
+				];
+
 				$text = Text\HTML::toPlaintext($html);
 				$results[] = [
 					'title' => L10n::t('HTML::toPlaintext'),
@@ -124,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'],
-- 
2.39.5