Browse Source

First version of ParsedownCheckbox

tags/0.0.1
Simon Leblanc 2 years ago
commit
a3b1e7d051
9 changed files with 282 additions and 0 deletions
  1. 3
    0
      .gitignore
  2. 8
    0
      LICENSE
  3. 54
    0
      ParsedownCheckbox.php
  4. 38
    0
      README.md
  5. 22
    0
      composer.json
  6. 105
    0
      composer.lock
  7. 9
    0
      phpunit.xml.dist
  8. 35
    0
      test/ParsedownCheckboxTest.php
  9. 8
    0
      test/bootstrap.php

+ 3
- 0
.gitignore View File

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

+ 8
- 0
LICENSE View File

@@ -0,0 +1,8 @@
1
+Copyright (c) 2017 Simon Leblanc <contact@leblanc-simon.eu>
2
+
3
+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:
4
+
5
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+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.
8
+

+ 54
- 0
ParsedownCheckbox.php View File

@@ -0,0 +1,54 @@
1
+<?php
2
+/**
3
+ * This file is part of the ParsedownCheckbox package.
4
+ *
5
+ * (c) Simon Leblanc <contact@leblanc-simon.eu>
6
+ *
7
+ * For the full copyright and license information, please view the LICENSE
8
+ * file that was distributed with this source code.
9
+ */
10
+
11
+class ParsedownCheckbox extends ParsedownExtra
12
+{
13
+    const VERSION = '0.0.1';
14
+
15
+    protected function blockListComplete($block)
16
+    {
17
+        if (null === $block) {
18
+            return null;
19
+        }
20
+
21
+        if (
22
+            false === isset($block['element'])
23
+            || false === isset($block['element']['text'])
24
+            || false === is_array($block['element']['text'])
25
+        ) {
26
+            return $block;
27
+        }
28
+
29
+        $count_element = count($block['element']['text']);
30
+        for ($iterator_element = 0; $iterator_element < $count_element; $iterator_element++) {
31
+            if (
32
+                false === isset($block['element']['text'][$iterator_element]['text'])
33
+                || false === is_array($block['element']['text'][$iterator_element]['text'])
34
+            ) {
35
+                continue;
36
+            }
37
+
38
+            $count_text = count($block['element']['text'][$iterator_element]['text']);
39
+            for ($iterator_text = 0; $iterator_text < $count_text; $iterator_text++) {
40
+                $begin_line = substr(trim($block['element']['text'][$iterator_element]['text'][$iterator_text]), 0, 4);
41
+                if ('[ ] ' === $begin_line) {
42
+                    $block['element']['text'][$iterator_element]['text'][$iterator_text] = '<input type="checkbox" /> '.
43
+                        substr(trim($block['element']['text'][$iterator_element]['text'][$iterator_text]), 4);
44
+                } elseif ('[x] ' === $begin_line) {
45
+                    $block['element']['text'][$iterator_element]['text'][$iterator_text] = '<input type="checkbox" checked /> '.
46
+                        substr(trim($block['element']['text'][$iterator_element]['text'][$iterator_text]), 4);
47
+                }
48
+            }
49
+        }
50
+
51
+        return $block;
52
+    }
53
+}
54
+

+ 38
- 0
README.md View File

