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

Compare commits

...

5 Commits
0.8.2 ... 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
5 changed files with 57 additions and 53 deletions

View File

@ -37,11 +37,11 @@ class Parsedown
# Setters
#
private $break_marker = " \n";
private $breaks_enabled = false;
function set_breaks_enabled($breaks_enabled)
{
$this->break_marker = $breaks_enabled ? "\n" : " \n";
$this->breaks_enabled = $breaks_enabled;
return $this;
}
@ -51,7 +51,6 @@ class Parsedown
#
private $reference_map = array();
private $escape_sequence_map = array();
#
# Public Methods
@ -66,25 +65,6 @@ 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 = trim($text, "\n");
@ -93,15 +73,6 @@ class Parsedown
$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;
@ -111,8 +82,6 @@ class Parsedown
# Private Methods
#
private function parse_block_elements(array $lines, $context = '')
{
$elements = array();
@ -551,6 +520,8 @@ class Parsedown
}
else
{
$this->breaks_enabled and $element['text'] .= ' ';
$element['text'] .= "\n".$line;
}
}
@ -610,17 +581,22 @@ class Parsedown
break;
case 'code block':
$text = htmlspecialchars($element['text'], ENT_NOQUOTES, 'UTF-8');
$markup .= '<pre><code>'.$text.'</code></pre>'."\n";
break;
case 'fenced block':
$text = htmlspecialchars($element['text'], ENT_NOQUOTES, 'UTF-8');
strpos($text, "\x1A\\") !== FALSE and $text = strtr($text, $this->escape_sequence_map);
$markup .= '<pre><code';
$markup .= isset($element['language'])
? '<pre><code class="language-'.$element['language'].'">'.$text.'</code></pre>'
: '<pre><code>'.$text.'</code></pre>';
isset($element['language']) and $markup .= ' class="language-'.$element['language'].'"';
$markup .= "\n";
$markup .= '>'.$text.'</code></pre>'."\n";
break;
@ -675,9 +651,9 @@ class Parsedown
return $markup;
}
private function parse_span_elements($text, $markers = array('![', '&', '*', '<', '[', '_', '`', 'http', '~~'))
private function parse_span_elements($text, $markers = array(" \n", '![', '&', '*', '<', '[', '\\', '_', '`', 'http', '~~'))
{
if (isset($text[2]) === false or $markers === array())
if (isset($text[1]) === false or $markers === array())
{
return $text;
}
@ -713,7 +689,7 @@ class Parsedown
# ~
if ($closest_marker === null or isset($text[$closest_marker_position + 2]) === false)
if ($closest_marker === null or isset($text[$closest_marker_position + 1]) === false)
{
$markup .= $text;
@ -734,6 +710,14 @@ class Parsedown
switch ($closest_marker)
{
case " \n":
$markup .= '<br />'."\n";
$offset = 3;
break;
case '![':
case '[':
@ -825,9 +809,18 @@ class Parsedown
case '&':
$markup .= '&amp;';
if (preg_match('/^&#?\w+;/', $text, $matches))
{
$markup .= $matches[0];
$offset = substr($text, 0, 5) === '&amp;' ? 5 : 1;
$offset = strlen($matches[0]);
}
else
{
$markup .= '&amp;';
$offset = 1;
}
break;
@ -914,6 +907,23 @@ class Parsedown
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))
@ -921,11 +931,6 @@ class Parsedown
$element_text = $matches[1];
$element_text = htmlspecialchars($element_text, ENT_NOQUOTES, 'UTF-8');
if ($this->escape_sequence_map and strpos($element_text, "\x1A") !== false)
{
$element_text = strtr($element_text, $this->escape_sequence_map);
}
$markup .= '<code>'.$element_text.'</code>';
$offset = strlen($matches[0]);
@ -988,8 +993,6 @@ class Parsedown
$markers[$closest_marker_index] = $closest_marker;
}
$markup = str_replace($this->break_marker, '<br />'."\n", $markup);
return $markup;
}
@ -1004,6 +1007,8 @@ class Parsedown
'span', 'strong', 'sub', 'sup', 'textarea', 'tt', 'var',
);
private $special_characters = array('\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!');
# ~
private $strong_regex = array(

View File

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

View File

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

View File

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

View File

@ -1,7 +1,5 @@
AT&T has an ampersand in their name
AT&T is another way to write it
this & that
4 < 5 and 6 > 5