From c8c3ea7f191568871d6a20dcd526cfcd43010c98 Mon Sep 17 00:00:00 2001 From: KENNYSOFT Date: Sun, 7 Oct 2018 18:32:05 +0900 Subject: [PATCH] v1.0.0 --- LICENSE | 20 +++++++ ParsedownTablespan.php | 127 +++++++++++++++++++++++++++++++++++++++++ README.md | 83 +++++++++++++++++++++++++++ composer.json | 21 +++++++ 4 files changed, 251 insertions(+) create mode 100644 LICENSE create mode 100644 ParsedownTablespan.php create mode 100644 README.md create mode 100644 composer.json diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..bbfa36e --- /dev/null +++ b/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. \ No newline at end of file diff --git a/ParsedownTablespan.php b/ParsedownTablespan.php new file mode 100644 index 0000000..ad372fd --- /dev/null +++ b/ParsedownTablespan.php @@ -0,0 +1,127 @@ += 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; + } +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..94b4342 --- /dev/null +++ b/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: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Colspanfor thead
Loremipsumdolorsitamet
-right align.
,center align2x2 cell
another 2x2+
!
+ +_(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 \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..8332a67 --- /dev/null +++ b/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": ""} + } +}