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

1327 lines
32 KiB
PHP
Raw Normal View History

2013-11-09 01:40:00 +04:00
<?php
2013-07-11 00:22:16 +04:00
2013-11-09 01:40:00 +04:00
#
#
2013-07-11 00:22:16 +04:00
# Parsedown
# http://parsedown.org
2013-11-09 01:40:00 +04:00
#
# (c) Emanuil Rusev
2013-07-11 00:22:16 +04:00
# http://erusev.com
2013-11-09 01:40:00 +04:00
#
2014-01-24 03:28:03 +04:00
# For the full license information, view the LICENSE file that was distributed
# with this source code.
2013-11-09 01:40:00 +04:00
#
#
2013-07-11 00:22:16 +04:00
class Parsedown
{
2014-02-21 04:23:17 +04:00
#
# Philosophy
2014-02-21 04:23:17 +04:00
# Markdown is intended to be easy-to-read by humans - those of us who read
# line by line, left to right, top to bottom. In order to take advantage of
# this, Parsedown tries to read in a similar way. It breaks texts into
# lines, it iterates through them and it looks at how they start and relate
# to each other.
#
2014-04-17 11:59:35 +04:00
# ~
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
function text($text)
{
2014-01-24 02:41:45 +04:00
# standardize line breaks
$text = str_replace("\r\n", "\n", $text);
$text = str_replace("\r", "\n", $text);
2013-11-09 01:40:00 +04:00
2014-01-24 02:41:45 +04:00
# replace tabs with spaces
$text = str_replace("\t", ' ', $text);
2013-11-09 01:40:00 +04:00
2014-01-24 02:41:45 +04:00
# remove surrounding line breaks
$text = trim($text, "\n");
2013-11-09 01:40:00 +04:00
2014-01-24 02:41:45 +04:00
# split text into lines
$lines = explode("\n", $text);
2013-11-09 01:40:00 +04:00
# iterate through lines to identify blocks
2014-04-17 11:59:35 +04:00
$markup = $this->lines($lines);
2013-11-09 01:40:00 +04:00
# trim line breaks
$markup = trim($markup, "\n");
# clean up
2014-04-27 02:54:52 +04:00
$this->definitions = array();
return $markup;
}
2013-11-09 01:40:00 +04:00
#
2014-04-17 11:59:35 +04:00
# Setters
#
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
function setBreaksEnabled($breaksEnabled)
{
2014-04-17 11:59:35 +04:00
$this->breaksEnabled = $breaksEnabled;
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
return $this;
}
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
private $breaksEnabled;
2014-04-17 11:59:35 +04:00
#
# Blocks
#
2014-04-17 11:59:35 +04:00
protected $blockMarkers = array(
'#' => array('Atx'),
'*' => array('Rule', 'List'),
'+' => array('List'),
'-' => array('Setext', 'Table', 'Rule', 'List'),
'0' => array('List'),
'1' => array('List'),
'2' => array('List'),
'3' => array('List'),
'4' => array('List'),
'5' => array('List'),
'6' => array('List'),
'7' => array('List'),
'8' => array('List'),
'9' => array('List'),
2014-04-18 01:19:22 +04:00
':' => array('Table'),
2014-04-17 11:59:35 +04:00
'<' => array('Markup'),
'=' => array('Setext'),
'>' => array('Quote'),
'[' => array('Reference'),
'_' => array('Rule'),
'`' => array('FencedCode'),
'|' => array('Table'),
'~' => array('FencedCode'),
);
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
protected $definitionMarkers = array(
'[' => array('Reference'),
);
2014-04-17 11:59:35 +04:00
protected $unmarkedBlockTypes = array(
'CodeBlock',
);
2014-04-17 11:59:35 +04:00
private function lines(array $lines)
{
$CurrentBlock = null;
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
foreach ($lines as $line)
{
2014-04-28 03:27:05 +04:00
if (chop($line) === '')
2014-04-17 11:59:35 +04:00
{
2014-04-28 03:27:05 +04:00
if (isset($CurrentBlock))
2014-04-17 11:59:35 +04:00
{
2014-04-28 03:27:05 +04:00
$CurrentBlock['interrupted'] = true;
2014-04-17 11:59:35 +04:00
}
2014-04-28 03:27:05 +04:00
continue;
}
$indent = 0;
while (isset($line[$indent]) and $line[$indent] === ' ')
{
$indent ++;
2014-04-17 11:59:35 +04:00
}
2014-04-17 11:59:35 +04:00
$text = $indent > 0 ? substr($line, $indent) : $line;
2014-04-17 11:59:35 +04:00
# ~
2014-04-17 11:59:35 +04:00
$Line = array('body' => $line, 'indent' => $indent, 'text' => $text);
2014-04-17 11:59:35 +04:00
# Multiline block types define "addTo" methods.
2014-04-17 11:59:35 +04:00
if (isset($CurrentBlock['incomplete']))
{
$Block = $this->{'addTo'.$CurrentBlock['type']}($Line, $CurrentBlock);
2014-04-17 11:59:35 +04:00
if (isset($Block))
{
$CurrentBlock = $Block;
2014-04-17 11:59:35 +04:00
continue;
}
else
{
if (method_exists($this, 'complete'.$CurrentBlock['type']))
{
2014-04-17 11:59:35 +04:00
$CurrentBlock = $this->{'complete'.$CurrentBlock['type']}($CurrentBlock);
}
unset($CurrentBlock['incomplete']);
2014-04-17 11:59:35 +04:00
}
}
2014-04-17 11:59:35 +04:00
# ~
2014-04-17 11:59:35 +04:00
$marker = $text[0];
2013-11-09 01:40:00 +04:00
2014-04-27 02:54:52 +04:00
# 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;
}
}
}
# ~
$blockTypes = $this->unmarkedBlockTypes;
2014-04-17 11:59:35 +04:00
if (isset($this->blockMarkers[$marker]))
{
foreach ($this->blockMarkers[$marker] as $blockType)
{
$blockTypes []= $blockType;
}
}
2014-04-17 11:59:35 +04:00
#
# ~
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
foreach ($blockTypes as $blockType)
{
# Block types define "identify" methods.
2014-04-17 11:59:35 +04:00
$Block = $this->{'identify'.$blockType}($Line, $CurrentBlock);
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
if (isset($Block))
{
$Block['type'] = $blockType;
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
if ( ! isset($Block['identified'])) # »
{
2014-04-25 00:58:00 +04:00
$Elements []= $CurrentBlock['element'];
2014-04-17 11:59:35 +04:00
$Block['identified'] = true;
}
2013-11-10 00:23:56 +04:00
2014-04-17 11:59:35 +04:00
# Multiline block types define "addTo" methods.
2014-04-17 11:59:35 +04:00
if (method_exists($this, 'addTo'.$blockType))
{
2014-04-17 11:59:35 +04:00
$Block['incomplete'] = true;
}
2014-04-17 11:59:35 +04:00
$CurrentBlock = $Block;
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
continue 2;
}
}
2014-04-17 11:59:35 +04:00
# ~
2014-04-17 11:59:35 +04:00
if ($CurrentBlock['type'] === 'Paragraph' and ! isset($CurrentBlock['interrupted']))
{
$CurrentBlock['element']['text'] .= "\n".$text;
}
else
{
2014-04-25 00:58:00 +04:00
$Elements []= $CurrentBlock['element'];
2014-04-17 11:59:35 +04:00
$CurrentBlock = array(
'type' => 'Paragraph',
'identified' => true,
'element' => array(
'name' => 'p',
'text' => $text,
'handler' => 'line',
),
);
}
}
# ~
if (isset($CurrentBlock['incomplete']) and method_exists($this, 'complete'.$CurrentBlock['type']))
{
$CurrentBlock = $this->{'complete'.$CurrentBlock['type']}($CurrentBlock);
}
# ~
2014-04-25 00:58:00 +04:00
$Elements []= $CurrentBlock['element'];
2014-04-25 00:58:00 +04:00
unset($Elements[0]);
2014-04-17 11:59:35 +04:00
# ~
2014-04-25 00:58:00 +04:00
$markup = $this->elements($Elements);
2014-04-17 11:59:35 +04:00
# ~
2014-04-17 11:59:35 +04:00
return $markup;
}
2014-04-17 11:59:35 +04:00
#
# Atx
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
protected function identifyAtx($Line)
{
if (isset($Line['text'][1]))
{
$level = 1;
2014-04-17 11:59:35 +04:00
while (isset($Line['text'][$level]) and $Line['text'][$level] === '#')
{
$level ++;
}
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
$text = trim($Line['text'], '# ');
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
$Block = array(
'element' => array(
'name' => 'h'.$level,
'text' => $text,
'handler' => 'line',
),
);
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
return $Block;
}
}
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
#
# Rule
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
protected function identifyRule($Line)
{
if (preg_match('/^(['.$Line['text'][0].'])([ ]{0,2}\1){2,}[ ]*$/', $Line['text']))
{
$Block = array(
'element' => array(
'name' => 'hr'
),
);
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
return $Block;
}
}
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
#
# Reference
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
protected function identifyReference($Line)
{
if (preg_match('/^\[(.+?)\]:[ ]*<?(\S+?)>?(?:[ ]+["\'(](.+)["\')])?[ ]*$/', $Line['text'], $matches))
{
2014-04-27 02:54:52 +04:00
$Definition = array(
'id' => strtolower($matches[1]),
'data' => array(
'url' => $matches[2],
),
2014-04-17 11:59:35 +04:00
);
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
if (isset($matches[3]))
{
2014-04-27 02:54:52 +04:00
$Definition['data']['title'] = $matches[3];
2014-04-17 11:59:35 +04:00
}
2014-02-23 20:55:34 +04:00
2014-04-27 02:54:52 +04:00
return $Definition;
2014-04-17 11:59:35 +04:00
}
}
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
#
# Setext
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
protected function identifySetext($Line, array $Block = null)
{
if ( ! isset($Block) or $Block['type'] !== 'Paragraph' or isset($Block['interrupted']))
{
return;
}
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
if (chop($Line['text'], $Line['text'][0]) === '')
{
$Block['element']['name'] = $Line['text'][0] === '=' ? 'h1' : 'h2';
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
return $Block;
}
}
2014-04-17 11:59:35 +04:00
#
# Markup
2014-04-17 11:59:35 +04:00
protected function identifyMarkup($Line)
{
if (preg_match('/^<(\w[\w\d]*)(?:[ ][^>\/]*)?(\/?)[ ]*>/', $Line['text'], $matches))
{
if (in_array($matches[1], $this->textLevelElements))
{
return;
}
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
$Block = array(
'element' => $Line['body'],
);
2014-04-17 11:59:35 +04:00
if ($matches[2] or $matches[1] === 'hr' or preg_match('/<\/'.$matches[1].'>[ ]*$/', $Line['text']))
{
$Block['closed'] = true;
}
else
{
$Block['depth'] = 0;
$Block['start'] = '<'.$matches[1].'>';
$Block['end'] = '</'.$matches[1].'>';
}
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
return $Block;
}
}
2014-01-17 02:36:11 +04:00
2014-04-17 11:59:35 +04:00
protected function addToMarkup($Line, array $Block)
{
if (isset($Block['closed']))
{
return;
}
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
if (stripos($Line['text'], $Block['start']) !== false) # opening tag
{
$Block['depth'] ++;
}
2014-04-17 11:59:35 +04:00
if (stripos($Line['text'], $Block['end']) !== false) # closing tag
{
if ($Block['depth'] > 0)
{
$Block['depth'] --;
}
else
{
$Block['closed'] = true;
}
}
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
$Block['element'] .= "\n".$Line['body'];
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
return $Block;
}
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
#
# Fenced Code
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
protected function identifyFencedCode($Line)
{
if (preg_match('/^(['.$Line['text'][0].']{3,})[ ]*(\w+)?[ ]*$/', $Line['text'], $matches))
{
$Element = array(
'name' => 'code',
'text' => '',
);
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
if (isset($matches[2]))
{
$class = 'language-'.$matches[2];
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
$Element['attributes'] = array(
'class' => $class,
);
}
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
$Block = array(
'char' => $Line['text'][0],
'element' => array(
'name' => 'pre',
'handler' => 'element',
'text' => $Element,
),
);
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
return $Block;
}
}
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
protected function addToFencedCode($Line, $Block)
{
if (isset($Block['complete']))
{
return;
}
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
if (isset($Block['interrupted']))
{
$Block['element']['text']['text'] .= "\n";
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
unset($Block['interrupted']);
}
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
if (preg_match('/^'.$Block['char'].'{3,}[ ]*$/', $Line['text']))
{
$Block['element']['text']['text'] = substr($Block['element']['text']['text'], 1);
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
$Block['complete'] = true;
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
return $Block;
}
2014-02-23 20:55:34 +04:00
$string = htmlspecialchars($Line['body'], ENT_NOQUOTES, 'UTF-8');
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
$Block['element']['text']['text'] .= "\n".$string;;
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
return $Block;
}
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
#
# List
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
protected function identifyList($Line)
{
list($name, $pattern) = $Line['text'][0] <= '-' ? array('ul', '[*+-]') : array('ol', '[0-9]+[.]');
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
if (preg_match('/^('.$pattern.'[ ]+)(.*)/', $Line['text'], $matches))
{
$Block = array(
'indent' => $Line['indent'],
'pattern' => $pattern,
'element' => array(
'name' => $name,
'handler' => 'elements',
),
);
$Block['li'] = array(
'name' => 'li',
'handler' => 'li',
'text' => array(
$matches[2],
),
);
$Block['element']['text'] []= & $Block['li'];
return $Block;
}
}
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
protected function addToList($Line, array $Block)
{
if ($Block['indent'] === $Line['indent'] and preg_match('/^'.$Block['pattern'].'[ ]+(.*)/', $Line['text'], $matches))
{
if (isset($Block['interrupted']))
{
$Block['li']['text'] []= '';
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
unset($Block['interrupted']);
}
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
unset($Block['li']);
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
$Block['li'] = array(
'name' => 'li',
'handler' => 'li',
'text' => array(
$matches[1],
),
);
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
$Block['element']['text'] []= & $Block['li'];
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
return $Block;
}
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
if ( ! isset($Block['interrupted']))
{
$text = preg_replace('/^[ ]{0,2}/', '', $Line['body']);
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
$Block['li']['text'] []= $text;
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
return $Block;
}
2014-02-23 20:55:34 +04:00
2014-04-17 11:59:35 +04:00
if ($Line['indent'] > 0)
{
$Block['li']['text'] []= '';
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
$text = preg_replace('/^[ ]{0,2}/', '', $Line['body']);
2014-04-17 11:59:35 +04:00
$Block['li']['text'] []= $text;
2014-04-17 11:59:35 +04:00
unset($Block['interrupted']);
2014-04-17 11:59:35 +04:00
return $Block;
}
}
2014-04-17 11:59:35 +04:00
#
# Quote
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
protected function identifyQuote($Line)
{
if (preg_match('/^>[ ]?(.*)/', $Line['text'], $matches))
{
$Block = array(
'element' => array(
'name' => 'blockquote',
'handler' => 'lines',
'text' => (array) $matches[1],
),
);
return $Block;
}
}
2014-04-17 11:59:35 +04:00
protected function addToQuote($Line, array $Block)
{
if ($Line['text'][0] === '>' and preg_match('/^>[ ]?(.*)/', $Line['text'], $matches))
{
if (isset($Block['interrupted']))
{
$Block['element']['text'] []= '';
}
2014-04-17 11:59:35 +04:00
$Block['element']['text'] []= $matches[1];
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
return $Block;
}
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
if ( ! isset($Block['interrupted']))
{
$Block['element']['text'] []= $Line['text'];
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
return $Block;
}
}
2014-04-17 11:59:35 +04:00
#
# Table
2014-04-17 11:59:35 +04:00
protected function identifyTable($Line, array $Block = null)
{
if ( ! isset($Block) or $Block['type'] !== 'Paragraph' or isset($Block['interrupted']))
{
return;
}
2014-04-17 11:59:35 +04:00
if (strpos($Block['element']['text'], '|') !== false and chop($Line['text'], ' -:|') === '')
{
$alignments = array();
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
$divider = $Line['text'];
2014-04-17 11:59:35 +04:00
$divider = trim($divider);
$divider = trim($divider, '|');
2013-11-10 00:23:56 +04:00
2014-04-17 11:59:35 +04:00
$dividerCells = explode('|', $divider);
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
foreach ($dividerCells as $dividerCell)
{
$dividerCell = trim($dividerCell);
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
if ($dividerCell === '')
{
continue;
}
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
$alignment = null;
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
if ($dividerCell[0] === ':')
{
$alignment = 'left';
}
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
if (substr($dividerCell, -1) === ':')
{
$alignment = $alignment === 'left' ? 'center' : 'right';
}
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
$alignments []= $alignment;
}
2014-04-17 11:59:35 +04:00
# ~
2014-04-17 11:59:35 +04:00
$HeaderElements = array();
2014-04-17 11:59:35 +04:00
$header = $Block['element']['text'];
2014-04-17 11:59:35 +04:00
$header = trim($header);
$header = trim($header, '|');
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
$headerCells = explode('|', $header);
2014-02-05 16:03:23 +04:00
2014-04-17 11:59:35 +04:00
foreach ($headerCells as $index => $headerCell)
{
$headerCell = trim($headerCell);
2013-11-02 23:42:55 +04:00
2014-04-17 11:59:35 +04:00
$HeaderElement = array(
'name' => 'th',
'text' => $headerCell,
'handler' => 'line',
);
2014-02-05 16:03:23 +04:00
2014-04-17 11:59:35 +04:00
if (isset($alignments[$index]))
{
$alignment = $alignments[$index];
2014-02-05 16:03:23 +04:00
2014-04-17 11:59:35 +04:00
$HeaderElement['attributes'] = array(
'align' => $alignment,
);
}
2014-02-05 16:03:23 +04:00
2014-04-17 11:59:35 +04:00
$HeaderElements []= $HeaderElement;
}
2014-02-05 16:03:23 +04:00
2014-04-17 11:59:35 +04:00
# ~
2013-12-26 23:53:48 +04:00
2014-04-17 11:59:35 +04:00
$Block = array(
'alignments' => $alignments,
'identified' => true,
'element' => array(
'name' => 'table',
'handler' => 'elements',
),
);
$Block['element']['text'] []= array(
'name' => 'thead',
'handler' => 'elements',
);
$Block['element']['text'] []= array(
'name' => 'tbody',
'handler' => 'elements',
'text' => array(),
);
$Block['element']['text'][0]['text'] []= array(
'name' => 'tr',
'handler' => 'elements',
'text' => $HeaderElements,
);
return $Block;
}
}
2014-02-05 16:03:23 +04:00
2014-04-17 11:59:35 +04:00
protected function addToTable($Line, array $Block)
{
if ($Line['text'][0] === '|' or strpos($Line['text'], '|'))
{
$Elements = array();
2014-02-05 16:03:23 +04:00
2014-04-17 11:59:35 +04:00
$row = $Line['text'];
2014-02-05 16:03:23 +04:00
2014-04-17 11:59:35 +04:00
$row = trim($row);
$row = trim($row, '|');
2014-02-05 16:03:23 +04:00
2014-04-17 11:59:35 +04:00
$cells = explode('|', $row);
2014-02-06 04:36:22 +04:00
2014-04-17 11:59:35 +04:00
foreach ($cells as $index => $cell)
{
$cell = trim($cell);
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
$Element = array(
'name' => 'td',
'handler' => 'line',
'text' => $cell,
);
2014-02-05 16:03:23 +04:00
2014-04-17 11:59:35 +04:00
if (isset($Block['alignments'][$index]))
{
$Element['attributes'] = array(
'align' => $Block['alignments'][$index],
);
}
2014-04-17 11:59:35 +04:00
$Elements []= $Element;
}
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
$Element = array(
'name' => 'tr',
'handler' => 'elements',
'text' => $Elements,
);
2014-04-17 11:59:35 +04:00
$Block['element']['text'][1]['text'] []= $Element;
2014-04-17 11:59:35 +04:00
return $Block;
}
}
2014-04-17 11:59:35 +04:00
#
# Code
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
protected function identifyCodeBlock($Line)
{
if ($Line['indent'] >= 4)
{
$text = substr($Line['body'], 4);
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
$Block = array(
'element' => array(
'name' => 'pre',
'handler' => 'element',
'text' => array(
'name' => 'code',
'text' => $text,
),
),
);
2014-04-17 11:59:35 +04:00
return $Block;
}
}
2014-04-17 11:59:35 +04:00
protected function addToCodeBlock($Line, $Block)
{
if ($Line['indent'] >= 4)
{
if (isset($Block['interrupted']))
{
2014-04-17 11:59:35 +04:00
$Block['element']['text']['text'] .= "\n";
2014-02-03 00:27:22 +04:00
2014-04-17 11:59:35 +04:00
unset($Block['interrupted']);
}
2014-04-17 11:59:35 +04:00
$Block['element']['text']['text'] .= "\n";
2014-04-17 11:59:35 +04:00
$text = substr($Line['body'], 4);
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
$Block['element']['text']['text'] .= $text;
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
return $Block;
}
}
2014-02-03 00:27:22 +04:00
2014-04-17 11:59:35 +04:00
#
# ~
#
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
private function element(array $Element)
{
$markup = '<'.$Element['name'];
2014-04-17 11:59:35 +04:00
if (isset($Element['attributes']))
{
foreach ($Element['attributes'] as $name => $value)
{
$markup .= ' '.$name.'="'.$value.'"';
}
2014-04-17 11:59:35 +04:00
}
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
if (isset($Element['text']))
{
$markup .= '>';
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
if (isset($Element['handler']))
{
$markup .= $this->$Element['handler']($Element['text']);
}
else
{
2014-04-17 11:59:35 +04:00
$markup .= $Element['text'];
}
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
$markup .= '</'.$Element['name'].'>';
}
else
{
2014-04-17 11:59:35 +04:00
$markup .= ' />';
}
2013-11-17 00:04:26 +04:00
2014-04-17 11:59:35 +04:00
return $markup;
}
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
private function elements(array $Elements)
2014-02-21 04:22:31 +04:00
{
$markup = '';
2014-04-17 11:59:35 +04:00
foreach ($Elements as $Element)
2014-02-21 04:22:31 +04:00
{
2014-04-17 11:59:35 +04:00
if ($Element === null)
2014-02-21 04:22:31 +04:00
{
2014-04-17 11:59:35 +04:00
continue;
2014-02-21 04:22:31 +04:00
}
2014-04-17 11:59:35 +04:00
$markup .= "\n";
2014-02-21 04:22:31 +04:00
2014-04-17 11:59:35 +04:00
if (is_string($Element)) # because of markup
{
$markup .= $Element;
2014-02-21 04:22:31 +04:00
2014-04-17 11:59:35 +04:00
continue;
2014-02-21 04:22:31 +04:00
}
2014-04-17 11:59:35 +04:00
$markup .= $this->element($Element);
2014-02-21 04:22:31 +04:00
}
$markup .= "\n";
return $markup;
}
2014-04-17 11:59:35 +04:00
#
# Spans
#
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
protected $spanMarkers = array(
'!' => array('Link'), # ?
'&' => array('Ampersand'),
'*' => array('Emphasis'),
'/' => array('Url'),
'<' => array('UrlTag', 'EmailTag', 'Tag', 'LessThan'),
'[' => array('Link'),
'_' => array('Emphasis'),
'`' => array('InlineCode'),
'~' => array('Strikethrough'),
'\\' => array('EscapeSequence'),
);
2014-04-17 11:59:35 +04:00
protected $spanMarkerList = '*_!&[</`~\\';
2014-04-17 11:59:35 +04:00
public function line($text)
{
$markup = '';
2014-04-17 11:59:35 +04:00
$remainder = $text;
2014-04-17 11:59:35 +04:00
$markerPosition = 0;
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
while ($markedExcerpt = strpbrk($remainder, $this->spanMarkerList))
{
$marker = $markedExcerpt[0];
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
$markerPosition += strpos($remainder, $marker);
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
foreach ($this->spanMarkers[$marker] as $spanType)
{
2014-04-17 11:59:35 +04:00
$handler = 'identify'.$spanType;
2014-01-20 11:26:25 +04:00
2014-04-17 11:59:35 +04:00
$Span = $this->$handler($markedExcerpt, $text);
2014-01-20 11:26:25 +04:00
2014-04-28 03:10:18 +04:00
if ( ! isset($Span))
2014-04-17 11:59:35 +04:00
{
2014-04-28 03:10:18 +04:00
continue;
}
2014-01-20 11:26:25 +04:00
2014-04-28 03:10:18 +04:00
# The identified span can be ahead of the marker.
if (isset($Span['position']) and $Span['position'] > $markerPosition)
{
continue;
}
2014-01-20 11:26:25 +04:00
2014-04-28 03:10:18 +04:00
# Spans that start at the position of their marker don't have to set a position.
2014-04-28 03:10:18 +04:00
if ( ! isset($Span['position']))
{
$Span['position'] = $markerPosition;
}
2014-04-28 03:14:46 +04:00
$plainText = substr($text, 0, $Span['position']);
2014-04-28 03:14:46 +04:00
$markup .= $this->readPlainText($plainText);
2014-01-21 00:19:23 +04:00
2014-04-28 03:10:18 +04:00
$markup .= isset($Span['element']) ? $this->element($Span['element']) : $Span['markup'];
2014-01-21 00:19:23 +04:00
2014-04-28 03:10:18 +04:00
$text = substr($text, $Span['position'] + $Span['extent']);
2014-04-28 03:10:18 +04:00
$remainder = $text;
2014-01-21 00:49:10 +04:00
2014-04-28 03:10:18 +04:00
$markerPosition = 0;
2014-01-21 00:49:10 +04:00
2014-04-28 03:10:18 +04:00
continue 2;
2014-04-17 11:59:35 +04:00
}
2014-04-17 11:59:35 +04:00
$remainder = substr($markedExcerpt, 1);
2013-07-11 00:22:16 +04:00
2014-04-17 11:59:35 +04:00
$markerPosition ++;
}
2014-04-17 11:59:35 +04:00
$markup .= $this->readPlainText($text);
2014-04-17 11:59:35 +04:00
return $markup;
}
2014-04-17 11:59:35 +04:00
#
# ~
#
2014-04-17 11:59:35 +04:00
protected function identifyUrl($excerpt, $text)
{
if ( ! isset($excerpt[1]) or $excerpt[1] !== '/')
{
return;
}
2014-04-17 11:59:35 +04:00
if (preg_match('/\bhttps?:[\/]{2}[^\s]+\b\/*/ui', $text, $matches, PREG_OFFSET_CAPTURE))
{
$url = str_replace(array('&', '<'), array('&amp;', '&lt;'), $matches[0][0]);
return array(
'extent' => strlen($matches[0][0]),
'position' => $matches[0][1],
'element' => array(
'name' => 'a',
'text' => $url,
'attributes' => array(
'href' => $url,
),
),
);
}
}
2014-04-17 11:59:35 +04:00
protected function identifyAmpersand($excerpt)
{
if ( ! preg_match('/^&#?\w+;/', $excerpt))
{
return array(
'markup' => '&amp;',
'extent' => 1,
);
}
}
2014-04-17 11:59:35 +04:00
protected function identifyStrikethrough($excerpt)
{
if ( ! isset($excerpt[1]))
{
return;
}
2014-04-17 11:59:35 +04:00
if ($excerpt[1] === $excerpt[0] and preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $excerpt, $matches))
{
return array(
'extent' => strlen($matches[0]),
'element' => array(
'name' => 'del',
'text' => $matches[1],
'handler' => 'line',
),
);
}
}
2014-04-17 11:59:35 +04:00
protected function identifyEscapeSequence($excerpt)
{
if (in_array($excerpt[1], $this->specialCharacters))
{
return array(
'markup' => $excerpt[1],
'extent' => 2,
);
}
}
2014-04-17 11:59:35 +04:00
protected function identifyLessThan()
{
return array(
'markup' => '&lt;',
'extent' => 1,
);
}
2014-04-17 11:59:35 +04:00
protected function identifyUrlTag($excerpt)
{
if (strpos($excerpt, '>') !== false and preg_match('/^<(https?:[\/]{2}[^\s]+?)>/i', $excerpt, $matches))
{
$url = str_replace(array('&', '<'), array('&amp;', '&lt;'), $matches[1]);
return array(
'extent' => strlen($matches[0]),
'element' => array(
'name' => 'a',
'text' => $url,
'attributes' => array(
'href' => $url,
),
),
);
}
}
2014-04-17 11:59:35 +04:00
protected function identifyEmailTag($excerpt)
{
if (strpos($excerpt, '>') !== false and preg_match('/<(\S+?@\S+?)>/', $excerpt, $matches))
{
return array(
'extent' => strlen($matches[0]),
'element' => array(
'name' => 'a',
'text' => $matches[1],
'attributes' => array(
'href' => 'mailto:'.$matches[1],
),
),
);
}
}
2014-04-17 11:59:35 +04:00
protected function identifyTag($excerpt)
{
if (strpos($excerpt, '>') !== false and preg_match('/^<\/?\w.*?>/', $excerpt, $matches))
{
return array(
'markup' => $matches[0],
'extent' => strlen($matches[0]),
);
}
}
2014-04-17 11:59:35 +04:00
protected function identifyInlineCode($excerpt)
{
$marker = $excerpt[0];
2014-04-17 11:59:35 +04:00
if (preg_match('/^('.$marker.'+)[ ]*(.+?)[ ]*(?<!'.$marker.')\1(?!'.$marker.')/', $excerpt, $matches))
{
$text = $matches[2];
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
return array(
'extent' => strlen($matches[0]),
'element' => array(
'name' => 'code',
'text' => $text,
),
);
}
}
2014-04-17 11:59:35 +04:00
protected function identifyLink($excerpt)
{
$extent = $excerpt[0] === '!' ? 1 : 0;
2014-04-17 11:59:35 +04:00
if (strpos($excerpt, ']') and preg_match('/\[((?:[^][]|(?R))*)\]/', $excerpt, $matches))
{
$Link = array('text' => $matches[1], 'label' => strtolower($matches[1]));
2014-04-17 11:59:35 +04:00
$extent += strlen($matches[0]);
2014-01-20 00:49:43 +04:00
2014-04-17 11:59:35 +04:00
$substring = substr($excerpt, $extent);
2014-01-20 00:49:43 +04:00
2014-04-17 11:59:35 +04:00
if (preg_match('/^\s*\[(.+?)\]/', $substring, $matches))
{
$Link['label'] = strtolower($matches[1]);
2014-01-20 00:49:43 +04:00
2014-04-27 02:54:52 +04:00
if (isset($this->definitions['Reference'][$Link['label']]))
2014-04-17 11:59:35 +04:00
{
2014-04-27 02:54:52 +04:00
$Link += $this->definitions['Reference'][$Link['label']];
2014-01-20 00:49:43 +04:00
2014-04-17 11:59:35 +04:00
$extent += strlen($matches[0]);
}
else
{
return;
}
}
2014-04-27 02:54:52 +04:00
elseif (isset($this->definitions['Reference'][$Link['label']]))
2014-04-17 11:59:35 +04:00
{
2014-04-27 02:54:52 +04:00
$Link += $this->definitions['Reference'][$Link['label']];
2014-01-20 00:49:43 +04:00
2014-04-17 11:59:35 +04:00
if (preg_match('/^[ ]*\[\]/', $substring, $matches))
{
$extent += strlen($matches[0]);
}
}
elseif (preg_match('/^\([ ]*(.*?)(?:[ ]+[\'"](.+?)[\'"])?[ ]*\)/', $substring, $matches))
{
$Link['url'] = $matches[1];
2014-04-17 11:59:35 +04:00
if (isset($matches[2]))
{
$Link['title'] = $matches[2];
}
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
$extent += strlen($matches[0]);
}
else
{
return;
}
}
else
{
return;
}
2014-04-17 11:59:35 +04:00
$url = str_replace(array('&', '<'), array('&amp;', '&lt;'), $Link['url']);
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
if ($excerpt[0] === '!')
{
$Element = array(
'name' => 'img',
'attributes' => array(
'alt' => $Link['text'],
'src' => $url,
),
);
}
else
{
$Element = array(
'name' => 'a',
'handler' => 'line',
'text' => $Link['text'],
'attributes' => array(
'href' => $url,
),
);
}
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
if (isset($Link['title']))
{
$Element['attributes']['title'] = $Link['title'];
}
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
return array(
'extent' => $extent,
'element' => $Element,
);
}
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
protected function identifyEmphasis($excerpt)
{
if ( ! isset($excerpt[1]))
{
return;
}
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
$marker = $excerpt[0];
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
if ($excerpt[1] === $marker and preg_match($this->strongRegex[$marker], $excerpt, $matches))
{
$emphasis = 'strong';
}
elseif (preg_match($this->emRegex[$marker], $excerpt, $matches))
{
$emphasis = 'em';
}
else
{
return;
}
2013-11-22 02:20:45 +04:00
2014-04-17 11:59:35 +04:00
return array(
'extent' => strlen($matches[0]),
'element' => array(
'name' => $emphasis,
'handler' => 'line',
'text' => $matches[1],
),
);
}
2013-11-22 02:20:45 +04:00
2014-04-17 11:59:35 +04:00
#
# ~
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
protected function readPlainText($text)
{
$breakMarker = $this->breaksEnabled ? "\n" : " \n";
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
$text = str_replace($breakMarker, "<br />\n", $text);
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
return $text;
}
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
#
# ~
#
2013-11-21 15:39:00 +04:00
2014-04-17 11:59:35 +04:00
protected function li($lines)
{
$markup = $this->lines($lines);
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
$trimmedMarkup = trim($markup);
2014-04-17 11:59:35 +04:00
if ( ! in_array('', $lines) and substr($trimmedMarkup, 0, 3) === '<p>')
{
$markup = $trimmedMarkup;
$markup = substr($markup, 3);
2013-11-09 01:40:00 +04:00
2014-04-17 11:59:35 +04:00
$position = strpos($markup, "</p>");
$markup = substr_replace($markup, '', $position, 4);
}
2013-11-09 01:40:00 +04:00
return $markup;
}
2014-01-18 17:10:24 +04:00
2014-02-21 04:22:31 +04:00
#
2014-04-17 11:59:35 +04:00
# Multiton
#
2014-02-21 04:22:31 +04:00
static function instance($name = 'default')
{
if (isset(self::$instances[$name]))
{
return self::$instances[$name];
}
2014-04-17 11:59:35 +04:00
$instance = new self();
2014-02-21 04:22:31 +04:00
self::$instances[$name] = $instance;
return $instance;
}
private static $instances = array();
#
# Deprecated Methods
#
/**
2014-04-17 11:59:35 +04:00
* @deprecated in favor of "text"
*/
2014-04-17 11:59:35 +04:00
function parse($text)
{
2014-04-17 11:59:35 +04:00
$markup = $this->text($text);
return $markup;
}
#
2014-01-26 21:53:24 +04:00
# Fields
#
2014-04-27 02:54:52 +04:00
protected $definitions;
2014-01-26 21:53:24 +04:00
#
# Read-only
2014-01-18 17:10:24 +04:00
2014-04-17 11:59:35 +04:00
protected $specialCharacters = array(
'\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!',
);
protected $strongRegex = array(
'*' => '/^[*]{2}((?:[^*]|[*][^*]*[*])+?)[*]{2}(?![*])/s',
'_' => '/^__((?:[^_]|_[^_]*_)+?)__(?!_)/us',
);
2014-01-18 17:10:24 +04:00
2014-04-17 11:59:35 +04:00
protected $emRegex = array(
'*' => '/^[*]((?:[^*]|[*][*][^*]+?[*][*])+?)[*](?![*])/s',
'_' => '/^_((?:[^_]|__[^_]*__)+?)_(?!_)\b/us',
);
2014-01-27 02:58:18 +04:00
2014-04-17 11:59:35 +04:00
protected $textLevelElements = array(
2014-01-27 02:58:18 +04:00
'a', 'br', 'bdo', 'abbr', 'blink', 'nextid', 'acronym', 'basefont',
'b', 'em', 'big', 'cite', 'small', 'spacer', 'listing',
'i', 'rp', 'sub', 'code', 'strike', 'marquee',
'q', 'rt', 'sup', 'font', 'strong',
's', 'tt', 'var', 'mark',
'u', 'xm', 'wbr', 'nobr',
'ruby',
'span',
'time',
);
}