diff --git a/admin/js/hyperdown.js b/admin/js/hyperdown.js index 62987e76..0211ed87 100644 --- a/admin/js/hyperdown.js +++ b/admin/js/hyperdown.js @@ -393,7 +393,7 @@ }; Parser.prototype.parseBlock = function(text, lines) { - var align, aligns, block, emptyCount, head, isAfterList, j, key, l, len, len1, len2, line, m, matches, num, ref, row, rows, space, special, tag; + var align, aligns, block, emptyCount, head, indent, isAfterList, j, key, l, len, len1, len2, line, m, matches, num, ref, row, rows, space, special, tag; ref = text.split("\n"); for (j = 0, len = ref.length; j < len; j++) { line = ref[j]; @@ -510,8 +510,10 @@ this.definitions[matches[1]] = this.cleanUrl(matches[2]); this.startBlock('definition', key).endBlock(); break; - case !!(line.match(/^\s*>/)): - if (this.isBlock('quote')) { + case !!(matches = line.match(/^(\s*)>/)): + if ((this.isBlock('list')) && matches[1].length > 0) { + this.setBlock(key); + } else if (this.isBlock('quote')) { this.setBlock(key); } else { this.startBlock('quote', key); @@ -571,13 +573,18 @@ break; default: if (this.isBlock('list')) { - if (line.match(/^(\s*)/)) { - if (emptyCount > 0) { + if (!!(matches = line.match(/^(\s*)/))) { + indent = matches[1].length > 0; + if (emptyCount > 0 && !indent) { this.startBlock('normal', key); } else { this.setBlock(key); } - emptyCount += 1; + if (indent) { + emptyCount = 0; + } else { + emptyCount += 1; + } } else if (emptyCount === 0) { this.setBlock(key); } else { @@ -598,14 +605,7 @@ this.startBlock('normal', key); } } else if (this.isBlock('quote')) { - if (line.match(/^(\s*)/)) { - if (emptyCount > 0) { - this.startBlock('normal', key); - } else { - this.setBlock(key); - } - emptyCount += 1; - } else if (emptyCount === 0) { + if (!line.match(/^(\s*)$/)) { this.setBlock(key); } else { this.startBlock('normal', key); @@ -734,9 +734,11 @@ }; Parser.prototype.parseList = function(lines) { - var found, html, j, key, l, lastType, leftLines, len, len1, len2, line, m, matches, minSpace, row, rows, secondMinSpace, space, text, type; + var found, html, j, key, l, lastType, leftLines, len, len1, line, matches, minSpace, row, rows, secondMinSpace, space, text, type; html = ''; minSpace = 99999; + secondMinSpace = 99999; + found = false; rows = []; for (key = j = 0, len = lines.length; j < len; key = ++j) { line = lines[key]; @@ -747,22 +749,20 @@ rows.push([space, type, line, matches[4]]); } else { rows.push(line); - } - } - found = false; - secondMinSpace = 99999; - for (l = 0, len1 = rows.length; l < len1; l++) { - row = rows[l]; - if (row instanceof Array && row[0] !== minSpace) { - secondMinSpace = Math.min(secondMinSpace, row[0]); - found = true; + if (!!(matches = line.match(/^(\s*)/))) { + space = matches[1].length; + if (space > 0) { + secondMinSpace = Math.min(space, secondMinSpace); + found = true; + } + } } } secondMinSpace = found ? secondMinSpace : minSpace; lastType = ''; leftLines = []; - for (m = 0, len2 = rows.length; m < len2; m++) { - row = rows[m]; + for (l = 0, len1 = rows.length; l < len1; l++) { + row = rows[l]; if (row instanceof Array) { space = row[0], type = row[1], line = row[2], text = row[3]; if (space !== minSpace) { diff --git a/var/HyperDown.php b/var/HyperDown.php index 5792d41b..f4a6a50c 100644 --- a/var/HyperDown.php +++ b/var/HyperDown.php @@ -601,7 +601,7 @@ class HyperDown if ($this->isBlock('pre') || $this->isBlock('list')) { $this->setBlock($key); - } else if ($this->isBlock('normal')) { + } else { $this->startBlock('pre', $key); } @@ -674,8 +674,10 @@ class HyperDown break; // block quote - case preg_match("/^\s*>/", $line): - if ($this->isBlock('quote')) { + case preg_match("/^(\s*)>/", $line, $matches): + if ($this->isBlock('list') && strlen($matches[1]) > 0) { + $this->setBlock($key); + } else if ($this->isBlock('quote')) { $this->setBlock($key); } else { $this->startBlock('quote', $key); @@ -758,14 +760,20 @@ class HyperDown // normal default: if ($this->isBlock('list')) { - if (preg_match("/^(\s*)/", $line)) { // empty line - if ($emptyCount > 0) { + if (preg_match("/^(\s*)/", $line, $matches)) { // empty line + $indent = strlen($matches[1]) > 0; + + if ($emptyCount > 0 && !$indent) { $this->startBlock('normal', $key); } else { $this->setBlock($key); } - $emptyCount ++; + if ($indent) { + $emptyCount = 0; + } else { + $emptyCount ++; + } } else if ($emptyCount == 0) { $this->setBlock($key); } else { @@ -786,15 +794,7 @@ class HyperDown $this->startBlock('normal', $key); } } else if ($this->isBlock('quote')) { - if (preg_match("/^(\s*)/", $line)) { // empty line - if ($emptyCount > 0) { - $this->startBlock('normal', $key); - } else { - $this->setBlock($key); - } - - $emptyCount ++; - } else if ($emptyCount == 0) { + if (!preg_match("/^(\s*)$/", $line)) { // empty line $this->setBlock($key); } else { $this->startBlock('normal', $key); @@ -993,6 +993,8 @@ class HyperDown { $html = ''; $minSpace = 99999; + $secondMinSpace = 99999; + $found = false; $rows = array(); // count levels @@ -1005,17 +1007,18 @@ class HyperDown $rows[] = array($space, $type, $line, $matches[4]); } else { $rows[] = $line; + + if (preg_match("/^(\s*)/", $line, $matches)) { + $space = strlen($matches[1]); + + if ($space > 0) { + $secondMinSpace = min($space, $secondMinSpace); + $found = true; + } + } } } - $found = false; - $secondMinSpace = 99999; - foreach ($rows as $row) { - if (is_array($row) && $row[0] != $minSpace) { - $secondMinSpace = min($secondMinSpace, $row[0]); - $found = true; - } - } $secondMinSpace = $found ? $secondMinSpace : $minSpace; $lastType = '';