diff --git a/src/ConverterExtra.php b/src/ConverterExtra.php index a6461de..8bb653d 100644 --- a/src/ConverterExtra.php +++ b/src/ConverterExtra.php @@ -74,6 +74,13 @@ class ConverterExtra extends Converter */ protected $addCssClass = true; + /** + * Max column width for table markdown + * + * @var bool + */ + protected $maxColWidth = 50; + /** * constructor, see Markdownify::Markdownify() for more information */ @@ -393,18 +400,19 @@ protected function alignTdContent(&$content, $col) switch ($this->table['aligns'][$col]) { default: case 'left': - $content .= str_repeat(' ', $this->table['col_widths'][$col] - $this->strlen($content)); + $content .= str_repeat(' ', $this->table['col_widths'][$col] - $this->getMaxColWidth($content, $this->table['col_widths'][$col])); break; case 'right': - $content = str_repeat(' ', $this->table['col_widths'][$col] - $this->strlen($content)) . $content; + $content = str_repeat(' ', $this->table['col_widths'][$col] - $this->getMaxColWidth($content, $this->table['col_widths'][$col])) . $content; break; case 'center': - $paddingNeeded = $this->table['col_widths'][$col] - $this->strlen($content); + $paddingNeeded = $this->table['col_widths'][$col] - $this->getMaxColWidth($content, $this->table['col_widths'][$col]); $left = floor($paddingNeeded / 2); $right = $paddingNeeded - $left; $content = str_repeat(' ', $left) . $content . str_repeat(' ', $right); break; } + $content = $this->nl2br($content); } /** @@ -447,7 +455,7 @@ protected function handleTag_td() if (!isset($this->table['col_widths'][$this->col])) { $this->table['col_widths'][$this->col] = 0; } - $this->table['col_widths'][$this->col] = max($this->table['col_widths'][$this->col], $this->strlen($buffer)); + $this->table['col_widths'][$this->col] = $this->getMaxColWidth($buffer); $this->table['rows'][$this->row][$this->col] = $buffer; } } @@ -659,4 +667,42 @@ public function setAddCssClass($addCssClass) { $this->addCssClass = $addCssClass; } + + /** + * Replace linebreaks with HTML line breaks + * + * @param string $content + * @return string $content + */ + protected function nl2br($content) + { + $content = str_replace( ["\r\n", "\r", "\n"], '
', $content ); + + return $content; + } + + /** + * Get the max column width + * + * @param string $content + * @param integer $curColWidth + * @return integer $maxColLength + */ + protected function getMaxColWidth($content, $curColWidth = NULL) + { + if ($curColWidth === NULL) { + $curMaxColWidth = $this->table['col_widths'][$this->col]; + } + else { + $curMaxColWidth = 0; + } + + $maxColWidth = max($curMaxColWidth, $this->strlen($content)); + + if ($curColWidth !== NULL && $maxColWidth > $curColWidth) { + return $curColWidth; + } + + return( ( $maxColWidth < $this->maxColWidth ) ? $maxColWidth : $this->maxColWidth ); + } } diff --git a/test/ConverterExtraTest.php b/test/ConverterExtraTest.php index 52fcc6b..f7a857b 100644 --- a/test/ConverterExtraTest.php +++ b/test/ConverterExtraTest.php @@ -323,6 +323,69 @@ public function testTableConversionWithRowHeadersAndTopHeaderRow() | January | 42 | | February | 51 | | March | 39 | +EOF; + $this->assertEquals($md, $this->converter->parseString($html)); + } + + public function testTableConversionWithLineBreaks() + { + $html = << + + + First Header + Second Header + + + + + Content
Cell + Content Cell + + + Content
Cell + Content
+ Cell + + + +EOF; + $md = <<Cell | Content Cell | +| Content
Cell | Content
Cell | +EOF; + $this->assertEquals($md, $this->converter->parseString($html)); + } + + public function testTableConversionWithLongColumns() + { + $html = << + + + First Long Header 123456789012345678901234567890123456789012345678901234567890 + Second Header + + + + + Content Cell + Content Cell + + + Content Cell + Long Content Cell 123456789012345678901234567890123456789012345678901234567890 + + + +EOF; + $md = <<assertEquals($md, $this->converter->parseString($html)); }