PageRenderTime 65ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Vanilla/Formatting/BaseFormat.php

http://github.com/vanillaforums/Garden
PHP | 60 lines | 27 code | 8 blank | 25 comment | 2 complexity | f9deabd429eb534b6bf8847b2fe13928 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * @author Adam Charron <adam.c@vanillaforums.com>
  4. * @copyright 2009-2019 Vanilla Forums Inc.
  5. * @license GPL-2.0-only
  6. */
  7. namespace Vanilla\Formatting;
  8. use Vanilla\Contracts\Formatting\FormatInterface;
  9. /**
  10. * Base format with simple simple implementations.
  11. */
  12. abstract class BaseFormat implements FormatInterface {
  13. /** @var int */
  14. const EXCERPT_MAX_LENGTH = 325;
  15. /**
  16. * Implement rendering of excerpts based on the plain-text version of format.
  17. *
  18. * @inheritdoc
  19. */
  20. public function renderExcerpt(string $content): string {
  21. $plainText = $this->renderPlainText($content);
  22. $excerpt = mb_ereg_replace("\n", ' ', $plainText);
  23. $excerpt = mb_ereg_replace("\s{2,}", ' ', $excerpt);
  24. if (mb_strlen($excerpt) > self::EXCERPT_MAX_LENGTH) {
  25. $excerpt = mb_substr($excerpt, 0, self::EXCERPT_MAX_LENGTH);
  26. if ($lastSpace = mb_strrpos($excerpt, ' ')) {
  27. $excerpt = mb_substr($excerpt, 0, $lastSpace);
  28. }
  29. $excerpt .= '…';
  30. }
  31. return $excerpt;
  32. }
  33. /**
  34. * @inheritdoc
  35. */
  36. public function renderQuote(string $content): string {
  37. return $this->renderHTML($content);
  38. }
  39. /**
  40. * @inheritdoc
  41. */
  42. public function getPlainTextLength(string $content): int {
  43. return mb_strlen($this->renderPlainText($content), 'UTF-8');
  44. }
  45. /**
  46. * Set the status for extended content.
  47. *
  48. * @param bool $extendContent
  49. */
  50. public function setAllowExtendedContent(bool $extendContent): void {
  51. }
  52. }