Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
KENNYSOFT committed Oct 7, 2018
0 parents commit c8c3ea7
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 0 deletions.
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright © 2018 KENNYSOFT

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the “Software”), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
127 changes: 127 additions & 0 deletions ParsedownTablespan.php
@@ -0,0 +1,127 @@
<?php
class ParsedownTablespan extends ParsedownExtra
{
const VERSION = '1.0.0';

protected function blockTableComplete(array $Block)
{
if ( ! isset($Block))
{
return null;
}

$HeaderElements =& $Block['element']['text'][0]['text'][0]['text'];

for ($index = count($HeaderElements) - 1; $index >= 0; --$index)
{
$colspan = 1;
$HeaderElement =& $HeaderElements[$index];

while ($index && $HeaderElements[$index - 1]['text'] === '>')
{
$colspan++;
$PreviousHeaderElement =& $HeaderElements[--$index];
$PreviousHeaderElement['merged'] = true;
if (isset($PreviousHeaderElement['attributes']))
{
$HeaderElement['attributes'] = $PreviousHeaderElement['attributes'];
}
}

if ($colspan > 1)
{
if ( ! isset($HeaderElement['attributes']))
{
$HeaderElement['attributes'] = array();
}
$HeaderElement['attributes']['colspan'] = $colspan;
}
}

for ($index = count($HeaderElements) - 1; $index >= 0; --$index)
{
if (isset($HeaderElements[$index]['merged']))
{
array_splice($HeaderElements, $index, 1);
}
}

$Rows =& $Block['element']['text'][1]['text'];

foreach ($Rows as $RowNo => &$Row)
{
$Elements =& $Row['text'];

for ($index = count($Elements) - 1; $index >= 0; --$index)
{
$colspan = 1;
$Element =& $Elements[$index];

while ($index && $Elements[$index - 1]['text'] === '>')
{
$colspan++;
$PreviousElement =& $Elements[--$index];
$PreviousElement['merged'] = true;
if (isset($PreviousElement['attributes']))
{
$Element['attributes'] = $PreviousElement['attributes'];
}
}

if ($colspan > 1)
{
if ( ! isset($Element['attributes']))
{
$Element['attributes'] = array();
}
$Element['attributes']['colspan'] = $colspan;
}
}
}

foreach ($Rows as $RowNo => &$Row)
{
$Elements =& $Row['text'];

foreach ($Elements as $index => &$Element)
{
$rowspan = 1;

if (isset($Element['merged']))
{
continue;
}

while ($RowNo + $rowspan < count($Rows) && $index < count($Rows[$RowNo + $rowspan]['text']) && $Rows[$RowNo + $rowspan]['text'][$index]['text'] === '^' && (@$Element['attributes']['colspan'] ?: null) === (@$Rows[$RowNo + $rowspan]['text'][$index]['attributes']['colspan'] ?: null))
{
$Rows[$RowNo + $rowspan]['text'][$index]['merged'] = true;
$rowspan++;
}

if ($rowspan > 1)
{
if ( ! isset($Element['attributes']))
{
$Element['attributes'] = array();
}
$Element['attributes']['rowspan'] = $rowspan;
}
}
}

foreach ($Rows as $RowNo => &$Row)
{
$Elements =& $Row['text'];

for ($index = count($Elements) - 1; $index >= 0; --$index)
{
if (isset($Elements[$index]['merged']))
{
array_splice($Elements, $index, 1);
}
}
}

return $Block;
}
}
83 changes: 83 additions & 0 deletions README.md
@@ -0,0 +1,83 @@
# Parsedown Tablespan

An extension of [ParsedownExtra](https://github.com/erusev/parsedown-extra) and [Parsedown](http://parsedown.org/) that adds support for rowspan and colspan.

## Installation

### Composer

Install the [composer package](https://packagist.org/packages/KENNYSOFT/parsedown-tablespan "The Parsedown Tablespan package on packagist.org") by running the following command:

```bash
composer require KENNYSOFT/parsedown-tablespan
```

### Manual

1. Download the "Source code" from the [latest release](https://github.com/KENNYSOFT/parsedown-tablespan/releases/latest "The latest release of Parsedown Tablespan")
2. Include `Parsedown.php`, `ParsedownExtra.php`, and `ParsedownTablespan.php`

## Example

```php
$ParsedownTablespan = new ParsedownTablespan();

echo $ParsedownTablespan->text('
| > | > | Colspan | > | for thead |
| ----- | :---------: | -----------: | ----------- | --------- |
| Lorem | ipsum | dolor | sit | amet |
| ^ | - | > | right align | . |
| , | > | center align | > | 2x2 cell |
| > | another 2x2 | + | > | ^ |
| > | ^ | | | ! |
');
```

Prints:

<table>
<thead>
<tr>
<th align="center" colspan="3">Colspan</th>
<th colspan="2">for thead</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2">Lorem</td>
<td align="center">ipsum</td>
<td align="right">dolor</td>
<td>sit</td>
<td>amet</td>
</tr>
<tr>
<td align="center">-</td>
<td align="right" colspan="2">right align</td>
<td>.</td>
</tr>
<tr>
<td>,</td>
<td align="center" colspan="2">center align</td>
<td colspan="2" rowspan="2">2x2 cell</td>
</tr>
<tr>
<td align="center" colspan="2" rowspan="2">another 2x2</td>
<td align="right">+</td>
</tr>
<tr>
<td align="right"></td>
<td></td>
<td>!</td>
</tr>
</tbody>
</table>

_(Since GFM does not accept style attribute for cells, upper table is made with `align` attribute which is not supported in HTML5, just to show what will be generated.)_

## License

- [MIT](http://opensource.org/licenses/MIT)

## Author

KENNYSOFT <hyeonmin.park@kennysoft.kr>
21 changes: 21 additions & 0 deletions composer.json
@@ -0,0 +1,21 @@
{
"name": "kennysoft/parsedown-tablespan",
"description": "An extension of ParsedownExtra that adds support for rowspan and colspan.",
"keywords": ["markdown", "markdown extra", "parser", "parsedown"],
"homepage": "https://github.com/KENNYSOFT/parsedown-tablespan",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "KENNYSOFT",
"email": "hyeonmin.park@kennysoft.kr",
"homepage": "https://kennysoft.kr"
}
],
"require": {
"erusev/parsedown-extra": "~0.7"
},
"autoload": {
"psr-0": {"ParsedownTablespan": ""}
}
}

0 comments on commit c8c3ea7

Please sign in to comment.