mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
4e83d79d76 | |||
354842fd6e | |||
2b73e94c6c | |||
0182812d6c | |||
f5dd3455f9 | |||
1017f22cdd | |||
88854955d6 | |||
654dd74074 | |||
fee5b71998 |
236
Parsedown.php
236
Parsedown.php
@ -37,11 +37,11 @@ class Parsedown
|
||||
# Setters
|
||||
#
|
||||
|
||||
private $break_marker = " \n";
|
||||
private $breaks_enabled = false;
|
||||
|
||||
function set_breaks_enabled($breaks_enabled)
|
||||
{
|
||||
$this->break_marker = $breaks_enabled ? "\n" : " \n";
|
||||
$this->breaks_enabled = $breaks_enabled;
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -51,7 +51,6 @@ class Parsedown
|
||||
#
|
||||
|
||||
private $reference_map = array();
|
||||
private $escape_sequence_map = array();
|
||||
|
||||
#
|
||||
# Public Methods
|
||||
@ -66,25 +65,6 @@ class Parsedown
|
||||
# replaces tabs with spaces
|
||||
$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");
|
||||
@ -93,15 +73,6 @@ class Parsedown
|
||||
|
||||
$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");
|
||||
|
||||
return $text;
|
||||
@ -149,12 +120,12 @@ class Parsedown
|
||||
|
||||
if ( ! isset($element['closed']))
|
||||
{
|
||||
if (preg_match('{<'.$element['root '].'>$}', $line)) # opening tag
|
||||
if (strpos($line, $element['start']) !== false) # opening tag
|
||||
{
|
||||
$element['depth']++;
|
||||
}
|
||||
|
||||
if (preg_match('{</'.$element['root '].'>$}', $line)) # closing tag
|
||||
if (strpos($line, $element['end']) !== false) # closing tag
|
||||
{
|
||||
$element['depth'] > 0
|
||||
? $element['depth']--
|
||||
@ -322,8 +293,6 @@ class Parsedown
|
||||
|
||||
if ($element['type'] === 'paragraph' and isset($element['interrupted']) === false)
|
||||
{
|
||||
$is_heading = true;
|
||||
|
||||
$chopped_line = rtrim($line);
|
||||
|
||||
$i = 1;
|
||||
@ -332,19 +301,14 @@ class Parsedown
|
||||
{
|
||||
if ($chopped_line[$i] !== $line[0])
|
||||
{
|
||||
$is_heading = false;
|
||||
|
||||
break;
|
||||
break 2;
|
||||
}
|
||||
|
||||
$i++;
|
||||
}
|
||||
|
||||
if ($is_heading)
|
||||
{
|
||||
$element['type'] = 'heading';
|
||||
$element['level'] = $line[0] === '-' ? 2 : 1;
|
||||
}
|
||||
$element['type'] = 'heading';
|
||||
$element['level'] = $line[0] === '-' ? 2 : 1;
|
||||
|
||||
continue 2;
|
||||
}
|
||||
@ -358,34 +322,63 @@ class Parsedown
|
||||
{
|
||||
case '<':
|
||||
|
||||
# self-closing tag
|
||||
$position = strpos($deindented_line, '>');
|
||||
|
||||
if (preg_match('{^<.+?/>$}', $deindented_line))
|
||||
if ($position > 1) # tag
|
||||
{
|
||||
$name = substr($deindented_line, 1, $position - 1);
|
||||
$name = rtrim($name);
|
||||
|
||||
if (substr($name, -1) === '/')
|
||||
{
|
||||
$self_closing = true;
|
||||
|
||||
$name = substr($name, 0, -1);
|
||||
}
|
||||
|
||||
$position = strpos($name, ' ');
|
||||
|
||||
if ($position)
|
||||
{
|
||||
$name = substr($name, 0, $position);
|
||||
}
|
||||
|
||||
if ( ! ctype_alpha($name))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (in_array($name, $this->inline_tags))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
$elements []= $element;
|
||||
|
||||
$element = array(
|
||||
'type' => 'self-closing tag',
|
||||
'text' => $deindented_line,
|
||||
);
|
||||
if (isset($self_closing))
|
||||
{
|
||||
$element = array(
|
||||
'type' => 'self-closing tag',
|
||||
'text' => $deindented_line,
|
||||
);
|
||||
|
||||
continue 2;
|
||||
}
|
||||
unset($self_closing);
|
||||
|
||||
# opening tag
|
||||
|
||||
if (preg_match('{^<(\w+)(?:[ ].*?)?>}', $deindented_line, $matches))
|
||||
{
|
||||
$elements []= $element;
|
||||
continue 2;
|
||||
}
|
||||
|
||||
$element = array(
|
||||
'type' => 'block-level markup',
|
||||
'text' => $deindented_line,
|
||||
'root ' => strtolower($matches[1]),
|
||||
'start' => '<'.$name.'>',
|
||||
'end' => '</'.$name.'>',
|
||||
'depth' => 0,
|
||||
);
|
||||
|
||||
preg_match('{</'.$matches[1].'>\s*$}', $deindented_line) and $element['closed'] = true;
|
||||
if (strpos($deindented_line, $element['end']))
|
||||
{
|
||||
$element['closed'] = true;
|
||||
}
|
||||
|
||||
continue 2;
|
||||
}
|
||||
@ -527,6 +520,8 @@ class Parsedown
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->breaks_enabled and $element['text'] .= ' ';
|
||||
|
||||
$element['text'] .= "\n".$line;
|
||||
}
|
||||
}
|
||||
@ -586,17 +581,22 @@ class Parsedown
|
||||
break;
|
||||
|
||||
case 'code block':
|
||||
|
||||
$text = htmlspecialchars($element['text'], ENT_NOQUOTES, 'UTF-8');
|
||||
|
||||
$markup .= '<pre><code>'.$text.'</code></pre>'."\n";
|
||||
|
||||
break;
|
||||
|
||||
case 'fenced block':
|
||||
|
||||
$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'])
|
||||
? '<pre><code class="language-'.$element['language'].'">'.$text.'</code></pre>'
|
||||
: '<pre><code>'.$text.'</code></pre>';
|
||||
isset($element['language']) and $markup .= ' class="language-'.$element['language'].'"';
|
||||
|
||||
$markup .= "\n";
|
||||
$markup .= '>'.$text.'</code></pre>'."\n";
|
||||
|
||||
break;
|
||||
|
||||
@ -638,7 +638,7 @@ class Parsedown
|
||||
|
||||
case 'block-level markup':
|
||||
|
||||
$markup .= $this->parse_span_elements($element['text'])."\n";
|
||||
$markup .= $element['text']."\n";
|
||||
|
||||
break;
|
||||
|
||||
@ -651,31 +651,9 @@ class Parsedown
|
||||
return $markup;
|
||||
}
|
||||
|
||||
# ~
|
||||
|
||||
private $strong_regex = array(
|
||||
'*' => '/^[*]{2}([^*]+?)[*]{2}(?![*])/s',
|
||||
'_' => '/^__([^_]+?)__(?!_)/s',
|
||||
);
|
||||
|
||||
private $em_regex = array(
|
||||
'*' => '/^[*]([^*]+?)[*](?![*])/s',
|
||||
'_' => '/^_([^_]+?)[_](?![_])\b/s',
|
||||
);
|
||||
|
||||
private $strong_em_regex = array(
|
||||
'*' => '/^[*]{2}(.*?)[*](.+?)[*](.*?)[*]{2}/s',
|
||||
'_' => '/^__(.*?)_(.+?)_(.*?)__/s',
|
||||
);
|
||||
|
||||
private $em_strong_regex = array(
|
||||
'*' => '/^[*](.*?)[*]{2}(.+?)[*]{2}(.*?)[*]/s',
|
||||
'_' => '/^_(.*?)__(.+?)__(.*?)_/s',
|
||||
);
|
||||
|
||||
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;
|
||||
}
|
||||
@ -711,7 +689,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;
|
||||
|
||||
@ -732,6 +710,14 @@ class Parsedown
|
||||
|
||||
switch ($closest_marker)
|
||||
{
|
||||
case " \n":
|
||||
|
||||
$markup .= '<br />'."\n";
|
||||
|
||||
$offset = 3;
|
||||
|
||||
break;
|
||||
|
||||
case '![':
|
||||
case '[':
|
||||
|
||||
@ -823,9 +809,18 @@ class Parsedown
|
||||
|
||||
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;
|
||||
|
||||
@ -912,6 +907,23 @@ class Parsedown
|
||||
|
||||
break;
|
||||
|
||||
case '\\':
|
||||
|
||||
if (in_array($text[1], $this->special_characters))
|
||||
{
|
||||
$markup .= $text[1];
|
||||
|
||||
$offset = 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
$markup .= '\\';
|
||||
|
||||
$offset = 1;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case '`':
|
||||
|
||||
if (preg_match('/^`(.+?)`/', $text, $matches))
|
||||
@ -919,11 +931,6 @@ class Parsedown
|
||||
$element_text = $matches[1];
|
||||
$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>';
|
||||
|
||||
$offset = strlen($matches[0]);
|
||||
@ -986,8 +993,41 @@ class Parsedown
|
||||
$markers[$closest_marker_index] = $closest_marker;
|
||||
}
|
||||
|
||||
$markup = str_replace($this->break_marker, '<br />'."\n", $markup);
|
||||
|
||||
return $markup;
|
||||
}
|
||||
|
||||
#
|
||||
# Read-only
|
||||
#
|
||||
|
||||
private $inline_tags = array(
|
||||
'a', 'abbr', 'acronym', 'b', 'bdo', 'big', 'br', 'button',
|
||||
'cite', 'code', 'dfn', 'em', 'i', 'img', 'input', 'kbd',
|
||||
'label', 'map', 'object', 'q', 'samp', 'script', 'select', 'small',
|
||||
'span', 'strong', 'sub', 'sup', 'textarea', 'tt', 'var',
|
||||
);
|
||||
|
||||
private $special_characters = array('\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!');
|
||||
|
||||
# ~
|
||||
|
||||
private $strong_regex = array(
|
||||
'*' => '/^[*]{2}([^*]+?)[*]{2}(?![*])/s',
|
||||
'_' => '/^__([^_]+?)__(?!_)/s',
|
||||
);
|
||||
|
||||
private $em_regex = array(
|
||||
'*' => '/^[*]([^*]+?)[*](?![*])/s',
|
||||
'_' => '/^_([^_]+?)[_](?![_])\b/s',
|
||||
);
|
||||
|
||||
private $strong_em_regex = array(
|
||||
'*' => '/^[*]{2}(.*?)[*](.+?)[*](.*?)[*]{2}/s',
|
||||
'_' => '/^__(.*?)_(.+?)_(.*?)__/s',
|
||||
);
|
||||
|
||||
private $em_strong_regex = array(
|
||||
'*' => '/^[*](.*?)[*]{2}(.+?)[*]{2}(.*?)[*]/s',
|
||||
'_' => '/^_(.*?)__(.+?)__(.*?)_/s',
|
||||
);
|
||||
}
|
@ -14,7 +14,7 @@ Better [Markdown](http://en.wikipedia.org/wiki/Markdown) parser for PHP.
|
||||
* [consistent](http://parsedown.org/consistency)
|
||||
* [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 and 5.5 as well as in [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/)
|
||||
|
||||
### Installation
|
||||
|
||||
@ -23,9 +23,9 @@ Include `Parsedown.php` or install [the composer package](https://packagist.org/
|
||||
### Example
|
||||
|
||||
```php
|
||||
$text = 'Hello **Parsedown**!';
|
||||
$text = 'Hello _Parsedown_!';
|
||||
|
||||
$result = Parsedown::instance()->parse($text);
|
||||
|
||||
echo $result; # prints: <p>Hello <strong>Parsedown</strong>!</p>
|
||||
echo $result; # prints: <p>Hello <em>Parsedown</em>!</p>
|
||||
```
|
||||
|
@ -1,9 +1,5 @@
|
||||
<div>content</div>
|
||||
<hr style="background: #eaa;" />
|
||||
<p>nested elements:</p>
|
||||
<div>_content_</div>
|
||||
<p>sparse:</p>
|
||||
<div>
|
||||
parent
|
||||
<div>
|
||||
child
|
||||
</div>
|
||||
_content_
|
||||
</div>
|
@ -1,12 +1,7 @@
|
||||
<div>content</div>
|
||||
<div>_content_</div>
|
||||
|
||||
<hr style="background: #eaa;" />
|
||||
|
||||
nested elements:
|
||||
sparse:
|
||||
|
||||
<div>
|
||||
parent
|
||||
<div>
|
||||
child
|
||||
</div>
|
||||
_content_
|
||||
</div>
|
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 @@
|
||||
& © {
|
10
tests/data/nested_block-level_html.html
Normal file
10
tests/data/nested_block-level_html.html
Normal file
@ -0,0 +1,10 @@
|
||||
<div>
|
||||
_parent_
|
||||
<div>
|
||||
_child_
|
||||
</div>
|
||||
<pre>
|
||||
_adopted child_
|
||||
</pre>
|
||||
</div>
|
||||
<p><em>outside</em></p>
|
11
tests/data/nested_block-level_html.md
Normal file
11
tests/data/nested_block-level_html.md
Normal file
@ -0,0 +1,11 @@
|
||||
<div>
|
||||
_parent_
|
||||
<div>
|
||||
_child_
|
||||
</div>
|
||||
<pre>
|
||||
_adopted child_
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
_outside_
|
12
tests/data/paragraph_list.html
Normal file
12
tests/data/paragraph_list.html
Normal file
@ -0,0 +1,12 @@
|
||||
<p>paragraph</p>
|
||||
<ul>
|
||||
<li>li</li>
|
||||
<li>li</li>
|
||||
</ul>
|
||||
<p>paragraph</p>
|
||||
<ul>
|
||||
<li>
|
||||
<p>li</p>
|
||||
</li>
|
||||
<li>li</li>
|
||||
</ul>
|
9
tests/data/paragraph_list.md
Normal file
9
tests/data/paragraph_list.md
Normal file
@ -0,0 +1,9 @@
|
||||
paragraph
|
||||
- li
|
||||
- li
|
||||
|
||||
paragraph
|
||||
|
||||
* li
|
||||
|
||||
* li
|
4
tests/data/self-closing_block-level_html.html
Normal file
4
tests/data/self-closing_block-level_html.html
Normal file
@ -0,0 +1,4 @@
|
||||
<hr />
|
||||
<p>attributes:</p>
|
||||
<hr style="background: #9bd;" />
|
||||
<p>...</p>
|
7
tests/data/self-closing_block-level_html.md
Normal file
7
tests/data/self-closing_block-level_html.md
Normal file
@ -0,0 +1,7 @@
|
||||
<hr />
|
||||
|
||||
attributes:
|
||||
|
||||
<hr style="background: #9bd;" />
|
||||
|
||||
...
|
@ -1,3 +1,4 @@
|
||||
<p>an <b>important</b> <a href=''>link</a></p>
|
||||
<p>broken<br/>
|
||||
line</p>
|
||||
line</p>
|
||||
<p><b>inline tag</b> at the beginning</p>
|
@ -1,4 +1,6 @@
|
||||
an <b>important</b> <a href=''>link</a>
|
||||
|
||||
broken<br/>
|
||||
line
|
||||
line
|
||||
|
||||
<b>inline tag</b> at the beginning
|
@ -1,5 +1,4 @@
|
||||
<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><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 is another way to write it
|
||||
|
||||
this & that
|
||||
|
||||
4 < 5 and 6 > 5
|
||||
|
Reference in New Issue
Block a user