This commit is contained in:
2020-10-06 14:27:47 +07:00
commit 586be80cf6
16613 changed files with 3274099 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
<?php
/**
* @copyright Copyright (c) 2014 Carsten Brandt
* @license https://github.com/cebe/markdown/blob/master/LICENSE
* @link https://github.com/cebe/markdown#readme
*/
namespace cebe\markdown\block;
/**
* Adds the 4 space indented code blocks
*/
trait CodeTrait
{
/**
* identify a line as the beginning of a code block.
*/
protected function identifyCode($line)
{
// indentation >= 4 or one tab is code
return ($l = $line[0]) === ' ' && $line[1] === ' ' && $line[2] === ' ' && $line[3] === ' ' || $l === "\t";
}
/**
* Consume lines for a code block element
*/
protected function consumeCode($lines, $current)
{
// consume until newline
$content = [];
for ($i = $current, $count = count($lines); $i < $count; $i++) {
$line = $lines[$i];
// a line is considered to belong to this code block as long as it is intended by 4 spaces or a tab
if (isset($line[0]) && ($line[0] === "\t" || strncmp($line, ' ', 4) === 0)) {
$line = $line[0] === "\t" ? substr($line, 1) : substr($line, 4);
$content[] = $line;
// but also if it is empty and the next line is intended by 4 spaces or a tab
} elseif (($line === '' || rtrim($line) === '') && isset($lines[$i + 1][0]) &&
($lines[$i + 1][0] === "\t" || strncmp($lines[$i + 1], ' ', 4) === 0)) {
if ($line !== '') {
$line = $line[0] === "\t" ? substr($line, 1) : substr($line, 4);
}
$content[] = $line;
} else {
break;
}
}
$block = [
'code',
'content' => implode("\n", $content),
];
return [$block, --$i];
}
/**
* Renders a code block
*/
protected function renderCode($block)
{
$class = isset($block['language']) ? ' class="language-' . $block['language'] . '"' : '';
return "<pre><code$class>" . htmlspecialchars($block['content'] . "\n", ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8') . "</code></pre>\n";
}
}

View File

@@ -0,0 +1,54 @@
<?php
/**
* @copyright Copyright (c) 2014 Carsten Brandt
* @license https://github.com/cebe/markdown/blob/master/LICENSE
* @link https://github.com/cebe/markdown#readme
*/
namespace cebe\markdown\block;
/**
* Adds the fenced code blocks
*
* automatically included 4 space indented code blocks
*/
trait FencedCodeTrait
{
use CodeTrait;
/**
* identify a line as the beginning of a fenced code block.
*/
protected function identifyFencedCode($line)
{
return ($l = $line[0]) === '`' && strncmp($line, '```', 3) === 0 ||
$l === '~' && strncmp($line, '~~~', 3) === 0;
}
/**
* Consume lines for a fenced code block
*/
protected function consumeFencedCode($lines, $current)
{
// consume until ```
$line = rtrim($lines[$current]);
$fence = substr($line, 0, $pos = strrpos($line, $line[0]) + 1);
$language = substr($line, $pos);
$content = [];
for ($i = $current + 1, $count = count($lines); $i < $count; $i++) {
if (rtrim($line = $lines[$i]) !== $fence) {
$content[] = $line;
} else {
break;
}
}
$block = [
'code',
'content' => implode("\n", $content),
];
if (!empty($language)) {
$block['language'] = $language;
}
return [$block, $i];
}
}

View File

@@ -0,0 +1,70 @@
<?php
/**
* @copyright Copyright (c) 2014 Carsten Brandt
* @license https://github.com/cebe/markdown/blob/master/LICENSE
* @link https://github.com/cebe/markdown#readme
*/
namespace cebe\markdown\block;
/**
* Adds the headline blocks
*/
trait HeadlineTrait
{
/**
* identify a line as a headline
*/
protected function identifyHeadline($line, $lines, $current)
{
return (
// heading with #
$line[0] === '#' && !preg_match('/^#\d+/', $line)
||
// underlined headline
!empty($lines[$current + 1]) &&
(($l = $lines[$current + 1][0]) === '=' || $l === '-') &&
preg_match('/^(\-+|=+)\s*$/', $lines[$current + 1])
);
}
/**
* Consume lines for a headline
*/
protected function consumeHeadline($lines, $current)
{
if ($lines[$current][0] === '#') {
// ATX headline
$level = 1;
while (isset($lines[$current][$level]) && $lines[$current][$level] === '#' && $level < 6) {
$level++;
}
$block = [
'headline',
'content' => $this->parseInline(trim($lines[$current], "# \t")),
'level' => $level,
];
return [$block, $current];
} else {
// underlined headline
$block = [
'headline',
'content' => $this->parseInline($lines[$current]),
'level' => $lines[$current + 1][0] === '=' ? 1 : 2,
];
return [$block, $current + 1];
}
}
/**
* Renders a headline
*/
protected function renderHeadline($block)
{
$tag = 'h' . $block['level'];
return "<$tag>" . $this->renderAbsy($block['content']) . "</$tag>\n";
}
abstract protected function parseInline($text);
abstract protected function renderAbsy($absy);
}

168
vendor/cebe/markdown/block/HtmlTrait.php vendored Normal file
View File

@@ -0,0 +1,168 @@
<?php
/**
* @copyright Copyright (c) 2014 Carsten Brandt
* @license https://github.com/cebe/markdown/blob/master/LICENSE
* @link https://github.com/cebe/markdown#readme
*/
namespace cebe\markdown\block;
/**
* Adds inline and block HTML support
*/
trait HtmlTrait
{
/**
* @var array HTML elements considered as inline elements.
* @see http://www.w3.org/wiki/HTML/Elements#Text-level_semantics
*/
protected $inlineHtmlElements = [
'a', 'abbr', 'acronym',
'b', 'basefont', 'bdo', 'big', 'br', 'button', 'blink',
'cite', 'code',
'del', 'dfn',
'em',
'font',
'i', 'img', 'ins', 'input', 'iframe',
'kbd',
'label', 'listing',
'map', 'mark',
'nobr',
'object',
'q',
'rp', 'rt', 'ruby',
's', 'samp', 'script', 'select', 'small', 'spacer', 'span', 'strong', 'sub', 'sup',
'tt', 'var',
'u',
'wbr',
'time',
];
/**
* @var array HTML elements known to be self-closing.
*/
protected $selfClosingHtmlElements = [
'br', 'hr', 'img', 'input', 'nobr',
];
/**
* identify a line as the beginning of a HTML block.
*/
protected function identifyHtml($line, $lines, $current)
{
if ($line[0] !== '<' || isset($line[1]) && $line[1] == ' ') {
return false; // no html tag
}
if (strncmp($line, '<!--', 4) === 0) {
return true; // a html comment
}
$gtPos = strpos($lines[$current], '>');
$spacePos = strpos($lines[$current], ' ');
if ($gtPos === false && $spacePos === false) {
return false; // no html tag
} elseif ($spacePos === false) {
$tag = rtrim(substr($line, 1, $gtPos - 1), '/');
} else {
$tag = rtrim(substr($line, 1, min($gtPos, $spacePos) - 1), '/');
}
if (!ctype_alnum($tag) || in_array(strtolower($tag), $this->inlineHtmlElements)) {
return false; // no html tag or inline html tag
}
return true;
}
/**
* Consume lines for an HTML block
*/
protected function consumeHtml($lines, $current)
{
$content = [];
if (strncmp($lines[$current], '<!--', 4) === 0) { // html comment
for ($i = $current, $count = count($lines); $i < $count; $i++) {
$line = $lines[$i];
$content[] = $line;
if (strpos($line, '-->') !== false) {
break;
}
}
} else {
$tag = rtrim(substr($lines[$current], 1, min(strpos($lines[$current], '>'), strpos($lines[$current] . ' ', ' ')) - 1), '/');
$level = 0;
if (in_array($tag, $this->selfClosingHtmlElements)) {
$level--;
}
for ($i = $current, $count = count($lines); $i < $count; $i++) {
$line = $lines[$i];
$content[] = $line;
$level += substr_count($line, "<$tag") - substr_count($line, "</$tag>") - substr_count($line, "/>");
if ($level <= 0) {
break;
}
}
}
$block = [
'html',
'content' => implode("\n", $content),
];
return [$block, $i];
}
/**
* Renders an HTML block
*/
protected function renderHtml($block)
{
return $block['content'] . "\n";
}
/**
* Parses an & or a html entity definition.
* @marker &
*/
protected function parseEntity($text)
{
// html entities e.g. &copy; &#169; &#x00A9;
if (preg_match('/^&#?[\w\d]+;/', $text, $matches)) {
return [['inlineHtml', $matches[0]], strlen($matches[0])];
} else {
return [['text', '&amp;'], 1];
}
}
/**
* renders a html entity.
*/
protected function renderInlineHtml($block)
{
return $block[1];
}
/**
* Parses inline HTML.
* @marker <
*/
protected function parseInlineHtml($text)
{
if (strpos($text, '>') !== false) {
if (preg_match('~^</?(\w+\d?)( .*?)?>~s', $text, $matches)) {
// HTML tags
return [['inlineHtml', $matches[0]], strlen($matches[0])];
} elseif (preg_match('~^<!--.*?-->~s', $text, $matches)) {
// HTML comments
return [['inlineHtml', $matches[0]], strlen($matches[0])];
}
}
return [['text', '&lt;'], 1];
}
/**
* Escapes `>` characters.
* @marker >
*/
protected function parseGt($text)
{
return [['text', '&gt;'], 1];
}
}

197
vendor/cebe/markdown/block/ListTrait.php vendored Normal file
View File

@@ -0,0 +1,197 @@
<?php
/**
* @copyright Copyright (c) 2014 Carsten Brandt
* @license https://github.com/cebe/markdown/blob/master/LICENSE
* @link https://github.com/cebe/markdown#readme
*/
namespace cebe\markdown\block;
/**
* Adds the list blocks
*/
trait ListTrait
{
/**
* @var bool enable support `start` attribute of ordered lists. This means that lists
* will start with the number you actually type in markdown and not the HTML generated one.
* Defaults to `false` which means that numeration of all ordered lists(<ol>) starts with 1.
*/
public $keepListStartNumber = false;
/**
* identify a line as the beginning of an ordered list.
*/
protected function identifyOl($line)
{
return (($l = $line[0]) > '0' && $l <= '9' || $l === ' ') && preg_match('/^ {0,3}\d+\.[ \t]/', $line);
}
/**
* identify a line as the beginning of an unordered list.
*/
protected function identifyUl($line)
{
$l = $line[0];
return ($l === '-' || $l === '+' || $l === '*') && (isset($line[1]) && (($l1 = $line[1]) === ' ' || $l1 === "\t")) ||
($l === ' ' && preg_match('/^ {0,3}[\-\+\*][ \t]/', $line));
}
/**
* Consume lines for an ordered list
*/
protected function consumeOl($lines, $current)
{
// consume until newline
$block = [
'list',
'list' => 'ol',
'attr' => [],
'items' => [],
];
return $this->consumeList($lines, $current, $block, 'ol');
}
/**
* Consume lines for an unordered list
*/
protected function consumeUl($lines, $current)
{
// consume until newline
$block = [
'list',
'list' => 'ul',
'items' => [],
];
return $this->consumeList($lines, $current, $block, 'ul');
}
private function consumeList($lines, $current, $block, $type)
{
$item = 0;
$indent = '';
$len = 0;
$lastLineEmpty = false;
// track the indentation of list markers, if indented more than previous element
// a list marker is considered to be long to a lower level
$leadSpace = 3;
$marker = $type === 'ul' ? ltrim($lines[$current])[0] : '';
for ($i = $current, $count = count($lines); $i < $count; $i++) {
$line = $lines[$i];
// match list marker on the beginning of the line
$pattern = ($type == 'ol') ? '/^( {0,'.$leadSpace.'})(\d+)\.[ \t]+/' : '/^( {0,'.$leadSpace.'})\\'.$marker.'[ \t]+/';
if (preg_match($pattern, $line, $matches)) {
if (($len = substr_count($matches[0], "\t")) > 0) {
$indent = str_repeat("\t", $len);
$line = substr($line, strlen($matches[0]));
} else {
$len = strlen($matches[0]);
$indent = str_repeat(' ', $len);
$line = substr($line, $len);
}
if ($i === $current) {
$leadSpace = strlen($matches[1]) + 1;
}
if ($type == 'ol' && $this->keepListStartNumber) {
// attr `start` for ol
if (!isset($block['attr']['start']) && isset($matches[2])) {
$block['attr']['start'] = $matches[2];
}
}
$block['items'][++$item][] = $line;
$block['lazyItems'][$item] = $lastLineEmpty;
$lastLineEmpty = false;
} elseif (ltrim($line) === '') {
// line is empty, may be a lazy list
$lastLineEmpty = true;
// two empty lines will end the list
if (!isset($lines[$i + 1][0])) {
break;
// next item is the continuation of this list -> lazy list
} elseif (preg_match($pattern, $lines[$i + 1])) {
$block['items'][$item][] = $line;
$block['lazyItems'][$item] = true;
// next item is indented as much as this list -> lazy list if it is not a reference
} elseif (strncmp($lines[$i + 1], $indent, $len) === 0 || !empty($lines[$i + 1]) && $lines[$i + 1][0] == "\t") {
$block['items'][$item][] = $line;
$nextLine = $lines[$i + 1][0] === "\t" ? substr($lines[$i + 1], 1) : substr($lines[$i + 1], $len);
$block['lazyItems'][$item] = empty($nextLine) || !method_exists($this, 'identifyReference') || !$this->identifyReference($nextLine);
// everything else ends the list
} else {
break;
}
} else {
if ($line[0] === "\t") {
$line = substr($line, 1);
} elseif (strncmp($line, $indent, $len) === 0) {
$line = substr($line, $len);
}
$block['items'][$item][] = $line;
$lastLineEmpty = false;
}
}
foreach($block['items'] as $itemId => $itemLines) {
$content = [];
if (!$block['lazyItems'][$itemId]) {
$firstPar = [];
while (!empty($itemLines) && rtrim($itemLines[0]) !== '' && $this->detectLineType($itemLines, 0) === 'paragraph') {
$firstPar[] = array_shift($itemLines);
}
$content = $this->parseInline(implode("\n", $firstPar));
}
if (!empty($itemLines)) {
$content = array_merge($content, $this->parseBlocks($itemLines));
}
$block['items'][$itemId] = $content;
}
return [$block, $i];
}
/**
* Renders a list
*/
protected function renderList($block)
{
$type = $block['list'];
if (!empty($block['attr'])) {
$output = "<$type " . $this->generateHtmlAttributes($block['attr']) . ">\n";
} else {
$output = "<$type>\n";
}
foreach ($block['items'] as $item => $itemLines) {
$output .= '<li>' . $this->renderAbsy($itemLines). "</li>\n";
}
return $output . "</$type>\n";
}
/**
* Return html attributes string from [attrName => attrValue] list
* @param array $attributes the attribute name-value pairs.
* @return string
*/
private function generateHtmlAttributes($attributes)
{
foreach ($attributes as $name => $value) {
$attributes[$name] = "$name=\"$value\"";
}
return implode(' ', $attributes);
}
abstract protected function parseBlocks($lines);
abstract protected function parseInline($text);
abstract protected function renderAbsy($absy);
abstract protected function detectLineType($lines, $current);
}

View File

@@ -0,0 +1,63 @@
<?php
/**
* @copyright Copyright (c) 2014 Carsten Brandt
* @license https://github.com/cebe/markdown/blob/master/LICENSE
* @link https://github.com/cebe/markdown#readme
*/
namespace cebe\markdown\block;
/**
* Adds the block quote elements
*/
trait QuoteTrait
{
/**
* identify a line as the beginning of a block quote.
*/
protected function identifyQuote($line)
{
return $line[0] === '>' && (!isset($line[1]) || ($l1 = $line[1]) === ' ' || $l1 === "\t");
}
/**
* Consume lines for a blockquote element
*/
protected function consumeQuote($lines, $current)
{
// consume until newline
$content = [];
for ($i = $current, $count = count($lines); $i < $count; $i++) {
$line = $lines[$i];
if (ltrim($line) !== '') {
if ($line[0] == '>' && !isset($line[1])) {
$line = '';
} elseif (strncmp($line, '> ', 2) === 0) {
$line = substr($line, 2);
}
$content[] = $line;
} else {
break;
}
}
$block = [
'quote',
'content' => $this->parseBlocks($content),
'simple' => true,
];
return [$block, $i];
}
/**
* Renders a blockquote
*/
protected function renderQuote($block)
{
return '<blockquote>' . $this->renderAbsy($block['content']) . "</blockquote>\n";
}
abstract protected function parseBlocks($lines);
abstract protected function renderAbsy($absy);
}

View File

@@ -0,0 +1,40 @@
<?php
/**
* @copyright Copyright (c) 2014 Carsten Brandt
* @license https://github.com/cebe/markdown/blob/master/LICENSE
* @link https://github.com/cebe/markdown#readme
*/
namespace cebe\markdown\block;
/**
* Adds horizontal rules
*/
trait RuleTrait
{
/**
* identify a line as a horizontal rule.
*/
protected function identifyHr($line)
{
// at least 3 of -, * or _ on one line make a hr
return (($l = $line[0]) === ' ' || $l === '-' || $l === '*' || $l === '_') && preg_match('/^ {0,3}([\-\*_])\s*\1\s*\1(\1|\s)*$/', $line);
}
/**
* Consume a horizontal rule
*/
protected function consumeHr($lines, $current)
{
return [['hr'], $current];
}
/**
* Renders a horizontal rule
*/
protected function renderHr($block)
{
return $this->html5 ? "<hr>\n" : "<hr />\n";
}
}

View File

@@ -0,0 +1,122 @@
<?php
/**
* @copyright Copyright (c) 2014 Carsten Brandt
* @license https://github.com/cebe/markdown/blob/master/LICENSE
* @link https://github.com/cebe/markdown#readme
*/
namespace cebe\markdown\block;
/**
* Adds the table blocks
*/
trait TableTrait
{
private $_tableCellTag = 'td';
private $_tableCellCount = 0;
private $_tableCellAlign = [];
/**
* identify a line as the beginning of a table block.
*/
protected function identifyTable($line, $lines, $current)
{
return strpos($line, '|') !== false && isset($lines[$current + 1])
&& preg_match('~^\\s*\\|?(\\s*:?-[\\-\\s]*:?\\s*\\|\\s*:?-[\\-\\s]*:?\\s*)+\\|?\\s*$~', $lines[$current + 1]);
}
/**
* Consume lines for a table
*/
protected function consumeTable($lines, $current)
{
// consume until newline
$block = [
'table',
'cols' => [],
'rows' => [],
];
$beginsWithPipe = $lines[$current][0] === '|';
for ($i = $current, $count = count($lines); $i < $count; $i++) {
$line = rtrim($lines[$i]);
// extract alignment from second line
if ($i == $current+1) {
$cols = explode('|', trim($line, ' |'));
foreach($cols as $col) {
$col = trim($col);
if (empty($col)) {
$block['cols'][] = '';
continue;
}
$l = ($col[0] === ':');
$r = (substr($col, -1, 1) === ':');
if ($l && $r) {
$block['cols'][] = 'center';
} elseif ($l) {
$block['cols'][] = 'left';
} elseif ($r) {
$block['cols'][] = 'right';
} else {
$block['cols'][] = '';
}
}
continue;
}
if ($line === '' || $beginsWithPipe && $line[0] !== '|') {
break;
}
if ($line[0] === '|') {
$line = substr($line, 1);
}
if (substr($line, -1, 1) === '|' && (substr($line, -2, 2) !== '\\|' || substr($line, -3, 3) === '\\\\|')) {
$line = substr($line, 0, -1);
}
$block['rows'][] = $line;
}
return [$block, --$i];
}
/**
* render a table block
*/
protected function renderTable($block)
{
$content = '';
$this->_tableCellAlign = $block['cols'];
$content .= "<thead>\n";
$first = true;
foreach($block['rows'] as $row) {
$this->_tableCellTag = $first ? 'th' : 'td';
$align = empty($this->_tableCellAlign[$this->_tableCellCount]) ? '' : ' align="' . $this->_tableCellAlign[$this->_tableCellCount] . '"';
$this->_tableCellCount++;
$tds = "<$this->_tableCellTag$align>" . trim($this->renderAbsy($this->parseInline($row))) . "</$this->_tableCellTag>"; // TODO move this to the consume step
$content .= "<tr>$tds</tr>\n";
if ($first) {
$content .= "</thead>\n<tbody>\n";
}
$first = false;
$this->_tableCellCount = 0;
}
return "<table>\n$content</tbody>\n</table>\n";
}
/**
* @marker |
*/
protected function parseTd($markdown)
{
if (isset($this->context[1]) && $this->context[1] === 'table') {
$align = empty($this->_tableCellAlign[$this->_tableCellCount]) ? '' : ' align="' . $this->_tableCellAlign[$this->_tableCellCount] . '"';
$this->_tableCellCount++;
return [['text', "</$this->_tableCellTag><$this->_tableCellTag$align>"], isset($markdown[1]) && $markdown[1] === ' ' ? 2 : 1]; // TODO make a absy node
}
return [['text', $markdown[0]], 1];
}
abstract protected function parseInline($text);
abstract protected function renderAbsy($absy);
}