Guest User

Untitled

a guest
Nov 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. function simpledown_make_html_nolinks($text, $format, &$toc, $createToc = false) {
  2.  
  3. $labels = array();
  4. $storeVerbatimPieces = function($match) use (&$labels) {
  5. // remove comments
  6. // comment in strings inside <script>... and other blocks are safe, because of split
  7. if (substr($match[0], 0, 4) == '<!--') return '';
  8.  
  9. $label = '<div>@@' . uniqid() . '</div>';
  10. $labels[$label] = $match[0];
  11. return $label;
  12. };
  13. $text = preg_replace_callback('~<!--.*?-->|<(pre|script|style|object).*?>.*?<\\/\\1>~sim', $storeVerbatimPieces, $text);
  14.  
  15. $text = simpledown_make_paragraphs($text);
  16.  
  17. $text = str_replace(array_keys($labels), array_values($labels), $text);
  18.  
  19. return $text;
  20. }
  21.  
  22.  
  23. function simpledown_make_paragraphs($chunk) {
  24. $block = '(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|p|h[1-6]|hr)';
  25.  
  26. $chunk = preg_replace('|\n*$|', '', $chunk) . "\n\n"; // just to make things a little easier, pad the end
  27. $chunk = preg_replace('|<br />\s*<br />|', "\n\n", $chunk);
  28. $chunk = preg_replace('!(<' . $block . '[^>]*>)!', "\n$1", $chunk); // Space things out a little
  29. $chunk = preg_replace('!(</' . $block . '>)!', "$1\n\n", $chunk); // Space things out a little
  30. $chunk = preg_replace("/\n\n+/", "\n\n", $chunk); // take care of duplicates
  31. $chunk = preg_replace('/^\n|\n\s*\n$/', '', $chunk);
  32. $chunk = '<p>' . preg_replace('/\n\s*\n\n?(.)/', "</p>\n<p>$1", $chunk) . "</p>\n"; // make paragraphs, including one at the end
  33. $chunk = preg_replace("|<p>(<li.+?)</p>|", "$1", $chunk); // problem with nested lists
  34. $chunk = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $chunk);
  35. $chunk = str_replace('</blockquote></p>', '</p></blockquote>', $chunk);
  36. $chunk = preg_replace('|<p>\s*</p>\n?|', '', $chunk); // under certain strange conditions it could create a P of entirely whitespace
  37. $chunk = preg_replace('!<p>\s*(</?' . $block . '[^>]*>)!', "$1", $chunk);
  38. $chunk = preg_replace('!(</?' . $block . '[^>]*>)\s*</p>!', "$1", $chunk);
  39. $chunk = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $chunk); // make line breaks
  40. $chunk = preg_replace('!(</?' . $block . '[^>]*>)\s*<br />!', "$1", $chunk);
  41. $chunk = preg_replace('!<br />(\s*</?(?:p|li|div|th|pre|td|ul|ol)>)!', '$1', $chunk);
  42.  
  43. return $chunk;
  44. }
Add Comment
Please, Sign In to add comment