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

Compare commits

...

9 Commits
1.3.0 ... 1.4.2

Author SHA1 Message Date
f08d017bcb resolve #196 2015-01-15 02:45:45 +02:00
e61a6114b0 resolve #175 2015-01-15 02:37:20 +02:00
9ed72ccd09 resolve #126 2015-01-15 02:24:39 +02:00
09e1184d9f resolve #265 2015-01-15 00:56:12 +02:00
2de60a9a8b improve readme 2015-01-13 15:28:18 +02:00
73a75299f5 improve readme 2015-01-13 15:18:35 +02:00
0d28808392 void markup blocks be marked 2015-01-12 23:24:13 +02:00
78960cf792 improve formatting 2015-01-12 18:53:24 +02:00
8f2e9c7cf6 definitions are blocks
in the old implementation it wasn’t possible to have multiline
definitions
2015-01-12 18:52:17 +02:00
6 changed files with 72 additions and 62 deletions

View File

@ -29,11 +29,10 @@ class Parsedown
function text($text) function text($text)
{ {
# make sure no definitions are set # make sure no definitions are set
$this->Definitions = array(); $this->DefinitionData = array();
# standardize line breaks # standardize line breaks
$text = str_replace("\r\n", "\n", $text); $text = str_replace(array("\r\n", "\r"), "\n", $text);
$text = str_replace("\r", "\n", $text);
# remove surrounding line breaks # remove surrounding line breaks
$text = trim($text, "\n"); $text = trim($text, "\n");
@ -104,6 +103,7 @@ class Parsedown
'<' => array('Comment', 'Markup'), '<' => array('Comment', 'Markup'),
'=' => array('SetextHeader'), '=' => array('SetextHeader'),
'>' => array('Quote'), '>' => array('Quote'),
'[' => array('Reference'),
'_' => array('Rule'), '_' => array('Rule'),
'`' => array('FencedCode'), '`' => array('FencedCode'),
'|' => array('Table'), '|' => array('Table'),
@ -199,21 +199,6 @@ class Parsedown
$marker = $text[0]; $marker = $text[0];
if (isset($this->DefinitionTypes[$marker]))
{
foreach ($this->DefinitionTypes[$marker] as $definitionType)
{
$Definition = $this->{'definition'.$definitionType}($Line, $CurrentBlock);
if (isset($Definition))
{
$this->Definitions[$definitionType][$Definition['id']] = $Definition['data'];
continue 2;
}
}
}
# ~ # ~
$blockTypes = $this->unmarkedBlockTypes; $blockTypes = $this->unmarkedBlockTypes;
@ -290,6 +275,11 @@ class Parsedown
foreach ($Blocks as $Block) foreach ($Blocks as $Block)
{ {
if (isset($Block['hidden']))
{
continue;
}
$markup .= "\n"; $markup .= "\n";
$markup .= isset($Block['markup']) ? $Block['markup'] : $this->element($Block['element']); $markup .= isset($Block['markup']) ? $Block['markup'] : $this->element($Block['element']);
} }
@ -491,7 +481,7 @@ class Parsedown
$level ++; $level ++;
} }
if ($level > 6 or $Line['text'][$level] !== ' ') if ($level > 6)
{ {
return; return;
} }
@ -701,6 +691,8 @@ class Parsedown
if (isset($matches[2]) or in_array($matches[1], $this->voidElements)) if (isset($matches[2]) or in_array($matches[1], $this->voidElements))
{ {
$Block['closed'] = true; $Block['closed'] = true;
$Block['void'] = true;
} }
} }
else else
@ -758,6 +750,35 @@ class Parsedown
return $Block; return $Block;
} }
#
# Reference
protected function blockReference($Line)
{
if (preg_match('/^\[(.+?)\]:[ ]*<?(\S+?)>?(?:[ ]+["\'(](.+)["\')])?[ ]*$/', $Line['text'], $matches))
{
$id = strtolower($matches[1]);
$Data = array(
'url' => $matches[2],
'title' => null,
);
if (isset($matches[3]))
{
$Data['title'] = $matches[3];
}
$this->DefinitionData['Reference'][$id] = $Data;
$Block = array(
'hidden' => true,
);
return $Block;
}
}
# #
# Table # Table
@ -870,6 +891,11 @@ class Parsedown
protected function blockTableContinue($Line, array $Block) protected function blockTableContinue($Line, array $Block)
{ {
if (isset($Block['interrupted']))
{
return;
}
if ($Line['text'][0] === '|' or strpos($Line['text'], '|')) if ($Line['text'][0] === '|' or strpos($Line['text'], '|'))
{ {
$Elements = array(); $Elements = array();
@ -913,31 +939,6 @@ class Parsedown
} }
} }
#
# Definitions
#
protected function definitionReference($Line)
{
if (preg_match('/^\[(.+?)\]:[ ]*<?(\S+?)>?(?:[ ]+["\'(](.+)["\')])?[ ]*$/', $Line['text'], $matches))
{
$Definition = array(
'id' => strtolower($matches[1]),
'data' => array(
'url' => $matches[2],
'title' => null,
),
);
if (isset($matches[3]))
{
$Definition['data']['title'] = $matches[3];
}
return $Definition;
}
}
# #
# ~ # ~
# #
@ -1301,7 +1302,7 @@ class Parsedown
return; return;
} }
if (preg_match('/^\([ ]*([^ ]+?)(?:[ ]+(".+?"|\'.+?\'))?[ ]*\)/', $remainder, $matches)) if (preg_match('/^[(]((?:[^ (]|[(][^ )]+[)])+)(?:[ ]+("[^"]+"|\'[^\']+\'))?[)]/', $remainder, $matches))
{ {
$Element['attributes']['href'] = $matches[1]; $Element['attributes']['href'] = $matches[1];
@ -1326,12 +1327,12 @@ class Parsedown
$definition = strtolower($Element['text']); $definition = strtolower($Element['text']);
} }
if ( ! isset($this->Definitions['Reference'][$definition])) if ( ! isset($this->DefinitionData['Reference'][$definition]))
{ {
return; return;
} }
$Definition = $this->Definitions['Reference'][$definition]; $Definition = $this->DefinitionData['Reference'][$definition];
$Element['attributes']['href'] = $Definition['url']; $Element['attributes']['href'] = $Definition['url'];
$Element['attributes']['title'] = $Definition['title']; $Element['attributes']['title'] = $Definition['title'];
@ -1380,10 +1381,7 @@ class Parsedown
# #
# ~ # ~
protected $unmarkedInlineTypes = array( protected $unmarkedInlineTypes = array("\n" => 'Break', '://' => 'Url');
"\n" => 'Break',
'://' => 'Url',
);
# ~ # ~
@ -1507,7 +1505,7 @@ class Parsedown
# Fields # Fields
# #
protected $Definitions; protected $DefinitionData;
# #
# Read-only # Read-only

