First version of ParsedownCheckbox

master
Simon Leblanc 7年前
コミット a3b1e7d051

3
.gitignore vendored

@ -0,0 +1,3 @@
phpunit.xml
/vendor/

@ -0,0 +1,8 @@
Copyright (c) 2017 Simon Leblanc <contact@leblanc-simon.eu>
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.

@ -0,0 +1,54 @@
<?php
/**
* This file is part of the ParsedownCheckbox package.
*
* (c) Simon Leblanc <contact@leblanc-simon.eu>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class ParsedownCheckbox extends ParsedownExtra
{
const VERSION = '0.0.1';
protected function blockListComplete($block)
{
if (null === $block) {
return null;
}
if (
false === isset($block['element'])
|| false === isset($block['element']['text'])
|| false === is_array($block['element']['text'])
) {
return $block;
}
$count_element = count($block['element']['text']);
for ($iterator_element = 0; $iterator_element < $count_element; $iterator_element++) {
if (
false === isset($block['element']['text'][$iterator_element]['text'])
|| false === is_array($block['element']['text'][$iterator_element]['text'])
) {
continue;
}
$count_text = count($block['element']['text'][$iterator_element]['text']);
for ($iterator_text = 0; $iterator_text < $count_text; $iterator_text++) {
$begin_line = substr(trim($block['element']['text'][$iterator_element]['text'][$iterator_text]), 0, 4);
if ('[ ] ' === $begin_line) {
$block['element']['text'][$iterator_element]['text'][$iterator_text] = '<input type="checkbox" /> '.
substr(trim($block['element']['text'][$iterator_element]['text'][$iterator_text]), 4);
} elseif ('[x] ' === $begin_line) {
$block['element']['text'][$iterator_element]['text'][$iterator_text] = '<input type="checkbox" checked /> '.
substr(trim($block['element']['text'][$iterator_element]['text'][$iterator_text]), 4);
}
}
}
return $block;
}
}

@ -0,0 +1,38 @@
# Parsedown Checkbox
An extension of [ParsedownExtra](https://github.com/erusev/parsedown-extra) and [Parsedown](http://parsedown.org/) that adds support for task list
## Installation
```bash
composer require leblanc-simon/parsedown-checkbox
```
## Example
```php
<?php
require_once __DIR__.'/vendor/autoload.php';
$parsedown = new ParsedownCheckbox();
echo $parsedown->text('
- [ ] Add a pull request
- [x] Check the issues
');
```
Prints :
- [ ] Add a pull request
- [x] Check the issues
## License
- [MIT](http://opensource.org/licenses/MIT)
## Author
Simon Leblanc <contact@leblanc-simon.eu>

@ -0,0 +1,22 @@
{
"name": "leblanc-simon/parsedown-checkbox",
"description": "An extension of Parsedown and ParsedownExtra that adds support for checkbox",
"keywords": ["markdown", "markdown extra", "parser", "parsedown", "checkbox"],
"homepage": "https://github.com/leblanc-simon/parsedown-checkbox",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Simon Leblanc",
"email": "contact@leblanc-simon.eu",
"homepage": "https://www.leblanc-simon.fr"
}
],
"require": {
"erusev/parsedown-extra": "^0.7.1"
},
"autoload": {
"files": ["ParsedownCheckbox.php"]
}
}

105
composer.lock generated

@ -0,0 +1,105 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "9736195b93bfa2cec806d78b0c8661aa",
"content-hash": "655332acfbe94cf5bf8be89a4df9b70e",
"packages": [
{
"name": "erusev/parsedown",
"version": "1.6.2",
"source": {
"type": "git",
"url": "https://github.com/erusev/parsedown.git",
"reference": "1bf24f7334fe16c88bf9d467863309ceaf285b01"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/erusev/parsedown/zipball/1bf24f7334fe16c88bf9d467863309ceaf285b01",
"reference": "1bf24f7334fe16c88bf9d467863309ceaf285b01",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"autoload": {
"psr-0": {
"Parsedown": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Emanuil Rusev",
"email": "hello@erusev.com",
"homepage": "http://erusev.com"
}
],
"description": "Parser for Markdown.",
"homepage": "http://parsedown.org",
"keywords": [
"markdown",
"parser"
],
"time": "2017-03-29 16:04:15"
},
{
"name": "erusev/parsedown-extra",
"version": "0.7.1",
"source": {
"type": "git",
"url": "https://github.com/erusev/parsedown-extra.git",
"reference": "0db5cce7354e4b76f155d092ab5eb3981c21258c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/erusev/parsedown-extra/zipball/0db5cce7354e4b76f155d092ab5eb3981c21258c",
"reference": "0db5cce7354e4b76f155d092ab5eb3981c21258c",
"shasum": ""
},
"require": {
"erusev/parsedown": "~1.4"
},
"type": "library",
"autoload": {
"psr-0": {
"ParsedownExtra": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Emanuil Rusev",
"email": "hello@erusev.com",
"homepage": "http://erusev.com"
}
],
"description": "An extension of Parsedown that adds support for Markdown Extra.",
"homepage": "https://github.com/erusev/parsedown-extra",
"keywords": [
"markdown",
"markdown extra",
"parsedown",
"parser"
],
"time": "2015-11-01 10:19:22"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": []
}

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="test/bootstrap.php" colors="true">
<testsuites>
<testsuite>
<file>test/ParsedownCheckboxTest.php</file>
</testsuite>
</testsuites>
</phpunit>

@ -0,0 +1,35 @@
<?php
class ParsedownCheckboxTest extends PHPUnit_Framework_TestCase
{
public function testCheckbox()
{
$markdown = <<<EOF
- [ ] test 1
- [] test 2
- [x] test 3
- [ ] test 4
- [x] test 5
- [test](https://markdown.org) 6
- [x](https://markdown.org) test 7
EOF;
$html = <<<EOF
<ul>
<li><input type="checkbox" /> test 1</li>
<li>[] test 2</li>
<li><input type="checkbox" checked /> test 3
<ul>
<li><input type="checkbox" /> test 4</li>
<li><input type="checkbox" checked /> test 5</li>
</ul></li>
<li><a href="https://markdown.org">test</a> 6</li>
<li><a href="https://markdown.org">x</a> test 7</li>
</ul>
EOF;
$parsedown = new ParsedownCheckbox();
$this->assertEquals($html, $parsedown->text($markdown));
}
}

@ -0,0 +1,8 @@
<?php
require_once dirname(__DIR__).'/vendor/autoload.php';
if ( ! class_exists('\PHPUnit_Framework_TestCase')) {
class_alias('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase');
}
読み込み中…
キャンセル
保存