From 9fd9262f1692bb6021aadb5be82d803905e14462 Mon Sep 17 00:00:00 2001 From: Emanuil Rusev Date: Sun, 23 Feb 2014 18:55:34 +0200 Subject: [PATCH] implement tables --- Parsedown.php | 152 ++++++++++++++++++++++++++ tests/data/aligned_table.html | 21 ++++ tests/data/aligned_table.md | 4 + tests/data/simple_table.html | 18 +++ tests/data/simple_table.md | 4 + tests/data/table_inline_markdown.html | 18 +++ tests/data/table_inline_markdown.md | 4 + tests/data/untidy_table.html | 18 +++ tests/data/untidy_table.md | 4 + 9 files changed, 243 insertions(+) create mode 100644 tests/data/aligned_table.html create mode 100644 tests/data/aligned_table.md create mode 100644 tests/data/simple_table.html create mode 100644 tests/data/simple_table.md create mode 100644 tests/data/table_inline_markdown.html create mode 100644 tests/data/table_inline_markdown.md create mode 100644 tests/data/untidy_table.html create mode 100644 tests/data/untidy_table.md diff --git a/Parsedown.php b/Parsedown.php index a9702af..a56ae6e 100755 --- a/Parsedown.php +++ b/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: diff --git a/tests/data/aligned_table.html b/tests/data/aligned_table.html new file mode 100644 index 0000000..0657bd1 --- /dev/null +++ b/tests/data/aligned_table.html @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + +
header 1header 2header 2
cell 1.1cell 1.2cell 1.3
cell 2.1cell 2.2cell 2.3
\ No newline at end of file diff --git a/tests/data/aligned_table.md b/tests/data/aligned_table.md new file mode 100644 index 0000000..69a45f9 --- /dev/null +++ b/tests/data/aligned_table.md @@ -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 | \ No newline at end of file diff --git a/tests/data/simple_table.html b/tests/data/simple_table.html new file mode 100644 index 0000000..88e1c2b --- /dev/null +++ b/tests/data/simple_table.html @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + +
header 1header 2
cell 1.1cell 1.2
cell 2.1cell 2.2
\ No newline at end of file diff --git a/tests/data/simple_table.md b/tests/data/simple_table.md new file mode 100644 index 0000000..5245e6c --- /dev/null +++ b/tests/data/simple_table.md @@ -0,0 +1,4 @@ +| header 1 | header 2 | +| -------- | -------- | +| cell 1.1 | cell 1.2 | +| cell 2.1 | cell 2.2 | \ No newline at end of file diff --git a/tests/data/table_inline_markdown.html b/tests/data/table_inline_markdown.html new file mode 100644 index 0000000..53d0eb8 --- /dev/null +++ b/tests/data/table_inline_markdown.html @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + +
header 1header 2
cell 1.1cell 1.2
cell 2.1cell 2.2
\ No newline at end of file diff --git a/tests/data/table_inline_markdown.md b/tests/data/table_inline_markdown.md new file mode 100644 index 0000000..c2fe108 --- /dev/null +++ b/tests/data/table_inline_markdown.md @@ -0,0 +1,4 @@ +| _header_ 1 | header 2 | +| ------------ | ------------ | +| _cell_ 1.1 | ~~cell~~ 1.2 | +| `cell` 2.1 | cell 2.2 | \ No newline at end of file diff --git a/tests/data/untidy_table.html b/tests/data/untidy_table.html new file mode 100644 index 0000000..88e1c2b --- /dev/null +++ b/tests/data/untidy_table.html @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + +
header 1header 2
cell 1.1cell 1.2
cell 2.1cell 2.2
\ No newline at end of file diff --git a/tests/data/untidy_table.md b/tests/data/untidy_table.md new file mode 100644 index 0000000..8524eb1 --- /dev/null +++ b/tests/data/untidy_table.md @@ -0,0 +1,4 @@ +| header 1 | header 2 | +| ------------- | ----------- | +| cell 1.1 | cell 1.2 | +| cell 2.1 | cell 2.2 | \ No newline at end of file