1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00

Compare commits

..

53 Commits
0.6.0 ... 0.8.3

Author SHA1 Message Date
4e83d79d76 setters should know nothing 2014-01-20 09:26:25 +02:00
354842fd6e simplify compiling 2014-01-19 23:34:20 +02:00
2b73e94c6c simplify parsing of escaped characters 2014-01-19 22:49:43 +02:00
0182812d6c remove unnecessary blank lines 2014-01-19 15:37:05 +02:00
f5dd3455f9 resolve #58 2014-01-19 00:52:07 +02:00
1017f22cdd fix paragraph list 2014-01-18 16:45:39 +02:00
88854955d6 take $inline_tags out of the method 2014-01-18 16:45:39 +02:00
654dd74074 lines that start with inline html should not get parsed as block-level markup, should resolve #54 and #57 2014-01-18 16:45:39 +02:00
fee5b71998 improve readme 2014-01-17 01:33:20 +02:00
149b687ee7 improve tests 2014-01-17 01:25:41 +02:00
98b17e3354 setext heading doesn't have to use regex 2014-01-17 01:23:25 +02:00
da966b83f1 atx heading doesn't have to use regex 2014-01-17 00:36:11 +02:00
b9ab495cb4 parse method doesn't have to use regex 2014-01-16 23:43:34 +02:00
408cb5c21f code block doesn't have to use regex 2014-01-16 23:43:12 +02:00
5dd0e8cb7b $deindented_line >= ... doesn't make sense 2014-01-16 23:39:56 +02:00
5521afde31 refactor $element 2014-01-13 23:45:31 +02:00
4317add3a2 add hhvm to PHP versions to test against 2013-12-28 14:57:25 +02:00
47d8a1382b implement automatic line breaks as an option 2013-12-26 23:43:25 +02:00
fd42f2e864 improve tests 2013-12-26 21:55:33 +02:00
c0b7155572 implement link titles 2013-12-26 21:53:48 +02:00
be366b63ea improve parsing of emphasis
**strong** and *em* and **strong** and *em*
2013-12-24 23:40:12 +02:00
68484504ca separate compiling of link / image
there are 2 types of input that produce link / image
2013-12-24 16:05:13 +02:00
4fecd91f7d span elements should get parsed on a first-come, first-served basis 2013-12-24 03:17:23 +02:00
9dae844a6e post-increment operators should not be separated 2013-12-22 02:03:53 +02:00
df6fe915c6 Merge pull request #48 from kaamaru/master
Fix HTML Bug
2013-12-18 10:36:06 -08:00
576b0ea761 Fix HTML Bug
If you add markdown after HTML on the same line, all the remaining markdown will not be parsed.

Demo:
Add "<span></span> *test*" without quotes to the top of a markdown page on it's own line and then parse.
2013-12-18 12:32:49 +00:00
0f027dc04b Merge pull request #47 from malorisdead/link-titles
Add ability to specify link titles on inline and reference links.
2013-12-15 14:56:12 -08:00
179862bd6e improve readme 2013-12-15 03:32:34 +02:00
019a4af2af Added ability to specify link titles to inline and reference links.
Fixed whitespace bug with reference link regex.
Updated tests.
2013-12-14 02:13:53 -05:00
51a08fad85 improve parsing of emphasis 2013-12-07 17:21:36 +02:00
7fb08f334a improve comments 2013-12-07 10:54:05 +02:00
85ad014f74 parse code span after recursive types to resolve #44 2013-12-06 01:43:55 +02:00
22336a1bcc simplify special characters test 2013-12-06 00:45:26 +02:00
f713e380ee add comment for automatic link 2013-12-06 00:29:51 +02:00
5b01915a63 interrupted list items should not add nonexistent empty lines 2013-12-06 00:15:17 +02:00
18d112a614 improve readme 2013-12-03 23:19:50 +02:00
1b9641ad03 improve readme 2013-12-03 22:49:50 +02:00
8baf537c12 resolve #40 2013-12-02 23:26:43 +02:00
05823567bc simplify comments 2013-12-02 23:02:15 +02:00
b7029ab176 improve readme 2013-12-01 00:10:30 +02:00
102a947c7a improve readme 2013-11-23 15:58:58 +02:00
7bb70186c1 simplify test for em strong 2013-11-23 13:35:15 +02:00
3225c66863 ***strong em** inside of em* should produce valid markup 2013-11-23 13:19:06 +02:00
d6dc5ba25b update introduction text to match website 2013-11-23 09:26:44 +02:00
f5451a9eff Merge pull request #37 from hkdobrev/htmlspecialshars-utf8 2013-11-22 13:23:21 -08:00
849a89b121 Use UTF-8 encoding for htmlspecialchars. See #36.
Prior to PHP 5.4.0 the default encoding for `htmlentities()`
and `htmlspecialchars` is "ISO-8859-1". For PHP 5.4+ is "UTF-8".

