Completed
Pull Request — master (#170)
by
unknown
03:46
created

GithubMarkdown::parseEscape()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 5
cts 5
cp 1
rs 9.7333
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 3
1
<?php
2
/**
3
 * @copyright Copyright (c) 2014 Carsten Brandt
4
 * @license https://github.com/cebe/markdown/blob/master/LICENSE
5
 * @link https://github.com/cebe/markdown#readme
6
 */
7
8
namespace cebe\markdown;
9
10
/**
11
 * Markdown parser for github flavored markdown.
12
 *
13
 * @author Carsten Brandt <[email protected]>
14
 */
15
class GithubMarkdown extends Markdown
16
{
17
	// include block element parsing using traits
18
	use block\TableTrait;
19
	use block\FencedCodeTrait;
20
21
	// include inline element parsing using traits
22
	use inline\StrikeoutTrait;
23
	use inline\UrlLinkTrait;
24
25
	/**
26
	 * @var boolean whether to interpret newlines as `<br />`-tags.
27
	 * This feature is useful for comments where newlines are often meant to be real new lines.
28
	 */
29
	public $enableNewlines = false;
30
31
	/**
32
	 * @inheritDoc
33
	 */
34
	protected $escapeCharacters = [
35
		// from Markdown
36
		'\\', // backslash
37
		'`', // backtick
38
		'*', // asterisk
39
		'_', // underscore
40
		'{', '}', // curly braces
41
		'[', ']', // square brackets
42
		'(', ')', // parentheses
43
		'#', // hash mark
44
		'+', // plus sign
45
		'-', // minus sign (hyphen)
46
		'.', // dot
47
		'!', // exclamation mark
48
		'<', '>',
49
		// added by GithubMarkdown
50
		':', // colon
51
		'|', // pipe
52
	];
53
54
55
56
	/**
57
	 * Consume lines for a paragraph
58
	 *
59
	 * Allow headlines, lists and code to break paragraphs
60
	 */
61 72
	protected function consumeParagraph($lines, $current)
62
	{
63
		// consume until newline
64 72
		$content = [];
65 72
		for ($i = $current, $count = count($lines); $i < $count; $i++) {
66 72
			$line = $lines[$i];
67 72
			if ($line === ''
68 72
				|| ltrim($line) === ''
69 72
				|| !ctype_alpha($line[0]) && (
70 34
					$this->identifyQuote($line, $lines, $i) ||
71 34
					$this->identifyFencedCode($line, $lines, $i) ||
72 34
					$this->identifyUl($line, $lines, $i) ||
73 33
					$this->identifyOl($line, $lines, $i) ||
74 72
					$this->identifyHr($line, $lines, $i)
75
				)
76 72
				|| $this->identifyHeadline($line, $lines, $i))
77
			{
78 62
				break;
79 72
			} elseif ($this->identifyCode($line, $lines, $i)) {
80
				// possible beginning of a code block
81
				// but check for continued inline HTML
82
				// e.g. <img src="file.jpg"
83
				//           alt="some alt aligned with src attribute" title="some text" />
84 2
				if (preg_match('~<\w+([^>]+)$~s', implode("\n", $content))) {
85 1
					$content[] = $line;
86
				} else {
87 2
					break;
88
				}
89
			} else {
90 72
				$content[] = $line;
91
			}
92
		}
93
		$block = [
94 72
			'paragraph',
95 72
			'content' => $this->parseInline(implode("\n", $content)),
96
		];
97 72
		return [$block, --$i];
98
	}
99
100
	/**
101
	 * @inheritdocs
102
	 *
103
	 * Parses a newline indicated by two spaces on the end of a markdown line.
104
	 */
105 74
	protected function renderText($text)
106
	{
107 74
		if ($this->enableNewlines) {
108 1
			$br = $this->html5 ? "<br>\n" : "<br />\n";
109 1
			return strtr($text[1], ["  \n" => $br, "\n" => $br]);
110
		} else {
111 74
			return parent::renderText($text);
112
		}
113
	}
114
115
	/**
116
	 * @inheritDoc
117
	 *
118
	 * Allows escaping newlines to create line breaks.
119
	 *
120
	 * @marker \
121
	 */
122 10
	protected function parseEscape($text)
123
	{
124 10
		$br = $this->html5 ? "<br>\n" : "<br />\n";
125
126
		# If the backslash is followed by a newline.
127
		# Note: GFM doesn't allow spaces after the backslash.
128 10
		if ($text[1] === "\n") {
129
130
			# Return the line break
131 1
			return [["text", $br], 2];
132
		}
133
134
		# Otherwise parse the sequence normally
135 10
		return parent::parseEscape($text);
136
137
	}
138
}
139