-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGithubMarkdown.php
182 lines (165 loc) · 3.9 KB
/
GithubMarkdown.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* @copyright Copyright (c) 2014 Carsten Brandt
* @license https://github.com/cebe/markdown/blob/master/LICENSE
* @link https://github.com/cebe/markdown#readme
*/
namespace cebe\markdown\latex;
use cebe\markdown\block\FencedCodeTrait;
use cebe\markdown\block\TableTrait;
use cebe\markdown\inline\StrikeoutTrait;
use cebe\markdown\inline\UrlLinkTrait;
/**
* Markdown parser for github flavored markdown.
*
* - uses the [tabularx](http://www.ctan.org/pkg/tabularx) environment for tables.
*
* @author Carsten Brandt <[email protected]>
*/
class GithubMarkdown extends Markdown
{
// include block element parsing using traits
use TableTrait;
use FencedCodeTrait;
// include inline element parsing using traits
use StrikeoutTrait;
use UrlLinkTrait;
/**
* @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 GithubMarkdown
':', // colon
'|', // pipe
];
/**
* Consume lines for a paragraph
*
* Allow headlines, lists and code to break paragraphs
*/
protected function consumeParagraph($lines, $current)
{
// consume until newline
$content = [];
for ($i = $current, $count = count($lines); $i < $count; $i++) {
$line = $lines[$i];
if (!empty($line) && ltrim($line) !== '' &&
!($line[0] === "\t" || $line[0] === " " && strncmp($line, ' ', 4) === 0) &&
!$this->identifyHeadline($line, $lines, $i) &&
!$this->identifyUl($line, $lines, $i) &&
!$this->identifyOl($line, $lines, $i))
{
$content[] = $line;
} else {
break;
}
}
$block = [
'paragraph',
'content' => $this->parseInline(implode("\n", $content)),
];
return [$block, --$i];
}
/**
* @inheritdoc
*/
protected function renderCode($block)
{
// make sure this is not replaced by the trait
return parent::renderCode($block);
}
/**
* @inheritdoc
*/
protected function renderAutoUrl($block)
{
return '\url{' . $this->escapeUrl($block[1]) . '}';
}
/**
* @inheritdoc
*/
protected function renderStrike($block)
{
return '\sout{' . $this->renderAbsy($block[1]) . '}';
}
/**
* @inheritdocs
*
* Parses a newline indicated by two spaces on the end of a markdown line.
*/
protected function renderText($text)
{
if ($this->enableNewlines) {
return preg_replace("/( \n|\n)/", "\\\\\\\\\n", $this->escapeLatex($text[1]));
} else {
return parent::renderText($text);
}
}
private $_tableCellHead = false;
private $_tds = 0;
protected function renderTable($block)
{
$align = [];
foreach($block['cols'] as $col) {
if (empty($col)) {
$align[] = 'X';
} else {
$align[] = $col[0];
}
}
$align = implode('|', $align);
$content = '';
$first = true;
$numThs = 0;
foreach($block['rows'] as $row) {
$this->_tableCellHead = $first;
$this->_tds = 0;
$content .= $this->renderAbsy($this->parseInline($row)); // TODO move this to the consume step
if ($first) {
$numThs = $this->_tds;
} else {
while ($this->_tds < $numThs) {
$content .= ' & ';
$this->_tds++;
}
}
$content .= "\\\\ \\hline\n";
$first = false;
}
return "\n\\noindent\\begin{tabularx}{\\textwidth}{|$align|}\\hline\n$content\\end{tabularx}\n\n";
}
/**
* @marker |
*/
protected function parseTd($markdown)
{
if (isset($this->context[1]) && $this->context[1] === 'table') {
$this->_tds++;
return [['tableSep'], 1];
}
return [['text', $markdown[0]], 1];
}
protected function renderTableSep($block)
{
return '&';
}
}