mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
6d113f47fb | |||
d4d3612710 |
178
Parsedown.php
178
Parsedown.php
@ -217,56 +217,14 @@ class Parsedown
|
|||||||
|
|
||||||
# Quick Paragraph
|
# Quick Paragraph
|
||||||
|
|
||||||
if ($line[0] >= 'A' and $line['0'] !== '_')
|
if ($line[0] >= 'A' and $line[0] !== '_')
|
||||||
{
|
{
|
||||||
goto paragraph; # trust me
|
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
|
# Code
|
||||||
|
|
||||||
if (preg_match('/^[ ]{4}(.*)/', $line, $matches))
|
if ($line[0] === ' ' and preg_match('/^[ ]{4}(.*)/', $line, $matches))
|
||||||
{
|
{
|
||||||
if ($element['type'] === 'code')
|
if ($element['type'] === 'code')
|
||||||
{
|
{
|
||||||
@ -279,10 +237,20 @@ class Parsedown
|
|||||||
$elements []= $element;
|
$elements []= $element;
|
||||||
|
|
||||||
$element = array(
|
$element = array(
|
||||||
'type' => 'code',
|
'type' => 'code',
|
||||||
'text' => $matches[1],
|
'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;
|
continue;
|
||||||
}
|
}
|
||||||
@ -296,17 +264,31 @@ class Parsedown
|
|||||||
$level = strlen($matches[1]);
|
$level = strlen($matches[1]);
|
||||||
|
|
||||||
$element = array(
|
$element = array(
|
||||||
'type' => 'h.',
|
'type' => 'h.',
|
||||||
'text' => $matches[2],
|
'text' => $matches[2],
|
||||||
'level' => $level,
|
'level' => $level,
|
||||||
);
|
);
|
||||||
|
|
||||||
continue;
|
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);
|
||||||
|
|
||||||
# Blockquote
|
# Blockquote
|
||||||
|
|
||||||
if (preg_match('/^[ ]*>[ ]?(.*)/', $line, $matches))
|
if ($pure_line[0] === '>' and preg_match('/^>[ ]?(.*)/', $pure_line, $matches))
|
||||||
{
|
{
|
||||||
if ($element['type'] === 'blockquote')
|
if ($element['type'] === 'blockquote')
|
||||||
{
|
{
|
||||||
@ -334,45 +316,72 @@ class Parsedown
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Setext Header (===)
|
# HTML
|
||||||
|
|
||||||
if ($element['type'] === 'p' and ! isset($element['interrupted']) and preg_match('/^[=]+[ ]*$/', $line))
|
if ($pure_line[0] === '<')
|
||||||
{
|
{
|
||||||
$element['type'] = 'h.';
|
# Block-Level HTML <self-closing/>
|
||||||
$element['level'] = 1;
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
# 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (preg_match('{^<.+?/>$}', $line))
|
# Horizontal Rule
|
||||||
|
|
||||||
|
if (preg_match('/^([-*_])([ ]{0,2}\1){2,}[ ]*$/', $pure_line))
|
||||||
{
|
{
|
||||||
$elements []= $element;
|
$elements []= $element;
|
||||||
|
|
||||||
$element = array(
|
$element = array(
|
||||||
'type' => '',
|
'type' => 'hr',
|
||||||
'text' => $line,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Block-Level HTML <open>
|
# List Item
|
||||||
|
|
||||||
if (preg_match('{^<(\w+)(?:[ ].*?)?>}', $line, $matches))
|
if (preg_match('/^([ ]*)(\d+[.]|[*+-])[ ](.*)/', $line, $matches))
|
||||||
{
|
{
|
||||||
$elements []= $element;
|
$elements []= $element;
|
||||||
|
|
||||||
$element = array(
|
$element = array(
|
||||||
'type' => 'block',
|
'type' => 'li',
|
||||||
'subtype' => strtolower($matches[1]),
|
'ordered' => isset($matches[2][1]),
|
||||||
'text' => $line,
|
'indentation' => $matches[1],
|
||||||
'depth' => 0,
|
'last' => true,
|
||||||
|
'lines' => array(
|
||||||
|
preg_replace('/^[ ]{0,4}/', '', $matches[3]),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
preg_match('{</'.$matches[1].'>\s*$}', $line) and $element['closed'] = true;
|
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -550,15 +559,17 @@ class Parsedown
|
|||||||
{
|
{
|
||||||
foreach ($matches as $matches)
|
foreach ($matches as $matches)
|
||||||
{
|
{
|
||||||
|
$url = $this->escape_special_characters($matches[4]);
|
||||||
|
|
||||||
if ($matches[1]) # image
|
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_text = $this->parse_inline_elements($matches[3]);
|
||||||
|
|
||||||
$element = '<a href="'.$matches[4].'">'.$element_text.'</a>';
|
$element = '<a href="'.$url.'">'.$element_text.'</a>';
|
||||||
}
|
}
|
||||||
|
|
||||||
# ~
|
# ~
|
||||||
@ -588,6 +599,7 @@ class Parsedown
|
|||||||
if (isset($this->reference_map[$link_definition]))
|
if (isset($this->reference_map[$link_definition]))
|
||||||
{
|
{
|
||||||
$url = $this->reference_map[$link_definition];
|
$url = $this->reference_map[$link_definition];
|
||||||
|
$url = $this->escape_special_characters($url);
|
||||||
|
|
||||||
if ($matches[1]) # image
|
if ($matches[1]) # image
|
||||||
{
|
{
|
||||||
@ -613,13 +625,17 @@ class Parsedown
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Automatic Links
|
||||||
|
|
||||||
if (strpos($text, '<') !== FALSE and preg_match_all('/<((https?|ftp|dict):[^\^\s]+?)>/i', $text, $matches, PREG_SET_ORDER))
|
if (strpos($text, '<') !== FALSE and preg_match_all('/<((https?|ftp|dict):[^\^\s]+?)>/i', $text, $matches, PREG_SET_ORDER))
|
||||||
{
|
{
|
||||||
foreach ($matches as $matches)
|
foreach ($matches as $matches)
|
||||||
{
|
{
|
||||||
|
$url = $this->escape_special_characters($matches[1]);
|
||||||
|
|
||||||
$element = '<a href=":href">:text</a>';
|
$element = '<a href=":href">:text</a>';
|
||||||
$element = str_replace(':text', $matches[1], $element);
|
$element = str_replace(':text', $url, $element);
|
||||||
$element = str_replace(':href', $matches[1], $element);
|
$element = str_replace(':href', $url, $element);
|
||||||
|
|
||||||
# ~
|
# ~
|
||||||
|
|
||||||
@ -633,6 +649,12 @@ class Parsedown
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# ~
|
||||||
|
|
||||||
|
$text = $this->escape_special_characters($text);
|
||||||
|
|
||||||
|
# ~
|
||||||
|
|
||||||
if (strpos($text, '_') !== FALSE)
|
if (strpos($text, '_') !== FALSE)
|
||||||
{
|
{
|
||||||
$text = preg_replace('/__(?=\S)(.+?)(?<=\S)__/', '<strong>$1</strong>', $text);
|
$text = preg_replace('/__(?=\S)(.+?)(?<=\S)__/', '<strong>$1</strong>', $text);
|
||||||
@ -649,5 +671,13 @@ class Parsedown
|
|||||||
|
|
||||||
return $text;
|
return $text;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
private function escape_special_characters($text)
|
||||||
|
{
|
||||||
|
strpos($text, '&') !== FALSE and $text = preg_replace('/&(?!#?\w+;)/', '&', $text);
|
||||||
|
|
||||||
|
$text = str_replace('<', '<', $text);
|
||||||
|
|
||||||
|
return $text;
|
||||||
|
}
|
||||||
|
}
|
@ -1,14 +1,16 @@
|
|||||||
<p>Here's a list where items are separated by empty lines:</p>
|
<p>Here's a sparse list:</p>
|
||||||
<ul>
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
<p>list item</p>
|
<p>list item</p>
|
||||||
</li>
|
</li>
|
||||||
<li>another list item</li>
|
<li>another list item</li>
|
||||||
</ul>
|
</ul>
|
||||||
<p>Here's an ordered one:</p>
|
<p>Here's one with an indented list item:</p>
|
||||||
<ol>
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
<p>item one</p>
|
<p>li</p>
|
||||||
</li>
|
<ul>
|
||||||
<li>item two</li>
|
<li>li</li>
|
||||||
</ol>
|
</ul>
|
||||||
|
</li>
|
||||||
|
</ul>
|
@ -1,11 +1,11 @@
|
|||||||
Here's a list where items are separated by empty lines:
|
Here's a sparse list:
|
||||||
|
|
||||||
- list item
|
- list item
|
||||||
|
|
||||||
- another list item
|
- another list item
|
||||||
|
|
||||||
Here's an ordered one:
|
Here's one with an indented list item:
|
||||||
|
|
||||||
1. item one
|
- li
|
||||||
|
|
||||||
2. item two
|
- 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