mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Compare commits
25 Commits
Author | SHA1 | Date | |
---|---|---|---|
85ad014f74 | |||
22336a1bcc | |||
f713e380ee | |||
5b01915a63 | |||
18d112a614 | |||
1b9641ad03 | |||
8baf537c12 | |||
05823567bc | |||
b7029ab176 | |||
102a947c7a | |||
7bb70186c1 | |||
3225c66863 | |||
d6dc5ba25b | |||
f5451a9eff | |||
849a89b121 | |||
28064a63b3 | |||
800aac5b56 | |||
b15d40e8a3 | |||
ddc5b7e2dd | |||
5a563008aa | |||
b6f795962f | |||
cdb2646063 | |||
e3b8026e39 | |||
d96f668c42 | |||
96bf75bd91 |
@ -5,7 +5,3 @@ php:
|
|||||||
- 5.4
|
- 5.4
|
||||||
- 5.3
|
- 5.3
|
||||||
- 5.2
|
- 5.2
|
||||||
|
|
||||||
matrix:
|
|
||||||
allow_failures:
|
|
||||||
- php: 5.2
|
|
123
Parsedown.php
123
Parsedown.php
@ -110,7 +110,6 @@ class Parsedown
|
|||||||
|
|
||||||
foreach ($lines as $line)
|
foreach ($lines as $line)
|
||||||
{
|
{
|
||||||
#
|
|
||||||
# fenced elements
|
# fenced elements
|
||||||
|
|
||||||
switch ($element['type'])
|
switch ($element['type'])
|
||||||
@ -168,7 +167,6 @@ class Parsedown
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
#
|
|
||||||
# composite elements
|
# composite elements
|
||||||
|
|
||||||
switch ($element['type'])
|
switch ($element['type'])
|
||||||
@ -223,6 +221,8 @@ class Parsedown
|
|||||||
|
|
||||||
$element['lines'] []= $line;
|
$element['lines'] []= $line;
|
||||||
|
|
||||||
|
unset($element['interrupted']);
|
||||||
|
|
||||||
continue 2;
|
continue 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -238,20 +238,10 @@ class Parsedown
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
# ~
|
# indentation sensitive types
|
||||||
|
|
||||||
if ($line[0] >= 'a' and $line[0] !== '~' or $line[0] >= 'A' and $line[0] <= 'Z')
|
|
||||||
{
|
|
||||||
goto paragraph;
|
|
||||||
}
|
|
||||||
|
|
||||||
# ~
|
|
||||||
|
|
||||||
$deindented_line = $line;
|
$deindented_line = $line;
|
||||||
|
|
||||||
#
|
|
||||||
# indentation sensitive types
|
|
||||||
|
|
||||||
switch ($line[0])
|
switch ($line[0])
|
||||||
{
|
{
|
||||||
case ' ':
|
case ' ':
|
||||||
@ -345,7 +335,6 @@ class Parsedown
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
#
|
|
||||||
# indentation insensitive types
|
# indentation insensitive types
|
||||||
|
|
||||||
switch ($deindented_line[0])
|
switch ($deindented_line[0])
|
||||||
@ -500,9 +489,7 @@ class Parsedown
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
# ~
|
# paragraph
|
||||||
|
|
||||||
paragraph:
|
|
||||||
|
|
||||||
if ($element['type'] === 'p')
|
if ($element['type'] === 'p')
|
||||||
{
|
{
|
||||||
@ -579,11 +566,15 @@ class Parsedown
|
|||||||
case 'code_block':
|
case 'code_block':
|
||||||
case 'fenced_code_block':
|
case 'fenced_code_block':
|
||||||
|
|
||||||
$text = htmlentities($element['text'], ENT_NOQUOTES);
|
$text = htmlspecialchars($element['text'], ENT_NOQUOTES, 'UTF-8');
|
||||||
|
|
||||||
strpos($text, "\x1A\\") !== FALSE and $text = strtr($text, $this->escape_sequence_map);
|
strpos($text, "\x1A\\") !== FALSE and $text = strtr($text, $this->escape_sequence_map);
|
||||||
|
|
||||||
$markup .= '<pre><code>'.$text.'</code></pre>'."\n";
|
$markup .= isset($element['language'])
|
||||||
|
? '<pre><code class="language-'.$element['language'].'">'.$text.'</code></pre>'
|
||||||
|
: '<pre><code>'.$text.'</code></pre>';
|
||||||
|
|
||||||
|
$markup .= "\n";
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -638,38 +629,7 @@ class Parsedown
|
|||||||
|
|
||||||
$index = 0;
|
$index = 0;
|
||||||
|
|
||||||
# code span
|
# inline link or image (recursive)
|
||||||
|
|
||||||
if (strpos($text, '`') !== FALSE and preg_match_all('/`(.+?)`/', $text, $matches, PREG_SET_ORDER))
|
|
||||||
{
|
|
||||||
foreach ($matches as $matches)
|
|
||||||
{
|
|
||||||
$element_text = $matches[1];
|
|
||||||
$element_text = htmlentities($element_text, ENT_NOQUOTES);
|
|
||||||
|
|
||||||
# decodes escape sequences
|
|
||||||
|
|
||||||
$this->escape_sequence_map
|
|
||||||
and strpos($element_text, "\x1A") !== FALSE
|
|
||||||
and $element_text = strtr($element_text, $this->escape_sequence_map);
|
|
||||||
|
|
||||||
# composes element
|
|
||||||
|
|
||||||
$element = '<code>'.$element_text.'</code>';
|
|
||||||
|
|
||||||
# encodes element
|
|
||||||
|
|
||||||
$code = "\x1A".'$'.$index;
|
|
||||||
|
|
||||||
$text = str_replace($matches[0], $code, $text);
|
|
||||||
|
|
||||||
$map[$code] = $element;
|
|
||||||
|
|
||||||
$index ++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# inline link or image
|
|
||||||
|
|
||||||
if (strpos($text, '](') !== FALSE and preg_match_all('/(!?)(\[((?:[^\[\]]|(?2))*)\])\((.*?)\)/', $text, $matches, PREG_SET_ORDER)) # inline
|
if (strpos($text, '](') !== FALSE and preg_match_all('/(!?)(\[((?:[^\[\]]|(?2))*)\])\((.*?)\)/', $text, $matches, PREG_SET_ORDER)) # inline
|
||||||
{
|
{
|
||||||
@ -702,7 +662,7 @@ class Parsedown
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# reference link or image
|
# reference link or image (recursive)
|
||||||
|
|
||||||
if ($this->reference_map and strpos($text, '[') !== FALSE and preg_match_all('/(!?)\[(.+?)\](?:\n?[ ]?\[(.*?)\])?/ms', $text, $matches, PREG_SET_ORDER))
|
if ($this->reference_map and strpos($text, '[') !== FALSE and preg_match_all('/(!?)\[(.+?)\](?:\n?[ ]?\[(.*?)\])?/ms', $text, $matches, PREG_SET_ORDER))
|
||||||
{
|
{
|
||||||
@ -744,10 +704,46 @@ class Parsedown
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# code span
|
||||||
|
|
||||||
|
if (strpos($text, '`') !== FALSE and preg_match_all('/`(.+?)`/', $text, $matches, PREG_SET_ORDER))
|
||||||
|
{
|
||||||
|
foreach ($matches as $matches)
|
||||||
|
{
|
||||||
|
$element_text = $matches[1];
|
||||||
|
$element_text = htmlspecialchars($element_text, ENT_NOQUOTES, 'UTF-8');
|
||||||
|
|
||||||
|
# decodes escape sequences
|
||||||
|
|
||||||
|
$this->escape_sequence_map
|
||||||
|
and strpos($element_text, "\x1A") !== FALSE
|
||||||
|
and $element_text = strtr($element_text, $this->escape_sequence_map);
|
||||||
|
|
||||||
|
# composes element
|
||||||
|
|
||||||
|
$element = '<code>'.$element_text.'</code>';
|
||||||
|
|
||||||
|
# encodes element
|
||||||
|
|
||||||
|
$code = "\x1A".'$'.$index;
|
||||||
|
|
||||||
|
$text = str_replace($matches[0], $code, $text);
|
||||||
|
|
||||||
|
$map[$code] = $element;
|
||||||
|
|
||||||
|
$index ++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
# automatic link
|
# automatic link
|
||||||
|
|
||||||
if (strpos($text, '<') !== FALSE and preg_match_all('/<((https?|ftp|dict):[^\^\s]+?)>/i', $text, $matches, PREG_SET_ORDER))
|
if (strpos($text, '://') !== FALSE)
|
||||||
{
|
{
|
||||||
|
switch (TRUE)
|
||||||
|
{
|
||||||
|
case preg_match_all('{<(https?:[/]{2}[^\s]+)>}i', $text, $matches, PREG_SET_ORDER):
|
||||||
|
case preg_match_all('{\b(https?:[/]{2}[^\s]+)\b}i', $text, $matches, PREG_SET_ORDER):
|
||||||
|
|
||||||
foreach ($matches as $matches)
|
foreach ($matches as $matches)
|
||||||
{
|
{
|
||||||
$url = $matches[1];
|
$url = $matches[1];
|
||||||
@ -768,6 +764,9 @@ class Parsedown
|
|||||||
|
|
||||||
$index ++;
|
$index ++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# ~
|
# ~
|
||||||
@ -777,16 +776,26 @@ class Parsedown
|
|||||||
|
|
||||||
# ~
|
# ~
|
||||||
|
|
||||||
|
if (strpos($text, '~~') !== FALSE)
|
||||||
|
{
|
||||||
|
$text = preg_replace('/~~(?=\S)(.+?)(?<=\S)~~/s', '<del>$1</del>', $text);
|
||||||
|
}
|
||||||
|
|
||||||
if (strpos($text, '_') !== FALSE)
|
if (strpos($text, '_') !== FALSE)
|
||||||
{
|
{
|
||||||
$text = preg_replace('/__(?=\S)(.+?)(?<=\S)__(?!_)/s', '<strong>$1</strong>', $text);
|
$text = preg_replace('/__(?=\S)([^_]+?)(?<=\S)__/s', '<strong>$1</strong>', $text, -1, $count);
|
||||||
$text = preg_replace('/_(?=\S)(.+?)(?<=\S)_/s', '<em>$1</em>', $text);
|
$count or $text = preg_replace('/__(?=\S)(.+?)(?<=\S)__(?!_)/s', '<strong>$1</strong>', $text);
|
||||||
|
|
||||||
|
$text = preg_replace('/\b_(?=\S)(.+?)(?<=\S)_\b/s', '<em>$1</em>', $text);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strpos($text, '*') !== FALSE)
|
if (strpos($text, '*') !== FALSE)
|
||||||
{
|
{
|
||||||
$text = preg_replace('/\*\*(?=\S)(.+?)(?<=\S)\*\*(?!\*)/s', '<strong>$1</strong>', $text);
|
$text = preg_replace('/\*\*(?=\S)([^*]+?)(?<=\S)\*\*/s', '<strong>$1</strong>', $text, -1, $count);
|
||||||
$text = preg_replace('/\*(?=\S)(.+?)(?<=\S)\*/s', '<em>$1</em>', $text);
|
$count or $text = preg_replace('/\*\*(?=\S)(.+?)(?<=\S)\*\*(?!\*)/s', '<strong>$1</strong>', $text);
|
||||||
|
|
||||||
|
$text = preg_replace('/\*(?=\S)([^*]+?)(?<=\S)\*/s', '<em>$1</em>', $text, -1, $count);
|
||||||
|
$count or $text = preg_replace('/\*(?=\S)(.+?)(?<=\S)\*(?!\*)/s', '<em>$1</em>', $text);
|
||||||
}
|
}
|
||||||
|
|
||||||
$text = strtr($text, $map);
|
$text = strtr($text, $map);
|
||||||
|
16
README.md
16
README.md
@ -1,8 +1,20 @@
|
|||||||
## Parsedown
|
## Parsedown
|
||||||
|
|
||||||
Parsedown is a Markdown parser for PHP. It is fast, consistent and easy to use.
|
Better [Markdown](http://daringfireball.net/projects/markdown/) parser for PHP.
|
||||||
|
|
||||||
[Home](http://parsedown.org) · [Demo](http://parsedown.org/explorer/) · [Tests](http://parsedown.org/tests/)
|
***
|
||||||
|
|
||||||
|
[demo](http://parsedown.org/demo) · [tests](http://parsedown.org/tests/)
|
||||||
|
|
||||||
|
***
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* [fast](http://parsedown.org/speed)
|
||||||
|
* [consistent](http://parsedown.org/consistency)
|
||||||
|
* [GitHub Flavored](https://help.github.com/articles/github-flavored-markdown)
|
||||||
|
* [tested](https://travis-ci.org/erusev/parsedown) in PHP 5.2, 5.3, 5.4 and 5.5
|
||||||
|
* friendly to international input
|
||||||
|
|
||||||
### Installation
|
### Installation
|
||||||
|
|
||||||
|
@ -20,20 +20,29 @@ class Test extends PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$provider = array();
|
$provider = array();
|
||||||
|
|
||||||
$DirectoryIterator = new DirectoryIterator(__DIR__ . '/' . self::provider_dir);
|
$path = dirname(__FILE__).'/';
|
||||||
|
|
||||||
|
$DirectoryIterator = new DirectoryIterator($path . '/' . self::provider_dir);
|
||||||
|
|
||||||
foreach ($DirectoryIterator as $Item)
|
foreach ($DirectoryIterator as $Item)
|
||||||
{
|
{
|
||||||
if ($Item->isFile() and $Item->getExtension() === 'md')
|
if ($Item->isFile())
|
||||||
{
|
{
|
||||||
|
$filename = $Item->getFilename();
|
||||||
|
|
||||||
|
$extension = pathinfo($filename, PATHINFO_EXTENSION);
|
||||||
|
|
||||||
|
if ($extension !== 'md')
|
||||||
|
continue;
|
||||||
|
|
||||||
$basename = $Item->getBasename('.md');
|
$basename = $Item->getBasename('.md');
|
||||||
|
|
||||||
$markdown = file_get_contents(__DIR__ . '/' . self::provider_dir . $basename . '.md');
|
$markdown = file_get_contents($path . '/' . self::provider_dir . $basename . '.md');
|
||||||
|
|
||||||
if (!$markdown)
|
if (!$markdown)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
$expected_markup = file_get_contents(__DIR__ . '/' . self::provider_dir . $basename . '.html');
|
$expected_markup = file_get_contents($path . '/' . self::provider_dir . $basename . '.html');
|
||||||
$expected_markup = str_replace("\r\n", "\n", $expected_markup);
|
$expected_markup = str_replace("\r\n", "\n", $expected_markup);
|
||||||
$expected_markup = str_replace("\r", "\n", $expected_markup);
|
$expected_markup = str_replace("\r", "\n", $expected_markup);
|
||||||
|
|
||||||
@ -44,4 +53,3 @@ class Test extends PHPUnit_Framework_TestCase
|
|||||||
return $provider;
|
return $provider;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
<p><strong><em>em strong</em></strong></p>
|
<p><strong><em>em strong</em> strong</strong></p>
|
||||||
<p><strong><em>one</em> at the start</strong></p>
|
<p><strong>strong <em>em strong</em></strong></p>
|
||||||
<p><strong>one at the <em>end</em></strong></p>
|
<p><strong>strong <em>em strong</em> strong</strong></p>
|
||||||
<p><strong>one <em>in the</em> middle</strong></p>
|
<p><strong>strong <em>em strong</em></strong></p>
|
||||||
<p><strong>one with <em>asterisks</em></strong></p>
|
|
@ -1,9 +1,7 @@
|
|||||||
___em strong___
|
___em strong_ strong__
|
||||||
|
|
||||||
___one_ at the start__
|
__strong _em strong___
|
||||||
|
|
||||||
__one at the _end___
|
__strong _em strong_ strong__
|
||||||
|
|
||||||
__one _in the_ middle__
|
**strong *em strong***
|
||||||
|
|
||||||
**one with *asterisks***
|
|
@ -2,4 +2,5 @@
|
|||||||
<p><em>multiline
|
<p><em>multiline
|
||||||
emphasis</em></p>
|
emphasis</em></p>
|
||||||
<p>_ this _ is not an emphasis, neither is _ this_, _this _, or _this*</p>
|
<p>_ this _ is not an emphasis, neither is _ this_, _this _, or _this*</p>
|
||||||
|
<p>this_is_not_an_emphasis</p>
|
||||||
<p>an empty emphasis __ ** is not an emphasis</p>
|
<p>an empty emphasis __ ** is not an emphasis</p>
|
@ -5,4 +5,6 @@ emphasis_
|
|||||||
|
|
||||||
_ this _ is not an emphasis, neither is _ this_, _this _, or _this*
|
_ this _ is not an emphasis, neither is _ this_, _this _, or _this*
|
||||||
|
|
||||||
|
this_is_not_an_emphasis
|
||||||
|
|
||||||
an empty emphasis __ ** is not an emphasis
|
an empty emphasis __ ** is not an emphasis
|
@ -1,2 +1,3 @@
|
|||||||
<p><a href="http://example.com">link</a></p>
|
<p><a href="http://example.com">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"></a></p>
|
@ -1,3 +1,5 @@
|
|||||||
[link](http://example.com)
|
[link](http://example.com)
|
||||||
|
|
||||||
|
[`link`](http://example.com)
|
||||||
|
|
||||||
[](http://example.com)
|
[](http://example.com)
|
7
tests/data/multiline_list_paragraph.html
Normal file
7
tests/data/multiline_list_paragraph.html
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
<p>li</p>
|
||||||
|
<p>line
|
||||||
|
line</p>
|
||||||
|
</li>
|
||||||
|
</ul>
|
4
tests/data/multiline_list_paragraph.md
Normal file
4
tests/data/multiline_list_paragraph.md
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
- li
|
||||||
|
|
||||||
|
line
|
||||||
|
line
|
3
tests/data/strikethrough.html
Normal file
3
tests/data/strikethrough.html
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<p><del>strikethrough</del></p>
|
||||||
|
<p>in the <del>middle</del> of a sentence</p>
|
||||||
|
<p>in the middle of a w<del>or</del>d</p>
|
5
tests/data/strikethrough.md
Normal file
5
tests/data/strikethrough.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
~~strikethrough~~
|
||||||
|
|
||||||
|
in the ~~middle~~ of a sentence
|
||||||
|
|
||||||
|
in the middle of a w~~or~~d
|
6
tests/data/strong_em.html
Normal file
6
tests/data/strong_em.html
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<p><em><strong>strong em</strong></em> </p>
|
||||||
|
<p><em>em <strong>strong em</strong></em></p>
|
||||||
|
<p><em><strong>strong em</strong> em</em></p>
|
||||||
|
<p><em><strong>strong em</strong></em></p>
|
||||||
|
<p><em>em <strong>strong em</strong></em></p>
|
||||||
|
<p><em><strong>strong em</strong> em</em></p>
|
11
tests/data/strong_em.md
Normal file
11
tests/data/strong_em.md
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
***strong em***
|
||||||
|
|
||||||
|
*em **strong em***
|
||||||
|
|
||||||
|
***strong em** em*
|
||||||
|
|
||||||
|
___strong em___
|
||||||
|
|
||||||
|
_em __strong em___
|
||||||
|
|
||||||
|
___strong em__ em_
|
@ -4,3 +4,4 @@
|
|||||||
<p><a href="http://example.com">multiline
|
<p><a href="http://example.com">multiline
|
||||||
one</a> defined on 2 lines</p>
|
one</a> defined on 2 lines</p>
|
||||||
<p><a href="http://example.com">one</a> with an upper case label</p>
|
<p><a href="http://example.com">one</a> with an upper case label</p>
|
||||||
|
<p><a href="http://example.com"><code>link</code></a></p>
|
@ -14,3 +14,5 @@ one][website] defined on 2 lines
|
|||||||
[one][label] with an upper case label
|
[one][label] with an upper case label
|
||||||
|
|
||||||
[LABEL]: http://example.com
|
[LABEL]: http://example.com
|
||||||
|
|
||||||
|
[`link`][website]
|
1
tests/data/url_autolinking.html
Normal file
1
tests/data/url_autolinking.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
<p>Here's an autolink <a href="http://example.com">http://example.com</a>.</p>
|
1
tests/data/url_autolinking.md
Normal file
1
tests/data/url_autolinking.md
Normal file
@ -0,0 +1 @@
|
|||||||
|
Here's an autolink http://example.com.
|
Reference in New Issue
Block a user