This ensures always the right encoding is used no matter the PHP version
and the locale settings.
2013-11-22 23:06:20 +02:00
28064a63b3 simplify encoding of special characters 2013-11-22 21:57:21 +02:00
800aac5b56 Merge pull request #36 from josephok/patch-1 2013-11-22 11:21:38 -08:00
b15d40e8a3 Update Parsedown.php
Changes the htmlentities() to htmlspecialchars(). The htmlentities() has some problems encoding non-english words(like Chinese)
2013-11-22 23:05:26 +08:00
ddc5b7e2dd implement URL auto-linking 2013-11-22 00:20:45 +02:00
5a563008aa implement GFM strikethrough 2013-11-21 13:39:00 +02:00
b6f795962f resolve #21 2013-11-21 00:59:30 +02:00
cdb2646063 update readme to match website 2013-11-20 23:10:03 +02:00
49 changed files with 724 additions and 315 deletions

View File

@ -4,4 +4,5 @@ php:
- 5.5
- 5.4
- 5.3
- 5.2
- 5.2
- hhvm

View File

@ -33,12 +33,24 @@ class Parsedown
private static $instances = array();
#
# Setters
#
private $breaks_enabled = false;
function set_breaks_enabled($breaks_enabled)
{
$this->breaks_enabled = $breaks_enabled;
return $this;
}
#
# Fields
#
private $reference_map = array();
private $escape_sequence_map = array();
#
# Public Methods
@ -46,9 +58,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);
@ -56,41 +65,14 @@ 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 = preg_replace('/\n\s*\n/', "\n\n", $text);
$text = trim($text, "\n");
$lines = explode("\n", $text);
$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;
@ -110,12 +92,11 @@ class Parsedown
foreach ($lines as $line)
{
#
# fenced elements
switch ($element['type'])
{
case 'fenced_code_block':
case 'fenced block':
if ( ! isset($element['closed']))
{
@ -135,16 +116,16 @@ class Parsedown
break;
case 'markup':
case 'block-level markup':
if ( ! isset($element['closed']))
{
if (preg_match('{<'.$element['subtype'].'>$}', $line)) # opening tag
if (strpos($line, $element['start']) !== false) # opening tag
{
$element['depth']++;
}
if (preg_match('{</'.$element['subtype'].'>$}', $line)) # closing tag
if (strpos($line, $element['end']) !== false) # closing tag
{
$element['depth'] > 0
? $element['depth']--
@ -161,14 +142,15 @@ class Parsedown
# *
if ($line === '')
$deindented_line = ltrim($line);
if ($deindented_line === '')
{
$element['interrupted'] = true;
continue;
}
#
# composite elements
switch ($element['type'])
@ -223,6 +205,8 @@ class Parsedown
$element['lines'] []= $line;
unset($element['interrupted']);
continue 2;
}
}
@ -238,29 +222,19 @@ class Parsedown
break;
}
#
# 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']))
{
@ -269,15 +243,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,
);
}
@ -290,15 +264,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,
);
@ -308,27 +287,28 @@ 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;
$chopped_line = rtrim($line);
$i = 1;
while (isset($chopped_line[$i]))
{
if ($chopped_line[$i] !== $line[0])
{
break 2;
}
$i++;
}
$element['type'] = 'heading';
$element['level'] = $line[0] === '-' ? 2 : 1;
continue 2;
}
@ -336,41 +316,69 @@ class Parsedown
break;
}
#
# indentation insensitive types
switch ($deindented_line[0])
{
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' => '',
'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' => 'markup',
'subtype' => strtolower($matches[1]),
'type' => 'block-level markup',
'text' => $deindented_line,
'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;
}
@ -401,11 +409,18 @@ class Parsedown
# reference
if (preg_match('/^\[(.+?)\]:[ ]*([^ ]+)/', $deindented_line, $matches))
if (preg_match('/^\[(.+?)\]:[ ]*(.+?)(?:[ ]+[\'"](.+?)[\'"])?[ ]*$/', $deindented_line, $matches))
{
$label = strtolower($matches[1]);
$this->reference_map[$label] = trim($matches[2], '<>');;
$this->reference_map[$label] = array(
'»' => trim($matches[2], '<>'),
);
if (isset($matches[3]))
{
$this->reference_map[$label]['#'] = $matches[3];
}
continue 2;
}
@ -422,7 +437,7 @@ class Parsedown
$elements []= $element;
$element = array(
'type' => 'fenced_code_block',
'type' => 'fenced block',
'text' => '',
'fence' => $matches[1],
);
@ -474,7 +489,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;
@ -493,7 +508,7 @@ class Parsedown
# paragraph
if ($element['type'] === 'p')
if ($element['type'] === 'paragraph')
{
if (isset($element['interrupted']))
{
@ -505,6 +520,8 @@ class Parsedown
}
else
{
$this->breaks_enabled and $element['text'] .= ' ';
$element['text'] .= "\n".$line;
}
}
@ -513,7 +530,7 @@ class Parsedown
$elements []= $element;
$element = array(
'type' => 'p',
'type' => 'paragraph',
'text' => $line,
);
}
@ -533,12 +550,10 @@ class Parsedown
{
switch ($element['type'])
{
case 'p':
case 'paragraph':
$text = $this->parse_span_elements($element['text']);
$text = preg_replace('/[ ]{2}\n/', '<br />'."\n", $text);
if ($context === 'li' and $markup === '')
{
if (isset($element['interrupted']))
@ -565,18 +580,27 @@ class Parsedown
break;
case 'code_block':
case 'fenced_code_block':
case 'code block':
$text = htmlentities($element['text'], ENT_NOQUOTES);
strpos($text, "\x1A\\") !== FALSE and $text = strtr($text, $this->escape_sequence_map);
$text = htmlspecialchars($element['text'], ENT_NOQUOTES, 'UTF-8');
$markup .= '<pre><code>'.$text.'</code></pre>'."\n";
break;
case 'h.':
case 'fenced block':
$text = htmlspecialchars($element['text'], ENT_NOQUOTES, 'UTF-8');
$markup .= '<pre><code';
isset($element['language']) and $markup .= ' class="language-'.$element['language'].'"';
$markup .= '>'.$text.'</code></pre>'."\n";
break;
case 'heading':
$text = $this->parse_span_elements($element['text']);
@ -612,6 +636,12 @@ class Parsedown
break;
case 'block-level markup':
$markup .= $element['text']."\n";
break;
default:
$markup .= $element['text']."\n";
@ -621,165 +651,383 @@ class Parsedown
return $markup;
}
private function parse_span_elements($text)
private function parse_span_elements($text, $markers = array(" \n", '![', '&', '*', '<', '[', '\\', '_', '`', 'http', '~~'))
{
$map = array();
$index = 0;
# code span
if (strpos($text, '`') !== FALSE and preg_match_all('/`(.+?)`/', $text, $matches, PREG_SET_ORDER))
if (isset($text[1]) === false or $markers === array())
{
foreach ($matches as $matches)
{
$element_text = $matches[1];
$element_text = htmlentities($element_text, ENT_NOQUOTES);
# decodes escape sequences
$this->escape_sequence_map
and strpos($element_text, "\x1A") !== FALSE
and $element_text = strtr($element_text, $this->escape_sequence_map);
# composes element
$element = '<code>'.$element_text.'</code>';
# encodes element
$code = "\x1A".'$'.$index;
$text = str_replace($matches[0], $code, $text);
$map[$code] = $element;
$index ++;
}
}
# inline link or image
if (strpos($text, '](') !== FALSE and preg_match_all('/(!?)(\[((?:[^\[\]]|(?2))*)\])\((.*?)\)/', $text, $matches, PREG_SET_ORDER)) # inline
{
foreach ($matches as $matches)
{
$url = $matches[4];
strpos($url, '&') !== FALSE and $url = preg_replace('/&(?!#?\w+;)/', '&amp;', $url);
if ($matches[1]) # image
{
$element = '<img alt="'.$matches[3].'" src="'.$url.'">';
}
else
{
$element_text = $this->parse_span_elements($matches[3]);
$element = '<a href="'.$url.'">'.$element_text.'</a>';
}
# ~
$code = "\x1A".'$'.$index;
$text = str_replace($matches[0], $code, $text);
$map[$code] = $element;
$index ++;
}
}
# reference link or image
if ($this->reference_map and strpos($text, '[') !== FALSE and preg_match_all('/(!?)\[(.+?)\](?:\n?[ ]?\[(.*?)\])?/ms', $text, $matches, PREG_SET_ORDER))
{
foreach ($matches as $matches)
{
$link_definition = isset($matches[3]) && $matches[3]
? $matches[3]
: $matches[2]; # implicit
$link_definition = strtolower($link_definition);
if (isset($this->reference_map[$link_definition]))
{
$url = $this->reference_map[$link_definition];
strpos($url, '&') !== FALSE and $url = preg_replace('/&(?!#?\w+;)/', '&amp;', $url);
if ($matches[1]) # image
{
$element = '<img alt="'.$matches[2].'" src="'.$url.'">';
}
else # anchor
{
$element_text = $this->parse_span_elements($matches[2]);
$element = '<a href="'.$url.'">'.$element_text.'</a>';
}
# ~
$code = "\x1A".'$'.$index;
$text = str_replace($matches[0], $code, $text);
$map[$code] = $element;
$index ++;
}
}
}
# automatic link
if (strpos($text, '<') !== FALSE and preg_match_all('/<((https?|ftp|dict):[^\^\s]+?)>/i', $text, $matches, PREG_SET_ORDER))
{
foreach ($matches as $matches)
{
$url = $matches[1];
strpos($url, '&') !== FALSE and $url = preg_replace('/&(?!#?\w+;)/', '&amp;', $url);
$element = '<a href=":href">:text</a>';
$element = str_replace(':text', $url, $element);
$element = str_replace(':href', $url, $element);
# ~
$code = "\x1A".'$'.$index;
$text = str_replace($matches[0], $code, $text);
$map[$code] = $element;
$index ++;
}
return $text;
}
# ~
strpos($text, '&') !== FALSE and $text = preg_replace('/&(?!#?\w+;)/', '&amp;', $text);
strpos($text, '<') !== FALSE and $text = preg_replace('/<(?!\/?\w.*?>)/', '&lt;', $text);
$markup = '';
# ~
if (strpos($text, '_') !== FALSE)
while ($markers)
{
$text = preg_replace('/__(?=\S)(.+?)(?<=\S)__(?!_)/s', '<strong>$1</strong>', $text);
$text = preg_replace('/_(?=\S)(.+?)(?<=\S)_/s', '<em>$1</em>', $text);
$closest_marker = null;
$closest_marker_index = 0;
$closest_marker_position = null;
foreach ($markers as $index => $marker)
{
$marker_position = strpos($text, $marker);
if ($marker_position === false)
{
unset($markers[$index]);
continue;
}
if ($closest_marker === null or $marker_position < $closest_marker_position)
{
$closest_marker = $marker;
$closest_marker_index = $index;
$closest_marker_position = $marker_position;
}
}
# ~
if ($closest_marker === null or isset($text[$closest_marker_position + 1]) === false)
{
$markup .= $text;
break;
}
else
{
$markup .= substr($text, 0, $closest_marker_position);
}
$text = substr($text, $closest_marker_position);
# ~
unset($markers[$closest_marker_index]);
# ~
switch ($closest_marker)
{
case " \n":
$markup .= '<br />'."\n";
$offset = 3;
break;
case '![':
case '[':
if (strpos($text, ']') and preg_match('/\[((?:[^][]|(?R))*)\]/', $text, $matches))
{
$element = array(
'!' => $text[0] === '!',
'a' => $matches[1],
);
$offset = strlen($matches[0]);
$element['!'] and $offset++;
$remaining_text = substr($text, $offset);
if ($remaining_text[0] === '(' and preg_match('/\([ ]*(.*?)(?:[ ]+[\'"](.+?)[\'"])?[ ]*\)/', $remaining_text, $matches))
{
$element['»'] = $matches[1];
if (isset($matches[2]))
{
$element['#'] = $matches[2];
}
$offset += strlen($matches[0]);
}
elseif ($this->reference_map)
{
$reference = $element['a'];
if (preg_match('/^\s*\[(.*?)\]/', $remaining_text, $matches))
{
$reference = $matches[1] ? $matches[1] : $element['a'];
$offset += strlen($matches[0]);
}
$reference = strtolower($reference);
if (isset($this->reference_map[$reference]))
{
$element['»'] = $this->reference_map[$reference]['»'];
if (isset($this->reference_map[$reference]['#']))
{
$element['#'] = $this->reference_map[$reference]['#'];
}
}
else
{
unset($element);
}
}
else
{
unset($element);
}
}
if (isset($element))
{
$element['»'] = str_replace('&', '&amp;', $element['»']);
$element['»'] = str_replace('<', '&lt;', $element['»']);
if ($element['!'])
{
$markup .= '<img alt="'.$element['a'].'" src="'.$element['»'].'" />';
}
else
{
$element['a'] = $this->parse_span_elements($element['a'], $markers);
$markup .= isset($element['#'])
? '<a href="'.$element['»'].'" title="'.$element['#'].'">'.$element['a'].'</a>'
: '<a href="'.$element['»'].'">'.$element['a'].'</a>';
}
unset($element);
}
else
{
$markup .= $closest_marker;
$offset = $closest_marker === '![' ? 2 : 1;
}
break;
case '&':
if (preg_match('/^&#?\w+;/', $text, $matches))
{
$markup .= $matches[0];
$offset = strlen($matches[0]);
}
else
{
$markup .= '&amp;';
$offset = 1;
}
break;
case '*':
case '_':
if ($text[1] === $closest_marker and preg_match($this->strong_regex[$closest_marker], $text, $matches))
{
$matches[1] = $this->parse_span_elements($matches[1], $markers);
$markup .= '<strong>'.$matches[1].'</strong>';
}
elseif (preg_match($this->em_regex[$closest_marker], $text, $matches))
{
$matches[1] = $this->parse_span_elements($matches[1], $markers);
$markup .= '<em>'.$matches[1].'</em>';
}
elseif ($text[1] === $closest_marker and preg_match($this->strong_em_regex[$closest_marker], $text, $matches))
{
$matches[2] = $this->parse_span_elements($matches[2], $markers);
$matches[1] and $matches[1] = $this->parse_span_elements($matches[1], $markers);
$matches[3] and $matches[3] = $this->parse_span_elements($matches[3], $markers);
$markup .= '<strong>'.$matches[1].'<em>'.$matches[2].'</em>'.$matches[3].'</strong>';
}
elseif (preg_match($this->em_strong_regex[$closest_marker], $text, $matches))
{
$matches[2] = $this->parse_span_elements($matches[2], $markers);
$matches[1] and $matches[1] = $this->parse_span_elements($matches[1], $markers);
$matches[3] and $matches[3] = $this->parse_span_elements($matches[3], $markers);
$markup .= '<em>'.$matches[1].'<strong>'.$matches[2].'</strong>'.$matches[3].'</em>';
}
if (isset($matches) and $matches)
{
$offset = strlen($matches[0]);
}
else
{
$markup .= $closest_marker;
$offset = 1;
}
break;
case '<':
if (strpos($text, '>') !== false)
{
if ($text[1] === 'h' and preg_match('/^<(https?:[\/]{2}[^\s]+?)>/i', $text, $matches))
{
$element_url = $matches[1];
$element_url = str_replace('&', '&amp;', $element_url);
$element_url = str_replace('<', '&lt;', $element_url);
$markup .= '<a href="'.$element_url.'">'.$element_url.'</a>';
$offset = strlen($matches[0]);
}
elseif (preg_match('/^<\/?\w.*?>/', $text, $matches))
{
$markup .= $matches[0];
$offset = strlen($matches[0]);
}
else
{
$markup .= '&lt;';
$offset = 1;
}
}
else
{
$markup .= '&lt;';
$offset = 1;
}
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))
{
$element_text = $matches[1];
$element_text = htmlspecialchars($element_text, ENT_NOQUOTES, 'UTF-8');
$markup .= '<code>'.$element_text.'</code>';
$offset = strlen($matches[0]);
}
else
{
$markup .= '`';
$offset = 1;
}
break;
case 'http':
if (preg_match('/^https?:[\/]{2}[^\s]+\b/i', $text, $matches))
{
$element_url = $matches[0];
$element_url = str_replace('&', '&amp;', $element_url);
$element_url = str_replace('<', '&lt;', $element_url);
$markup .= '<a href="'.$element_url.'">'.$element_url.'</a>';
$offset = strlen($matches[0]);
}
else
{
$markup .= 'http';
$offset = 4;
}
break;
case '~~':
if (preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $text, $matches))
{
$matches[1] = $this->parse_span_elements($matches[1], $markers);
$markup .= '<del>'.$matches[1].'</del>';
$offset = strlen($matches[0]);
}
else
{
$markup .= '~~';
$offset = 2;
}
break;
}
if (isset($offset))
{
$text = substr($text, $offset);
}
$markers[$closest_marker_index] = $closest_marker;
}
if (strpos($text, '*') !== FALSE)
{
$text = preg_replace('/\*\*(?=\S)(.+?)(?<=\S)\*\*(?!\*)/s', '<strong>$1</strong>', $text);
$text = preg_replace('/\*(?=\S)(.+?)(?<=\S)\*/s', '<em>$1</em>', $text);
}
$text = strtr($text, $map);
return $text;
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',
);
}

