Completed
Pull Request — master (#130)
by Hisateru
03:32
created

GithubMarkdown.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 66
	protected function consumeParagraph($lines, $current)
62
	{
63
		// consume until newline
64 66
		$content = [];
65 66
		for ($i = $current, $count = count($lines); $i < $count; $i++) {
66 66
			$line = $lines[$i];
67
			if ($line === ''
68 66
				|| ltrim($line) === ''
69 66
				|| !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 33
					$this->identifyHr($line, $lines, $i) ||
75 33
					$this->identifyHtml($line, $lines, $i)
76 66
				)
77 66
				|| $this->identifyHeadline($line, $lines, $i))
78 58
			{
79 66
				break;
80 View Code Duplication
			} elseif ($this->identifyCode($line, $lines, $i)) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
				// possible beginning of a code block
82
				// but check for continued inline HTML
83
				// e.g. <img src="file.jpg"
84 2
				//           alt="some alt aligned with src attribute" title="some text" />
85 1
				if (preg_match('~<\w+([^>]+)$~s', implode("\n", $content))) {
86 1
					$content[] = $line;
87 2
				} else {
88
					break;
89 1
				}
90 66
			} else {
91
				$content[] = $line;
92 66
			}
93
		}
94 66
		$block = [
95 66
			'paragraph',
96 66
			'content' => $this->parseInline(implode("\n", $content)),
97 66
		];
98
		return [$block, --$i];
99
	}
100
101
	/**
102
	 * @inheritdocs
103
	 *
104
	 * Parses a newline indicated by two spaces on the end of a markdown line.
105 68
	 */
106
	protected function renderText($text)
107 68
	{
108 1
		if ($this->enableNewlines) {
109 1
			$br = $this->html5 ? "<br>\n" : "<br />\n";
110
			return strtr($text[1], ["  \n" => $br, "\n" => $br]);
111 68
		} else {
112
			return parent::renderText($text);
113
		}
114
	}
115
}
116