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

Add ID and class attribute support to tables #153

Open
wants to merge 1 commit 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
59 changes: 34 additions & 25 deletions Michelf/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -2512,21 +2512,23 @@ 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.
[|] ([ ]*[-:]+[-| :]*) \n # $3: Header underline

( # $3: Cells
( # $4: 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 +2542,36 @@ 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.
([-:]+[ ]*[|][-| :]*) \n # $3: Header underline

( # $3: Cells
( # $4: 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];
$content = $matches[4];

# 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, $content));
}
protected function _doTable_makeAlignAttr($alignname)
{
Expand All @@ -2578,9 +2583,10 @@ 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];
$content = $matches[4];

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

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

# Write column headers.
$text = "<table>\n";
$text = "<table{$tableAttrStr}>\n";
$text .= "<thead>\n";
$text .= "<tr>\n";
foreach ($headers as $n => $header)
Expand Down