KXStudio Website https://kx.studio/
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.

50 lines
2.4KB

  1. <?php
  2. class ReadMore extends Modules {
  3. public function __init() {
  4. $this->addAlias("markup_post_text", "makesafe", 8);
  5. }
  6. # Replace the "read more" indicator before markup modules get to it.
  7. static function makesafe($text, $post = null) {
  8. if (!is_string($text) or !preg_match("/<!--more(\((.+)\))?-->/", $text)) return $text;
  9. $controller = Route::current()->controller;
  10. if ($controller instanceof MainController and $controller->feed)
  11. return str_replace("<!--more-->", "", $text);
  12. $url = (isset($post) and !$post->no_results) ? $post->url() : "#" ;
  13. # For the curious: e51b2b9a58824dd068d8777ec6e97e4d is a md5 of "replace me!"
  14. return preg_replace("/<!--more(\((.+)\))?-->/", '<a class="read_more" href="'.$url.'">e51b2b9a58824dd068d8777ec6e97e4d</a>(((more\\1)))', $text);
  15. }
  16. # To be used in the Twig template as ${ post.body | read_more("Read more...") }
  17. static function read_more($text, $string = null) {
  18. if (!substr_count($text, "e51b2b9a58824dd068d8777ec6e97e4d"))
  19. return $text;
  20. if (Route::current()->action == "view")
  21. return preg_replace('/(<p>)?<a class="read_more" href="([^"]+)">e51b2b9a58824dd068d8777ec6e97e4d<\/a>\(\(\(more(\((.+)\))?\)\)\)(<\/p>(\n\n<\/p>(\n\n)?)?)?/', "", $text);
  22. preg_match_all("/e51b2b9a58824dd068d8777ec6e97e4d(\(\(\(more(\((.+)\))?\)\)\))/", preg_replace("/<[^>]+>/", "", $text), $more, PREG_OFFSET_CAPTURE);
  23. $body = truncate($text, $more[1][0][1], "", true, true, true);
  24. $body.= @$more[3][0];
  25. if (!empty($more[2][0]))
  26. $string = $more[2][0];
  27. elseif (!isset($string) or $string instanceof Post) # If it's called from anywhere but Twig the post will be passed as a second argument.
  28. $string = __("Read More &raquo;", "theme");
  29. return str_replace("e51b2b9a58824dd068d8777ec6e97e4d", $string, $body);
  30. }
  31. static function title_from_excerpt($text) {
  32. $split = preg_split("/(<p>)?<a class=\"read_more\" href=\"([^\"]+)\">e51b2b9a58824dd068d8777ec6e97e4d<\/a>(<\/p>(\n\n<\/p>(\n\n)?)?|<br \/>)?/", $text);
  33. return $split[0];
  34. }
  35. public function preview($text) {
  36. return preg_replace("/<!--more(\(([^\)]+)\))?-->/", "<hr />", $text);
  37. }
  38. }