Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: extract header, table, tr logic to helper #15

Merged
merged 3 commits into from
Sep 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 17 additions & 44 deletions src/BlockElementParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<h{$level}>" . \ltrim(\ltrim($this->trimmedLine, '# ')) . "</h{$level}>";
$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<h{$level}>{$this->trimmedLine}</h{$level}>";
$this->markup .= $this->h($this->nextLine, $this->trimmedLine);

$this->pointer++;

Expand All @@ -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<pre><code{$lang}>";
$this->markup .= $this->codeStart($codeMatch);

if (!$codeBlock) {
$this->markup .= $this->escape(\substr($this->line, $this->indentLen));
Expand All @@ -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<hr />";
$this->markup .= $hr = $this->hr($this->trimmedPrevLine, $this->trimmedLine);

return \true;
}
return (bool) $hr;
}

protected function listt()
Expand All @@ -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++;
}
Expand Down Expand Up @@ -222,18 +213,7 @@ protected function table()
return $this->tableInternal($headerCount);
}

$this->markup .= "<tr>\n";

foreach (\explode('|', \trim($this->trimmedLine, '|')) as $i => $col) {
if ($i > $headerCount) {
break;
}

$col = \trim($col);
$this->markup .= "<td>{$col}</td>\n";
}

$this->markup .= "</tr>\n";
$this->markup .= $this->tableRow($this->trimmedLine, $headerCount);

if (empty($this->trimmedNextLine)
|| !\substr_count(\trim($this->trimmedNextLine, '|'), '|')
Expand All @@ -254,14 +234,7 @@ private function tableInternal($headerCount)
$this->pointer++;

$this->inTable = \true;
$this->markup .= "<table>\n<thead>\n<tr>\n";
$this->trimmedLine = \trim($this->trimmedLine, '|');

foreach (\explode('|', $this->trimmedLine) as $hdr) {
$this->markup .= '<th>' . \trim($hdr) . "</th>\n";
}

$this->markup .= "</tr>\n</thead>\n<tbody>\n";
$this->markup .= $this->tableStart($this->trimmedLine);

return \true;
}
Expand Down
68 changes: 68 additions & 0 deletions src/HtmlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<h{$level}>" . \ltrim(\ltrim($line, '# ')) . "</h{$level}>";
}

return '';
}

public function hr($prevLine, $line)
{
if ($prevLine === '' && \preg_match(BlockElementParser::RE_MD_RULE, $line)) {
return "\n<hr />";
}
}

public function codeStart($lang)
{
$lang = isset($lang[1])
? ' class="language-' . $lang[1] . '"'
: '';

return "\n<pre><code{$lang}>";
}

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 = "<table>\n<thead>\n<tr>\n";

foreach (\explode($delim, \trim($line, $delim)) as $hdr) {
$table .= '<th>' . \trim($hdr) . "</th>\n";
}

$table .= "</tr>\n</thead>\n<tbody>\n";

return $table;
}

public function tableRow($line, $colCount, $delim = '|')
{
$row = "<tr>\n";

foreach (\explode($delim, \trim($line, $delim)) as $i => $col) {
if ($i > $colCount) {
break;
}

$col = \trim($col);
$row .= "<td>{$col}</td>\n";
}

$row .= "</tr>\n";

return $row;
}
}