1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00

implement tables

This commit is contained in:
Emanuil Rusev 2014-02-23 18:55:34 +02:00
parent a6756fd4fa
commit 9fd9262f16
9 changed files with 243 additions and 0 deletions

View File

@ -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:

View 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>

View 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 |

View 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>

View File

@ -0,0 +1,4 @@
| header 1 | header 2 |
| -------- | -------- |
| cell 1.1 | cell 1.2 |
| cell 2.1 | cell 2.2 |

View 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>

View File

@ -0,0 +1,4 @@
| _header_ 1 | header 2 |
| ------------ | ------------ |
| _cell_ 1.1 | ~~cell~~ 1.2 |
| `cell` 2.1 | cell 2.2 |

View 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>

View File

@ -0,0 +1,4 @@
| header 1 | header 2 |
| ------------- | ----------- |
| cell 1.1 | cell 1.2 |
| cell 2.1 | cell 2.2 |