diff --git a/src/BlockElementParser.php b/src/BlockElementParser.php
index a1a369c..2509e23 100644
--- a/src/BlockElementParser.php
+++ b/src/BlockElementParser.php
@@ -78,23 +78,20 @@ protected function isBlock()
protected function atx()
{
- if (isset($this->trimmedLine[0]) && $this->trimmedLine[0] === '#') {
+ if (\substr($this->trimmedLine, 0, 1) === '#') {
$level = \strlen($this->trimmedLine) - \strlen(\ltrim($this->trimmedLine, '#'));
+ $head = $this->h($level, $this->trimmedLine);
- if ($level < 7) {
- $this->markup .= "\n" . \ltrim(\ltrim($this->trimmedLine, '# ')) . "";
+ $this->markup .= $head;
- return \true;
- }
+ return (bool) $head;
}
}
protected function setext()
{
if (\preg_match(static::RE_MD_SETEXT, $this->nextLine)) {
- $level = \trim($this->nextLine, '- ') === '' ? 2 : 1;
-
- $this->markup .= "\n{$this->trimmedLine}";
+ $this->markup .= $this->h($this->nextLine, $this->trimmedLine);
$this->pointer++;
@@ -108,11 +105,7 @@ protected function code()
$codeBlock = \preg_match(static::RE_MD_CODE, $this->line, $codeMatch);
if ($codeBlock || (!$this->inList && !$this->inQuote && $isShifted)) {
- $lang = isset($codeMatch[1])
- ? ' class="language-' . $codeMatch[1] . '"'
- : '';
-
- $this->markup .= "\n
";
+ $this->markup .= $this->codeStart($codeMatch);
if (!$codeBlock) {
$this->markup .= $this->escape(\substr($this->line, $this->indentLen));
@@ -136,25 +129,22 @@ private function codeInternal($codeBlock)
if (($codeBlock && \substr(\ltrim($this->line), 0, 3) !== '```')
|| \strpos($this->line, $this->indentStr) === 0
) {
- $this->markup .= "\n"; // @todo: donot use \n for first line
- $this->markup .= $codeBlock ? $this->line : \substr($this->line, $this->indentLen);
+ $this->markup .= $this->codeLine($this->line, $codeBlock, $this->indentLen);
$this->pointer++;
- } else {
- break;
+
+ continue;
}
+
+ break;
}
}
protected function rule()
{
- if ($this->trimmedPrevLine === ''
- && \preg_match(static::RE_MD_RULE, $this->trimmedLine)
- ) {
- $this->markup .= "\n
";
+ $this->markup .= $hr = $this->hr($this->trimmedPrevLine, $this->trimmedLine);
- return \true;
- }
+ return (bool) $hr;
}
protected function listt()
@@ -166,8 +156,9 @@ protected function listt()
if (!$this->inList) {
$this->stackList[] = "$wrapper>";
+
$this->markup .= "\n<$wrapper>\n";
- $this->inList = \true;
+ $this->inList = \true;
$this->listLevel++;
}
@@ -222,18 +213,7 @@ protected function table()
return $this->tableInternal($headerCount);
}
- $this->markup .= "\n";
-
- foreach (\explode('|', \trim($this->trimmedLine, '|')) as $i => $col) {
- if ($i > $headerCount) {
- break;
- }
-
- $col = \trim($col);
- $this->markup .= "{$col} | \n";
- }
-
- $this->markup .= "
\n";
+ $this->markup .= $this->tableRow($this->trimmedLine, $headerCount);
if (empty($this->trimmedNextLine)
|| !\substr_count(\trim($this->trimmedNextLine, '|'), '|')
@@ -254,14 +234,7 @@ private function tableInternal($headerCount)
$this->pointer++;
$this->inTable = \true;
- $this->markup .= "\n\n\n";
- $this->trimmedLine = \trim($this->trimmedLine, '|');
-
- foreach (\explode('|', $this->trimmedLine) as $hdr) {
- $this->markup .= '' . \trim($hdr) . " | \n";
- }
-
- $this->markup .= "
\n\n\n";
+ $this->markup .= $this->tableStart($this->trimmedLine);
return \true;
}
diff --git a/src/HtmlHelper.php b/src/HtmlHelper.php
index 1b69bb2..8334b28 100644
--- a/src/HtmlHelper.php
+++ b/src/HtmlHelper.php
@@ -17,4 +17,72 @@ public function escape($input)
{
return \htmlspecialchars($input);
}
+
+ public function h($level, $line)
+ {
+ if (\is_string($level)) {
+ $level = \trim($level, '- ') === '' ? 2 : 1;
+ }
+
+ if ($level < 7) {
+ return "\n" . \ltrim(\ltrim($line, '# ')) . "";
+ }
+
+ return '';
+ }
+
+ public function hr($prevLine, $line)
+ {
+ if ($prevLine === '' && \preg_match(BlockElementParser::RE_MD_RULE, $line)) {
+ return "\n
";
+ }
+ }
+
+ public function codeStart($lang)
+ {
+ $lang = isset($lang[1])
+ ? ' class="language-' . $lang[1] . '"'
+ : '';
+
+ return "\n";
+ }
+
+ public function codeLine($line, $isBlock, $indentLen = 4)
+ {
+ $code = "\n"; // @todo: donot use \n for first line
+ $code .= $isBlock ? $line : \substr($line, $indentLen);
+
+ return $code;
+ }
+
+ public function tableStart($line, $delim = '|')
+ {
+ $table = "\n\n\n";
+
+ foreach (\explode($delim, \trim($line, $delim)) as $hdr) {
+ $table .= '' . \trim($hdr) . " | \n";
+ }
+
+ $table .= "
\n\n\n";
+
+ return $table;
+ }
+
+ public function tableRow($line, $colCount, $delim = '|')
+ {
+ $row = "\n";
+
+ foreach (\explode($delim, \trim($line, $delim)) as $i => $col) {
+ if ($i > $colCount) {
+ break;
+ }
+
+ $col = \trim($col);
+ $row .= "{$col} | \n";
+ }
+
+ $row .= "
\n";
+
+ return $row;
+ }
}