mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Compare commits
15 Commits
1.0.0-rc.1
...
1.0.0-rc.3
Author | SHA1 | Date | |
---|---|---|---|
2a5f99547c | |||
e373391e7d | |||
9fa415bcc5 | |||
37416b5f07 | |||
83d3e3dbbf | |||
307a987cb6 | |||
eab3cbf255 | |||
cf7f32f891 | |||
4150e00dc4 | |||
22affa124b | |||
5e95242318 | |||
504991a04e | |||
3d84201d74 | |||
4f027386b1 | |||
cd1c030362 |
@ -7,3 +7,4 @@ php:
|
||||
- 5.3
|
||||
- 5.2
|
||||
- hhvm
|
||||
|
197
Parsedown.php
197
Parsedown.php
@ -49,7 +49,7 @@ class Parsedown
|
||||
$markup = trim($markup, "\n");
|
||||
|
||||
# clean up
|
||||
$this->references = array();
|
||||
$this->definitions = array();
|
||||
|
||||
return $markup;
|
||||
}
|
||||
@ -97,7 +97,6 @@ class Parsedown
|
||||
'~' => array('FencedCode'),
|
||||
);
|
||||
|
||||
# Draft
|
||||
protected $definitionMarkers = array(
|
||||
'[' => array('Reference'),
|
||||
);
|
||||
@ -112,30 +111,21 @@ class Parsedown
|
||||
|
||||
foreach ($lines as $line)
|
||||
{
|
||||
if (chop($line) === '')
|
||||
{
|
||||
if (isset($CurrentBlock))
|
||||
{
|
||||
$CurrentBlock['interrupted'] = true;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$indent = 0;
|
||||
|
||||
while (true)
|
||||
while (isset($line[$indent]) and $line[$indent] === ' ')
|
||||
{
|
||||
if (isset($line[$indent]))
|
||||
{
|
||||
if ($line[$indent] === ' ')
|
||||
{
|
||||
$indent ++;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
else # blank line
|
||||
{
|
||||
if (isset($CurrentBlock))
|
||||
{
|
||||
$CurrentBlock['interrupted'] = true;
|
||||
}
|
||||
|
||||
continue 2;
|
||||
}
|
||||
$indent ++;
|
||||
}
|
||||
|
||||
$text = $indent > 0 ? substr($line, $indent) : $line;
|
||||
@ -158,12 +148,33 @@ class Parsedown
|
||||
}
|
||||
else
|
||||
{
|
||||
unset($CurrentBlock['incomplete']);
|
||||
|
||||
if (method_exists($this, 'complete'.$CurrentBlock['type']))
|
||||
{
|
||||
$CurrentBlock = $this->{'complete'.$CurrentBlock['type']}($CurrentBlock);
|
||||
}
|
||||
|
||||
unset($CurrentBlock['incomplete']);
|
||||
}
|
||||
}
|
||||
|
||||
# ~
|
||||
|
||||
$marker = $text[0];
|
||||
|
||||
# Definitions
|
||||
|
||||
if (isset($this->definitionMarkers[$marker]))
|
||||
{
|
||||
foreach ($this->definitionMarkers[$marker] as $definitionType)
|
||||
{
|
||||
$Definition = $this->{'identify'.$definitionType}($Line, $CurrentBlock);
|
||||
|
||||
if (isset($Definition))
|
||||
{
|
||||
$this->definitions[$definitionType][$Definition['id']] = $Definition['data'];
|
||||
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -171,8 +182,6 @@ class Parsedown
|
||||
|
||||
$blockTypes = $this->unmarkedBlockTypes;
|
||||
|
||||
$marker = $text[0];
|
||||
|
||||
if (isset($this->blockMarkers[$marker]))
|
||||
{
|
||||
foreach ($this->blockMarkers[$marker] as $blockType)
|
||||
@ -196,7 +205,7 @@ class Parsedown
|
||||
|
||||
if ( ! isset($Block['identified'])) # »
|
||||
{
|
||||
$elements []= $CurrentBlock['element'];
|
||||
$Elements []= $CurrentBlock['element'];
|
||||
|
||||
$Block['identified'] = true;
|
||||
}
|
||||
@ -222,7 +231,7 @@ class Parsedown
|
||||
}
|
||||
else
|
||||
{
|
||||
$elements []= $CurrentBlock['element'];
|
||||
$Elements []= $CurrentBlock['element'];
|
||||
|
||||
$CurrentBlock = array(
|
||||
'type' => 'Paragraph',
|
||||
@ -236,13 +245,22 @@ class Parsedown
|
||||
}
|
||||
}
|
||||
|
||||
$elements []= $CurrentBlock['element'];
|
||||
# ~
|
||||
|
||||
unset($elements[0]);
|
||||
if (isset($CurrentBlock['incomplete']) and method_exists($this, 'complete'.$CurrentBlock['type']))
|
||||
{
|
||||
$CurrentBlock = $this->{'complete'.$CurrentBlock['type']}($CurrentBlock);
|
||||
}
|
||||
|
||||
# ~
|
||||
|
||||
$markup = $this->elements($elements);
|
||||
$Elements []= $CurrentBlock['element'];
|
||||
|
||||
unset($Elements[0]);
|
||||
|
||||
# ~
|
||||
|
||||
$markup = $this->elements($Elements);
|
||||
|
||||
# ~
|
||||
|
||||
@ -301,22 +319,19 @@ class Parsedown
|
||||
{
|
||||
if (preg_match('/^\[(.+?)\]:[ ]*<?(\S+?)>?(?:[ ]+["\'(](.+)["\')])?[ ]*$/', $Line['text'], $matches))
|
||||
{
|
||||
$label = strtolower($matches[1]);
|
||||
|
||||
$this->references[$label] = array(
|
||||
'url' => $matches[2],
|
||||
$Definition = array(
|
||||
'id' => strtolower($matches[1]),
|
||||
'data' => array(
|
||||
'url' => $matches[2],
|
||||
),
|
||||
);
|
||||
|
||||
if (isset($matches[3]))
|
||||
{
|
||||
$this->references[$label]['title'] = $matches[3];
|
||||
$Definition['data']['title'] = $matches[3];
|
||||
}
|
||||
|
||||
$Block = array(
|
||||
'element' => null,
|
||||
);
|
||||
|
||||
return $Block;
|
||||
return $Definition;
|
||||
}
|
||||
}
|
||||
|
||||
@ -455,9 +470,18 @@ class Parsedown
|
||||
return $Block;
|
||||
}
|
||||
|
||||
$string = htmlspecialchars($Line['body'], ENT_NOQUOTES, 'UTF-8');
|
||||
$Block['element']['text']['text'] .= "\n".$Line['body'];;
|
||||
|
||||
$Block['element']['text']['text'] .= "\n".$string;;
|
||||
return $Block;
|
||||
}
|
||||
|
||||
protected function completeFencedCode($Block)
|
||||
{
|
||||
$text = $Block['element']['text']['text'];
|
||||
|
||||
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
|
||||
|
||||
$Block['element']['text']['text'] = $text;
|
||||
|
||||
return $Block;
|
||||
}
|
||||
@ -522,7 +546,7 @@ class Parsedown
|
||||
|
||||
if ( ! isset($Block['interrupted']))
|
||||
{
|
||||
$text = preg_replace('/^[ ]{0,2}/', '', $Line['body']);
|
||||
$text = preg_replace('/^[ ]{0,4}/', '', $Line['body']);
|
||||
|
||||
$Block['li']['text'] []= $text;
|
||||
|
||||
@ -533,7 +557,7 @@ class Parsedown
|
||||
{
|
||||
$Block['li']['text'] []= '';
|
||||
|
||||
$text = preg_replace('/^[ ]{0,2}/', '', $Line['body']);
|
||||
$text = preg_replace('/^[ ]{0,4}/', '', $Line['body']);
|
||||
|
||||
$Block['li']['text'] []= $text;
|
||||
|
||||
@ -747,7 +771,6 @@ class Parsedown
|
||||
if ($Line['indent'] >= 4)
|
||||
{
|
||||
$text = substr($Line['body'], 4);
|
||||
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
|
||||
|
||||
$Block = array(
|
||||
'element' => array(
|
||||
@ -778,7 +801,6 @@ class Parsedown
|
||||
$Block['element']['text']['text'] .= "\n";
|
||||
|
||||
$text = substr($Line['body'], 4);
|
||||
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
|
||||
|
||||
$Block['element']['text']['text'] .= $text;
|
||||
|
||||
@ -786,6 +808,17 @@ class Parsedown
|
||||
}
|
||||
}
|
||||
|
||||
protected function completeCodeBlock($Block)
|
||||
{
|
||||
$text = $Block['element']['text']['text'];
|
||||
|
||||
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
|
||||
|
||||
$Block['element']['text']['text'] = $text;
|
||||
|
||||
return $Block;
|
||||
}
|
||||
|
||||
#
|
||||
# ~
|
||||
#
|
||||
@ -892,36 +925,38 @@ class Parsedown
|
||||
|
||||
$Span = $this->$handler($markedExcerpt, $text);
|
||||
|
||||
if (isset($Span))
|
||||
if ( ! isset($Span))
|
||||
{
|
||||
# The identified span can be ahead of the marker.
|
||||
|
||||
if (isset($Span['position']) and $Span['position'] > $markerPosition)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
# Spans that start at the position of their marker don't have to set a position.
|
||||
|
||||
if ( ! isset($Span['position']))
|
||||
{
|
||||
$Span['position'] = $markerPosition;
|
||||
}
|
||||
|
||||
$unmarkedText = substr($text, 0, $Span['position']);
|
||||
|
||||
$markup .= $this->readPlainText($unmarkedText);
|
||||
|
||||
$markup .= isset($Span['element']) ? $this->element($Span['element']) : $Span['markup'];
|
||||
|
||||
$text = substr($text, $Span['position'] + $Span['extent']);
|
||||
|
||||
$remainder = $text;
|
||||
|
||||
$markerPosition = 0;
|
||||
|
||||
continue 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
# The identified span can be ahead of the marker.
|
||||
|
||||
if (isset($Span['position']) and $Span['position'] > $markerPosition)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
# Spans that start at the position of their marker don't have to set a position.
|
||||
|
||||
if ( ! isset($Span['position']))
|
||||
{
|
||||
$Span['position'] = $markerPosition;
|
||||
}
|
||||
|
||||
$plainText = substr($text, 0, $Span['position']);
|
||||
|
||||
$markup .= $this->readPlainText($plainText);
|
||||
|
||||
$markup .= isset($Span['element']) ? $this->element($Span['element']) : $Span['markup'];
|
||||
|
||||
$text = substr($text, $Span['position'] + $Span['extent']);
|
||||
|
||||
$remainder = $text;
|
||||
|
||||
$markerPosition = 0;
|
||||
|
||||
continue 2;
|
||||
}
|
||||
|
||||
$remainder = substr($markedExcerpt, 1);
|
||||
@ -1095,9 +1130,9 @@ class Parsedown
|
||||
{
|
||||
$Link['label'] = strtolower($matches[1]);
|
||||
|
||||
if (isset($this->references[$Link['label']]))
|
||||
if (isset($this->definitions['Reference'][$Link['label']]))
|
||||
{
|
||||
$Link += $this->references[$Link['label']];
|
||||
$Link += $this->definitions['Reference'][$Link['label']];
|
||||
|
||||
$extent += strlen($matches[0]);
|
||||
}
|
||||
@ -1106,9 +1141,9 @@ class Parsedown
|
||||
return;
|
||||
}
|
||||
}
|
||||
elseif ($this->references and isset($this->references[$Link['label']]))
|
||||
elseif (isset($this->definitions['Reference'][$Link['label']]))
|
||||
{
|
||||
$Link += $this->references[$Link['label']];
|
||||
$Link += $this->definitions['Reference'][$Link['label']];
|
||||
|
||||
if (preg_match('/^[ ]*\[\]/', $substring, $matches))
|
||||
{
|
||||
@ -1276,7 +1311,7 @@ class Parsedown
|
||||
# Fields
|
||||
#
|
||||
|
||||
protected $references = array(); # » Definitions['reference']
|
||||
protected $definitions;
|
||||
|
||||
#
|
||||
# Read-only
|
||||
|
@ -1,8 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit colors="true">
|
||||
<phpunit bootstrap="test/bootstrap.php" colors="true">
|
||||
<testsuites>
|
||||
<testsuite>
|
||||
<file>tests/Test.php</file>
|
||||
<file>test/Test.php</file>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
</phpunit>
|
@ -1,7 +1,5 @@
|
||||
<?php
|
||||
|
||||
include 'Parsedown.php';
|
||||
|
||||
class Test extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function __construct($name = null, array $data = array(), $dataName = '')
|
3
test/bootstrap.php
Normal file
3
test/bootstrap.php
Normal file
@ -0,0 +1,3 @@
|
||||
<?php
|
||||
|
||||
include 'Parsedown.php';
|
@ -1,8 +1,8 @@
|
||||
<h1>h1</h1>
|
||||
<h2>h2</h2>
|
||||
<h3>h3</h3>
|
||||
<h4>h4</h4>
|
||||
<h5>h5</h5>
|
||||
<h6>h6</h6>
|
||||
<h1>closed h1</h1>
|
||||
<h1>h1</h1>
|
||||
<h2>h2</h2>
|
||||
<h3>h3</h3>
|
||||
<h4>h4</h4>
|
||||
<h5>h5</h5>
|
||||
<h6>h6</h6>
|
||||
<h1>closed h1</h1>
|
||||
<p>#</p>
|
@ -1,15 +1,15 @@
|
||||
# h1
|
||||
|
||||
## h2
|
||||
|
||||
### h3
|
||||
|
||||
#### h4
|
||||
|
||||
##### h5
|
||||
|
||||
###### h6
|
||||
|
||||
# closed h1 #
|
||||
|
||||
# h1
|
||||
|
||||
## h2
|
||||
|
||||
### h3
|
||||
|
||||
#### h4
|
||||
|
||||
##### h5
|
||||
|
||||
###### h6
|
||||
|
||||
# closed h1 #
|
||||
|
||||
#
|
@ -1,8 +1,8 @@
|
||||
<pre><code><?php
|
||||
|
||||
$message = 'Hello World!';
|
||||
echo $message;</code></pre>
|
||||
<hr />
|
||||
<pre><code>> not a quote
|
||||
- not a list item
|
||||
<pre><code><?php
|
||||
|
||||
$message = 'Hello World!';
|
||||
echo $message;</code></pre>
|
||||
<hr />
|
||||
<pre><code>> not a quote
|
||||
- not a list item
|
||||
[not a reference]: http://foo.com</code></pre>
|
@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
$message = 'Hello World!';
|
||||
echo $message;
|
||||
|
||||
---
|
||||
|
||||
> not a quote
|
||||
- not a list item
|
||||
<?php
|
||||
|
||||
$message = 'Hello World!';
|
||||
echo $message;
|
||||
|
||||
---
|
||||
|
||||
> not a quote
|
||||
- not a list item
|
||||
[not a reference]: http://foo.com
|
@ -1,6 +1,6 @@
|
||||
- li
|
||||
- li
|
||||
- li
|
||||
- li
|
||||
- li
|
||||
- li
|
||||
- li
|
||||
- li
|
@ -1,8 +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><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><em>underscore</em>, <em>asterisk</em>, <em>one two</em>, <em>three four</em>, <em>a</em>, <em>b</em></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>
|
@ -1,13 +1,13 @@
|
||||
_underscore_, *asterisk*, _one two_, *three four*, _a_, *b*
|
||||
|
||||
**strong** and *em* and **strong** and *em*
|
||||
|
||||
_line
|
||||
line
|
||||
line_
|
||||
|
||||
this_is_not_an_emphasis
|
||||
|
||||
an empty emphasis __ ** is not an emphasis
|
||||
|
||||
_underscore_, *asterisk*, _one two_, *three four*, _a_, *b*
|
||||
|
||||
**strong** and *em* and **strong** and *em*
|
||||
|
||||
_line
|
||||
line
|
||||
line_
|
||||
|
||||
this_is_not_an_emphasis
|
||||
|
||||
an empty emphasis __ ** is not an emphasis
|
||||
|
||||
*mixed **double and* single asterisk** spans
|
@ -1,5 +1,5 @@
|
||||
<hr />
|
||||
<hr />
|
||||
<hr />
|
||||
<hr />
|
||||
<hr />
|
||||
<hr />
|
||||
<hr />
|
||||
<hr />
|
||||
<hr />
|
@ -1,9 +1,9 @@
|
||||
---
|
||||
|
||||
- - -
|
||||
|
||||
- - -
|
||||
|
||||
***
|
||||
|
||||
---
|
||||
|
||||
- - -
|
||||
|
||||
- - -
|
||||
|
||||
***
|
||||
|
||||
___
|
@ -1,2 +1,2 @@
|
||||
<p>line<br />
|
||||
<p>line<br />
|
||||
line</p>
|
@ -1,2 +1,2 @@
|
||||
line
|
||||
line
|
||||
line
|
9
test/data/separated_nested_list.html
Normal file
9
test/data/separated_nested_list.html
Normal file
@ -0,0 +1,9 @@
|
||||
<ul>
|
||||
<li>
|
||||
<p>li</p>
|
||||
<ul>
|
||||
<li>li</li>
|
||||
<li>li</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
4
test/data/separated_nested_list.md
Normal file
4
test/data/separated_nested_list.md
Normal file
@ -0,0 +1,4 @@
|
||||
- li
|
||||
|
||||
- li
|
||||
- li
|
@ -1,5 +1,5 @@
|
||||
<h1>h1</h1>
|
||||
<h2>h2</h2>
|
||||
<h2>single character</h2>
|
||||
<p>not a header</p>
|
||||
<h1>h1</h1>
|
||||
<h2>h2</h2>
|
||||
<h2>single character</h2>
|
||||
<p>not a header</p>
|
||||
<hr />
|
@ -1,12 +1,12 @@
|
||||
h1
|
||||
==
|
||||
|
||||
h2
|
||||
--
|
||||
|
||||
single character
|
||||
-
|
||||
|
||||
not a header
|
||||
|
||||
h1
|
||||
==
|
||||
|
||||
h2
|
||||
--
|
||||
|
||||
single character
|
||||
-
|
||||
|
||||
not a header
|
||||
|
||||
------------
|
@ -1,6 +1,6 @@
|
||||
<p>AT&T has an ampersand in their name</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>
|
||||
<p><a href="/script?a=1&b=2">inline link</a></p>
|
||||
<p>AT&T has an ampersand in their name</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>
|
||||
<p><a href="/script?a=1&b=2">inline link</a></p>
|
||||
<p><a href="http://example.com/?a=1&b=2">reference link</a></p>
|
@ -1,13 +1,13 @@
|
||||
AT&T has an ampersand in their name
|
||||
|
||||
this & that
|
||||
|
||||
4 < 5 and 6 > 5
|
||||
|
||||
<http://example.com/autolink?a=1&b=2>
|
||||
|
||||
[inline link](/script?a=1&b=2)
|
||||
|
||||
[reference link][1]
|
||||
|
||||
AT&T has an ampersand in their name
|
||||
|
||||
this & that
|
||||
|
||||
4 < 5 and 6 > 5
|
||||
|
||||
<http://example.com/autolink?a=1&b=2>
|
||||
|
||||
[inline link](/script?a=1&b=2)
|
||||
|
||||
[reference link][1]
|
||||
|
||||
[1]: http://example.com/?a=1&b=2
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user