@@ -0,0 +1,38 @@
1
+# Parsedown Checkbox
2
+
3
+An extension of [ParsedownExtra](https://github.com/erusev/parsedown-extra) and [Parsedown](http://parsedown.org/) that adds support for task list
4
+
5
+## Installation
6
+
7
+```bash
8
+composer require leblanc-simon/parsedown-checkbox
9
+```
10
+
11
+## Example
12
+
13
+```php
14
+<?php
15
+
16
+require_once __DIR__.'/vendor/autoload.php';
17
+
18
+$parsedown = new ParsedownCheckbox();
19
+
20
+echo $parsedown->text('
21
+- [ ] Add a pull request
22
+- [x] Check the issues
23
+');
24
+```
25
+
26
+Prints :
27
+
28
+- [ ] Add a pull request
29
+- [x] Check the issues
30
+
31
+## License
32
+
33
+- [MIT](http://opensource.org/licenses/MIT)
34
+
35
+## Author
36
+
37
+Simon Leblanc <contact@leblanc-simon.eu>
38
+

+ 22
- 0
composer.json View File

@@ -0,0 +1,22 @@
1
+{
2
+    "name": "leblanc-simon/parsedown-checkbox",
3
+    "description": "An extension of Parsedown and ParsedownExtra that adds support for checkbox",
4
+    "keywords": ["markdown", "markdown extra", "parser", "parsedown", "checkbox"],
5
+    "homepage": "https://github.com/leblanc-simon/parsedown-checkbox",
6
+    "type": "library",
7
+    "license": "MIT",
8
+    "authors": [
9
+        {
10
+            "name": "Simon Leblanc",
11
+            "email": "contact@leblanc-simon.eu",
12
+            "homepage": "https://www.leblanc-simon.fr"
13
+        }
14
+    ],
15
+    "require": {
16
+        "erusev/parsedown-extra": "^0.7.1"
17
+    },
18
+    "autoload": {
19
+        "files": ["ParsedownCheckbox.php"]
20
+    }
21
+}
22
+

+ 105
- 0
composer.lock View File

@@ -0,0 +1,105 @@
1
+{
2
+    "_readme": [
3
+        "This file locks the dependencies of your project to a known state",
4
+        "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
5
+        "This file is @generated automatically"
6
+    ],
7
+    "hash": "9736195b93bfa2cec806d78b0c8661aa",
8
+    "content-hash": "655332acfbe94cf5bf8be89a4df9b70e",
9
+    "packages": [
10
+        {
11
+            "name": "erusev/parsedown",
12
+            "version": "1.6.2",
13
+            "source": {
14
+                "type": "git",
15
+                "url": "https://github.com/erusev/parsedown.git",
16
+                "reference": "1bf24f7334fe16c88bf9d467863309ceaf285b01"
17
+            },
18
+            "dist": {
19
+                "type": "zip",
20
+                "url": "https://api.github.com/repos/erusev/parsedown/zipball/1bf24f7334fe16c88bf9d467863309ceaf285b01",
21
+                "reference": "1bf24f7334fe16c88bf9d467863309ceaf285b01",
22
+                "shasum": ""
23
+            },
24
+            "require": {
25
+                "php": ">=5.3.0"
26
+            },
27
+            "type": "library",
28
+            "autoload": {
29
+                "psr-0": {
30
+                    "Parsedown": ""
31
+                }
32
+            },
33
+            "notification-url": "https://packagist.org/downloads/",
34
+            "license": [
35
+                "MIT"
36
+            ],
37
+            "authors": [
38
+                {
39
+                    "name": "Emanuil Rusev",
40
+                    "email": "hello@erusev.com",
41
+                    "homepage": "http://erusev.com"
42
+                }
43
+            ],
44
+            "description": "Parser for Markdown.",
45
+            "homepage": "http://parsedown.org",
46
+            "keywords": [
47
+                "markdown",
48
+                "parser"
49
+            ],
50
+            "time": "2017-03-29 16:04:15"
51
+        },
52
+        {
53
+            "name": "erusev/parsedown-extra",
54
+            "version": "0.7.1",
55
+            "source": {
56
+                "type": "git",
57
+                "url": "https://github.com/erusev/parsedown-extra.git",
58
+                "reference": "0db5cce7354e4b76f155d092ab5eb3981c21258c"
59
+            },
60
+            "dist": {
61
+                "type": "zip",
62
+                "url": "https://api.github.com/repos/erusev/parsedown-extra/zipball/0db5cce7354e4b76f155d092ab5eb3981c21258c",
63
+                "reference": "0db5cce7354e4b76f155d092ab5eb3981c21258c",
64
+                "shasum": ""
65
+            },
66
+            "require": {
67
+                "erusev/parsedown": "~1.4"
68
+            },
69
+            "type": "library",
70
+            "autoload": {
71
+                "psr-0": {
72
+                    "ParsedownExtra": ""
73
+                }
74
+            },
75
+            "notification-url": "https://packagist.org/downloads/",
76
+            "license": [
77
+                "MIT"
78
+            ],
79
+            "authors": [
80
+                {
81
+                    "name": "Emanuil Rusev",
82
+                    "email": "hello@erusev.com",
83
+                    "homepage": "http://erusev.com"
84
+                }
85
+            ],
86
+            "description": "An extension of Parsedown that adds support for Markdown Extra.",
87
+            "homepage": "https://github.com/erusev/parsedown-extra",
88
+            "keywords": [
89
+                "markdown",
90
+                "markdown extra",
91
+                "parsedown",
92
+                "parser"
93
+            ],
94
+            "time": "2015-11-01 10:19:22"
95
+        }
96
+    ],
97
+    "packages-dev": [],
98
+    "aliases": [],
99
+    "minimum-stability": "stable",
100
+    "stability-flags": [],
101
+    "prefer-stable": false,
102
+    "prefer-lowest": false,
103
+    "platform": [],
104
+    "platform-dev": []
105
+}

+ 9
- 0
phpunit.xml.dist View File

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

+ 35
- 0
test/ParsedownCheckboxTest.php View File

@@ -0,0 +1,35 @@
1
+<?php
2
+
3
+class ParsedownCheckboxTest extends PHPUnit_Framework_TestCase
4
+{
5
+    public function testCheckbox()
6
+    {
7
+        $markdown = <<<EOF
8
+- [ ] test 1
9
+- [] test 2
10
+- [x] test 3
11
+    - [ ] test 4
12
+    - [x] test 5
13
+- [test](https://markdown.org) 6
14
+- [x](https://markdown.org) test 7
15
+EOF;
16
+
17
+        $html = <<<EOF
18
+<ul>
19
+<li><input type="checkbox" /> test 1</li>
20
+<li>[] test 2</li>
21
+<li><input type="checkbox" checked /> test 3
22
+<ul>
23
+<li><input type="checkbox" /> test 4</li>
24
+<li><input type="checkbox" checked /> test 5</li>
25
+</ul></li>
26
+<li><a href="https://markdown.org">test</a> 6</li>
27
+<li><a href="https://markdown.org">x</a> test 7</li>
28
+</ul>
29
+EOF;
30
+
31
+        $parsedown = new ParsedownCheckbox();
32
+        $this->assertEquals($html, $parsedown->text($markdown));
33
+    }
34
+}
35
+

+ 8
- 0
test/bootstrap.php View File

@@ -0,0 +1,8 @@
1
+<?php
2
+
3
+require_once dirname(__DIR__).'/vendor/autoload.php';
4
+
5
+if ( ! class_exists('\PHPUnit_Framework_TestCase')) {
6
+    class_alias('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase');
7
+}
8
+

Loading…
Cancel
Save