QuoteTrait.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2014 Carsten Brandt
  4. * @license https://github.com/cebe/markdown/blob/master/LICENSE
  5. * @link https://github.com/cebe/markdown#readme
  6. */
  7. namespace cebe\markdown\block;
  8. /**
  9. * Adds the block quote elements
  10. */
  11. trait QuoteTrait
  12. {
  13. /**
  14. * identify a line as the beginning of a block quote.
  15. */
  16. protected function identifyQuote($line)
  17. {
  18. return $line[0] === '>' && (!isset($line[1]) || ($l1 = $line[1]) === ' ' || $l1 === "\t");
  19. }
  20. /**
  21. * Consume lines for a blockquote element
  22. */
  23. protected function consumeQuote($lines, $current)
  24. {
  25. // consume until newline
  26. $content = [];
  27. for ($i = $current, $count = count($lines); $i < $count; $i++) {
  28. $line = $lines[$i];
  29. if (ltrim($line) !== '') {
  30. if ($line[0] == '>' && !isset($line[1])) {
  31. $line = '';
  32. } elseif (strncmp($line, '> ', 2) === 0) {
  33. $line = substr($line, 2);
  34. }
  35. $content[] = $line;
  36. } else {
  37. break;
  38. }
  39. }
  40. $block = [
  41. 'quote',
  42. 'content' => $this->parseBlocks($content),
  43. 'simple' => true,
  44. ];
  45. return [$block, $i];
  46. }
  47. /**
  48. * Renders a blockquote
  49. */
  50. protected function renderQuote($block)
  51. {
  52. return '<blockquote>' . $this->renderAbsy($block['content']) . "</blockquote>\n";
  53. }
  54. abstract protected function parseBlocks($lines);
  55. abstract protected function renderAbsy($absy);
  56. }