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.

100 line
2.2KB

  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;
  8. /**
  9. * Markdown parser for github flavored markdown.
  10. *
  11. * @author Carsten Brandt <mail@cebe.cc>
  12. */
  13. class GithubMarkdown extends Markdown
  14. {
  15. // include block element parsing using traits
  16. use block\TableTrait;
  17. use block\FencedCodeTrait;
  18. // include inline element parsing using traits
  19. use inline\StrikeoutTrait;
  20. use inline\UrlLinkTrait;
  21. /**
  22. * @var boolean whether to interpret newlines as `<br />`-tags.
  23. * This feature is useful for comments where newlines are often meant to be real new lines.
  24. */
  25. public $enableNewlines = false;
  26. /**
  27. * @inheritDoc
  28. */
  29. protected $escapeCharacters = [
  30. // from Markdown
  31. '\\', // backslash
  32. '`', // backtick
  33. '*', // asterisk
  34. '_', // underscore
  35. '{', '}', // curly braces
  36. '[', ']', // square brackets
  37. '(', ')', // parentheses
  38. '#', // hash mark
  39. '+', // plus sign
  40. '-', // minus sign (hyphen)
  41. '.', // dot
  42. '!', // exclamation mark
  43. '<', '>',
  44. // added by GithubMarkdown
  45. ':', // colon
  46. '|', // pipe
  47. ];
  48. /**
  49. * Consume lines for a paragraph
  50. *
  51. * Allow headlines, lists and code to break paragraphs
  52. */
  53. protected function consumeParagraph($lines, $current)
  54. {
  55. // consume until newline
  56. $content = [];
  57. for ($i = $current, $count = count($lines); $i < $count; $i++) {
  58. $line = $lines[$i];
  59. if (!empty($line) && ltrim($line) !== '' &&
  60. !($line[0] === "\t" || $line[0] === " " && strncmp($line, ' ', 4) === 0) &&
  61. !$this->identifyHeadline($line, $lines, $i) &&
  62. !$this->identifyUl($line, $lines, $i) &&
  63. !$this->identifyOl($line, $lines, $i))
  64. {
  65. $content[] = $line;
  66. } else {
  67. break;
  68. }
  69. }
  70. $block = [
  71. 'paragraph',
  72. 'content' => $this->parseInline(implode("\n", $content)),
  73. ];
  74. return [$block, --$i];
  75. }
  76. /**
  77. * @inheritdocs
  78. *
  79. * Parses a newline indicated by two spaces on the end of a markdown line.
  80. */
  81. protected function renderText($text)
  82. {
  83. if ($this->enableNewlines) {
  84. $br = $this->html5 ? "<br>\n" : "<br />\n";
  85. return strtr($text[1], [" \n" => $br, "\n" => $br]);
  86. } else {
  87. return parent::renderText($text);
  88. }
  89. }
  90. }