Skip to content
Open
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
54 changes: 50 additions & 4 deletions src/ConverterExtra.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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"], '<br />', $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 );
}
}
63 changes: 63 additions & 0 deletions test/ConverterExtraTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<EOF
<table>
<thead>
<tr>
<th>First Header</th>
<th>Second Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>Content<br />Cell</td>
<td>Content Cell</td>
</tr>
<tr>
<td>Content <br />Cell</td>
<td>Content<br />
Cell</td>
</tr>
</tbody>
</table>
EOF;
$md = <<<EOF
| First Header | Second Header |
| -------------- | -------------- |
| Content <br />Cell | Content Cell |
| Content <br />Cell | Content <br />Cell |
EOF;
$this->assertEquals($md, $this->converter->parseString($html));
}

public function testTableConversionWithLongColumns()
{
$html = <<<EOF
<table>
<thead>
<tr>
<th>First Long Header 123456789012345678901234567890123456789012345678901234567890</th>
<th>Second Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>Content Cell</td>
<td>Content Cell</td>
</tr>
<tr>
<td>Content Cell</td>
<td>Long Content Cell 123456789012345678901234567890123456789012345678901234567890</td>
</tr>
</tbody>
</table>
EOF;
$md = <<<EOF
| First Long Header 123456789012345678901234567890123456789012345678901234567890 | Second Header |
| -------------------------------------------------- | -------------------------------------------------- |
| Content Cell | Content Cell |
| Content Cell | Long Content Cell 123456789012345678901234567890123456789012345678901234567890 |
EOF;
$this->assertEquals($md, $this->converter->parseString($html));
}
Expand Down