Skip to content

Add ID and class attribute support to tables, header rows and data rows #154

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

Open
wants to merge 2 commits into
base: lib
Choose a base branch
from
Open
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
84 changes: 57 additions & 27 deletions Michelf/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -2512,21 +2512,25 @@ protected function doTables($text) {
#
$text = preg_replace_callback('
{
^ # Start of a line
[ ]{0,'.$less_than_tab.'} # Allowed whitespace.
[|] # Optional leading pipe (present)
(.+) \n # $1: Header row (at least one pipe)
^ # Start of a line
[ ]{0,'.$less_than_tab.'} # Allowed whitespace.
[|] # Optional leading pipe (present)
(.+?) # $1: Header row (at least one pipe)
(?:'.$this->id_class_attr_catch_re.')? # $2: Extra table attributes
[ ]*\n # Allowed whitespace and newline.

[ ]{0,'.$less_than_tab.'} # Allowed whitespace.
[|] ([ ]*[-:]+[-| :]*) \n # $2: Header underline
[ ]{0,'.$less_than_tab.'} # Allowed whitespace.
[|] ([ ]*[-:]+[-| :]*) # $3: Header underline
(?:'.$this->id_class_attr_catch_re.')? # $4: Extra header row attributes
[ ]*\n # Allowed whitespace and newline.

( # $3: Cells
( # $5: Cells
(?>
[ ]* # Allowed whitespace.
[|] .* \n # Row content.
[ ]* # Allowed whitespace.
[|] .* \n # Row content.
)*
)
(?=\n|\Z) # Stop at final double newline.
(?=\n|\Z) # Stop at final double newline.
}xm',
array(&$this, '_doTable_leadingPipe_callback'), $text);

Expand All @@ -2540,33 +2544,39 @@ protected function doTables($text) {
#
$text = preg_replace_callback('
{
^ # Start of a line
[ ]{0,'.$less_than_tab.'} # Allowed whitespace.
(\S.*[|].*) \n # $1: Header row (at least one pipe)
^ # Start of a line
[ ]{0,'.$less_than_tab.'} # Allowed whitespace.
(\S.*[|].*?) # $1: Header row (at least one pipe)
(?:'.$this->id_class_attr_catch_re.')? # $2: Extra table attributes
[ ]*\n # Allowed whitespace and newline.

[ ]{0,'.$less_than_tab.'} # Allowed whitespace.
([-:]+[ ]*[|][-| :]*) \n # $2: Header underline
[ ]{0,'.$less_than_tab.'} # Allowed whitespace.
([-:]+[ ]*[|][-| :]*) # $3: Header underline
(?:'.$this->id_class_attr_catch_re.')? # $4: Extra header row attributes
[ ]*\n # Allowed whitespace and newline.

( # $3: Cells
( # $5: Cells
(?>
.* [|] .* \n # Row content
.* [|] .* \n # Row content
)*
)
(?=\n|\Z) # Stop at final double newline.
(?=\n|\Z) # Stop at final double newline.
}xm',
array(&$this, '_DoTable_callback'), $text);

return $text;
}
protected function _doTable_leadingPipe_callback($matches) {
$head = $matches[1];
$underline = $matches[2];
$content = $matches[3];
$tableAttrs = $matches[2];
$underline = $matches[3];
$headAttrs = $matches[4];
$content = $matches[5];

# Remove leading pipe for each row.
$content = preg_replace('/^ *[|]/m', '', $content);

return $this->_doTable_callback(array($matches[0], $head, $underline, $content));
return $this->_doTable_callback(array($matches[0], $head, $tableAttrs, $underline, $headAttrs, $content));
}
protected function _doTable_makeAlignAttr($alignname)
{
Expand All @@ -2578,9 +2588,11 @@ protected function _doTable_makeAlignAttr($alignname)
}
protected function _doTable_callback($matches) {
$head = $matches[1];
$underline = $matches[2];
$content = $matches[3];

$tableAttrs = $matches[2];
$underline = $matches[3];
$headAttrs = $matches[4];
$content = $matches[5];

# Remove any tailing pipes for each line.
$head = preg_replace('/[|] *$/m', '', $head);
$underline = preg_replace('/[|] *$/m', '', $underline);
Expand All @@ -2606,10 +2618,16 @@ protected function _doTable_callback($matches) {
$col_count = count($headers);
$attr = array_pad($attr, $col_count, '');

# Process extra table attributes
$tableAttrStr = $this->doExtraAttributes(null, $tableAttrs);

# Process extra header row attributes
$headAttrStr = $this->doExtraAttributes(null, $headAttrs);

# Write column headers.
$text = "<table>\n";
$text = "<table{$tableAttrStr}>\n";
$text .= "<thead>\n";
$text .= "<tr>\n";
$text .= "<tr{$headAttrStr}>\n";
foreach ($headers as $n => $header)
$text .= " <th$attr[$n]>".$this->runSpanGamut(trim($header))."</th>\n";
$text .= "</tr>\n";
Expand All @@ -2620,6 +2638,18 @@ protected function _doTable_callback($matches) {

$text .= "<tbody>\n";
foreach ($rows as $row) {
# Find and process any attributes at the end of each row
$rowAttrMatches = array();
$rowAttrStr = '';
if (preg_match('{'.$this->id_class_attr_catch_re.' *$}', $row, $rowAttrMatches))
{
$rowAttrStr = $this->doExtraAttributes(null, $rowAttrMatches[1]);

# If valid attributes were found, remove the attribute tag and any trailing pipe
if (strlen($rowAttrStr))
$row = preg_replace('{[|]? *'.$this->id_class_attr_nocatch_re.' *$}', '', $row);
}

# Parsing span elements, including code spans, character escapes,
# and inline HTML tags, so that pipes inside those gets ignored.
$row = $this->parseSpan($row);
Expand All @@ -2628,7 +2658,7 @@ protected function _doTable_callback($matches) {
$row_cells = preg_split('/ *[|] */', $row, $col_count);
$row_cells = array_pad($row_cells, $col_count, '');

$text .= "<tr>\n";
$text .= "<tr{$rowAttrStr}>\n";
foreach ($row_cells as $n => $cell)
$text .= " <td$attr[$n]>".$this->runSpanGamut(trim($cell))."</td>\n";
$text .= "</tr>\n";
Expand Down