mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Compare commits
55 Commits
Author | SHA1 | Date | |
---|---|---|---|
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 |
680
Parsedown.php
680
Parsedown.php
File diff suppressed because it is too large
Load Diff
19
README.md
19
README.md
@ -1,6 +1,6 @@
|
||||
## Parsedown
|
||||
|
||||
Better [Markdown](http://en.wikipedia.org/wiki/Markdown) parser for PHP.
|
||||
Better Markdown Parser in PHP
|
||||
|
||||
[[ demo ]](http://parsedown.org/demo)
|
||||
|
||||
@ -8,11 +8,11 @@ Better [Markdown](http://en.wikipedia.org/wiki/Markdown) parser for PHP.
|
||||
|
||||
* [Fast](http://parsedown.org/speed)
|
||||
* [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/)
|
||||
* Extensible
|
||||
* [Markdown Extra extension](https://github.com/erusev/parsedown-extra) <sup>new</sup>
|
||||
* [JavaScript port](https://github.com/hkdobrev/parsedown.js) under development <sup>new</sup>
|
||||
* [Markdown Extra extension](https://github.com/erusev/parsedown-extra)
|
||||
* [JavaScript port](https://github.com/hkdobrev/parsedown.js) under development
|
||||
|
||||
### Installation
|
||||
|
||||
@ -31,13 +31,16 @@ More examples in [the wiki](https://github.com/erusev/parsedown/wiki/Usage) and
|
||||
### Questions
|
||||
|
||||
**How does Parsedown work?**<br/>
|
||||
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.
|
||||
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).
|
||||
|
||||
**Why doesn’t Parsedown use namespaces?**<br/>
|
||||
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.
|
||||
It'd mean no support for PHP 5.2. Would it be worth it?
|
||||
|
||||
**Is Parsedown compliant with CommonMark?**<br/>
|
||||
We are [working on it](https://github.com/erusev/parsedown/tree/commonmark).
|
||||
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?**<br/>
|
||||
[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).
|
||||
[phpDocumentor](http://www.phpdoc.org/), [October CMS](http://octobercms.com/), [Bolt CMS](http://bolt.cm/), [Kirby CMS](http://getkirby.com/), [RaspberryPi.org](http://www.raspberrypi.org/) and [more](https://www.versioneye.com/php/erusev:parsedown/references).
|
||||
|
||||
**How can I help?**<br/>
|
||||
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;
|
||||
}
|
||||
</style>
|
||||
|
||||
comment
|
||||
|
||||
<!-- html comment -->
|
||||
MARKDOWN_WITH_MARKUP;
|
||||
|
||||
$expectedHtml = <<<EXPECTED_HTML
|
||||
<p><div><em>content</em></div></p>
|
||||
<p><div><em>content</em></div></p>
|
||||
<p>sparse:</p>
|
||||
<p><div>
|
||||
<div class="inner">
|
||||
<p><div>
|
||||
<div class="inner">
|
||||
<em>content</em>
|
||||
</div>
|
||||
</div></p>
|
||||
</div>
|
||||
</div></p>
|
||||
<p>paragraph</p>
|
||||
<p><style type="text/css"></p>
|
||||
<pre><code>p {
|
||||
color: red;
|
||||
}</code></pre>
|
||||
<p></style></p>
|
||||
<p><style type="text/css">
|
||||
p {
|
||||
color: red;
|
||||
}
|
||||
</style></p>
|
||||
<p>comment</p>
|
||||
<p><!-- html comment --></p>
|
||||
EXPECTED_HTML;
|
||||
$parsedownWithNoMarkup = new Parsedown();
|
||||
$parsedownWithNoMarkup->setMarkupEscaped(true);
|
||||
|
@ -4,6 +4,6 @@
|
||||
<h4>h4</h4>
|
||||
<h5>h5</h5>
|
||||
<h6>h6</h6>
|
||||
<h6>h6</h6>
|
||||
<p>####### not a heading</p>
|
||||
<h1>closed h1</h1>
|
||||
<p>#</p>
|
@ -10,7 +10,7 @@
|
||||
|
||||
###### h6
|
||||
|
||||
####### h6
|
||||
####### not a heading
|
||||
|
||||
# closed h1 #
|
||||
|
||||
|
@ -1,4 +1,6 @@
|
||||
<p>escaped *emphasis*.</p>
|
||||
<p><code>escaped \*emphasis\* in a code span</code></p>
|
||||
<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>
|
@ -4,4 +4,8 @@ escaped \*emphasis\*.
|
||||
|
||||
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]
|
||||
|
||||
[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"><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 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" /></a></p>
|
||||
<p><a href="http://example.com"><img src="http://parsedown.org/md.png" alt="MD Logo" /> and text</a></p>
|
@ -1,12 +1,12 @@
|
||||
<hr>
|
||||
<hr />
|
||||
<p>paragraph</p>
|
||||
<hr/>
|
||||
<hr />
|
||||
<p>paragraph</p>
|
||||
<hr />
|
||||
<p>paragraph</p>
|
||||
<hr class="foo" id="bar" />
|
||||
<p>paragraph</p>
|
||||
<hr class="foo" id="bar"/>
|
||||
<hr class="foo" id="bar" />
|
||||
<p>paragraph</p>
|
||||
<hr class="foo" id="bar" >
|
||||
<hr class="foo" id="bar" />
|
||||
<p>paragraph</p>
|
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>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="/script?a=1&b=2">inline link</a></p>
|
||||
<p><a href="http://example.com/?a=1&b=2">reference link</a></p>
|
Reference in New Issue
Block a user