Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
4.1 KiB

4 years ago
4 years ago
4 years ago
  1. <?php
  2. /**
  3. * @file mod/babel.php
  4. */
  5. use Friendica\Content\Text;
  6. use Friendica\Core\L10n;
  7. use Friendica\Core\Renderer;
  8. function visible_whitespace($s)
  9. {
  10. $s = str_replace(' ', '&nbsp;', $s);
  11. return str_replace(["\r\n", "\n", "\r"], '<br />', $s);
  12. }
  13. function babel_content()
  14. {
  15. $results = [];
  16. if (!empty($_REQUEST['text'])) {
  17. switch (defaults($_REQUEST, 'type', 'bbcode')) {
  18. case 'bbcode':
  19. $bbcode = trim($_REQUEST['text']);
  20. $results[] = [
  21. 'title' => L10n::t('Source input'),
  22. 'content' => visible_whitespace($bbcode)
  23. ];
  24. $plain = Text\BBCode::toPlaintext($bbcode, false);
  25. $results[] = [
  26. 'title' => L10n::t('BBCode::toPlaintext'),
  27. 'content' => visible_whitespace($plain)
  28. ];
  29. $html = Text\BBCode::convert($bbcode);
  30. $results[] = [
  31. 'title' => L10n::t('BBCode::convert (raw HTML)'),
  32. 'content' => visible_whitespace(htmlspecialchars($html))
  33. ];
  34. $results[] = [
  35. 'title' => L10n::t('BBCode::convert'),
  36. 'content' => $html
  37. ];
  38. $bbcode2 = Text\HTML::toBBCode($html);
  39. $results[] = [
  40. 'title' => L10n::t('BBCode::convert => HTML::toBBCode'),
  41. 'content' => visible_whitespace($bbcode2)
  42. ];
  43. $markdown = Text\BBCode::toMarkdown($bbcode);
  44. $results[] = [
  45. 'title' => L10n::t('BBCode::toMarkdown'),
  46. 'content' => visible_whitespace($markdown)
  47. ];
  48. $html2 = Text\Markdown::convert($markdown);
  49. $results[] = [
  50. 'title' => L10n::t('BBCode::toMarkdown => Markdown::convert'),
  51. 'content' => $html2
  52. ];
  53. $bbcode3 = Text\Markdown::toBBCode($markdown);
  54. $results[] = [
  55. 'title' => L10n::t('BBCode::toMarkdown => Markdown::toBBCode'),
  56. 'content' => visible_whitespace($bbcode3)
  57. ];
  58. $bbcode4 = Text\HTML::toBBCode($html2);
  59. $results[] = [
  60. 'title' => L10n::t('BBCode::toMarkdown => Markdown::convert => HTML::toBBCode'),
  61. 'content' => visible_whitespace($bbcode4)
  62. ];
  63. break;
  64. case 'markdown':
  65. $markdown = trim($_REQUEST['text']);
  66. $results[] = [
  67. 'title' => L10n::t('Source input (Diaspora format)'),
  68. 'content' => '<pre>' . $markdown . '</pre>'
  69. ];
  70. $html = Text\Markdown::convert($markdown);
  71. $results[] = [
  72. 'title' => L10n::t('Markdown::convert (raw HTML)'),
  73. 'content' => htmlspecialchars($html)
  74. ];
  75. $results[] = [
  76. 'title' => L10n::t('Markdown::convert'),
  77. 'content' => $html
  78. ];
  79. $bbcode = Text\Markdown::toBBCode($markdown);
  80. $results[] = [
  81. 'title' => L10n::t('Markdown::toBBCode'),
  82. 'content' => '<pre>' . $bbcode . '</pre>'
  83. ];
  84. break;
  85. case 'html' :
  86. $html = trim($_REQUEST['text']);
  87. $results[] = [
  88. 'title' => L10n::t('Raw HTML input'),
  89. 'content' => htmlspecialchars($html)
  90. ];
  91. $results[] = [
  92. 'title' => L10n::t('HTML Input'),
  93. 'content' => $html
  94. ];
  95. $bbcode = Text\HTML::toBBCode($html);
  96. $results[] = [
  97. 'title' => L10n::t('HTML::toBBCode'),
  98. 'content' => visible_whitespace($bbcode)
  99. ];
  100. $html2 = Text\BBCode::convert($bbcode);
  101. $results[] = [
  102. 'title' => L10n::t('HTML::toBBCode => BBCode::convert'),
  103. 'content' => $html2
  104. ];
  105. $results[] = [
  106. 'title' => L10n::t('HTML::toBBCode => BBCode::convert (raw HTML)'),
  107. 'content' => htmlspecialchars($html2)
  108. ];
  109. $markdown = Text\HTML::toMarkdown($html);
  110. $results[] = [
  111. 'title' => L10n::t('HTML::toMarkdown'),
  112. 'content' => visible_whitespace($markdown)
  113. ];
  114. $text = Text\HTML::toPlaintext($html);
  115. $results[] = [
  116. 'title' => L10n::t('HTML::toPlaintext'),
  117. 'content' => '<pre>' . $text . '</pre>'
  118. ];
  119. }
  120. }
  121. $tpl = Renderer::getMarkupTemplate('babel.tpl');
  122. $o = Renderer::replaceMacros($tpl, [
  123. '$text' => ['text', L10n::t('Source text'), defaults($_REQUEST, 'text', ''), ''],
  124. '$type_bbcode' => ['type', L10n::t('BBCode'), 'bbcode', '', defaults($_REQUEST, 'type', 'bbcode') == 'bbcode'],
  125. '$type_markdown' => ['type', L10n::t('Markdown'), 'markdown', '', defaults($_REQUEST, 'type', 'bbcode') == 'markdown'],
  126. '$type_html' => ['type', L10n::t('HTML'), 'html', '', defaults($_REQUEST, 'type', 'bbcode') == 'html'],
  127. '$results' => $results
  128. ]);
  129. return $o;
  130. }