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.

183 lines
5.0KB

  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 list blocks
  10. */
  11. trait ListTrait
  12. {
  13. /**
  14. * @var bool enable support `start` attribute of ordered lists. This means that lists
  15. * will start with the number you actually type in markdown and not the HTML generated one.
  16. * Defaults to `false` which means that numeration of all ordered lists(<ol>) starts with 1.
  17. */
  18. public $keepListStartNumber = false;
  19. /**
  20. * identify a line as the beginning of an ordered list.
  21. */
  22. protected function identifyOl($line)
  23. {
  24. return (($l = $line[0]) > '0' && $l <= '9' || $l === ' ') && preg_match('/^ {0,3}\d+\.[ \t]/', $line);
  25. }
  26. /**
  27. * identify a line as the beginning of an unordered list.
  28. */
  29. protected function identifyUl($line)
  30. {
  31. $l = $line[0];
  32. return ($l === '-' || $l === '+' || $l === '*') && (isset($line[1]) && (($l1 = $line[1]) === ' ' || $l1 === "\t")) ||
  33. ($l === ' ' && preg_match('/^ {0,3}[\-\+\*][ \t]/', $line));
  34. }
  35. /**
  36. * Consume lines for an ordered list
  37. */
  38. protected function consumeOl($lines, $current)
  39. {
  40. // consume until newline
  41. $block = [
  42. 'list',
  43. 'list' => 'ol',
  44. 'attr' => [],
  45. 'items' => [],
  46. ];
  47. return $this->consumeList($lines, $current, $block, 'ol');
  48. }
  49. /**
  50. * Consume lines for an unordered list
  51. */
  52. protected function consumeUl($lines, $current)
  53. {
  54. // consume until newline
  55. $block = [
  56. 'list',
  57. 'list' => 'ul',
  58. 'items' => [],
  59. ];
  60. return $this->consumeList($lines, $current, $block, 'ul');
  61. }
  62. private function consumeList($lines, $current, $block, $type)
  63. {
  64. $item = 0;
  65. $indent = '';
  66. $len = 0;
  67. // track the indentation of list markers, if indented more than previous element
  68. // a list marker is considered to be long to a lower level
  69. $leadSpace = 3;
  70. $marker = $type === 'ul' ? ltrim($lines[$current])[0] : '';
  71. for ($i = $current, $count = count($lines); $i < $count; $i++) {
  72. $line = $lines[$i];
  73. // match list marker on the beginning of the line
  74. if (preg_match($type == 'ol' ? '/^( {0,'.$leadSpace.'})(\d+)\.[ \t]+/' : '/^( {0,'.$leadSpace.'})\\'.$marker.'[ \t]+/', $line, $matches)) {
  75. if (($len = substr_count($matches[0], "\t")) > 0) {
  76. $indent = str_repeat("\t", $len);
  77. $line = substr($line, strlen($matches[0]));
  78. } else {
  79. $len = strlen($matches[0]);
  80. $indent = str_repeat(' ', $len);
  81. $line = substr($line, $len);
  82. }
  83. if ($i === $current) {
  84. $leadSpace = strlen($matches[1]) + 1;
  85. }
  86. if ($type == 'ol' && $this->keepListStartNumber) {
  87. // attr `start` for ol
  88. if (!isset($block['attr']['start']) && isset($matches[2])) {
  89. $block['attr']['start'] = $matches[2];
  90. }
  91. }
  92. $block['items'][++$item][] = $line;
  93. } elseif (ltrim($line) === '') {
  94. // next line after empty one is also a list or indented -> lazy list
  95. if (isset($lines[$i + 1][0]) && (
  96. $this->{'identify' . $type}($lines[$i + 1], $lines, $i + 1) && ($type !== 'ul' || ltrim($lines[$i + 1])[0] === $marker) ||
  97. (strncmp($lines[$i + 1], $indent, $len) === 0 || !empty($lines[$i + 1]) && $lines[$i + 1][0] == "\t"))) {
  98. $block['items'][$item][] = $line;
  99. $block['lazyItems'][$item] = true;
  100. } else {
  101. break;
  102. }
  103. } else {
  104. if ($line[0] === "\t") {
  105. $line = substr($line, 1);
  106. } elseif (strncmp($line, $indent, $len) === 0) {
  107. $line = substr($line, $len);
  108. }
  109. $block['items'][$item][] = $line;
  110. }
  111. }
  112. // make last item lazy if item before was lazy
  113. if (isset($block['lazyItems'][$item - 1])) {
  114. $block['lazyItems'][$item] = true;
  115. }
  116. foreach($block['items'] as $itemId => $itemLines) {
  117. $content = [];
  118. if (!isset($block['lazyItems'][$itemId])) {
  119. $firstPar = [];
  120. while (!empty($itemLines) && rtrim($itemLines[0]) !== '' && $this->detectLineType($itemLines, 0) === 'paragraph') {
  121. $firstPar[] = array_shift($itemLines);
  122. }
  123. $content = $this->parseInline(implode("\n", $firstPar));
  124. }
  125. if (!empty($itemLines)) {
  126. $content = array_merge($content, $this->parseBlocks($itemLines));
  127. }
  128. $block['items'][$itemId] = $content;
  129. }
  130. return [$block, $i];
  131. }
  132. /**
  133. * Renders a list
  134. */
  135. protected function renderList($block)
  136. {
  137. $type = $block['list'];
  138. if (!empty($block['attr'])) {
  139. $output = "<$type " . $this->generateHtmlAttributes($block['attr']) . ">\n";
  140. } else {
  141. $output = "<$type>\n";
  142. }
  143. foreach ($block['items'] as $item => $itemLines) {
  144. $output .= '<li>' . $this->renderAbsy($itemLines). "</li>\n";
  145. }
  146. return $output . "</$type>\n";
  147. }
  148. /**
  149. * Return html attributes string from [attrName => attrValue] list
  150. * @param array $attributes the attribute name-value pairs.
  151. * @return string
  152. */
  153. private function generateHtmlAttributes($attributes)
  154. {
  155. foreach ($attributes as $name => $value) {
  156. $attributes[$name] = "$name=\"$value\"";
  157. }
  158. return implode(' ', $attributes);
  159. }
  160. abstract protected function parseInline($text);
  161. abstract protected function renderAbsy($absy);
  162. }