mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
implement tables
This commit is contained in:
parent
a6756fd4fa
commit
9fd9262f16
152
Parsedown.php
152
Parsedown.php
@ -293,6 +293,59 @@ class Parsedown
|
||||
|
||||
break;
|
||||
|
||||
case 'table':
|
||||
|
||||
if ($line === '')
|
||||
{
|
||||
$context = null;
|
||||
|
||||
continue 2;
|
||||
}
|
||||
|
||||
if ($line[0] === '|')
|
||||
{
|
||||
$nested_blocks = array();
|
||||
|
||||
$substring = preg_replace('/^[|][ ]*/', '', $line);
|
||||
$substring = preg_replace('/[|]?[ ]*$/', '', $substring);
|
||||
|
||||
$parts = explode('|', $substring);
|
||||
|
||||
foreach ($parts as $index => $part)
|
||||
{
|
||||
$substring = trim($part);
|
||||
|
||||
$nested_block = array(
|
||||
'name' => 'td',
|
||||
'content type' => 'markdown',
|
||||
'content' => $substring,
|
||||
);
|
||||
|
||||
if (isset($context_data['alignments'][$index]))
|
||||
{
|
||||
$nested_block['attributes'] = array(
|
||||
'align' => $context_data['alignments'][$index],
|
||||
);
|
||||
}
|
||||
|
||||
$nested_blocks []= $nested_block;
|
||||
}
|
||||
|
||||
$nested_block = array(
|
||||
'name' => 'tr',
|
||||
'content type' => 'blocks',
|
||||
'content' => $nested_blocks,
|
||||
);
|
||||
|
||||
$block['content'][1]['content'] []= $nested_block;
|
||||
|
||||
continue 2;
|
||||
}
|
||||
|
||||
$context = null;
|
||||
|
||||
break;
|
||||
|
||||
case 'paragraph':
|
||||
|
||||
if ($line === '')
|
||||
@ -322,6 +375,105 @@ class Parsedown
|
||||
continue 2;
|
||||
}
|
||||
|
||||
if ($line[0] === '|' and $block['content'][0] === '|' and chop($line, ' -:|') === '')
|
||||
{
|
||||
$values = array();
|
||||
|
||||
$substring = trim($line, ' |');
|
||||
|
||||
$parts = explode('|', $substring);
|
||||
|
||||
foreach ($parts as $part)
|
||||
{
|
||||
$substring = trim($part);
|
||||
|
||||
$value = null;
|
||||
|
||||
if ($substring[0] === ':')
|
||||
{
|
||||
$value = 'left';
|
||||
}
|
||||
|
||||
if (substr($substring, -1) === ':')
|
||||
{
|
||||
$value = $value === 'left' ? 'center' : 'right';
|
||||
}
|
||||
|
||||
$values []= $value;
|
||||
}
|
||||
|
||||
# ~
|
||||
|
||||
$nested_blocks = array();
|
||||
|
||||
$substring = preg_replace('/^[|][ ]*/', '', $block['content']);
|
||||
$substring = preg_replace('/[|]?[ ]*$/', '', $substring);
|
||||
|
||||
$parts = explode('|', $substring);
|
||||
|
||||
foreach ($parts as $index => $part)
|
||||
{
|
||||
$substring = trim($part);
|
||||
|
||||
$nested_block = array(
|
||||
'name' => 'th',
|
||||
'content type' => 'markdown',
|
||||
'content' => $substring,
|
||||
);
|
||||
|
||||
if (isset($values[$index]))
|
||||
{
|
||||
$value = $values[$index];
|
||||
|
||||
$nested_block['attributes'] = array(
|
||||
'align' => $value,
|
||||
);
|
||||
}
|
||||
|
||||
$nested_blocks []= $nested_block;
|
||||
}
|
||||
|
||||
# ~
|
||||
|
||||
$block = array(
|
||||
'name' => 'table',
|
||||
'content type' => 'blocks',
|
||||
'content' => array(),
|
||||
);
|
||||
|
||||
$block['content'] []= array(
|
||||
'name' => 'thead',
|
||||
'content type' => 'blocks',
|
||||
'content' => array(),
|
||||
);
|
||||
|
||||
$block['content'] []= array(
|
||||
'name' => 'tbody',
|
||||
'content type' => 'blocks',
|
||||
'content' => array(),
|
||||
);
|
||||
|
||||
$block['content'][0]['content'] []= array(
|
||||
'name' => 'tr',
|
||||
'content type' => 'blocks',
|
||||
'content' => array(),
|
||||
);
|
||||
|
||||
$block['content'][0]['content'][0]['content'] = $nested_blocks;
|
||||
|
||||
# ~
|
||||
|
||||
$context = 'table';
|
||||
|
||||
$context_data = array(
|
||||
'alignments' => $values,
|
||||
);
|
||||
|
||||
# ~
|
||||
|
||||
continue 2;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
21
tests/data/aligned_table.html
Normal file
21
tests/data/aligned_table.html
Normal file
@ -0,0 +1,21 @@
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th align="left">header 1</th>
|
||||
<th align="center">header 2</th>
|
||||
<th align="right">header 2</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td align="left">cell 1.1</td>
|
||||
<td align="center">cell 1.2</td>
|
||||
<td align="right">cell 1.3</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="left">cell 2.1</td>
|
||||
<td align="center">cell 2.2</td>
|
||||
<td align="right">cell 2.3</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
4
tests/data/aligned_table.md
Normal file
4
tests/data/aligned_table.md
Normal file
@ -0,0 +1,4 @@
|
||||
| header 1 | header 2 | header 2 |
|
||||
| :------- | :------: | -------: |
|
||||
| cell 1.1 | cell 1.2 | cell 1.3 |
|
||||
| cell 2.1 | cell 2.2 | cell 2.3 |
|
18
tests/data/simple_table.html
Normal file
18
tests/data/simple_table.html
Normal file
@ -0,0 +1,18 @@
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>header 1</th>
|
||||
<th>header 2</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>cell 1.1</td>
|
||||
<td>cell 1.2</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>cell 2.1</td>
|
||||
<td>cell 2.2</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
4
tests/data/simple_table.md
Normal file
4
tests/data/simple_table.md
Normal file
@ -0,0 +1,4 @@
|
||||
| header 1 | header 2 |
|
||||
| -------- | -------- |
|
||||
| cell 1.1 | cell 1.2 |
|
||||
| cell 2.1 | cell 2.2 |
|
18
tests/data/table_inline_markdown.html
Normal file
18
tests/data/table_inline_markdown.html
Normal file
@ -0,0 +1,18 @@
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th><em>header</em> 1</th>
|
||||
<th>header 2</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td><em>cell</em> 1.1</td>
|
||||
<td><del>cell</del> 1.2</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>cell</code> 2.1</td>
|
||||
<td>cell 2.2</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
4
tests/data/table_inline_markdown.md
Normal file
4
tests/data/table_inline_markdown.md
Normal file
@ -0,0 +1,4 @@
|
||||
| _header_ 1 | header 2 |
|
||||
| ------------ | ------------ |
|
||||
| _cell_ 1.1 | ~~cell~~ 1.2 |
|
||||
| `cell` 2.1 | cell 2.2 |
|
18
tests/data/untidy_table.html
Normal file
18
tests/data/untidy_table.html
Normal file
@ -0,0 +1,18 @@
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>header 1</th>
|
||||
<th>header 2</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>cell 1.1</td>
|
||||
<td>cell 1.2</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>cell 2.1</td>
|
||||
<td>cell 2.2</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
4
tests/data/untidy_table.md
Normal file
4
tests/data/untidy_table.md
Normal file
@ -0,0 +1,4 @@
|
||||
| header 1 | header 2 |
|
||||
| ------------- | ----------- |
|
||||
| cell 1.1 | cell 1.2 |
|
||||
| cell 2.1 | cell 2.2 |
|
Loading…
Reference in New Issue
Block a user