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

Compare commits

..

13 Commits
0.7.4 ... 0.7.7

Author SHA1 Message Date
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
16 changed files with 157 additions and 103 deletions

View File

@ -221,6 +221,8 @@ class Parsedown
$element['lines'] []= $line;
unset($element['interrupted']);
continue 2;
}
}
@ -397,11 +399,16 @@ class Parsedown
# reference
if (preg_match('/^\[(.+?)\]:[ ]*([^ ]+)/', $deindented_line, $matches))
if (preg_match('/^\[(.+?)\]:\s*([^\s]+)(?:\s+["\'\(](.+)["\'\)])?/', $deindented_line, $matches))
{
$label = strtolower($matches[1]);
$this->reference_map[$label] = trim($matches[2], '<>');;
$this->reference_map[$label] = trim($matches[2], '<>');
if (isset($matches[3]))
{
$this->reference_map[$label.":title"] = $matches[3];
}
continue 2;
}
@ -611,6 +618,12 @@ class Parsedown
isset($element['last']) and $markup .= '</'.$list_type.'>'."\n";
break;
case 'markup':
$markup .= $this->parse_span_elements($element['text'])."\n";
break;
default:
@ -627,6 +640,95 @@ class Parsedown
$index = 0;
# inline link / inline image (recursive)
if (strpos($text, '](') !== FALSE and preg_match_all('/(!?)(\[((?:[^\[\]]|(?2))*)\])\((.*?)(?:\s+["\'\(](.*?)["\'\)])?\)/', $text, $matches, PREG_SET_ORDER))
{
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 # link
{
$element_text = $this->parse_span_elements($matches[3]);
if (isset($matches[5]))
{
$element = '<a href="'.$url.'" title="'.$matches[5].'">'.$element_text.'</a>';
}
else
{
$element = '<a href="'.$url.'">'.$element_text.'</a>';
}
}
# ~
$code = "\x1A".'$'.$index;
$text = str_replace($matches[0], $code, $text);
$map[$code] = $element;
$index ++;
}
}
# reference link / reference image (recursive)
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 # link
{
$element_text = $this->parse_span_elements($matches[2]);
if (isset($this->reference_map[$link_definition.":title"]))
{
$element = '<a href="'.$url.'" title="'.$this->reference_map[$link_definition.":title"].'">'.$element_text.'</a>';
}
else
{
$element = '<a href="'.$url.'">'.$element_text.'</a>';
}
}
# ~
$code = "\x1A".'$'.$index;
$text = str_replace($matches[0], $code, $text);
$map[$code] = $element;
$index ++;
}
}
}
# code span
if (strpos($text, '`') !== FALSE and preg_match_all('/`(.+?)`/', $text, $matches, PREG_SET_ORDER))
@ -658,80 +760,7 @@ class Parsedown
}
}
# 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)
{
@ -780,18 +809,15 @@ class Parsedown
if (strpos($text, '_') !== FALSE)
{
$text = preg_replace('/__(?=\S)([^_]+?)(?<=\S)__/s', '<strong>$1</strong>', $text, -1, $count);
$count or $text = preg_replace('/__(?=\S)(.+?)(?<=\S)__(?!_)/s', '<strong>$1</strong>', $text);
$text = preg_replace('/\b_(?=\S)(.+?)(?<=\S)_\b/s', '<em>$1</em>', $text);
$text = preg_replace('/(\b|_)_(?=\S)([^_]+?)(?<=\S)_(\b|_)/s', '$1<em>$2</em>$3', $text);
$text = preg_replace('/__(?=\S)([^_]+?)(?<=\S)__/s', '<strong>$1</strong>', $text, -1, $count);
}
if (strpos($text, '*') !== FALSE)
{
$text = preg_replace('/\*\*(?=\S)([^*]+?)(?<=\S)\*\*/s', '<strong>$1</strong>', $text, -1, $count);
$count or $text = preg_replace('/\*\*(?=\S)(.+?)(?<=\S)\*\*(?!\*)/s', '<strong>$1</strong>', $text);
$text = preg_replace('/\*(?=\S)([^*]+?)(?<=\S)\*/s', '<em>$1</em>', $text, -1, $count);
$count or $text = preg_replace('/\*(?=\S)(.+?)(?<=\S)\*(?!\*)/s', '<em>$1</em>', $text);
$text = preg_replace('/\*\*(?=\S)([^*]+?)(?<=\S)\*\*/s', '<strong>$1</strong>', $text);
$text = preg_replace('/\*(?=\S)([^*]+?)(?<=\S)\*/s', '<em>$1</em>', $text);
$text = preg_replace('/\*\*(?=\S)([^*]+?)(?<=\S)\*\*/s', '<strong>$1</strong>', $text);
}
$text = strtr($text, $map);

View File

@ -1,19 +1,19 @@
## Parsedown
Better [Markdown][1] parser for PHP.
Better [Markdown](http://en.wikipedia.org/wiki/Markdown) parser for PHP.
***
[home](http://parsedown.org/) &middot; [demo](http://parsedown.org/demo) &middot; [tests](http://parsedown.org/tests/)
[ [demo](http://parsedown.org/demo) ] [ [tests](http://parsedown.org/tests/) ]
***
Features:
### Features
* [fast](http://parsedown.org/speed)
* [consistent](http://parsedown.org/consistency)
* [ GitHub Flavored ][2]
* tested in PHP 5.2, 5.3, 5.4 and 5.5
* [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
### Installation
@ -29,6 +29,3 @@ $result = Parsedown::instance()->parse($text);
echo $result; # prints: <p>Hello <strong>Parsedown</strong>!</p>
```
[1]: http://daringfireball.net/projects/markdown/
[2]: https://help.github.com/articles/github-flavored-markdown

View File

@ -1,4 +1,6 @@
<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>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

@ -4,4 +4,8 @@ __strong _em strong___
__strong _em strong_ strong__
**strong *em strong***
***em strong* strong**
**strong *em strong***
**strong *em strong* strong**

View File

@ -3,4 +3,5 @@
emphasis</em></p>
<p>_ this _ is not an emphasis, neither is _ this_, _this _, or _this*</p>
<p>this_is_not_an_emphasis</p>
<p>an empty emphasis __ ** 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

@ -7,4 +7,6 @@ _ this _ is not an emphasis, neither is _ this_, _this _, or _this*
this_is_not_an_emphasis
an empty emphasis __ ** is not an emphasis
an empty emphasis __ ** is not an emphasis
*mixed **double and* single asterisk** spans

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"><code>link</code></a></p>
<p><a href="http://example.com" title="Example">link with title</a></p>
<p><a href="http://example.com"><img alt="MD Logo" src="http://parsedown.org/md.png"></a></p>

View File

@ -1,3 +1,7 @@
[link](http://example.com)
[`link`](http://example.com)
[link with title](http://example.com "Example")
[![MD Logo](http://parsedown.org/md.png)](http://example.com)

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

@ -1,5 +1,4 @@
<p>AT&amp;T has an ampersand in their name</p>
<pre><code>Let's play some cards ♠ ♣ ♥ ♦</code></pre>
<p>AT&amp;T is another way to write it</p>
<p>this &amp; that</p>
<p>4 &lt; 5 and 6 > 5</p>

View File

@ -1,7 +1,5 @@
AT&T has an ampersand in their name
Let's play some cards ♠ ♣ ♥ ♦
AT&T is another way to write it
this & that

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]