mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
7249d02cff | |||
ecf86b073e | |||
b12973415f | |||
6d113f47fb | |||
d4d3612710 | |||
2e314ad474 | |||
e475602e2f | |||
f43f54b877 | |||
d733acc94e | |||
6a0695deb9 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
.DS_Store
|
||||
.idea
|
||||
nbproject
|
254
Parsedown.php
254
Parsedown.php
@ -75,22 +75,10 @@ class Parsedown
|
||||
}
|
||||
}
|
||||
|
||||
# Extracts link references.
|
||||
|
||||
if (preg_match_all('/^[ ]{0,3}\[(.+)\][ ]?:[ ]*\n?[ ]*(.+)$/m', $text, $matches, PREG_SET_ORDER))
|
||||
{
|
||||
foreach ($matches as $matches)
|
||||
{
|
||||
$this->reference_map[strtolower($matches[1])] = $matches[2];
|
||||
|
||||
$text = str_replace($matches[0], '', $text);
|
||||
}
|
||||
}
|
||||
|
||||
# ~
|
||||
|
||||
$text = preg_replace('/\n\s*\n/', "\n\n", $text);
|
||||
$text = trim($text, "\n");
|
||||
$text = trim($text, "\n ");
|
||||
|
||||
$lines = explode("\n", $text);
|
||||
|
||||
@ -122,14 +110,33 @@ class Parsedown
|
||||
|
||||
foreach ($lines as $line)
|
||||
{
|
||||
# Block-Level HTML
|
||||
|
||||
if ($element['type'] === 'block' and ! isset($element['closed']))
|
||||
{
|
||||
if (preg_match('{<'.$element['subtype'].'>$}', $line)) # <open>
|
||||
{
|
||||
$element['depth']++;
|
||||
}
|
||||
|
||||
if (preg_match('{</'.$element['subtype'].'>$}', $line)) # </close>
|
||||
{
|
||||
$element['depth'] > 0
|
||||
? $element['depth']--
|
||||
: $element['closed'] = true;
|
||||
}
|
||||
|
||||
$element['text'] .= "\n".$line;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
# Empty
|
||||
|
||||
if ($line === '')
|
||||
{
|
||||
$element['interrupted'] = true;
|
||||
|
||||
$element['type'] === 'code' and $element['text'] .= "\n";
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -198,59 +205,24 @@ class Parsedown
|
||||
|
||||
# Quick Paragraph
|
||||
|
||||
if ($line[0] >= 'A' and $line['0'] !== '_')
|
||||
if ($line[0] >= 'A' and $line[0] !== '_' and $line[0] !== '[')
|
||||
{
|
||||
goto paragraph; # trust me
|
||||
}
|
||||
|
||||
# Setext Header (---)
|
||||
|
||||
if ($element['type'] === 'p' and ! isset($element['interrupted']) and preg_match('/^[-]+[ ]*$/', $line))
|
||||
{
|
||||
$element['type'] = 'h.';
|
||||
$element['level'] = 2;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
# Horizontal Rule
|
||||
|
||||
if (preg_match('/^[ ]{0,3}([-*_])([ ]{0,2}\1){2,}[ ]*$/', $line))
|
||||
{
|
||||
$elements []= $element;
|
||||
|
||||
$element = array(
|
||||
'type' => 'hr',
|
||||
);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
# List Item
|
||||
|
||||
if (preg_match('/^([ ]{0,3})(\d+[.]|[*+-])[ ](.*)/', $line, $matches))
|
||||
{
|
||||
$elements []= $element;
|
||||
|
||||
$element = array(
|
||||
'type' => 'li',
|
||||
'ordered' => isset($matches[2][1]),
|
||||
'indentation' => $matches[1],
|
||||
'last' => true,
|
||||
'lines' => array(
|
||||
preg_replace('/^[ ]{0,4}/', '', $matches[3]),
|
||||
),
|
||||
);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
# Code
|
||||
|
||||
if (preg_match('/^[ ]{4}(.*)/', $line, $matches))
|
||||
if ($line[0] === ' ' and preg_match('/^[ ]{4}(.*)/', $line, $matches))
|
||||
{
|
||||
if ($element['type'] === 'code')
|
||||
{
|
||||
if (isset($element['interrupted']))
|
||||
{
|
||||
$element['text'] .= "\n";
|
||||
|
||||
unset ($element['interrupted']);
|
||||
}
|
||||
|
||||
$element['text'] .= "\n".$matches[1];
|
||||
}
|
||||
else
|
||||
@ -258,10 +230,20 @@ class Parsedown
|
||||
$elements []= $element;
|
||||
|
||||
$element = array(
|
||||
'type' => 'code',
|
||||
'type' => 'code',
|
||||
'text' => $matches[1],
|
||||
);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
# Setext Header (---)
|
||||
|
||||
if ($line[0] === '-' and $element['type'] === 'p' and ! isset($element['interrupted']) and preg_match('/^[-]+[ ]*$/', $line))
|
||||
{
|
||||
$element['type'] = 'h.';
|
||||
$element['level'] = 2;
|
||||
|
||||
continue;
|
||||
}
|
||||
@ -275,17 +257,43 @@ class Parsedown
|
||||
$level = strlen($matches[1]);
|
||||
|
||||
$element = array(
|
||||
'type' => 'h.',
|
||||
'text' => $matches[2],
|
||||
'type' => 'h.',
|
||||
'text' => $matches[2],
|
||||
'level' => $level,
|
||||
);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
# Setext Header (===)
|
||||
|
||||
if ($line[0] === '=' and $element['type'] === 'p' and ! isset($element['interrupted']) and preg_match('/^[=]+[ ]*$/', $line))
|
||||
{
|
||||
$element['type'] = 'h.';
|
||||
$element['level'] = 1;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
# ~
|
||||
|
||||
$pure_line = ltrim($line);
|
||||
|
||||
# Link Reference
|
||||
|
||||
if ($pure_line[0] === '[' and preg_match('/^\[(.+?)\]:[ ]*([^ ]+)/', $pure_line, $matches))
|
||||
{
|
||||
$label = $matches[1];
|
||||
$url = trim($matches[2], '<>');
|
||||
|
||||
$this->reference_map[$label] = $url;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
# Blockquote
|
||||
|
||||
if (preg_match('/^[ ]*>[ ]?(.*)/', $line, $matches))
|
||||
if ($pure_line[0] === '>' and preg_match('/^>[ ]?(.*)/', $pure_line, $matches))
|
||||
{
|
||||
if ($element['type'] === 'blockquote')
|
||||
{
|
||||
@ -313,12 +321,71 @@ class Parsedown
|
||||
continue;
|
||||
}
|
||||
|
||||
# Setext Header (===)
|
||||
# HTML
|
||||
|
||||
if ($element['type'] === 'p' and ! isset($element['interrupted']) and preg_match('/^[=]+[ ]*$/', $line))
|
||||
if ($pure_line[0] === '<')
|
||||
{
|
||||
$element['type'] = 'h.';
|
||||
$element['level'] = 1;
|
||||
# Block-Level HTML <self-closing/>
|
||||
|
||||
if (preg_match('{^<.+?/>$}', $pure_line))
|
||||
{
|
||||
$elements []= $element;
|
||||
|
||||
$element = array(
|
||||
'type' => '',
|
||||
'text' => $pure_line,
|
||||
);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
# Block-Level HTML <open>
|
||||
|
||||
if (preg_match('{^<(\w+)(?:[ ].*?)?>}', $pure_line, $matches))
|
||||
{
|
||||
$elements []= $element;
|
||||
|
||||
$element = array(
|
||||
'type' => 'block',
|
||||
'subtype' => strtolower($matches[1]),
|
||||
'text' => $pure_line,
|
||||
'depth' => 0,
|
||||
);
|
||||
|
||||
preg_match('{</'.$matches[1].'>\s*$}', $pure_line) and $element['closed'] = true;
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
# Horizontal Rule
|
||||
|
||||
if (preg_match('/^([-*_])([ ]{0,2}\1){2,}[ ]*$/', $pure_line))
|
||||
{
|
||||
$elements []= $element;
|
||||
|
||||
$element = array(
|
||||
'type' => 'hr',
|
||||
);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
# List Item
|
||||
|
||||
if (preg_match('/^([ ]*)(\d+[.]|[*+-])[ ](.*)/', $line, $matches))
|
||||
{
|
||||
$elements []= $element;
|
||||
|
||||
$element = array(
|
||||
'type' => 'li',
|
||||
'ordered' => isset($matches[2][1]),
|
||||
'indentation' => $matches[1],
|
||||
'last' => true,
|
||||
'lines' => array(
|
||||
preg_replace('/^[ ]{0,4}/', '', $matches[3]),
|
||||
),
|
||||
);
|
||||
|
||||
continue;
|
||||
}
|
||||
@ -415,9 +482,7 @@ class Parsedown
|
||||
|
||||
case 'code':
|
||||
|
||||
$text = rtrim($element['text'], "\n");
|
||||
|
||||
$text = htmlentities($text, ENT_NOQUOTES);
|
||||
$text = htmlentities($element['text'], ENT_NOQUOTES);
|
||||
|
||||
strpos($text, "\x1A\\") !== FALSE and $text = strtr($text, $this->escape_sequence_map);
|
||||
|
||||
@ -446,6 +511,10 @@ class Parsedown
|
||||
$markup .= '<hr />'."\n";
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
$markup .= $element['text']."\n";
|
||||
}
|
||||
}
|
||||
|
||||
@ -495,19 +564,19 @@ class Parsedown
|
||||
{
|
||||
foreach ($matches as $matches)
|
||||
{
|
||||
$url = $this->escape_special_characters($matches[4]);
|
||||
|
||||
if ($matches[1]) # image
|
||||
{
|
||||
$element = '<img alt="'.$matches[3].'" src="'.$matches[4].'">';
|
||||
$element = '<img alt="'.$matches[3].'" src="'.$url.'">';
|
||||
}
|
||||
else
|
||||
else
|
||||
{
|
||||
$element_text = $this->parse_inline_elements($matches[3]);
|
||||
|
||||
$element = '<a href="'.$matches[4].'">'.$element_text.'</a>';
|
||||
$element = '<a href="'.$url.'">'.$element_text.'</a>';
|
||||
}
|
||||
|
||||
$element_text = $this->parse_inline_elements($matches[1]);
|
||||
|
||||
# ~
|
||||
|
||||
$code = "\x1A".'$'.$index;
|
||||
@ -526,15 +595,16 @@ class Parsedown
|
||||
{
|
||||
foreach ($matches as $matches)
|
||||
{
|
||||
$link_difinition = isset($matches[3]) && $matches[3]
|
||||
$link_definition = isset($matches[3]) && $matches[3]
|
||||
? $matches[3]
|
||||
: $matches[2]; # implicit
|
||||
|
||||
$link_difinition = strtolower($link_difinition);
|
||||
$link_definition = strtolower($link_definition);
|
||||
|
||||
if (isset($this->reference_map[$link_difinition]))
|
||||
if (isset($this->reference_map[$link_definition]))
|
||||
{
|
||||
$url = $this->reference_map[$link_difinition];
|
||||
$url = $this->reference_map[$link_definition];
|
||||
$url = $this->escape_special_characters($url);
|
||||
|
||||
if ($matches[1]) # image
|
||||
{
|
||||
@ -560,13 +630,17 @@ class Parsedown
|
||||
}
|
||||
}
|
||||
|
||||
# Automatic Links
|
||||
|
||||
if (strpos($text, '<') !== FALSE and preg_match_all('/<((https?|ftp|dict):[^\^\s]+?)>/i', $text, $matches, PREG_SET_ORDER))
|
||||
{
|
||||
foreach ($matches as $matches)
|
||||
{
|
||||
$url = $this->escape_special_characters($matches[1]);
|
||||
|
||||
$element = '<a href=":href">:text</a>';
|
||||
$element = str_replace(':text', $matches[1], $element);
|
||||
$element = str_replace(':href', $matches[1], $element);
|
||||
$element = str_replace(':text', $url, $element);
|
||||
$element = str_replace(':href', $url, $element);
|
||||
|
||||
# ~
|
||||
|
||||
@ -580,6 +654,12 @@ class Parsedown
|
||||
}
|
||||
}
|
||||
|
||||
# ~
|
||||
|
||||
$text = $this->escape_special_characters($text);
|
||||
|
||||
# ~
|
||||
|
||||
if (strpos($text, '_') !== FALSE)
|
||||
{
|
||||
$text = preg_replace('/__(?=\S)(.+?)(?<=\S)__/', '<strong>$1</strong>', $text);
|
||||
@ -596,5 +676,13 @@ class Parsedown
|
||||
|
||||
return $text;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private function escape_special_characters($text)
|
||||
{
|
||||
strpos($text, '&') !== FALSE and $text = preg_replace('/&(?!#?\w+;)/', '&', $text);
|
||||
|
||||
$text = str_replace('<', '<', $text);
|
||||
|
||||
return $text;
|
||||
}
|
||||
}
|
@ -1,9 +1,8 @@
|
||||
<p>Here's a regular code block:</p>
|
||||
<pre><code><?php
|
||||
|
||||
echo 'Hello World!';
|
||||
|
||||
?></code></pre>
|
||||
<p>Here's one that holds a list:</p>
|
||||
<pre><code>- list item
|
||||
<p>Here's a code block:</p>
|
||||
<pre><code><?php
|
||||
|
||||
$message = 'Hello World!';
|
||||
echo $message;</code></pre>
|
||||
<p>Here's one that holds a list:</p>
|
||||
<pre><code>- list item
|
||||
- another list item</code></pre>
|
@ -1,13 +1,12 @@
|
||||
Here's a regular code block:
|
||||
|
||||
<?php
|
||||
|
||||
echo 'Hello World!';
|
||||
|
||||
?>
|
||||
|
||||
Here's one that holds a list:
|
||||
|
||||
- list item
|
||||
- another list item
|
||||
|
||||
Here's a code block:
|
||||
|
||||
<?php
|
||||
|
||||
$message = 'Hello World!';
|
||||
echo $message;
|
||||
|
||||
Here's one that holds a list:
|
||||
|
||||
- list item
|
||||
- another list item
|
||||
|
||||
|
15
tests/data/html.html
Normal file
15
tests/data/html.html
Normal file
@ -0,0 +1,15 @@
|
||||
<p>Self-closing tag:</p>
|
||||
<hr/>
|
||||
<p>Self-closing tag with attributes:</p>
|
||||
<hr style="background: #eaa" />
|
||||
<p>Bare element:</p>
|
||||
<div>content</div>
|
||||
<p>Element with attributes:</p>
|
||||
<a href="http://parsedown.org">link</a>
|
||||
<p>Nested elements:</p>
|
||||
<div>
|
||||
parent
|
||||
<div>
|
||||
child
|
||||
</div>
|
||||
</div>
|
24
tests/data/html.md
Normal file
24
tests/data/html.md
Normal file
@ -0,0 +1,24 @@
|
||||
Self-closing tag:
|
||||
|
||||
<hr/>
|
||||
|
||||
Self-closing tag with attributes:
|
||||
|
||||
<hr style="background: #eaa" />
|
||||
|
||||
Bare element:
|
||||
|
||||
<div>content</div>
|
||||
|
||||
Element with attributes:
|
||||
|
||||
<a href="http://parsedown.org">link</a>
|
||||
|
||||
Nested elements:
|
||||
|
||||
<div>
|
||||
parent
|
||||
<div>
|
||||
child
|
||||
</div>
|
||||
</div>
|
@ -1,14 +1,11 @@
|
||||
<p>Here's a <a href="http://parsedown.org">reference link</a>.</p>
|
||||
<p>Here's <a href="http://parsedown.org">one</a> with an alternative syntax.</p>
|
||||
<p>Here's <a href="http://parsedown.org">one</a> on the next line.</p>
|
||||
<p>Here's <a href="http://parsedown.org">one</a> on 2 lines.</p>
|
||||
<p>Here's <a href="http://parsedown.org/tests/">one</a> with a different URL.</p>
|
||||
<p>Here's <a href="http://parsedown.org">one</a> with a semantic name.</p>
|
||||
<p>Here's <a href="http://parsedown.org">one</a> with definition name on the next line.</p>
|
||||
<p>Here's [one][404] with no definition.</p>
|
||||
<p>Here's an image: <img alt="Markdown Logo" src="/md.png"></p>
|
||||
<p>Here's an <a href="http://google.com">implicit one</a>.</p>
|
||||
<p>Here's an <a href="http://google.com">implicit one</a>.</p>
|
||||
<p>Here's an <a href="http://google.com">implicit one</a> with an empty link definition.</p>
|
||||
<p>Here's a <a href="http://parsedown.org">multiline
|
||||
one</a> defined on 2 lines.</p>
|
@ -2,21 +2,12 @@ Here's a [reference link][1].
|
||||
|
||||
[1]: http://parsedown.org
|
||||
|
||||
Here's [one] [2] with an alternative syntax.
|
||||
Here's [one][2] on the next line.
|
||||
[2]: http://parsedown.org
|
||||
|
||||
[2] :http://parsedown.org
|
||||
Here's [one][3] with a different URL.
|
||||
|
||||
Here's [one][3] on the next line.
|
||||
[3]: http://parsedown.org
|
||||
|
||||
Here's [one][4] on 2 lines.
|
||||
|
||||
[4]:
|
||||
http://parsedown.org
|
||||
|
||||
Here's [one][5] with a different URL.
|
||||
|
||||
[5]: http://parsedown.org/tests/
|
||||
[3]: http://parsedown.org/tests/
|
||||
|
||||
Here's [one][website] with a semantic name.
|
||||
|
||||
@ -33,8 +24,6 @@ Here's an image: ![Markdown Logo][image]
|
||||
|
||||
Here's an [implicit one].
|
||||
|
||||
Here's an [implicit one].
|
||||
|
||||
[implicit one]: http://google.com
|
||||
|
||||
Here's an [implicit one][] with an empty link definition.
|
||||
|
@ -1,14 +1,16 @@
|
||||
<p>Here's a list where items are separated by empty lines:</p>
|
||||
<ul>
|
||||
<li>
|
||||
<p>list item</p>
|
||||
</li>
|
||||
<li>another list item</li>
|
||||
</ul>
|
||||
<p>Here's an ordered one:</p>
|
||||
<ol>
|
||||
<li>
|
||||
<p>item one</p>
|
||||
</li>
|
||||
<li>item two</li>
|
||||
</ol>
|
||||
<p>Here's a sparse list:</p>
|
||||
<ul>
|
||||
<li>
|
||||
<p>list item</p>
|
||||
</li>
|
||||
<li>another list item</li>
|
||||
</ul>
|
||||
<p>Here's one with an indented list item:</p>
|
||||
<ul>
|
||||
<li>
|
||||
<p>li</p>
|
||||
<ul>
|
||||
<li>li</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
@ -1,11 +1,11 @@
|
||||
Here's a list where items are separated by empty lines:
|
||||
|
||||
- list item
|
||||
|
||||
- another list item
|
||||
|
||||
Here's an ordered one:
|
||||
|
||||
1. item one
|
||||
|
||||
2. item two
|
||||
Here's a sparse list:
|
||||
|
||||
- list item
|
||||
|
||||
- another list item
|
||||
|
||||
Here's one with an indented list item:
|
||||
|
||||
- li
|
||||
|
||||
- li
|
8
tests/data/special_characters.html
Normal file
8
tests/data/special_characters.html
Normal file
@ -0,0 +1,8 @@
|
||||
<p>AT&T has an ampersand in their name.</p>
|
||||
<p>AT&T is another way to write it.</p>
|
||||
<p>This & that.</p>
|
||||
<p>4 < 5 and 6 > 5.</p>
|
||||
<p>Here's a <a href="http://example.com/?foo=1&bar=2">link</a> with an ampersand in the URL.</p>
|
||||
<p>Here's an inline <a href="/script?foo=1&bar=2">link</a>.</p>
|
||||
<hr />
|
||||
<p>Based on <a href="http://daringfireball.net/projects/downloads/MarkdownTest_1.0.zip">the original</a> test suite.</p>
|
17
tests/data/special_characters.md
Normal file
17
tests/data/special_characters.md
Normal file
@ -0,0 +1,17 @@
|
||||
AT&T has an ampersand in their name.
|
||||
|
||||
AT&T is another way to write it.
|
||||
|
||||
This & that.
|
||||
|
||||
4 < 5 and 6 > 5.
|
||||
|
||||
Here's a [link] [1] with an ampersand in the URL.
|
||||
|
||||
Here's an inline [link](/script?foo=1&bar=2).
|
||||
|
||||
[1]: http://example.com/?foo=1&bar=2
|
||||
|
||||
---
|
||||
|
||||
Based on [the original](http://daringfireball.net/projects/downloads/MarkdownTest_1.0.zip) test suite.
|
Reference in New Issue
Block a user