View File

@ -30,17 +30,20 @@ More examples in [the wiki](https://github.com/erusev/parsedown/wiki/Usage) and
### Questions ### Questions
**How does Parsedown work?**<br/> **How does Parsedown work?**
It tries to read Markdown like a human. First, it looks at the lines. Its interested in how the lines start. This helps it recognise blocks. It knows, for example, that if a line start with a `-` then it perhaps belong to a list. Once it recognises the blocks, it continues to the content. As it reads, it watches out for special characters. This helps it recognise inline elements (or inlines). It tries to read Markdown like a human. First, it looks at the lines. Its interested in how the lines start. This helps it recognise blocks. It knows, for example, that if a line start with a `-` then it perhaps belong to a list. Once it recognises the blocks, it continues to the content. As it reads, it watches out for special characters. This helps it recognise inline elements (or inlines).
**Why doesnt Parsedown use namespaces?**<br/> We call this approach "line based". We believe that Parsedown is the first Markdown parser to use it. Since the release of Parsedown, other developers have used the same approach to develop other Markdown parsers in PHP and in other languages.
It'd mean no support for PHP 5.2. Would it be worth it?
**Is Parsedown compliant with CommonMark?**
**Is Parsedown compliant with CommonMark?**<br/>
The majority of the CommonMark tests pass. Most of the tests that don't pass deal with cases that are quite extreme. Yet, we are working on them. As CommonMark matures, compliance should improve. The majority of the CommonMark tests pass. Most of the tests that don't pass deal with cases that are quite extreme. Yet, we are working on them. As CommonMark matures, compliance should improve.
**Who uses Parsedown?**<br/> **Who uses Parsedown?**
[phpDocumentor](http://www.phpdoc.org/), [October CMS](http://octobercms.com/), [Bolt CMS](http://bolt.cm/), [Kirby CMS](http://getkirby.com/), [Grav CMS](http://getgrav.org/), [Statamic CMS](http://www.statamic.com/), [RaspberryPi.org](http://www.raspberrypi.org/) and [more](https://www.versioneye.com/php/erusev:parsedown/references). [phpDocumentor](http://www.phpdoc.org/), [October CMS](http://octobercms.com/), [Bolt CMS](http://bolt.cm/), [Kirby CMS](http://getkirby.com/), [Grav CMS](http://getgrav.org/), [Statamic CMS](http://www.statamic.com/), [RaspberryPi.org](http://www.raspberrypi.org/) and [more](https://www.versioneye.com/php/erusev:parsedown/references).
**How can I help?**<br/> **How can I help?**
Use the project, tell friends about it and if you feel generous, [donate some money](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=528P3NZQMP8N2). Use the project, tell friends about it and if you feel generous, [donate some money](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=528P3NZQMP8N2).

View File

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

View File

@ -1,4 +1,4 @@
[link](http://example.com) and [another link](/tests/) [link](http://example.com) and [another link](/url-with-(parentheses))
[`link`](http://example.com) [`link`](http://example.com)

View File

@ -1 +1,4 @@
<p><a href="http://example.com" title="Title">single quotes</a> and <a href="http://example.com" title="Title">double quotes</a></p> <p><a href="http://example.com" title="Title">single quotes</a></p>
<p><a href="http://example.com" title="Title">double quotes</a></p>
<p><a href="http://example.com" title="2 Words">space</a></p>
<p><a href="http://example.com/url-(parentheses)" title="Title">parentheses</a></p>

View File

@ -1 +1,7 @@
[single quotes](http://example.com 'Title') and [double quotes](http://example.com "Title") [single quotes](http://example.com 'Title')
[double quotes](http://example.com "Title")
[space](http://example.com "2 Words")
[parentheses](http://example.com/url-(parentheses) "Title")