mirror of
https://github.com/erusev/parsedown.git
synced 2025-09-15 17:22:18 +02:00
Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
149b687ee7 | ||
|
98b17e3354 | ||
|
da966b83f1 | ||
|
b9ab495cb4 | ||
|
408cb5c21f | ||
|
5dd0e8cb7b | ||
|
5521afde31 | ||
|
4317add3a2 |
@@ -4,4 +4,5 @@ php:
|
||||
- 5.5
|
||||
- 5.4
|
||||
- 5.3
|
||||
- 5.2
|
||||
- 5.2
|
||||
- hhvm
|
118
Parsedown.php
118
Parsedown.php
@@ -59,9 +59,6 @@ class Parsedown
|
||||
|
||||
function parse($text)
|
||||
{
|
||||
# removes UTF-8 BOM and marker characters
|
||||
$text = preg_replace('{^\xEF\xBB\xBF|\x1A}', '', $text);
|
||||
|
||||
# removes \r characters
|
||||
$text = str_replace("\r\n", "\n", $text);
|
||||
$text = str_replace("\r", "\n", $text);
|
||||
@@ -90,7 +87,6 @@ class Parsedown
|
||||
|
||||
# ~
|
||||
|
||||
$text = preg_replace('/\n\s*\n/', "\n\n", $text);
|
||||
$text = trim($text, "\n");
|
||||
|
||||
$lines = explode("\n", $text);
|
||||
@@ -129,7 +125,7 @@ class Parsedown
|
||||
|
||||
switch ($element['type'])
|
||||
{
|
||||
case 'fenced_code_block':
|
||||
case 'fenced block':
|
||||
|
||||
if ( ! isset($element['closed']))
|
||||
{
|
||||
@@ -149,16 +145,16 @@ class Parsedown
|
||||
|
||||
break;
|
||||
|
||||
case 'markup':
|
||||
case 'block-level markup':
|
||||
|
||||
if ( ! isset($element['closed']))
|
||||
{
|
||||
if (preg_match('{<'.$element['subtype'].'>$}', $line)) # opening tag
|
||||
if (preg_match('{<'.$element['root '].'>$}', $line)) # opening tag
|
||||
{
|
||||
$element['depth']++;
|
||||
}
|
||||
|
||||
if (preg_match('{</'.$element['subtype'].'>$}', $line)) # closing tag
|
||||
if (preg_match('{</'.$element['root '].'>$}', $line)) # closing tag
|
||||
{
|
||||
$element['depth'] > 0
|
||||
? $element['depth']--
|
||||
@@ -175,7 +171,9 @@ class Parsedown
|
||||
|
||||
# *
|
||||
|
||||
if ($line === '')
|
||||
$deindented_line = ltrim($line);
|
||||
|
||||
if ($deindented_line === '')
|
||||
{
|
||||
$element['interrupted'] = true;
|
||||
|
||||
@@ -255,26 +253,17 @@ class Parsedown
|
||||
|
||||
# indentation sensitive types
|
||||
|
||||
$deindented_line = $line;
|
||||
|
||||
switch ($line[0])
|
||||
{
|
||||
case ' ':
|
||||
|
||||
# ~
|
||||
|
||||
$deindented_line = ltrim($line);
|
||||
|
||||
if ($deindented_line === '')
|
||||
{
|
||||
continue 2;
|
||||
}
|
||||
|
||||
# code block
|
||||
|
||||
if (preg_match('/^[ ]{4}(.*)/', $line, $matches))
|
||||
if (isset($line[3]) and $line[3] === ' ' and $line[2] === ' ' and $line[1] === ' ')
|
||||
{
|
||||
if ($element['type'] === 'code_block')
|
||||
$code_line = substr($line, 4);
|
||||
|
||||
if ($element['type'] === 'code block')
|
||||
{
|
||||
if (isset($element['interrupted']))
|
||||
{
|
||||
@@ -283,15 +272,15 @@ class Parsedown
|
||||
unset ($element['interrupted']);
|
||||
}
|
||||
|
||||
$element['text'] .= "\n".$matches[1];
|
||||
$element['text'] .= "\n".$code_line;
|
||||
}
|
||||
else
|
||||
{
|
||||
$elements []= $element;
|
||||
|
||||
$element = array(
|
||||
'type' => 'code_block',
|
||||
'text' => $matches[1],
|
||||
'type' => 'code block',
|
||||
'text' => $code_line,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -304,15 +293,20 @@ class Parsedown
|
||||
|
||||
# atx heading (#)
|
||||
|
||||
if (preg_match('/^(#{1,6})[ ]*(.+?)[ ]*#*$/', $line, $matches))
|
||||
if (isset($line[1]))
|
||||
{
|
||||
$elements []= $element;
|
||||
|
||||
$level = strlen($matches[1]);
|
||||
$level = 1;
|
||||
|
||||
while (isset($line[$level]) and $line[$level] === '#')
|
||||
{
|
||||
$level++;
|
||||
}
|
||||
|
||||
$element = array(
|
||||
'type' => 'h.',
|
||||
'text' => $matches[2],
|
||||
'type' => 'heading',
|
||||
'text' => trim($line, '# '),
|
||||
'level' => $level,
|
||||
);
|
||||
|
||||
@@ -322,27 +316,35 @@ class Parsedown
|
||||
break;
|
||||
|
||||
case '-':
|
||||
|
||||
# setext heading (---)
|
||||
|
||||
if ($line[0] === '-' and $element['type'] === 'p' and ! isset($element['interrupted']) and preg_match('/^[-]+[ ]*$/', $line))
|
||||
{
|
||||
$element['type'] = 'h.';
|
||||
$element['level'] = 2;
|
||||
|
||||
continue 2;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case '=':
|
||||
|
||||
# setext heading (===)
|
||||
# setext heading
|
||||
|
||||
if ($line[0] === '=' and $element['type'] === 'p' and ! isset($element['interrupted']) and preg_match('/^[=]+[ ]*$/', $line))
|
||||
if ($element['type'] === 'paragraph' and isset($element['interrupted']) === false)
|
||||
{
|
||||
$element['type'] = 'h.';
|
||||
$element['level'] = 1;
|
||||
$is_heading = true;
|
||||
|
||||
$chopped_line = rtrim($line);
|
||||
|
||||
$i = 1;
|
||||
|
||||
while (isset($chopped_line[$i]))
|
||||
{
|
||||
if ($chopped_line[$i] !== $line[0])
|
||||
{
|
||||
$is_heading = false;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
$i++;
|
||||
}
|
||||
|
||||
if ($is_heading)
|
||||
{
|
||||
$element['type'] = 'heading';
|
||||
$element['level'] = $line[0] === '-' ? 2 : 1;
|
||||
}
|
||||
|
||||
continue 2;
|
||||
}
|
||||
@@ -363,7 +365,7 @@ class Parsedown
|
||||
$elements []= $element;
|
||||
|
||||
$element = array(
|
||||
'type' => '',
|
||||
'type' => 'self-closing tag',
|
||||
'text' => $deindented_line,
|
||||
);
|
||||
|
||||
@@ -377,9 +379,9 @@ class Parsedown
|
||||
$elements []= $element;
|
||||
|
||||
$element = array(
|
||||
'type' => 'markup',
|
||||
'subtype' => strtolower($matches[1]),
|
||||
'type' => 'block-level markup',
|
||||
'text' => $deindented_line,
|
||||
'root ' => strtolower($matches[1]),
|
||||
'depth' => 0,
|
||||
);
|
||||
|
||||
@@ -442,7 +444,7 @@ class Parsedown
|
||||
$elements []= $element;
|
||||
|
||||
$element = array(
|
||||
'type' => 'fenced_code_block',
|
||||
'type' => 'fenced block',
|
||||
'text' => '',
|
||||
'fence' => $matches[1],
|
||||
);
|
||||
@@ -494,7 +496,7 @@ class Parsedown
|
||||
|
||||
# li
|
||||
|
||||
if ($deindented_line[0] <= '9' and $deindented_line >= '0' and preg_match('/^([ ]*)\d+[.][ ](.*)/', $line, $matches))
|
||||
if ($deindented_line[0] <= '9' and $deindented_line[0] >= '0' and preg_match('/^([ ]*)\d+[.][ ](.*)/', $line, $matches))
|
||||
{
|
||||
$elements []= $element;
|
||||
|
||||
@@ -513,7 +515,7 @@ class Parsedown
|
||||
|
||||
# paragraph
|
||||
|
||||
if ($element['type'] === 'p')
|
||||
if ($element['type'] === 'paragraph')
|
||||
{
|
||||
if (isset($element['interrupted']))
|
||||
{
|
||||
@@ -533,7 +535,7 @@ class Parsedown
|
||||
$elements []= $element;
|
||||
|
||||
$element = array(
|
||||
'type' => 'p',
|
||||
'type' => 'paragraph',
|
||||
'text' => $line,
|
||||
);
|
||||
}
|
||||
@@ -553,7 +555,7 @@ class Parsedown
|
||||
{
|
||||
switch ($element['type'])
|
||||
{
|
||||
case 'p':
|
||||
case 'paragraph':
|
||||
|
||||
$text = $this->parse_span_elements($element['text']);
|
||||
|
||||
@@ -583,8 +585,8 @@ class Parsedown
|
||||
|
||||
break;
|
||||
|
||||
case 'code_block':
|
||||
case 'fenced_code_block':
|
||||
case 'code block':
|
||||
case 'fenced block':
|
||||
|
||||
$text = htmlspecialchars($element['text'], ENT_NOQUOTES, 'UTF-8');
|
||||
|
||||
@@ -598,7 +600,7 @@ class Parsedown
|
||||
|
||||
break;
|
||||
|
||||
case 'h.':
|
||||
case 'heading':
|
||||
|
||||
$text = $this->parse_span_elements($element['text']);
|
||||
|
||||
@@ -634,7 +636,7 @@ class Parsedown
|
||||
|
||||
break;
|
||||
|
||||
case 'markup':
|
||||
case 'block-level markup':
|
||||
|
||||
$markup .= $this->parse_span_elements($element['text'])."\n";
|
||||
|
||||
|
@@ -13,8 +13,8 @@ Better [Markdown](http://en.wikipedia.org/wiki/Markdown) parser for PHP.
|
||||
* [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
|
||||
* [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/)
|
||||
|
||||
### Installation
|
||||
|
||||
|
@@ -4,4 +4,5 @@
|
||||
<h4>h4</h4>
|
||||
<h5>h5</h5>
|
||||
<h6>h6</h6>
|
||||
<h1>closed h1</h1>
|
||||
<h1>closed h1</h1>
|
||||
<p>#</p>
|
@@ -10,4 +10,6 @@
|
||||
|
||||
###### h6
|
||||
|
||||
# closed h1 #
|
||||
# closed h1 #
|
||||
|
||||
#
|
6
tests/data/tab-indented_code_block.html
Normal file
6
tests/data/tab-indented_code_block.html
Normal file
@@ -0,0 +1,6 @@
|
||||
<pre><code><?php
|
||||
|
||||
$message = 'Hello World!';
|
||||
echo $message;
|
||||
|
||||
echo "following a blank line";</code></pre>
|
6
tests/data/tab-indented_code_block.md
Normal file
6
tests/data/tab-indented_code_block.md
Normal file
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
$message = 'Hello World!';
|
||||
echo $message;
|
||||
|
||||
echo "following a blank line";
|
Reference in New Issue
Block a user