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

Compare commits

...

14 Commits
1.4.5 ... 1.5.0

3 changed files with 268 additions and 282 deletions

View File

@ -15,15 +15,10 @@
class Parsedown
{
#
# Philosophy
# ~
# Parsedown recognises that the Markdown syntax is optimised for humans so
# it tries to read like one. It goes through text line by line. It looks at
# how lines start to identify blocks. It looks for special characters to
# identify inline elements.
const version = '1.5.0';
#
# ~
function text($text)
@ -53,8 +48,6 @@ class Parsedown
# Setters
#
private $breaksEnabled;
function setBreaksEnabled($breaksEnabled)
{
$this->breaksEnabled = $breaksEnabled;
@ -62,7 +55,7 @@ class Parsedown
return $this;
}
private $markupEscaped;
protected $breaksEnabled;
function setMarkupEscaped($markupEscaped)
{
@ -71,7 +64,7 @@ class Parsedown
return $this;
}
private $urlsLinked = true;
protected $markupEscaped;
function setUrlsLinked($urlsLinked)
{
@ -80,6 +73,8 @@ class Parsedown
return $this;
}
protected $urlsLinked = true;
#
# Lines
#
@ -534,7 +529,7 @@ class Parsedown
protected function blockListContinue($Line, array $Block)
{
if ($Block['indent'] === $Line['indent'] and preg_match('/^'.$Block['pattern'].'[ ]+(.*)/', $Line['text'], $matches))
if ($Block['indent'] === $Line['indent'] and preg_match('/^'.$Block['pattern'].'(?:[ ]+(.*)|$)/', $Line['text'], $matches))
{
if (isset($Block['interrupted']))
{
@ -545,11 +540,13 @@ class Parsedown
unset($Block['li']);
$text = isset($matches[1]) ? $matches[1] : '';
$Block['li'] = array(
'name' => 'li',
'handler' => 'li',
'text' => array(
$matches[1],
$text,
),
);
@ -855,7 +852,7 @@ class Parsedown
$alignment = $alignments[$index];
$HeaderElement['attributes'] = array(
'align' => $alignment,
'style' => 'text-align: '.$alignment.';',
);
}
@ -925,7 +922,7 @@ class Parsedown
if (isset($Block['alignments'][$index]))
{
$Element['attributes'] = array(
'align' => $Block['alignments'][$index],
'style' => 'text-align: '.$Block['alignments'][$index].';',
);
}
@ -961,75 +958,18 @@ class Parsedown
return $Block;
}
#
# ~
#
protected function element(array $Element)
{
$markup = '<'.$Element['name'];
if (isset($Element['attributes']))
{
foreach ($Element['attributes'] as $name => $value)
{
if ($value === null)
{
continue;
}
$markup .= ' '.$name.'="'.$value.'"';
}
}
if (isset($Element['text']))
{
$markup .= '>';
if (isset($Element['handler']))
{
$markup .= $this->$Element['handler']($Element['text']);
}
else
{
$markup .= $Element['text'];
}
$markup .= '</'.$Element['name'].'>';
}
else
{
$markup .= ' />';
}
return $markup;
}
protected function elements(array $Elements)
{
$markup = '';
foreach ($Elements as $Element)
{
$markup .= "\n" . $this->element($Element);
}
$markup .= "\n";
return $markup;
}
#
# Inline Elements
#
protected $InlineTypes = array(
'"' => array('QuotationMark'),
'"' => array('SpecialCharacter'),
'!' => array('Image'),
'&' => array('Ampersand'),
'&' => array('SpecialCharacter'),
'*' => array('Emphasis'),
'<' => array('UrlTag', 'EmailTag', 'Tag', 'LessThan'),
'>' => array('GreaterThan'),
':' => array('Url'),
'<' => array('UrlTag', 'EmailTag', 'Markup', 'SpecialCharacter'),
'>' => array('SpecialCharacter'),
'[' => array('Link'),
'_' => array('Emphasis'),
'`' => array('Code'),
@ -1039,7 +979,7 @@ class Parsedown
# ~
protected $inlineMarkerList = '!"*_&[<>`~\\';
protected $inlineMarkerList = '!"*_&[:<>`~\\';
#
# ~
@ -1049,43 +989,53 @@ class Parsedown
{
$markup = '';
$remainder = $text;
$unexaminedText = $text;
$markerPosition = 0;
while ($excerpt = strpbrk($remainder, $this->inlineMarkerList))
while ($excerpt = strpbrk($unexaminedText, $this->inlineMarkerList))
{
$marker = $excerpt[0];
$markerPosition += strpos($remainder, $marker);
$markerPosition += strpos($unexaminedText, $marker);
$Excerpt = array('text' => $excerpt, 'context' => $text);
foreach ($this->InlineTypes[$marker] as $inlineType)
{
$handler = 'inline'.$inlineType;
$Inline = $this->$handler($excerpt);
$Inline = $this->{'inline'.$inlineType}($Excerpt);
if ( ! isset($Inline))
{
continue;
}
$plainText = substr($text, 0, $markerPosition);
if (isset($Inline['position']) and $Inline['position'] > $markerPosition) # position is ahead of marker
{
continue;
}
$markup .= $this->unmarkedText($plainText);
if ( ! isset($Inline['position']))
{
$Inline['position'] = $markerPosition;
}
$unmarkedText = substr($text, 0, $Inline['position']);
$markup .= $this->unmarkedText($unmarkedText);
$markup .= isset($Inline['markup']) ? $Inline['markup'] : $this->element($Inline['element']);
$text = substr($text, $markerPosition + $Inline['extent']);
$text = substr($text, $Inline['position'] + $Inline['extent']);
$remainder = $text;
$unexaminedText = $text;
$markerPosition = 0;
continue 2;
}
$remainder = substr($excerpt, 1);
$unexaminedText = substr($excerpt, 1);
$markerPosition ++;
}
@ -1099,94 +1049,29 @@ class Parsedown
# ~
#
protected function inlineAmpersand($excerpt)
protected function inlineCode($Excerpt)
{
if ( ! preg_match('/^&#?\w+;/', $excerpt))
{
return array(
'markup' => '&amp;',
'extent' => 1,
);
}
}
$marker = $Excerpt['text'][0];
protected function inlineStrikethrough($excerpt)
if (preg_match('/^('.$marker.'+)[ ]*(.+?)[ ]*(?<!'.$marker.')\1(?!'.$marker.')/s', $Excerpt['text'], $matches))
{
if ( ! isset($excerpt[1]))
{
return;
}
if ($excerpt[1] === '~' and preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $excerpt, $matches))
{
return array(
'extent' => strlen($matches[0]),
'element' => array(
'name' => 'del',
'text' => $matches[1],
'handler' => 'line',
),
);
}
}
protected function inlineEscapeSequence($excerpt)
{
if (isset($excerpt[1]) and in_array($excerpt[1], $this->specialCharacters))
{
return array(
'markup' => $excerpt[1],
'extent' => 2,
);
}
}
protected function inlineLessThan()
{
return array(
'markup' => '&lt;',
'extent' => 1,
);
}
protected function inlineGreaterThan()
{
return array(
'markup' => '&gt;',
'extent' => 1,
);
}
protected function inlineQuotationMark()
{
return array(
'markup' => '&quot;',
'extent' => 1,
);
}
protected function inlineUrlTag($excerpt)
{
if (strpos($excerpt, '>') !== false and preg_match('/^<(https?:[\/]{2}[^\s]+?)>/i', $excerpt, $matches))
{
$url = str_replace(array('&', '<'), array('&amp;', '&lt;'), $matches[1]);
$text = $matches[2];
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
$text = preg_replace("/[ ]*\n/", ' ', $text);
return array(
'extent' => strlen($matches[0]),
'element' => array(
'name' => 'a',
'text' => $url,
'attributes' => array(
'href' => $url,
),
'name' => 'code',
'text' => $text,
),
);
}
}
protected function inlineEmailTag($excerpt)
protected function inlineEmailTag($Excerpt)
{
if (strpos($excerpt, '>') !== false and preg_match('/^<((mailto:)?\S+?@\S+?)>/i', $excerpt, $matches))
if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<((mailto:)?\S+?@\S+?)>/i', $Excerpt['text'], $matches))
{
$url = $matches[1];
@ -1208,77 +1093,84 @@ class Parsedown
}
}
protected function inlineTag($excerpt)
protected function inlineEmphasis($Excerpt)
{
if ($this->markupEscaped)
if ( ! isset($Excerpt['text'][1]))
{
return;
}
if (strpos($excerpt, '>') !== false and preg_match('/^<\/?\w.*?>/s', $excerpt, $matches))
{
return array(
'markup' => $matches[0],
'extent' => strlen($matches[0]),
);
}
}
$marker = $Excerpt['text'][0];
protected function inlineCode($excerpt)
if ($Excerpt['text'][1] === $marker and preg_match($this->StrongRegex[$marker], $Excerpt['text'], $matches))
{
$marker = $excerpt[0];
if (preg_match('/^('.$marker.'+)[ ]*(.+?)[ ]*(?<!'.$marker.')\1(?!'.$marker.')/s', $excerpt, $matches))
$emphasis = 'strong';
}
elseif (preg_match($this->EmRegex[$marker], $Excerpt['text'], $matches))
{
$text = $matches[2];
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
$text = preg_replace("/[ ]*\n/", ' ', $text);
$emphasis = 'em';
}
else
{
return;
}
return array(
'extent' => strlen($matches[0]),
'element' => array(
'name' => 'code',
'text' => $text,
'name' => $emphasis,
'handler' => 'line',
'text' => $matches[1],
),
);
}
protected function inlineEscapeSequence($Excerpt)
{
if (isset($Excerpt['text'][1]) and in_array($Excerpt['text'][1], $this->specialCharacters))
{
return array(
'markup' => $Excerpt['text'][1],
'extent' => 2,
);
}
}
protected function inlineImage($excerpt)
protected function inlineImage($Excerpt)
{
if ( ! isset($excerpt[1]) or $excerpt[1] !== '[')
if ( ! isset($Excerpt['text'][1]) or $Excerpt['text'][1] !== '[')
{
return;
}
$excerpt = substr($excerpt, 1);
$Excerpt['text']= substr($Excerpt['text'], 1);
$InlineLink = $this->inlineLink($excerpt);
$Link = $this->inlineLink($Excerpt);
if ($InlineLink === null)
if ($Link === null)
{
return;
}
$Inline = array(
'extent' => $InlineLink['extent'] + 1,
'extent' => $Link['extent'] + 1,
'element' => array(
'name' => 'img',
'attributes' => array(
'src' => $InlineLink['element']['attributes']['href'],
'alt' => $InlineLink['element']['text'],
'src' => $Link['element']['attributes']['href'],
'alt' => $Link['element']['text'],
),
),
);
$Inline['element']['attributes'] += $InlineLink['element']['attributes'];
$Inline['element']['attributes'] += $Link['element']['attributes'];
unset($Inline['element']['attributes']['href']);
return $Inline;
}
protected function inlineLink($excerpt)
protected function inlineLink($Excerpt)
{
$Element = array(
'name' => 'a',
@ -1292,7 +1184,7 @@ class Parsedown
$extent = 0;
$remainder = $excerpt;
$remainder = $Excerpt['text'];
if (preg_match('/\[((?:[^][]|(?R))*)\]/', $remainder, $matches))
{
@ -1351,37 +1243,122 @@ class Parsedown
);
}
protected function inlineEmphasis($excerpt)
protected function inlineMarkup($Excerpt)
{
if ( ! isset($excerpt[1]))
if ($this->markupEscaped or strpos($Excerpt['text'], '>') === false)
{
return;
}
$marker = $excerpt[0];
if ($Excerpt['text'][1] === '/' and preg_match('/^<\/\w*[ ]*>/s', $Excerpt['text'], $matches))
{
return array(
'markup' => $matches[0],
'extent' => strlen($matches[0]),
);
}
if ($excerpt[1] === $marker and preg_match($this->StrongRegex[$marker], $excerpt, $matches))
if ($Excerpt['text'][1] === '!' and preg_match('/^<!---?[^>-](?:-?[^-])*-->/s', $Excerpt['text'], $matches))
{
$emphasis = 'strong';
return array(
'markup' => $matches[0],
'extent' => strlen($matches[0]),
);
}
elseif (preg_match($this->EmRegex[$marker], $excerpt, $matches))
if ($Excerpt['text'][1] !== ' ' and preg_match('/^<\w*(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*\/?>/s', $Excerpt['text'], $matches))
{
$emphasis = 'em';
return array(
'markup' => $matches[0],
'extent' => strlen($matches[0]),
);
}
else
}
protected function inlineSpecialCharacter($Excerpt)
{
if ($Excerpt['text'][0] === '&' and ! preg_match('/^&#?\w+;/', $Excerpt['text']))
{
return array(
'markup' => '&amp;',
'extent' => 1,
);
}
$SpecialCharacter = array('>' => 'gt', '<' => 'lt', '"' => 'quot');
if (isset($SpecialCharacter[$Excerpt['text'][0]]))
{
return array(
'markup' => '&'.$SpecialCharacter[$Excerpt['text'][0]].';',
'extent' => 1,
);
}
}
protected function inlineStrikethrough($Excerpt)
{
if ( ! isset($Excerpt['text'][1]))
{
return;
}
if ($Excerpt['text'][1] === '~' and preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $Excerpt['text'], $matches))
{
return array(
'extent' => strlen($matches[0]),
'element' => array(
'name' => 'del',
'text' => $matches[1],
'handler' => 'line',
),
);
}
}
protected function inlineUrl($Excerpt)
{
if ($this->urlsLinked !== true or ! isset($Excerpt['text'][2]) or $Excerpt['text'][2] !== '/')
{
return;
}
if (preg_match('/\bhttps?:[\/]{2}[^\s<]+\b\/*/ui', $Excerpt['context'], $matches, PREG_OFFSET_CAPTURE))
{
$Inline = array(
'extent' => strlen($matches[0][0]),
'position' => $matches[0][1],
'element' => array(
'name' => 'a',
'text' => $matches[0][0],
'attributes' => array(
'href' => $matches[0][0],
),
),
);
return $Inline;
}
}
protected function inlineUrlTag($Excerpt)
{
if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<(\w+:\/{2}[^ >]+)>/i', $Excerpt['text'], $matches))
{
$url = str_replace(array('&', '<'), array('&amp;', '&lt;'), $matches[1]);
return array(
'extent' => strlen($matches[0]),
'element' => array(
'name' => $emphasis,
'handler' => 'line',
'text' => $matches[1],
'name' => 'a',
'text' => $url,
'attributes' => array(
'href' => $url,
),
),
);
}
}
#
# ~
@ -1391,19 +1368,6 @@ class Parsedown
# ~
protected function unmarkedText($text)
{
foreach ($this->unmarkedInlineTypes as $snippet => $inlineType)
{
if (strpos($text, $snippet) !== false)
{
$text = $this->{'unmarkedInline'.$inlineType}($text);
}
}
return $text;
}
protected function unmarkedInlineBreak($text)
{
if ($this->breaksEnabled)
{
@ -1418,38 +1382,65 @@ class Parsedown
return $text;
}
protected function unmarkedInlineUrl($text)
{
if ($this->urlsLinked !== true)
{
return $text;
}
$re = '/\bhttps?:[\/]{2}[^\s<]+\b\/*/ui';
$offset = 0;
while (strpos($text, '://', $offset) and preg_match($re, $text, $matches, PREG_OFFSET_CAPTURE, $offset))
{
$url = $matches[0][0];
$urlLength = strlen($url);
$urlPosition = $matches[0][1];
$markup = '<a href="'.$url.'">'.$url.'</a>';
$markupLength = strlen($markup);
$text = substr_replace($text, $markup, $urlPosition, $urlLength);
$offset = $urlPosition + $markupLength;
}
return $text;
}
#
# Handlers
#
protected function element(array $Element)
{
$markup = '<'.$Element['name'];
if (isset($Element['attributes']))
{
foreach ($Element['attributes'] as $name => $value)
{
if ($value === null)
{
continue;
}
$markup .= ' '.$name.'="'.$value.'"';
}
}
if (isset($Element['text']))
{
$markup .= '>';
if (isset($Element['handler']))
{
$markup .= $this->$Element['handler']($Element['text']);
}
else
{
$markup .= $Element['text'];
}
$markup .= '</'.$Element['name'].'>';
}
else
{
$markup .= ' />';
}
return $markup;
}
protected function elements(array $Elements)
{
$markup = '';
foreach ($Elements as $Element)
{
$markup .= "\n" . $this->element($Element);
}
$markup .= "\n";
return $markup;
}
# ~
#
protected function li($lines)
{
@ -1471,7 +1462,18 @@ class Parsedown
}
#
# Multiton
# Deprecated Methods
#
function parse($text)
{
$markup = $this->text($text);
return $markup;
}
#
# Static Methods
#
static function instance($name = 'default')
@ -1490,22 +1492,6 @@ class Parsedown
private static $instances = array();
#
# Deprecated Methods
#
/**
* @deprecated in favor of "text"
* @param $text
* @return string
*/
function parse($text)
{
$markup = $this->text($text);
return $markup;
}
#
# Fields
#
@ -1513,7 +1499,7 @@ class Parsedown
protected $DefinitionData;
#
# Read-only
# Read-Only
protected $specialCharacters = array(
'\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!', '|',

View File

@ -1,21 +1,21 @@
<table>
<thead>
<tr>
<th align="left">header 1</th>
<th align="center">header 2</th>
<th align="right">header 2</th>
<th style="text-align: left;">header 1</th>
<th style="text-align: center;">header 2</th>
<th style="text-align: right;">header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">cell 1.1</td>
<td align="center">cell 1.2</td>
<td align="right">cell 1.3</td>
<td style="text-align: left;">cell 1.1</td>
<td style="text-align: center;">cell 1.2</td>
<td style="text-align: right;">cell 1.3</td>
</tr>
<tr>
<td align="left">cell 2.1</td>
<td align="center">cell 2.2</td>
<td align="right">cell 2.3</td>
<td style="text-align: left;">cell 2.1</td>
<td style="text-align: center;">cell 2.2</td>
<td style="text-align: right;">cell 2.3</td>
</tr>
</tbody>
</table>

View File

@ -20,17 +20,17 @@
<table>
<thead>
<tr>
<th align="left">header 1</th>
<th style="text-align: left;">header 1</th>
<th>header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">cell 1.1</td>
<td style="text-align: left;">cell 1.1</td>
<td>cell 1.2</td>
</tr>
<tr>
<td align="left">cell 2.1</td>
<td style="text-align: left;">cell 2.1</td>
<td>cell 2.2</td>
</tr>
</tbody>