mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
f0fbdaa6ca | |||
e20c0a29bd | |||
712dd23d30 | |||
68f2871996 | |||
17e7e33847 | |||
7cb9646d98 | |||
325bdd9ff6 | |||
2a0700abda | |||
4e83d79d76 | |||
354842fd6e | |||
2b73e94c6c | |||
0182812d6c | |||
f5dd3455f9 |
161
Parsedown.php
161
Parsedown.php
@ -37,11 +37,11 @@ class Parsedown
|
|||||||
# Setters
|
# Setters
|
||||||
#
|
#
|
||||||
|
|
||||||
private $break_marker = " \n";
|
private $breaks_enabled = false;
|
||||||
|
|
||||||
function set_breaks_enabled($breaks_enabled)
|
function set_breaks_enabled($breaks_enabled)
|
||||||
{
|
{
|
||||||
$this->break_marker = $breaks_enabled ? "\n" : " \n";
|
$this->breaks_enabled = $breaks_enabled;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -51,7 +51,6 @@ class Parsedown
|
|||||||
#
|
#
|
||||||
|
|
||||||
private $reference_map = array();
|
private $reference_map = array();
|
||||||
private $escape_sequence_map = array();
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Public Methods
|
# Public Methods
|
||||||
@ -66,25 +65,6 @@ class Parsedown
|
|||||||
# replaces tabs with spaces
|
# replaces tabs with spaces
|
||||||
$text = str_replace("\t", ' ', $text);
|
$text = str_replace("\t", ' ', $text);
|
||||||
|
|
||||||
# encodes escape sequences
|
|
||||||
|
|
||||||
if (strpos($text, '\\') !== FALSE)
|
|
||||||
{
|
|
||||||
$escape_sequences = array('\\\\', '\`', '\*', '\_', '\{', '\}', '\[', '\]', '\(', '\)', '\>', '\#', '\+', '\-', '\.', '\!');
|
|
||||||
|
|
||||||
foreach ($escape_sequences as $index => $escape_sequence)
|
|
||||||
{
|
|
||||||
if (strpos($text, $escape_sequence) !== FALSE)
|
|
||||||
{
|
|
||||||
$code = "\x1A".'\\'.$index.';';
|
|
||||||
|
|
||||||
$text = str_replace($escape_sequence, $code, $text);
|
|
||||||
|
|
||||||
$this->escape_sequence_map[$code] = $escape_sequence;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# ~
|
# ~
|
||||||
|
|
||||||
$text = trim($text, "\n");
|
$text = trim($text, "\n");
|
||||||
@ -93,15 +73,6 @@ class Parsedown
|
|||||||
|
|
||||||
$text = $this->parse_block_elements($lines);
|
$text = $this->parse_block_elements($lines);
|
||||||
|
|
||||||
# decodes escape sequences
|
|
||||||
|
|
||||||
foreach ($this->escape_sequence_map as $code => $escape_sequence)
|
|
||||||
{
|
|
||||||
$text = str_replace($code, $escape_sequence[1], $text);
|
|
||||||
}
|
|
||||||
|
|
||||||
# ~
|
|
||||||
|
|
||||||
$text = rtrim($text, "\n");
|
$text = rtrim($text, "\n");
|
||||||
|
|
||||||
return $text;
|
return $text;
|
||||||
@ -111,8 +82,6 @@ class Parsedown
|
|||||||
# Private Methods
|
# Private Methods
|
||||||
#
|
#
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private function parse_block_elements(array $lines, $context = '')
|
private function parse_block_elements(array $lines, $context = '')
|
||||||
{
|
{
|
||||||
$elements = array();
|
$elements = array();
|
||||||
@ -213,13 +182,11 @@ class Parsedown
|
|||||||
|
|
||||||
$elements []= $element;
|
$elements []= $element;
|
||||||
|
|
||||||
$element = array(
|
unset($element['first']);
|
||||||
'type' => 'li',
|
|
||||||
'indentation' => $matches[1],
|
$element['last'] = true;
|
||||||
'last' => true,
|
$element['lines'] = array(
|
||||||
'lines' => array(
|
preg_replace('/^[ ]{0,4}/', '', $matches[3]),
|
||||||
preg_replace('/^[ ]{0,4}/', '', $matches[3]),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -508,6 +475,7 @@ class Parsedown
|
|||||||
'type' => 'li',
|
'type' => 'li',
|
||||||
'ordered' => false,
|
'ordered' => false,
|
||||||
'indentation' => $matches[1],
|
'indentation' => $matches[1],
|
||||||
|
'first' => true,
|
||||||
'last' => true,
|
'last' => true,
|
||||||
'lines' => array(
|
'lines' => array(
|
||||||
preg_replace('/^[ ]{0,4}/', '', $matches[2]),
|
preg_replace('/^[ ]{0,4}/', '', $matches[2]),
|
||||||
@ -528,6 +496,7 @@ class Parsedown
|
|||||||
'type' => 'li',
|
'type' => 'li',
|
||||||
'ordered' => true,
|
'ordered' => true,
|
||||||
'indentation' => $matches[1],
|
'indentation' => $matches[1],
|
||||||
|
'first' => true,
|
||||||
'last' => true,
|
'last' => true,
|
||||||
'lines' => array(
|
'lines' => array(
|
||||||
preg_replace('/^[ ]{0,4}/', '', $matches[2]),
|
preg_replace('/^[ ]{0,4}/', '', $matches[2]),
|
||||||
@ -551,6 +520,8 @@ class Parsedown
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
$this->breaks_enabled and $element['text'] .= ' ';
|
||||||
|
|
||||||
$element['text'] .= "\n".$line;
|
$element['text'] .= "\n".$line;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -592,6 +563,11 @@ class Parsedown
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
$markup .= $text;
|
$markup .= $text;
|
||||||
|
|
||||||
|
if (isset($elements[2]))
|
||||||
|
{
|
||||||
|
$markup .= "\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -610,17 +586,22 @@ class Parsedown
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'code block':
|
case 'code block':
|
||||||
|
|
||||||
|
$text = htmlspecialchars($element['text'], ENT_NOQUOTES, 'UTF-8');
|
||||||
|
|
||||||
|
$markup .= '<pre><code>'.$text.'</code></pre>'."\n";
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
case 'fenced block':
|
case 'fenced block':
|
||||||
|
|
||||||
$text = htmlspecialchars($element['text'], ENT_NOQUOTES, 'UTF-8');
|
$text = htmlspecialchars($element['text'], ENT_NOQUOTES, 'UTF-8');
|
||||||
|
|
||||||
strpos($text, "\x1A\\") !== FALSE and $text = strtr($text, $this->escape_sequence_map);
|
$markup .= '<pre><code';
|
||||||
|
|
||||||
$markup .= isset($element['language'])
|
isset($element['language']) and $markup .= ' class="language-'.$element['language'].'"';
|
||||||
? '<pre><code class="language-'.$element['language'].'">'.$text.'</code></pre>'
|
|
||||||
: '<pre><code>'.$text.'</code></pre>';
|
|
||||||
|
|
||||||
$markup .= "\n";
|
$markup .= '>'.$text.'</code></pre>'."\n";
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -640,11 +621,11 @@ class Parsedown
|
|||||||
|
|
||||||
case 'li':
|
case 'li':
|
||||||
|
|
||||||
if (isset($element['ordered'])) # first
|
if (isset($element['first']))
|
||||||
{
|
{
|
||||||
$list_type = $element['ordered'] ? 'ol' : 'ul';
|
$type = $element['ordered'] ? 'ol' : 'ul';
|
||||||
|
|
||||||
$markup .= '<'.$list_type.'>'."\n";
|
$markup .= '<'.$type.'>'."\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($element['interrupted']) and ! isset($element['last']))
|
if (isset($element['interrupted']) and ! isset($element['last']))
|
||||||
@ -656,7 +637,12 @@ class Parsedown
|
|||||||
|
|
||||||
$markup .= '<li>'.$text.'</li>'."\n";
|
$markup .= '<li>'.$text.'</li>'."\n";
|
||||||
|
|
||||||
isset($element['last']) and $markup .= '</'.$list_type.'>'."\n";
|
if (isset($element['last']))
|
||||||
|
{
|
||||||
|
$type = $element['ordered'] ? 'ol' : 'ul';
|
||||||
|
|
||||||
|
$markup .= '</'.$type.'>'."\n";
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -675,9 +661,9 @@ class Parsedown
|
|||||||
return $markup;
|
return $markup;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function parse_span_elements($text, $markers = array('![', '&', '*', '<', '[', '_', '`', 'http', '~~'))
|
private function parse_span_elements($text, $markers = array(" \n", '![', '&', '*', '<', '[', '\\', '_', '`', 'http', '~~'))
|
||||||
{
|
{
|
||||||
if (isset($text[2]) === false or $markers === array())
|
if (isset($text[1]) === false or $markers === array())
|
||||||
{
|
{
|
||||||
return $text;
|
return $text;
|
||||||
}
|
}
|
||||||
@ -713,7 +699,7 @@ class Parsedown
|
|||||||
|
|
||||||
# ~
|
# ~
|
||||||
|
|
||||||
if ($closest_marker === null or isset($text[$closest_marker_position + 2]) === false)
|
if ($closest_marker === null or isset($text[$closest_marker_position + 1]) === false)
|
||||||
{
|
{
|
||||||
$markup .= $text;
|
$markup .= $text;
|
||||||
|
|
||||||
@ -734,6 +720,14 @@ class Parsedown
|
|||||||
|
|
||||||
switch ($closest_marker)
|
switch ($closest_marker)
|
||||||
{
|
{
|
||||||
|
case " \n":
|
||||||
|
|
||||||
|
$markup .= '<br />'."\n";
|
||||||
|
|
||||||
|
$offset = 3;
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
case '![':
|
case '![':
|
||||||
case '[':
|
case '[':
|
||||||
|
|
||||||
@ -801,15 +795,21 @@ class Parsedown
|
|||||||
|
|
||||||
if ($element['!'])
|
if ($element['!'])
|
||||||
{
|
{
|
||||||
$markup .= '<img alt="'.$element['a'].'" src="'.$element['»'].'" />';
|
$markup .= '<img alt="'.$element['a'].'" src="'.$element['»'].'"';
|
||||||
|
|
||||||
|
isset($element['#']) and $markup .= ' title="'.$element['#'].'"';
|
||||||
|
|
||||||
|
$markup .= ' />';
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$element['a'] = $this->parse_span_elements($element['a'], $markers);
|
$element['a'] = $this->parse_span_elements($element['a'], $markers);
|
||||||
|
|
||||||
$markup .= isset($element['#'])
|
$markup .= '<a href="'.$element['»'].'"';
|
||||||
? '<a href="'.$element['»'].'" title="'.$element['#'].'">'.$element['a'].'</a>'
|
|
||||||
: '<a href="'.$element['»'].'">'.$element['a'].'</a>';
|
isset($element['#']) and $markup .= ' title="'.$element['#'].'"';
|
||||||
|
|
||||||
|
$markup .= '>'.$element['a'].'</a>';
|
||||||
}
|
}
|
||||||
|
|
||||||
unset($element);
|
unset($element);
|
||||||
@ -825,9 +825,18 @@ class Parsedown
|
|||||||
|
|
||||||
case '&':
|
case '&':
|
||||||
|
|
||||||
$markup .= '&';
|
if (preg_match('/^&#?\w+;/', $text, $matches))
|
||||||
|
{
|
||||||
|
$markup .= $matches[0];
|
||||||
|
|
||||||
$offset = substr($text, 0, 5) === '&' ? 5 : 1;
|
$offset = strlen($matches[0]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$markup .= '&';
|
||||||
|
|
||||||
|
$offset = 1;
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -892,6 +901,12 @@ class Parsedown
|
|||||||
|
|
||||||
$offset = strlen($matches[0]);
|
$offset = strlen($matches[0]);
|
||||||
}
|
}
|
||||||
|
elseif (strpos($text, '@') > 1 and preg_match('/<(\S+?@\S+?)>/', $text, $matches))
|
||||||
|
{
|
||||||
|
$markup .= '<a href="mailto:'.$matches[1].'">'.$matches[1].'</a>';
|
||||||
|
|
||||||
|
$offset = strlen($matches[0]);
|
||||||
|
}
|
||||||
elseif (preg_match('/^<\/?\w.*?>/', $text, $matches))
|
elseif (preg_match('/^<\/?\w.*?>/', $text, $matches))
|
||||||
{
|
{
|
||||||
$markup .= $matches[0];
|
$markup .= $matches[0];
|
||||||
@ -914,18 +929,30 @@ class Parsedown
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case '\\':
|
||||||
|
|
||||||
|
if (in_array($text[1], $this->special_characters))
|
||||||
|
{
|
||||||
|
$markup .= $text[1];
|
||||||
|
|
||||||
|
$offset = 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$markup .= '\\';
|
||||||
|
|
||||||
|
$offset = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
case '`':
|
case '`':
|
||||||
|
|
||||||
if (preg_match('/^`(.+?)`/', $text, $matches))
|
if (preg_match('/^(`+)(.+?)\1(?!`)/', $text, $matches))
|
||||||
{
|
{
|
||||||
$element_text = $matches[1];
|
$element_text = $matches[2];
|
||||||
$element_text = htmlspecialchars($element_text, ENT_NOQUOTES, 'UTF-8');
|
$element_text = htmlspecialchars($element_text, ENT_NOQUOTES, 'UTF-8');
|
||||||
|
|
||||||
if ($this->escape_sequence_map and strpos($element_text, "\x1A") !== false)
|
|
||||||
{
|
|
||||||
$element_text = strtr($element_text, $this->escape_sequence_map);
|
|
||||||
}
|
|
||||||
|
|
||||||
$markup .= '<code>'.$element_text.'</code>';
|
$markup .= '<code>'.$element_text.'</code>';
|
||||||
|
|
||||||
$offset = strlen($matches[0]);
|
$offset = strlen($matches[0]);
|
||||||
@ -988,8 +1015,6 @@ class Parsedown
|
|||||||
$markers[$closest_marker_index] = $closest_marker;
|
$markers[$closest_marker_index] = $closest_marker;
|
||||||
}
|
}
|
||||||
|
|
||||||
$markup = str_replace($this->break_marker, '<br />'."\n", $markup);
|
|
||||||
|
|
||||||
return $markup;
|
return $markup;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1004,6 +1029,8 @@ class Parsedown
|
|||||||
'span', 'strong', 'sub', 'sup', 'textarea', 'tt', 'var',
|
'span', 'strong', 'sub', 'sup', 'textarea', 'tt', 'var',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
private $special_characters = array('\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!');
|
||||||
|
|
||||||
# ~
|
# ~
|
||||||
|
|
||||||
private $strong_regex = array(
|
private $strong_regex = array(
|
||||||
|
@ -13,8 +13,8 @@ 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)
|
||||||
* friendly to international input
|
|
||||||
* [tested](https://travis-ci.org/erusev/parsedown) in PHP 5.2, 5.3, 5.4, 5.5 and [hhvm](http://www.hhvm.com/)
|
* [tested](https://travis-ci.org/erusev/parsedown) in PHP 5.2, 5.3, 5.4, 5.5 and [hhvm](http://www.hhvm.com/)
|
||||||
|
* friendly to international input
|
||||||
|
|
||||||
### Installation
|
### Installation
|
||||||
|
|
||||||
@ -23,7 +23,7 @@ Include `Parsedown.php` or install [the composer package](https://packagist.org/
|
|||||||
### Example
|
### Example
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$text = 'Hello _Parsedown_!';
|
$text = 'Hello *Parsedown*!';
|
||||||
|
|
||||||
$result = Parsedown::instance()->parse($text);
|
$result = Parsedown::instance()->parse($text);
|
||||||
|
|
||||||
|
1
tests/data/email.html
Normal file
1
tests/data/email.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
<p>my email is <a href="mailto:me@example.com">me@example.com</a></p>
|
1
tests/data/email.md
Normal file
1
tests/data/email.md
Normal file
@ -0,0 +1 @@
|
|||||||
|
my email is <me@example.com>
|
1
tests/data/html_entity.html
Normal file
1
tests/data/html_entity.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
<p>& © {</p>
|
1
tests/data/html_entity.md
Normal file
1
tests/data/html_entity.md
Normal file
@ -0,0 +1 @@
|
|||||||
|
& © {
|
1
tests/data/image_title.html
Normal file
1
tests/data/image_title.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
<p><img alt="alt" src="/md.png" title="title" /></p>
|
1
tests/data/image_title.md
Normal file
1
tests/data/image_title.md
Normal file
@ -0,0 +1 @@
|
|||||||
|

|
@ -1,5 +1,4 @@
|
|||||||
<p>AT&T has an ampersand in their name</p>
|
<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>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>
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
AT&T has an ampersand in their name
|
AT&T has an ampersand in their name
|
||||||
|
|
||||||
AT&T is another way to write it
|
|
||||||
|
|
||||||
this & that
|
this & that
|
||||||
|
|
||||||
4 < 5 and 6 > 5
|
4 < 5 and 6 > 5
|
||||||
|
Reference in New Issue
Block a user