Skip to content

Commit

Permalink
Merge branch 'markdown-extra'
Browse files Browse the repository at this point in the history
* markdown-extra:
  finished table support for GFM and markdown Extra
  Markdown Extra tables
  added context information to the parser
  markdown extra special attributes on code blocks
  implemented markdown extra attributes
  created MarkdownExtra class
  tests for gfm tables

Conflicts:
	Parser.php
	phpunit.xml.dist
  • Loading branch information
cebe committed Jun 11, 2014
2 parents 78463a0 + 1106f6d commit 7dd5e50
Show file tree
Hide file tree
Showing 23 changed files with 882 additions and 63 deletions.
36 changes: 33 additions & 3 deletions GithubMarkdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,57 @@

namespace cebe\markdown;

use cebe\markdown\block\TableTrait;

/**
* Markdown parser for github flavored markdown.
*
* @author Carsten Brandt <mail@cebe.cc>
*/
class GithubMarkdown extends Markdown
{
use TableTrait;

/**
* @var boolean whether to interpret newlines as `<br />`-tags.
* This feature is useful for comments where newlines are often meant to be real new lines.
*/
public $enableNewlines = false;

/**
* @inheritDoc
*/
protected $escapeCharacters = [
// from Markdown
'\\', // backslash
'`', // backtick
'*', // asterisk
'_', // underscore
'{', '}', // curly braces
'[', ']', // square brackets
'(', ')', // parentheses
'#', // hash mark
'+', // plus sign
'-', // minus sign (hyphen)
'.', // dot
'!', // exclamation mark
'<', '>',
// added by MarkdownExtra
':', // colon
'|', // pipe
];

/**
* @inheritDoc
*/
protected function inlineMarkers()
{
$markers = [
return parent::inlineMarkers() + [
'http' => 'parseUrl',
'ftp' => 'parseUrl',
'~~' => 'parseStrike',
'|' => 'parseTd',
];
return array_merge(parent::inlineMarkers(), $markers);
}


Expand All @@ -45,6 +72,9 @@ protected function identifyLine($lines, $current)
if (isset($lines[$current]) && (strncmp($lines[$current], '```', 3) === 0 || strncmp($lines[$current], '~~~', 3) === 0)) {
return 'fencedCode';
}
if ($this->identifyTable($lines, $current)) {
return 'table';
}
return parent::identifyLine($lines, $current);
}

Expand Down Expand Up @@ -105,7 +135,7 @@ protected function parseUrl($markdown)
)/x
REGEXP;

if (preg_match($pattern, $markdown, $matches)) {
if (!in_array('parseLink', $this->context) && preg_match($pattern, $markdown, $matches)) {
$href = htmlspecialchars($matches[0], ENT_COMPAT | ENT_HTML401, 'UTF-8');
$text = htmlspecialchars(urldecode($matches[0]), ENT_NOQUOTES, 'UTF-8');
return [
Expand Down
37 changes: 21 additions & 16 deletions Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ protected function identifyHeadline($lines, $current)
/**
* Consume lines for a paragraph
*/
public function consumeParagraph($lines, $current)
protected function consumeParagraph($lines, $current)
{
// consume until newline or intended line

Expand Down Expand Up @@ -598,19 +598,22 @@ protected function parseEntity($text)
protected function parseLt($text)
{
if (strpos($text, '>') !== false) {
if (preg_match('/^<([^\s]*?@[^\s]*?\.\w+?)>/', $text, $matches)) {
// email address
$email = htmlspecialchars($matches[1], ENT_NOQUOTES, 'UTF-8');
return [
"<a href=\"mailto:$email\">$email</a>", // TODO encode mail with entities
strlen($matches[0])
];
} elseif (preg_match('/^<([a-z]{3,}:\/\/[^\s]+?)>/', $text, $matches)) {
// URL
$url = htmlspecialchars($matches[1], ENT_COMPAT | ENT_HTML401, 'UTF-8');
$text = htmlspecialchars(urldecode($matches[1]), ENT_NOQUOTES, 'UTF-8');
return ["<a href=\"$url\">$text</a>", strlen($matches[0])];
} elseif (preg_match('~^</?(\w+\d?)( .*?)?>~', $text, $matches)) {
if (!in_array('parseLink', $this->context)) { // do not allow links in links
if (preg_match('/^<([^\s]*?@[^\s]*?\.\w+?)>/', $text, $matches)) {
// email address
$email = htmlspecialchars($matches[1], ENT_NOQUOTES, 'UTF-8');
return [
"<a href=\"mailto:$email\">$email</a>", // TODO encode mail with entities
strlen($matches[0])
];
} elseif (preg_match('/^<([a-z]{3,}:\/\/[^\s]+?)>/', $text, $matches)) {
// URL
$url = htmlspecialchars($matches[1], ENT_COMPAT | ENT_HTML401, 'UTF-8');
$text = htmlspecialchars(urldecode($matches[1]), ENT_NOQUOTES, 'UTF-8');
return ["<a href=\"$url\">$text</a>", strlen($matches[0])];
}
}
if (preg_match('~^</?(\w+\d?)( .*?)?>~', $text, $matches)) {
// HTML tags
return [$matches[0], strlen($matches[0])];
} elseif (preg_match('~^<!--.*?-->~', $text, $matches)) {
Expand Down Expand Up @@ -645,7 +648,7 @@ protected function parseEscape($text)
*/
protected function parseLink($markdown)
{
if (($parts = $this->parseLinkOrImage($markdown)) !== false) {
if (!in_array('parseLink', array_slice($this->context, 1)) && ($parts = $this->parseLinkOrImage($markdown)) !== false) {
list($text, $url, $title, $offset) = $parts;

$link = '<a href="' . htmlspecialchars($url, ENT_COMPAT | ENT_HTML401, 'UTF-8') . '"'
Expand Down Expand Up @@ -712,8 +715,9 @@ protected function parseLinkOrImage($markdown)
isset($refMatches[2]) ? $refMatches[2] : '', // url
empty($refMatches[5]) ? null: $refMatches[5], // title
$offset + strlen($refMatches[0]), // offset
null, // reference key
];
} elseif (preg_match('/^[ \n]?(\[(.*?)\])?/', $markdown, $refMatches)) {
} elseif (preg_match('/^([ \n]?\[(.*?)\])?/', $markdown, $refMatches)) {
// reference style link
if (empty($refMatches[2])) {
$key = strtolower($text);
Expand All @@ -726,6 +730,7 @@ protected function parseLinkOrImage($markdown)
$this->references[$key]['url'], // url
empty($this->references[$key]['title']) ? null: $this->references[$key]['title'], // title
$offset + strlen($refMatches[0]), // offset
$key,
];
}
}
Expand Down
Loading

0 comments on commit 7dd5e50

Please sign in to comment.