mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Compare commits
78 Commits
Author | SHA1 | Date | |
---|---|---|---|
6bee326c92 | |||
3fe867d294 | |||
f08d017bcb | |||
e61a6114b0 | |||
9ed72ccd09 | |||
09e1184d9f | |||
2de60a9a8b | |||
73a75299f5 | |||
0d28808392 | |||
78960cf792 | |||
8f2e9c7cf6 | |||
3eb6d349f0 | |||
859b1b10c1 | |||
08b01a1a29 | |||
1686b2fbff | |||
15a32fcd0e | |||
4aca208f96 | |||
cedf96a64e | |||
9f58363e4b | |||
6b4a459f97 | |||
05bf198d26 | |||
30234a58fa | |||
03ff22c7df | |||
098f188552 | |||
e68a458105 | |||
86a27b48bc | |||
c45dee6850 | |||
06135cd75a | |||
7d3af6bf83 | |||
dfacf7a71a | |||
fd0d8125e7 | |||
b1be886d65 | |||
19bc6a7083 | |||
b5efe98e2f | |||
5639ef7d69 | |||
d42fcdc423 | |||
d29d879ec6 | |||
c9b4de3c9d | |||
38cc1ca7e0 | |||
23c4097fde | |||
05e87566a9 | |||
ac68800717 | |||
1aade35c5e | |||
361febf7c6 | |||
715f7572ad | |||
907bd11613 | |||
56c6169822 | |||
6d54fda73a | |||
3b5e4e23ec | |||
85ee06898b | |||
4c24e68b42 | |||
094cb88dac | |||
7ab3c60a77 | |||
2438c1a43d | |||
46196c1ac3 | |||
aa3d4d6eb7 | |||
6fb534bc34 | |||
28a202ee9e | |||
e46be110fb | |||
495e7ac73b | |||
5bc6d90f8b | |||
9816507a75 | |||
7000cbc2d2 | |||
6df242bc97 | |||
f4453fd729 | |||
d8011c00ab | |||
da5d75e97e | |||
2adb87ef41 | |||
74926c9831 | |||
68f3aea036 | |||
f91e4dece3 | |||
d53c7dbcd9 | |||
42222e6b01 | |||
e7d160049e | |||
ce4a29aec5 | |||
8ecf828777 | |||
c18ff7f370 | |||
6f1fac9823 |
702
Parsedown.php
702
Parsedown.php
File diff suppressed because it is too large
Load Diff
31
README.md
31
README.md
@ -1,6 +1,6 @@
|
|||||||
## Parsedown
|
## Parsedown
|
||||||
|
|
||||||
Better [Markdown](http://en.wikipedia.org/wiki/Markdown) parser for PHP.
|
Better Markdown Parser in PHP
|
||||||
|
|
||||||
[[ demo ]](http://parsedown.org/demo)
|
[[ demo ]](http://parsedown.org/demo)
|
||||||
|
|
||||||
@ -8,11 +8,10 @@ Better [Markdown](http://en.wikipedia.org/wiki/Markdown) parser for PHP.
|
|||||||
|
|
||||||
* [Fast](http://parsedown.org/speed)
|
* [Fast](http://parsedown.org/speed)
|
||||||
* [Consistent](http://parsedown.org/consistency)
|
* [Consistent](http://parsedown.org/consistency)
|
||||||
* [GitHub Flavored](https://help.github.com/articles/github-flavored-markdown)
|
* [GitHub flavored](https://help.github.com/articles/github-flavored-markdown)
|
||||||
* [Tested](http://parsedown.org/tests/) in PHP 5.2, 5.3, 5.4, 5.5, 5.6 and [hhvm](http://www.hhvm.com/)
|
* [Tested](http://parsedown.org/tests/) in PHP 5.2, 5.3, 5.4, 5.5, 5.6 and [hhvm](http://www.hhvm.com/)
|
||||||
* Extensible
|
* [Extensible](https://github.com/erusev/parsedown/wiki/Writing-Extensions)
|
||||||
* [Markdown Extra extension](https://github.com/erusev/parsedown-extra) <sup>new</sup>
|
* [Markdown Extra extension](https://github.com/erusev/parsedown-extra)
|
||||||
* [JavaScript port](https://github.com/hkdobrev/parsedown.js) under development <sup>new</sup>
|
|
||||||
|
|
||||||
### Installation
|
### Installation
|
||||||
|
|
||||||
@ -30,14 +29,20 @@ More examples in [the wiki](https://github.com/erusev/parsedown/wiki/Usage) and
|
|||||||
|
|
||||||
### Questions
|
### Questions
|
||||||
|
|
||||||
**How does Parsedown work?**<br/>
|
**How does Parsedown work?**
|
||||||
Parsedown recognises that the Markdown syntax is optimised for humans so it tries to read like one. It goes through text line by line. It looks at how lines start to identify blocks. It looks for special characters to identify inline elements.
|
|
||||||
|
|
||||||
**Why doesn’t Parsedown use namespaces?**<br/>
|
It tries to read Markdown like a human. First, it looks at the lines. It’s interested in how the lines start. This helps it recognise blocks. It knows, for example, that if a line start with a `-` then it perhaps belong to a list. Once it recognises the blocks, it continues to the content. As it reads, it watches out for special characters. This helps it recognise inline elements (or inlines).
|
||||||
Using namespaces would mean dropping support for PHP 5.2. We believe that since Parsedown is a single class with an uncommon name, making this trade wouldn't be worth it.
|
|
||||||
|
|
||||||
**Is Parsedown compliant with CommonMark?**<br/>
|
We call this approach "line based". We believe that Parsedown is the first Markdown parser to use it. Since the release of Parsedown, other developers have used the same approach to develop other Markdown parsers in PHP and in other languages.
|
||||||
We are [working on it](https://github.com/erusev/parsedown/tree/commonmark).
|
|
||||||
|
|
||||||
**Who uses Parsedown?**<br/>
|
**Is Parsedown compliant with CommonMark?**
|
||||||
[phpDocumentor](http://www.phpdoc.org/), [October CMS](http://octobercms.com/), [Bolt CMS](http://bolt.cm/), [RaspberryPi.org](http://www.raspberrypi.org/) and [more](https://www.versioneye.com/php/erusev:parsedown/references).
|
|
||||||
|
The majority of the CommonMark tests pass. Most of the tests that don't pass deal with cases that are quite extreme. Yet, we are working on them. As CommonMark matures, compliance should improve.
|
||||||
|
|
||||||
|
**Who uses Parsedown?**
|
||||||
|
|
||||||
|
[phpDocumentor](http://www.phpdoc.org/), [October CMS](http://octobercms.com/), [Bolt CMS](http://bolt.cm/), [Kirby CMS](http://getkirby.com/), [Grav CMS](http://getgrav.org/), [Statamic CMS](http://www.statamic.com/), [RaspberryPi.org](http://www.raspberrypi.org/) and [more](https://www.versioneye.com/php/erusev:parsedown/references).
|
||||||
|
|
||||||
|
**How can I help?**
|
||||||
|
|
||||||
|
Use the project, tell friends about it and if you feel generous, [donate some money](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=528P3NZQMP8N2).
|
||||||
|
74
test/CommonMarkTest.php
Normal file
74
test/CommonMarkTest.php
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test Parsedown against the CommonMark spec.
|
||||||
|
*
|
||||||
|
* Some code based on the original JavaScript test runner by jgm.
|
||||||
|
*
|
||||||
|
* @link http://commonmark.org/ CommonMark
|
||||||
|
* @link http://git.io/8WtRvQ JavaScript test runner
|
||||||
|
*/
|
||||||
|
class CommonMarkTest extends PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
const SPEC_URL = 'https://raw.githubusercontent.com/jgm/stmd/master/spec.txt';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider data
|
||||||
|
* @param $section
|
||||||
|
* @param $markdown
|
||||||
|
* @param $expectedHtml
|
||||||
|
*/
|
||||||
|
function test_($section, $markdown, $expectedHtml)
|
||||||
|
{
|
||||||
|
$Parsedown = new Parsedown();
|
||||||
|
$Parsedown->setUrlsLinked(false);
|
||||||
|
|
||||||
|
$actualHtml = $Parsedown->text($markdown);
|
||||||
|
$actualHtml = $this->normalizeMarkup($actualHtml);
|
||||||
|
|
||||||
|
$this->assertEquals($expectedHtml, $actualHtml);
|
||||||
|
}
|
||||||
|
|
||||||
|
function data()
|
||||||
|
{
|
||||||
|
$spec = file_get_contents(self::SPEC_URL);
|
||||||
|
$spec = strstr($spec, '<!-- END TESTS -->', true);
|
||||||
|
|
||||||
|
$tests = array();
|
||||||
|
$currentSection = '';
|
||||||
|
|
||||||
|
preg_replace_callback(
|
||||||
|
'/^\.\n([\s\S]*?)^\.\n([\s\S]*?)^\.$|^#{1,6} *(.*)$/m',
|
||||||
|
function($matches) use ( & $tests, & $currentSection, & $testCount) {
|
||||||
|
if (isset($matches[3]) and $matches[3]) {
|
||||||
|
$currentSection = $matches[3];
|
||||||
|
} else {
|
||||||
|
$testCount++;
|
||||||
|
$markdown = $matches[1];
|
||||||
|
$markdown = preg_replace('/→/', "\t", $markdown);
|
||||||
|
$expectedHtml = $matches[2];
|
||||||
|
$expectedHtml = $this->normalizeMarkup($expectedHtml);
|
||||||
|
$tests []= array(
|
||||||
|
$currentSection, # section
|
||||||
|
$markdown, # markdown
|
||||||
|
$expectedHtml, # html
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
$spec
|
||||||
|
);
|
||||||
|
|
||||||
|
return $tests;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function normalizeMarkup($markup)
|
||||||
|
{
|
||||||
|
$markup = preg_replace("/\n+/", "\n", $markup);
|
||||||
|
$markup = preg_replace('/^\s+/m', '', $markup);
|
||||||
|
$markup = preg_replace('/^((?:<[\w]+>)+)\n/m', '$1', $markup);
|
||||||
|
$markup = preg_replace('/\n((?:<\/[\w]+>)+)$/m', '$1', $markup);
|
||||||
|
$markup = trim($markup);
|
||||||
|
|
||||||
|
return $markup;
|
||||||
|
}
|
||||||
|
}
|
@ -109,22 +109,28 @@ paragraph
|
|||||||
color: red;
|
color: red;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
|
comment
|
||||||
|
|
||||||
|
<!-- html comment -->
|
||||||
MARKDOWN_WITH_MARKUP;
|
MARKDOWN_WITH_MARKUP;
|
||||||
|
|
||||||
$expectedHtml = <<<EXPECTED_HTML
|
$expectedHtml = <<<EXPECTED_HTML
|
||||||
<p><div><em>content</em></div></p>
|
<p><div><em>content</em></div></p>
|
||||||
<p>sparse:</p>
|
<p>sparse:</p>
|
||||||
<p><div>
|
<p><div>
|
||||||
<div class="inner">
|
<div class="inner">
|
||||||
<em>content</em>
|
<em>content</em>
|
||||||
</div>
|
</div>
|
||||||
</div></p>
|
</div></p>
|
||||||
<p>paragraph</p>
|
<p>paragraph</p>
|
||||||
<p><style type="text/css"></p>
|
<p><style type="text/css">
|
||||||
<pre><code>p {
|
p {
|
||||||
color: red;
|
color: red;
|
||||||
}</code></pre>
|
}
|
||||||
<p></style></p>
|
</style></p>
|
||||||
|
<p>comment</p>
|
||||||
|
<p><!-- html comment --></p>
|
||||||
EXPECTED_HTML;
|
EXPECTED_HTML;
|
||||||
$parsedownWithNoMarkup = new Parsedown();
|
$parsedownWithNoMarkup = new Parsedown();
|
||||||
$parsedownWithNoMarkup->setMarkupEscaped(true);
|
$parsedownWithNoMarkup->setMarkupEscaped(true);
|
||||||
|
@ -4,6 +4,6 @@
|
|||||||
<h4>h4</h4>
|
<h4>h4</h4>
|
||||||
<h5>h5</h5>
|
<h5>h5</h5>
|
||||||
<h6>h6</h6>
|
<h6>h6</h6>
|
||||||
<h6>h6</h6>
|
<p>####### not a heading</p>
|
||||||
<h1>closed h1</h1>
|
<h1>closed h1</h1>
|
||||||
<p>#</p>
|
<p>#</p>
|
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
###### h6
|
###### h6
|
||||||
|
|
||||||
####### h6
|
####### not a heading
|
||||||
|
|
||||||
# closed h1 #
|
# closed h1 #
|
||||||
|
|
||||||
|
@ -2,3 +2,5 @@
|
|||||||
<p><code>escaped \*emphasis\* in a code span</code></p>
|
<p><code>escaped \*emphasis\* in a code span</code></p>
|
||||||
<pre><code>escaped \*emphasis\* in a code block</code></pre>
|
<pre><code>escaped \*emphasis\* in a code block</code></pre>
|
||||||
<p>\ ` * _ { } [ ] ( ) > # + - . !</p>
|
<p>\ ` * _ { } [ ] ( ) > # + - . !</p>
|
||||||
|
<p><em>one_two</em> <strong>one_two</strong></p>
|
||||||
|
<p><em>one*two</em> <strong>one*two</strong></p>
|
@ -5,3 +5,7 @@ escaped \*emphasis\*.
|
|||||||
escaped \*emphasis\* in a code block
|
escaped \*emphasis\* in a code block
|
||||||
|
|
||||||
\\ \` \* \_ \{ \} \[ \] \( \) \> \# \+ \- \. \!
|
\\ \` \* \_ \{ \} \[ \] \( \) \> \# \+ \- \. \!
|
||||||
|
|
||||||
|
_one\_two_ __one\_two__
|
||||||
|
|
||||||
|
*one\*two* **one\*two**
|
@ -1 +1,2 @@
|
|||||||
<p><img alt="Markdown Logo" src="/md.png" /></p>
|
<p><img src="/md.png" alt="Markdown Logo" /></p>
|
||||||
|
<p>![missing reference]</p>
|
@ -1,3 +1,5 @@
|
|||||||
![Markdown Logo][image]
|
![Markdown Logo][image]
|
||||||
|
|
||||||
[image]: /md.png
|
[image]: /md.png
|
||||||
|
|
||||||
|
![missing reference]
|
@ -1 +1 @@
|
|||||||
<p><img alt="alt" src="/md.png" title="title" /></p>
|
<p><img src="/md.png" alt="alt" title="title" /></p>
|
@ -1,4 +1,4 @@
|
|||||||
<p><a href="http://example.com">link</a> and <a href="/tests/">another link</a></p>
|
<p><a href="http://example.com">link</a> and <a href="/url-with-(parentheses)">another link</a></p>
|
||||||
<p><a href="http://example.com"><code>link</code></a></p>
|
<p><a href="http://example.com"><code>link</code></a></p>
|
||||||
<p><a href="http://example.com"><img alt="MD Logo" src="http://parsedown.org/md.png" /></a></p>
|
<p><a href="http://example.com"><img src="http://parsedown.org/md.png" alt="MD Logo" /></a></p>
|
||||||
<p><a href="http://example.com"><img alt="MD Logo" src="http://parsedown.org/md.png" /> and text</a></p>
|
<p><a href="http://example.com"><img src="http://parsedown.org/md.png" alt="MD Logo" /> and text</a></p>
|
@ -1,4 +1,4 @@
|
|||||||
[link](http://example.com) and [another link](/tests/)
|
[link](http://example.com) and [another link](/url-with-(parentheses))
|
||||||
|
|
||||||
[`link`](http://example.com)
|
[`link`](http://example.com)
|
||||||
|
|
||||||
|
@ -1 +1,4 @@
|
|||||||
<p><a href="http://example.com" title="Title">single quotes</a> and <a href="http://example.com" title="Title">double quotes</a></p>
|
<p><a href="http://example.com" title="Title">single quotes</a></p>
|
||||||
|
<p><a href="http://example.com" title="Title">double quotes</a></p>
|
||||||
|
<p><a href="http://example.com" title="2 Words">space</a></p>
|
||||||
|
<p><a href="http://example.com/url-(parentheses)" title="Title">parentheses</a></p>
|
@ -1 +1,7 @@
|
|||||||
[single quotes](http://example.com 'Title') and [double quotes](http://example.com "Title")
|
[single quotes](http://example.com 'Title')
|
||||||
|
|
||||||
|
[double quotes](http://example.com "Title")
|
||||||
|
|
||||||
|
[space](http://example.com "2 Words")
|
||||||
|
|
||||||
|
[parentheses](http://example.com/url-(parentheses) "Title")
|
8
test/data/sparse_html.html
Normal file
8
test/data/sparse_html.html
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<div>
|
||||||
|
line 1
|
||||||
|
|
||||||
|
line 2
|
||||||
|
line 3
|
||||||
|
|
||||||
|
line 4
|
||||||
|
</div>
|
8
test/data/sparse_html.md
Normal file
8
test/data/sparse_html.md
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<div>
|
||||||
|
line 1
|
||||||
|
|
||||||
|
line 2
|
||||||
|
line 3
|
||||||
|
|
||||||
|
line 4
|
||||||
|
</div>
|
@ -1,6 +1,6 @@
|
|||||||
<p>AT&T has an ampersand in their name</p>
|
<p>AT&T has an ampersand in their name</p>
|
||||||
<p>this & that</p>
|
<p>this & that</p>
|
||||||
<p>4 < 5 and 6 > 5</p>
|
<p>4 < 5 and 6 > 5</p>
|
||||||
<p><a href="http://example.com/autolink?a=1&b=2">http://example.com/autolink?a=1&b=2</a></p>
|
<p><a href="http://example.com/autolink?a=1&b=2">http://example.com/autolink?a=1&b=2</a></p>
|
||||||
<p><a href="/script?a=1&b=2">inline link</a></p>
|
<p><a href="/script?a=1&b=2">inline link</a></p>
|
||||||
<p><a href="http://example.com/?a=1&b=2">reference link</a></p>
|
<p><a href="http://example.com/?a=1&b=2">reference link</a></p>
|
@ -11,8 +11,8 @@
|
|||||||
<td><del>cell</del> 1.2</td>
|
<td><del>cell</del> 1.2</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><code>cell</code> 2.1</td>
|
<td><code>|</code> 2.1</td>
|
||||||
<td>cell 2.2</td>
|
<td>| 2.2</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
@ -1,4 +1,4 @@
|
|||||||
| _header_ 1 | header 2 |
|
| _header_ 1 | header 2 |
|
||||||
| ------------ | ------------ |
|
| ------------ | ------------ |
|
||||||
| _cell_ 1.1 | ~~cell~~ 1.2 |
|
| _cell_ 1.1 | ~~cell~~ 1.2 |
|
||||||
| `cell` 2.1 | cell 2.2 |
|
| `|` 2.1 | \| 2.2 |
|
Reference in New Issue
Block a user