View File

@ -1,8 +1,20 @@
## Parsedown
Parsedown is a Markdown parser for PHP. It is fast, consistent and easy to use.
Better [Markdown](http://en.wikipedia.org/wiki/Markdown) parser for PHP.
[Home](http://parsedown.org) &middot; [Demo](http://parsedown.org/explorer/) &middot; [Tests](http://parsedown.org/tests/)
***
[ [demo](http://parsedown.org/demo) ] [ [tests](http://parsedown.org/tests/) ]
***
### Features
* [fast](http://parsedown.org/speed)
* [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, 5.5 and [hhvm](http://www.hhvm.com/)
### Installation
@ -11,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>
```

View File

@ -4,4 +4,5 @@
<h4>h4</h4>
<h5>h5</h5>
<h6>h6</h6>
<h1>closed h1</h1>
<h1>closed h1</h1>
<p>#</p>

View File

@ -10,4 +10,6 @@
###### h6
# closed h1 #
# closed h1 #
#

View File

@ -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>

View File

@ -1,12 +1,7 @@
<div>content</div>
<div>_content_</div>
<hr style="background: #eaa;" />
nested elements:
sparse:
<div>
parent
<div>
child
</div>
_content_
</div>

View File

@ -0,0 +1,2 @@
<p><em><code>code</code></em> <strong><code>code</code></strong></p>
<p><em><code>code</code><strong><code>code</code></strong><code>code</code></em></p>

View File

@ -0,0 +1,4 @@
_`code`_ __`code`__
*`code`**`code`**`code`*

View File

@ -1,5 +1,8 @@
<p><strong><em>em strong</em></strong></p>
<p><strong><em>one</em> at the start</strong></p>
<p><strong>one at the <em>end</em></strong></p>
<p><strong>one <em>in the</em> middle</strong></p>
<p><strong>one with <em>asterisks</em></strong></p>
<p><strong><em>em strong</em> strong</strong></p>
<p><strong>strong <em>em strong</em></strong></p>
<p><strong>strong <em>em strong</em> strong</strong></p>
<p><strong><em>em strong</em></strong></p>
<p><strong><em>em strong</em> strong</strong></p>
<p><strong>strong <em>em strong</em></strong></p>
<p><strong>strong <em>em strong</em> strong</strong></p>

View File

@ -1,9 +1,15 @@
___em strong___
___one_ at the start__
___em strong_ strong__
__one at the _end___
__strong _em strong___
__one _in the_ middle__
__strong _em strong_ strong__
**one with *asterisks***
***em strong***
***em strong* strong**
**strong *em strong***
**strong *em strong* strong**

View File

@ -1,5 +1,8 @@
<p><em>underscore</em>, <em>asterisk</em>, <em>one two</em>, <em>three four</em>, <em>a</em>, <em>b</em></p>
<p><em>multiline
emphasis</em></p>
<p>_ this _ is not an emphasis, neither is _ this_, _this _, or _this*</p>
<p>an empty emphasis __ ** is not an emphasis</p>
<p><strong>strong</strong> and <em>em</em> and <strong>strong</strong> and <em>em</em></p>
<p><em>line
line
line</em></p>
<p>this_is_not_an_emphasis</p>
<p>an empty emphasis __ ** is not an emphasis</p>
<p>*mixed *<em>double and</em> single asterisk** spans</p>

View File

@ -1,8 +1,13 @@
_underscore_, *asterisk*, _one two_, *three four*, _a_, *b*
_multiline
emphasis_
**strong** and *em* and **strong** and *em*
_ this _ is not an emphasis, neither is _ this_, _this _, or _this*
_line
line
line_
an empty emphasis __ ** is not an emphasis
this_is_not_an_emphasis
an empty emphasis __ ** is not an emphasis
*mixed **double and* single asterisk** spans

View File

@ -2,4 +2,5 @@
$message = 'fenced code block';
echo $message;</code></pre>
<pre><code>tilde</code></pre>
<pre><code>tilde</code></pre>
<pre><code class="language-php">echo 'language identifier';</code></pre>

View File

@ -7,4 +7,8 @@ echo $message;
~~~
tilde
~~~
~~~
```php
echo 'language identifier';
```

View File

@ -0,0 +1 @@
<p>&amp; &copy; &#123;</p>

View File

@ -0,0 +1 @@
&amp; &copy; &#123;

View File

@ -1 +1 @@
<p><img alt="Markdown Logo" src="/md.png"></p>
<p><img alt="Markdown Logo" src="/md.png" /></p>

View File

@ -1,3 +1,3 @@
![Markdown Logo][image]
[image]: /md.png
[image]: /md.png

View File

@ -1,2 +1,3 @@
<p>an <a href="http://example.com">implicit</a> reference link</p>
<p>an <a href="http://example.com">implicit</a> reference link with an empty link definition</p>
<p>an <a href="http://example.com">implicit</a> reference link with an empty link definition</p>
<p>an <a href="http://example.com" title="Example">explicit</a> reference link with a title</p>

View File

@ -2,4 +2,8 @@ an [implicit] reference link
[implicit]: http://example.com
an [implicit][] reference link with an empty link definition
an [implicit][] reference link with an empty link definition
an [explicit][example] reference link with a title
[example]: http://example.com "Example"

View File

@ -1,2 +1,4 @@
<p><a href="http://example.com">link</a></p>
<p><a href="http://example.com"><img alt="MD Logo" src="http://parsedown.org/md.png"></a></p>
<p><a href="http://example.com">link</a> and <a href="/tests/">another link</a></p>
<p><a href="http://example.com"><code>link</code></a></p>
<p><a href="http://example.com"><img alt="MD Logo" src="http://parsedown.org/md.png" /></a></p>
<p><a href="http://example.com"><img alt="MD Logo" src="http://parsedown.org/md.png" /> and text</a></p>

View File

@ -1,3 +1,7 @@
[link](http://example.com)
[link](http://example.com) and [another link](/tests/)
[![MD Logo](http://parsedown.org/md.png)](http://example.com)
[`link`](http://example.com)
[![MD Logo](http://parsedown.org/md.png)](http://example.com)
[![MD Logo](http://parsedown.org/md.png) and text](http://example.com)

View File

@ -0,0 +1 @@
<p><a href="http://example.com" title="Example">single quotes</a> and <a href="http://example.com" title="Example">double quotes</a></p>

View File

@ -0,0 +1 @@
[single quotes](http://example.com 'Example') and [double quotes](http://example.com "Example")

View File

@ -0,0 +1,7 @@
<ul>
<li>
<p>li</p>
<p>line
line</p>
</li>
</ul>

View File

@ -0,0 +1,4 @@
- li
line
line

View File

@ -0,0 +1,10 @@
<div>
_parent_
<div>
_child_
</div>
<pre>
_adopted child_
</pre>
</div>
<p><em>outside</em></p>

View File

@ -0,0 +1,11 @@
<div>
_parent_
<div>
_child_
</div>
<pre>
_adopted child_
</pre>
</div>
_outside_

View 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>

View File

@ -0,0 +1,9 @@
paragraph
- li
- li
paragraph
* li
* li

View File

@ -0,0 +1 @@
<p><a href="http://example.com" title="Title">single quotes</a> and <a href="http://example.com" title="Title">double quotes</a></p>

View File

@ -0,0 +1 @@
[single quotes](http://example.com 'Title') and [double quotes](http://example.com "Title")

View File

@ -0,0 +1,4 @@
<hr />
<p>attributes:</p>
<hr style="background: #9bd;" />
<p>...</p>

View File

@ -0,0 +1,7 @@
<hr />
attributes:
<hr style="background: #9bd;" />
...

View File

@ -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>

View File

@ -1,4 +1,6 @@
an <b>important</b> <a href=''>link</a>
broken<br/>
line
line
<b>inline tag</b> at the beginning

View File

@ -1,7 +1,6 @@
<p>AT&amp;T has an ampersand in their name</p>
<p>AT&amp;T is another way to write it</p>
<p>this &amp; that</p>
<p>4 &lt; 5 and 6 > 5</p>
<p><a href="http://example.com/autolink?a=1&amp;b=2">http://example.com/autolink?a=1&amp;b=2</a> </p>
<p><a href="http://example.com/autolink?a=1&amp;b=2">http://example.com/autolink?a=1&amp;b=2</a></p>
<p><a href="/script?a=1&amp;b=2">inline link</a></p>
<p><a href="http://example.com/?a=1&amp;b=2">reference link</a></p>

View File

@ -1,12 +1,10 @@
AT&T has an ampersand in their name
AT&T is another way to write it
this & that
4 < 5 and 6 > 5
<http://example.com/autolink?a=1&b=2>
<http://example.com/autolink?a=1&b=2>
[inline link](/script?a=1&b=2)

View File

@ -0,0 +1,3 @@
<p><del>strikethrough</del></p>
<p>here's <del>one</del> followed by <del>another one</del></p>
<p>~~ this ~~ is not one neither is ~this~</p>

View File

@ -0,0 +1,5 @@
~~strikethrough~~
here's ~~one~~ followed by ~~another one~~
~~ this ~~ is not one neither is ~this~

View File

@ -0,0 +1,6 @@
<p><em>em <strong>strong em</strong></em></p>
<p><em><strong>strong em</strong> em</em></p>
<p><em>em <strong>strong em</strong> em</em></p>
<p><em>em <strong>strong em</strong></em></p>
<p><em><strong>strong em</strong> em</em></p>
<p><em>em <strong>strong em</strong> em</em></p>

11
tests/data/strong_em.md Normal file
View File

@ -0,0 +1,11 @@
*em **strong em***
***strong em** em*
*em **strong em** em*
_em __strong em___
___strong em__ em_
_em __strong em__ em_

View File

@ -0,0 +1,6 @@
<pre><code>&lt;?php
$message = 'Hello World!';
echo $message;
echo "following a blank line";</code></pre>

View File

@ -0,0 +1,6 @@
<?php
$message = 'Hello World!';
echo $message;
echo "following a blank line";

View File

@ -3,4 +3,5 @@
<p>[one][404] with no definition</p>
<p><a href="http://example.com">multiline
one</a> defined on 2 lines</p>
<p><a href="http://example.com">one</a> with an upper case label</p>
<p><a href="http://example.com">one</a> with an upper case label</p>
<p><a href="http://example.com"><code>link</code></a></p>

View File

@ -13,4 +13,6 @@ one][website] defined on 2 lines
[one][label] with an upper case label
[LABEL]: http://example.com
[LABEL]: http://example.com
[`link`][website]

View File

@ -0,0 +1,2 @@
<p>an autolink <a href="http://example.com">http://example.com</a></p>
<p>inside of brackets [<a href="http://example.com">http://example.com</a>], inside of braces {<a href="http://example.com">http://example.com</a>}, inside of parentheses (<a href="http://example.com">http://example.com</a>)</p>

View File

@ -0,0 +1,3 @@
an autolink http://example.com
inside of brackets [http://example.com], inside of braces {http://example.com}, inside of parentheses (http://example.com)