From f35c24d35d8a08ee6666ef07018b68c88689918b Mon Sep 17 00:00:00 2001 From: Tristan Date: Tue, 29 Nov 2022 19:51:35 +0100 Subject: [PATCH] Replace console markdown --- composer.json | 1 + src/Utils/SymfonyConsoleMarkdown.php | 230 --------------------------- 2 files changed, 1 insertion(+), 230 deletions(-) delete mode 100644 src/Utils/SymfonyConsoleMarkdown.php diff --git a/composer.json b/composer.json index e0c7e4b..6a9156f 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,7 @@ "ext-mbstring": "*", "symfony/console": "^5", "cebe/markdown": "^1.2" + "phppkg/cli-markdown": "^2.0", }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.3", diff --git a/src/Utils/SymfonyConsoleMarkdown.php b/src/Utils/SymfonyConsoleMarkdown.php deleted file mode 100644 index 25fc7d6..0000000 --- a/src/Utils/SymfonyConsoleMarkdown.php +++ /dev/null @@ -1,230 +0,0 @@ -%s', $values, $text); - } - - public function render(string $text): string - { - return $this->parse($text); - } - - public function parse($text): string - { - $parsed = parent::parse($text); - - return str_replace(["\n\n\n", "\n\n\n\n"], "\n\n", ltrim($parsed)); - } - - protected function renderHeadline($block): string - { - $level = (int) $block['level']; - - $prefix = str_repeat('#', $level); - $title = $this->renderAbsy($block['content']); - - $hlText = $prefix.' '.$title; - - return self::NL.$this->wrapColor($hlText, fg: 'yellow', options: 'bold').self::NL2; - } - - protected function renderParagraph($block): string - { - return self::NL.$this->renderAbsy($block['content']).self::NL; - } - - protected function renderList($block): string - { - $output = self::NL; - - foreach ($block['items'] as $itemLines) { - $output .= '● '.$this->renderAbsy($itemLines)."\n"; - } - - return $output.self::NL2; - } - - protected function renderTable($block): string - { - $head = $body = ''; - // $cols = $block['cols']; - - $tabInfo = ['width' => 60]; - $colWidths = []; - foreach ($block['rows'] as $row) { - foreach ($row as $c => $cell) { - $cellLen = $this->getCellWith($cell); - - if (!isset($tabInfo[$c])) { - $colWidths[$c] = 16; - } - - $colWidths[$c] = $this->compareMax($cellLen, $colWidths[$c]); - } - } - - $colCount = count($colWidths); - $tabWidth = array_sum($colWidths); - - $first = true; - $splits = []; - foreach ($block['rows'] as $row) { - // $cellTag = $first ? 'th' : 'td'; - $tds = []; - foreach ($row as $c => $cell) { - $cellLen = $colWidths[$c]; - - // ︱||—― ̄====▪▪▭▭▃▃▄▄▁▁▕▏▎┇╇══ - if ($first) { - $splits[] = str_pad('=', $cellLen + 1, '='); - } - - $lastIdx = count($cell) - 1; - // padding space to last item contents. - foreach ($cell as $idx => &$item) { - if ($lastIdx === $idx) { - $item[1] = str_pad($item[1], $cellLen); - } else { - $cellLen -= mb_strlen($item[1]); - } - } - unset($item); - - $tds[] = trim($this->renderAbsy($cell), "\n\r"); - } - - $tdsStr = implode(' | ', $tds); - if ($first) { - $head .= sprintf("%s\n%s\n%s\n", implode('=', $splits), $tdsStr, implode('|', $splits)); - } else { - $body .= $tdsStr."\n"; - } - $first = false; - } - - // return $this->composeTable($head, $body); - return $head.$body.str_pad('=', $tabWidth + $colCount + 1, '=').self::NL; - } - - protected function getCellWith(array $cellElems): int - { - $width = 0; - foreach ($cellElems as $elem) { - $width += mb_strlen($elem[1] ?? ''); - } - - return $width; - } - - protected function renderLink($block): string - { - preg_match('/(\[.*])(\(.*\))/', $block['orig'], $matches); - - [, $title, $link] = $matches; - - $title = substr($title, 1, -1); - $link = substr($link, 1, -1); - - $value = $link === $title ? $link : sprintf('[%s](%s)', $title, $link); - - return $this->wrapColor($value, fg: 'bright-blue'); - } - - #[Pure] - protected function renderAutoUrl($block): string - { - return $this->wrapColor($block[1], fg: 'bright-blue'); - } - - #[Pure] - protected function renderImage($block): string - { - return self::NL.$this->wrapColor('▨ '.$block['orig'], fg: 'blue'); - } - - protected function renderQuote($block): string - { - // ¶ § - $content = ltrim($this->renderAbsy($block['content'])); - - return self::NL.'¶ '.$this->wrapColor($content, fg: 'green', options: 'bold'); - } - - #[Pure] - protected function renderCode($block): string - { - $lines = explode(self::NL, $block['content']); - $text = implode("\n ", $lines); - - return "\n ".$this->wrapColor($text, fg: 'gray').self::NL2; - } - - #[Pure] - protected function renderInlineCode($block): string - { - return $this->wrapColor($block[1], fg: 'bright-red'); - } - - protected function renderStrong($block): string - { - $text = $this->renderAbsy($block[1]); - - return $this->wrapColor(sprintf('**%s**', $text), options: 'bold'); - } - - protected function renderEmph($block): string - { - $text = $this->renderAbsy($block[1]); - - return $this->wrapColor(sprintf('_%s_', $text), fg: 'bright-white'); - } - - /** - * @psalm-suppress ParamNameMismatch Mismatch is caused by a package. - * - * @param mixed $block - */ - protected function renderText($block): string - { - return $block[1]; - } - - private function compareMax(int $len1, int $len2): int - { - return $len1 > $len2 ? $len1 : $len2; - } -}