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

529 lines
11 KiB
PHP
Raw Normal View History

2013-07-11 00:22:16 +04:00
<?php
#
#
# Parsedown
# http://parsedown.org
#
# (c) Emanuil Rusev
# http://erusev.com
#
# For the full license information, please view the LICENSE file that was
# distributed with this source code.
#
#
class Parsedown
{
#
# Multiton (http://en.wikipedia.org/wiki/Multiton_pattern)
#
static function instance($name = 'default')
{
if (isset(self::$instances[$name]))
return self::$instances[$name];
$instance = new Parsedown();
self::$instances[$name] = $instance;
return $instance;
}
private static $instances = array();
#
# Fields
#
private $reference_map = array();
private $escape_sequence_map = array();
#
# Public Methods
#
function parse($text)
{
# Removes UTF-8 BOM and marker characters.
$text = preg_replace('{^\xEF\xBB\xBF|\x1A}', '', $text);
# Removes \r characters.
$text = str_replace("\r\n", "\n", $text);
$text = str_replace("\r", "\n", $text);
2013-07-11 00:22:16 +04:00
# 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;
}
}
}
# Extracts link references.
if (preg_match_all('/^[ ]{0,3}\[(.+)\][ ]?:[ ]*\n?[ ]*(.+)$/m', $text, $matches, PREG_SET_ORDER))
{
foreach ($matches as $matches)
{
2013-08-31 20:55:07 +04:00
$this->reference_map[strtolower($matches[1])] = $matches[2];
2013-07-11 00:22:16 +04:00
$text = str_replace($matches[0], '', $text);
}
}
# ~
$text = trim($text, "\n");
$text = preg_replace('/\n\s*\n/', "\n\n", $text);
$text = $this->parse_lines($text);
2013-07-11 00:22:16 +04:00
# Decodes escape sequences (leaves out backslashes).
foreach ($this->escape_sequence_map as $code => $escape_sequence)
{
$text = str_replace($code, $escape_sequence[1], $text);
}
$text = rtrim($text, "\n");
return $text;
}
#
# Private Methods
#
private function parse_lines($text, $context = null)
2013-07-11 00:22:16 +04:00
{
$lines = explode("\n", $text);
2013-09-20 00:12:48 +04:00
$lines []= null;
2013-07-11 00:22:16 +04:00
$line_count = count($lines);
2013-07-11 00:22:16 +04:00
$markup = '';
foreach ($lines as $index => $line)
2013-07-11 00:22:16 +04:00
{
# ~
2013-07-11 00:22:16 +04:00
if (isset($line) and $line !== '' and $line[0] >= 'A')
2013-07-11 00:22:16 +04:00
{
$simple_line = $line;
2013-07-11 00:22:16 +04:00
unset($line);
2013-07-11 00:22:16 +04:00
}
# Setext Heading (-)
2013-07-11 00:22:16 +04:00
if (isset($line) and $line !== '' and isset($paragraph) and preg_match('/^[-]+[ ]*$/', $line))
2013-07-11 00:22:16 +04:00
{
$setext_heading_text = $this->parse_inline_elements($paragraph);
2013-07-11 00:22:16 +04:00
$markup .= '<h2>'.$setext_heading_text.'</h2>'."\n";
2013-07-11 00:22:16 +04:00
unset($paragraph, $line);
2013-07-11 00:22:16 +04:00
continue;
}
# Rule
2013-07-11 00:22:16 +04:00
if (isset($line) and preg_match('/^[ ]{0,3}([-*_])([ ]{0,2}\1){2,}[ ]*$/', $line))
2013-07-11 00:22:16 +04:00
{
$rule = true;
2013-07-11 00:22:16 +04:00
unset($line);
}
elseif (isset($rule))
2013-07-11 00:22:16 +04:00
{
$markup .= '<hr />'."\n";
2013-07-11 00:22:16 +04:00
unset($rule);
2013-07-11 00:22:16 +04:00
}
# List
# Unlike other types, consequent lines of type "list items" may not
# belong to the same block.
if (isset($line) and $line !== '' and preg_match('/^([ ]{0,3})(\d+[.]|[*+-])[ ](.*)/', $line, $matches)) # list item
2013-07-11 00:22:16 +04:00
{
$list_item_indentation = $matches[1];
2013-07-11 00:22:16 +04:00
$list_item_type = ($matches[2] === '-' or $matches[2] === '+' or $matches[2] === '*')
? 'ul'
: 'ol';
if (isset($list_items)) # subsequent
2013-07-11 00:22:16 +04:00
{
if ($list_item_indentation === $list_indentation and $list_item_type === $list_type)
{
$list_items []= $list_item;
2013-07-11 00:22:16 +04:00
$list_item = $matches[3];
}
else
{
$list_item .= "\n".$line;
}
}
else # first
{
$list_indentation = $list_item_indentation;
$list_type = $list_item_type;
$list_item = $matches[3];
$list_items = array();
2013-07-11 00:22:16 +04:00
}
unset($line);
}
elseif (isset($list_items)) # incomplete list item
2013-07-11 00:22:16 +04:00
{
if (isset($line) and ($line === '' or $line[0] === ' '))
2013-07-11 00:22:16 +04:00
{
$line and $line = preg_replace('/^[ ]{0,4}/', '', $line);;
$list_item .= "\n".$line;
unset($line);
}
else # line is consumed or does not belong to the list item
{
$list_item = rtrim($list_item, "\n");
$list_items []= $list_item;
2013-07-11 00:22:16 +04:00
$markup .= '<'.$list_type.'>'."\n";
foreach ($list_items as $list_item)
2013-07-11 00:22:16 +04:00
{
$list_item_text = strpos($list_item, "\n") !== false
? $this->parse_lines($list_item, 'li')
2013-07-11 00:22:16 +04:00
: $this->parse_inline_elements($list_item);
$markup .= '<li>'.$list_item_text.'</li>'."\n";
}
$markup .= '</'.$list_type.'>'."\n";
unset($list_items);
2013-07-11 00:22:16 +04:00
}
}
# Code Block
2013-07-11 00:22:16 +04:00
if (isset($line) and $line !== '' and preg_match('/^[ ]{4}(.*)/', $line, $matches))
2013-07-11 00:22:16 +04:00
{
if (isset($code_block))
2013-07-11 00:22:16 +04:00
{
$code_block .= "\n".$matches[1];
2013-07-11 00:22:16 +04:00
}
else
2013-07-11 00:22:16 +04:00
{
$code_block = $matches[1];
2013-07-11 00:22:16 +04:00
}
unset($line);
}
elseif (isset($code_block))
2013-07-11 00:22:16 +04:00
{
if (isset($line) and $line === '')
{
$code_block .= "\n";
# » continue;
}
else
2013-07-11 00:22:16 +04:00
{
$code_block = rtrim($code_block);
2013-07-11 00:22:16 +04:00
$code_block_text = htmlentities($code_block, ENT_NOQUOTES);
2013-07-11 00:22:16 +04:00
# Decodes encoded escape sequences if present.
strpos($code_block_text, "\x1A\\") !== FALSE and $code_block_text = strtr($code_block_text, $this->escape_sequence_map);
$markup .= '<pre><code>'.$code_block_text.'</code></pre>'."\n";
unset($code_block);
2013-07-11 00:22:16 +04:00
}
}
2013-09-20 00:28:12 +04:00
# Blockquote
2013-07-11 00:22:16 +04:00
if (isset($line) and $line !== '' and preg_match('/^[ ]*>[ ]?(.*)/', $line, $matches))
2013-07-11 00:22:16 +04:00
{
if (isset($blockquote))
{
$blockquote .= "\n".$matches[1];
}
else
{
$blockquote = $matches[1];
}
2013-07-11 00:22:16 +04:00
unset($line);
}
elseif (isset($blockquote))
{
if (isset($line) and $line === '')
{
$blockquote .= "\n";
}
else
{
$blockquote = $this->parse_lines($blockquote);
$markup .= '<blockquote>'."\n".$blockquote.'</blockquote>'."\n";
unset($blockquote);
}
2013-07-11 00:22:16 +04:00
}
# Atx Heading
if (isset($line) and $line !== '' and $line[0] === '#' and preg_match('/^(#{1,6})[ ]*(.+?)[ ]*#*$/', $line, $matches))
2013-07-11 00:22:16 +04:00
{
$atx_heading_level = strlen($matches[1]);
$atx_heading = $this->parse_inline_elements($matches[2]);
unset($line);
}
elseif (isset($atx_heading))
{
$markup .= '<h'.$atx_heading_level.'>'.$atx_heading.'</h'.$atx_heading_level.'>'."\n";
unset($atx_heading);
}
2013-07-11 00:22:16 +04:00
# Setext Heading (=)
2013-07-11 00:22:16 +04:00
if (isset($line) and $line !== '' and isset($paragraph) and preg_match('/^[=]+[ ]*$/', $line))
2013-07-11 00:22:16 +04:00
{
$setext_heading_text = $this->parse_inline_elements($paragraph);
2013-07-11 00:22:16 +04:00
$markup .= '<h1>'.$setext_heading_text.'</h1>'."\n";
unset($paragraph, $line);
continue;
2013-07-11 00:22:16 +04:00
}
# Paragraph
if (isset($simple_line))
2013-07-11 00:22:16 +04:00
{
$line = $simple_line;
2013-07-11 00:22:16 +04:00
unset($simple_line);
2013-07-11 00:22:16 +04:00
}
if (isset($line) and $line !== '')
2013-07-11 00:22:16 +04:00
{
substr($line, -2) === ' ' and $line = substr_replace($line, '<br />', -2);
2013-07-11 00:22:16 +04:00
if (isset($paragraph))
{
$paragraph .= "\n".$line;
}
else
{
$paragraph = $line;
}
}
elseif (isset($paragraph))
2013-07-11 00:22:16 +04:00
{
$paragraph_text = $this->parse_inline_elements($paragraph);
if ($context === 'li')
2013-07-11 00:22:16 +04:00
{
if ( ! $markup and $index + 1 === $line_count)
{
$text_is_simple = true;
}
else
{
$markup or $markup .= "\n";
}
2013-07-11 00:22:16 +04:00
$markup .= isset($text_is_simple)
2013-07-26 01:08:52 +04:00
? $paragraph_text
: '<p>'.$paragraph_text.'</p>'."\n";
2013-07-11 00:22:16 +04:00
}
else
{
$markup .= '<p>'.$paragraph_text.'</p>'."\n";
}
unset($paragraph);
2013-07-11 00:22:16 +04:00
}
}
return $markup;
}
private function parse_inline_elements($text)
{
$map = array();
$index = 0;
# Code Span
if (strpos($text, '`') !== FALSE and preg_match_all('/`(.+?)`/', $text, $matches, PREG_SET_ORDER))
{
foreach ($matches as $matches)
{
$element_text = $matches[1];
$element_text = htmlentities($element_text, ENT_NOQUOTES);
# Decodes escape sequences.
$this->escape_sequence_map
and strpos($element_text, "\x1A") !== FALSE
and $element_text = strtr($element_text, $this->escape_sequence_map);
# Composes element.
$element = '<code>'.$element_text.'</code>';
# Encodes element.
$code = "\x1A".'$'.$index;
$text = str_replace($matches[0], $code, $text);
$map[$code] = $element;
$index ++;
}
}
# Inline Link / Image
if (strpos($text, '](') !== FALSE and preg_match_all('/(!?)(\[((?:[^][]+|(?2))*)\])\((.*?)\)/', $text, $matches, PREG_SET_ORDER)) # inline
{
foreach ($matches as $matches)
{
if ($matches[1]) # image
{
$element = '<img alt="'.$matches[3].'" src="'.$matches[4].'">';
}
else
{
$element_text = $this->parse_inline_elements($matches[3]);
$element = '<a href="'.$matches[4].'">'.$element_text.'</a>';
}
$element_text = $this->parse_inline_elements($matches[1]);
# ~
$code = "\x1A".'$'.$index;
$text = str_replace($matches[0], $code, $text);
$map[$code] = $element;
$index ++;
}
}
2013-07-11 00:22:16 +04:00
# Reference(d) Link / Image
if ($this->reference_map and strpos($text, '[') !== FALSE and preg_match_all('/(!?)\[(.+?)\](?:\n?[ ]?\[(.*?)\])?/ms', $text, $matches, PREG_SET_ORDER))
2013-07-11 00:22:16 +04:00
{
foreach ($matches as $matches)
{
2013-08-31 20:55:07 +04:00
$link_difinition = isset($matches[3]) && $matches[3]
? $matches[3]
: $matches[2]; # implicit
$link_difinition = strtolower($link_difinition);
if (isset($this->reference_map[$link_difinition]))
2013-07-11 00:22:16 +04:00
{
2013-08-31 20:55:07 +04:00
$url = $this->reference_map[$link_difinition];
2013-07-11 00:22:16 +04:00
if ($matches[1]) # image
{
$element = '<img alt="'.$matches[2].'" src="'.$url.'">';
}
else # anchor
{
$element_text = $this->parse_inline_elements($matches[2]);
$element = '<a href="'.$url.'">'.$element_text.'</a>';
}
# ~
$code = "\x1A".'$'.$index;
$text = str_replace($matches[0], $code, $text);
$map[$code] = $element;
$index ++;
}
}
}
if (strpos($text, '<') !== FALSE and preg_match_all('/<((https?|ftp|dict):[^\^\s]+?)>/i', $text, $matches, PREG_SET_ORDER))
{
foreach ($matches as $matches)
{
$element = '<a href=":href">:text</a>';
$element = str_replace(':text', $matches[1], $element);
$element = str_replace(':href', $matches[1], $element);
# ~
$code = "\x1A".'$'.$index;
$text = str_replace($matches[0], $code, $text);
$map[$code] = $element;
$index ++;
}
}
if (strpos($text, '_') !== FALSE)
2013-07-11 00:22:16 +04:00
{
$text = preg_replace('/__(?=\S)(.+?)(?<=\S)__/', '<strong>$1</strong>', $text);
$text = preg_replace('/_(?=\S)(.+?)(?<=\S)_/', '<em>$1</em>', $text);
2013-07-11 00:22:16 +04:00
}
if (strpos($text, '*') !== FALSE)
{
$text = preg_replace('/\*\*(?=\S)(.+?)(?<=\S)\*\*/', '<strong>$1</strong>', $text);
$text = preg_replace('/\*(?=\S)(.+?)(?<=\S)\*/', '<em>$1</em>', $text);
}
2013-07-11 00:22:16 +04:00
$text = strtr($text, $map);
return $text;
}
}