This commit is contained in:
joyqi 2021-04-14 14:23:05 +08:00
parent 6558fd5e03
commit 27ebdf09f0
3 changed files with 44 additions and 39 deletions

File diff suppressed because one or more lines are too long

View File

@ -518,7 +518,9 @@
Parser.prototype.parseBlockList = function(block, key, line, state) {
var matches, space, type;
if ((this.isBlock('list')) && !line.match(/^\s*\[((?:[^\]]|\\\]|\\\[)+?)\]:\s*(.+)$/)) {
if ((state.empty <= 1) && !!(matches = line.match(/^(\s*)\S+/)) && matches[1].length >= (block[3][0] + state.empty)) {
if (!!(line.match(/^(\s*)(~{3,}|`{3,})([^`~]*)$/i))) {
return true;
} else if ((state.empty <= 1) && !!(matches = line.match(/^(\s*)\S+/)) && matches[1].length >= (block[3][0] + state.empty)) {
state.empty = 0;
this.setBlock(key);
return false;
@ -546,7 +548,7 @@
return true;
};
Parser.prototype.parseBlockCode = function(block, key, line) {
Parser.prototype.parseBlockCode = function(block, key, line, state) {
var isAfterList, matches, space;
if (!!(matches = line.match(/^(\s*)(~{3,}|`{3,})([^`~]*)$/i))) {
if (this.isBlock('code')) {
@ -559,8 +561,8 @@
} else {
isAfterList = false;
if (this.isBlock('list')) {
space = block[3];
isAfterList = (space > 0 && matches[1].length >= space) || matches[1].length > space;
space = block[3][0];
isAfterList = matches[1].length >= space + state.empty;
}
this.startBlock('code', key, [matches[1], matches[3], isAfterList]);
}

View File

@ -144,7 +144,7 @@ class HyperDown
});
foreach ($this->blockParsers as $parser) {
[$name] = $parser;
list($name) = $parser;
if (isset($parser[2])) {
$this->_parsers[$name] = $parser[2];
@ -256,7 +256,7 @@ class HyperDown
}
foreach ($blocks as $block) {
[$type, $start, $end, $value] = $block;
list($type, $start, $end, $value) = $block;
$extract = array_slice($lines, $start, $end - $start + 1);
$method = 'parse' . ucfirst($type);
@ -692,14 +692,17 @@ class HyperDown
private function parseBlockList($block, $key, $line, &$state)
{
if ($this->isBlock('list') && !preg_match("/^\s*\[((?:[^\]]|\\]|\\[)+?)\]:\s*(.+)$/", $line)) {
if ($state['empty'] <= 1
if (preg_match("/^(\s*)(~{3,}|`{3,})([^`~]*)$/i", $line)) {
// ignore code
return true;
} elseif ($state['empty'] <= 1
&& preg_match("/^(\s*)\S+/", $line, $matches)
&& strlen($matches[1]) >= ($block[3][0] + $state['empty'])) {
$state['empty'] = 0;
$this->setBlock($key);
return false;
} else if (preg_match("/^(\s*)$/", $line) && $state['empty'] == 0) {
} elseif (preg_match("/^(\s*)$/", $line) && $state['empty'] == 0) {
$state['empty'] ++;
$this->setBlock($key);
return false;
@ -732,9 +735,10 @@ class HyperDown
* @param $block
* @param $key
* @param $line
* @param $state
* @return bool
*/
private function parseBlockCode($block, $key, $line)
private function parseBlockCode($block, $key, $line, $state)
{
if (preg_match("/^(\s*)(~{3,}|`{3,})([^`~]*)$/i", $line, $matches)) {
if ($this->isBlock('code')) {
@ -751,10 +755,9 @@ class HyperDown
$isAfterList = false;
if ($this->isBlock('list')) {
$space = $block[3];
$space = $block[3][0];
$isAfterList = ($space > 0 && strlen($matches[1]) >= $space)
|| strlen($matches[1]) > $space;
$isAfterList = strlen($matches[1]) >= $space + $state['empty'];
}
$this->startBlock('code', $key, array(
@ -763,7 +766,7 @@ class HyperDown
}
return false;
} else if ($this->isBlock('code')) {
} elseif ($this->isBlock('code')) {
$this->setBlock($key);
return false;
}
@ -789,7 +792,7 @@ class HyperDown
}
return false;
} else if ($this->isBlock('shtml')) {
} elseif ($this->isBlock('shtml')) {
$this->setBlock($key);
return false;
}
@ -812,7 +815,7 @@ class HyperDown
if ($this->isBlock('ahtml')) {
$this->setBlock($key);
return false;
} else if (empty($matches[2]) || $matches[2] != '/') {
} elseif (empty($matches[2]) || $matches[2] != '/') {
$this->startBlock('ahtml', $key);
preg_match_all("/<({$this->_blockHtmlTags})(\s+[^>]*)?>/i", $line, $allMatches);
$lastMatch = $allMatches[1][count($allMatches[0]) - 1];
@ -824,14 +827,14 @@ class HyperDown
}
return false;
}
} else if (!!$state['html'] && strpos($line, "</{$state['html']}>") !== false) {
} elseif (!!$state['html'] && strpos($line, "</{$state['html']}>") !== false) {
$this->setBlock($key)->endBlock();
$state['html'] = false;
return false;
} else if ($this->isBlock('ahtml')) {
} elseif ($this->isBlock('ahtml')) {
$this->setBlock($key);
return false;
} else if (preg_match("/^\s*<!\-\-(.*?)\-\->\s*$/", $line, $matches)) {
} elseif (preg_match("/^\s*<!\-\-(.*?)\-\->\s*$/", $line, $matches)) {
$this->startBlock('ahtml', $key)->endBlock();
return false;
}
@ -856,7 +859,7 @@ class HyperDown
}
return false;
} else if ($this->isBlock('math')) {
} elseif ($this->isBlock('math')) {
$this->setBlock($key);
return false;
}
@ -881,7 +884,7 @@ class HyperDown
}
return false;
} else if ($this->isBlock('pre') && preg_match("/^\s*$/", $line)) {
} elseif ($this->isBlock('pre') && preg_match("/^\s*$/", $line)) {
$this->setBlock($key);
return false;
}
@ -905,7 +908,7 @@ class HyperDown
}
return false;
} else if (preg_match("/<\/({$state['special']})>\s*$/i", $line, $matches)) {
} elseif (preg_match("/<\/({$state['special']})>\s*$/i", $line, $matches)) {
$tag = strtolower($matches[1]);
if ($this->isBlock('html', $tag)) {
@ -914,7 +917,7 @@ class HyperDown
}
return false;
} else if ($this->isBlock('html')) {
} elseif ($this->isBlock('html')) {
$this->setBlock($key);
return false;
}
@ -972,7 +975,7 @@ class HyperDown
if (preg_match("/^(\s*)>/", $line, $matches)) {
if ($this->isBlock('list') && strlen($matches[1]) > 0) {
$this->setBlock($key);
} else if ($this->isBlock('quote')) {
} elseif ($this->isBlock('quote')) {
$this->setBlock($key);
} else {
$this->startBlock('quote', $key);
@ -1027,9 +1030,9 @@ class HyperDown
if (preg_match("/^\s*(:?)\-+(:?)\s*$/", $row, $matches)) {
if (!empty($matches[1]) && !empty($matches[2])) {
$align = 'center';
} else if (!empty($matches[1])) {
} elseif (!empty($matches[1])) {
$align = 'left';
} else if (!empty($matches[2])) {
} elseif (!empty($matches[2])) {
$align = 'right';
}
}
@ -1143,14 +1146,14 @@ class HyperDown
} else {
$this->startBlock('normal', $key);
}
} else if ($this->isBlock('table')) {
} elseif ($this->isBlock('table')) {
if (false !== strpos($line, '|')) {
$block[3][2] ++;
$this->setBlock($key, $block[3]);
} else {
$this->startBlock('normal', $key);
}
} else if ($this->isBlock('quote')) {
} elseif ($this->isBlock('quote')) {
if (!preg_match("/^(\s*)$/", $line)) { // empty line
$this->setBlock($key);
} else {
@ -1184,7 +1187,7 @@ class HyperDown
$prevBlock = isset($blocks[$key - 1]) ? $blocks[$key - 1] : NULL;
$nextBlock = isset($blocks[$key + 1]) ? $blocks[$key + 1] : NULL;
[$type, $from, $to] = $block;
list($type, $from, $to) = $block;
if ('pre' == $type) {
$isEmpty = array_reduce(
@ -1239,7 +1242,7 @@ class HyperDown
*/
private function parseCode(array $lines, array $parts, $start)
{
[$blank, $lang] = $parts;
list($blank, $lang) = $parts;
$lang = trim($lang);
$count = strlen($blank);
@ -1248,7 +1251,7 @@ class HyperDown
} else {
$parts = explode(':', $lang);
if (count($parts) > 1) {
[$lang, $rel] = $parts;
list($lang, $rel) = $parts;
$lang = trim($lang);
$rel = trim($rel);
}
@ -1388,7 +1391,7 @@ class HyperDown
private function parseList(array $lines, $value, $start)
{
$html = '';
[$space, $type] = $value;
list($space, $type) = $value;
$rows = array();
$last = 0;
@ -1417,7 +1420,7 @@ class HyperDown
*/
private function parseTable(array $lines, array $value, $start)
{
[$ignores, $aligns] = $value;
list($ignores, $aligns) = $value;
$head = count($ignores) > 0 && array_sum($ignores) > 0;
$html = '<table>';
@ -1462,7 +1465,7 @@ class HyperDown
$columns[$last] = array(
isset($columns[$last]) ? $columns[$last][0] + 1 : 1, $row
);
} else if (isset($columns[$last])) {
} elseif (isset($columns[$last])) {
$columns[$last][0] ++;
} else {
$columns[0] = array(1, $row);
@ -1471,7 +1474,7 @@ class HyperDown
if ($head) {
$html .= '<thead>';
} else if ($body) {
} elseif ($body) {
$html .= '<tbody>';
}
@ -1480,7 +1483,7 @@ class HyperDown
. '" data-id="' . $this->_uniqid . '"' : '') . '>';
foreach ($columns as $key => $column) {
[$num, $text] = $column;
list($num, $text) = $column;
$tag = $head ? 'th' : 'td';
$html .= "<{$tag}";
@ -1499,7 +1502,7 @@ class HyperDown
if ($head) {
$html .= '</thead>';
} else if ($body) {
} elseif ($body) {
$body = false;
}
}
@ -1562,7 +1565,7 @@ class HyperDown
*/
private function parseFootnote(array $lines, array $value)
{
[$space, $note] = $value;
list($space, $note) = $value;
$index = array_search($note, $this->_footnotes);
if (false !== $index) {
@ -1609,7 +1612,7 @@ class HyperDown
{
if (preg_match("/^\s*((http|https|ftp|mailto):\S+)/i", $url, $matches)) {
return $matches[1];
} else if (preg_match("/^\s*(\S+)/i", $url, $matches)) {
} elseif (preg_match("/^\s*(\S+)/i", $url, $matches)) {
return $matches[1];
} else {
return '#';