forked from DevlessTeam/docs.devless.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomParser.php
More file actions
167 lines (116 loc) · 4.29 KB
/
Copy pathCustomParser.php
File metadata and controls
167 lines (116 loc) · 4.29 KB
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
<?php namespace App;
use ParsedownExtra;
class CustomParser extends ParsedownExtra
{
/**
* Copy of the @lines function from Parsedown with final step commented out.
*
* @param string $markdown
* @param string $slug
* @return array
*/
public function getBlocks($markdown)
{
# make sure no definitions are set
$this->DefinitionData = array();
# standardize line breaks
$text = str_replace(array("\r\n", "\r"), "\n", $markdown);
# remove surrounding line breaks
$text = trim($text, "\n");
# split text into lines
$lines = explode("\n", $text);
$CurrentBlock = null;
foreach ($lines as $line) {
if (chop($line) === '') {
if (isset($CurrentBlock)) {
$CurrentBlock['interrupted'] = true;
}
continue;
}
if (strpos($line, "\t") !== false) {
$parts = explode("\t", $line);
$line = $parts[0];
unset($parts[0]);
foreach ($parts as $part) {
$shortage = 4 - mb_strlen($line, 'utf-8') % 4;
$line .= str_repeat(' ', $shortage);
$line .= $part;
}
}
$indent = 0;
while (isset($line[$indent]) and $line[$indent] === ' ') {
$indent ++;
}
$text = $indent > 0 ? substr($line, $indent) : $line;
# ~
$Line = array('body' => $line, 'indent' => $indent, 'text' => $text);
# ~
if (isset($CurrentBlock['incomplete'])) {
$Block = $this->{'block'.$CurrentBlock['type'].'Continue'}($Line, $CurrentBlock);
if (isset($Block)) {
$CurrentBlock = $Block;
continue;
} else {
if (method_exists($this, 'block'.$CurrentBlock['type'].'Complete')) {
$CurrentBlock = $this->{'block'.$CurrentBlock['type'].'Complete'}($CurrentBlock);
}
unset($CurrentBlock['incomplete']);
}
}
# ~
$marker = $text[0];
# ~
$blockTypes = $this->unmarkedBlockTypes;
if (isset($this->BlockTypes[$marker])) {
foreach ($this->BlockTypes[$marker] as $blockType) {
$blockTypes []= $blockType;
}
}
#
# ~
foreach ($blockTypes as $blockType) {
$Block = $this->{'block'.$blockType}($Line, $CurrentBlock);
if (isset($Block)) {
$Block['type'] = $blockType;
if (! isset($Block['identified'])) {
$Blocks []= $CurrentBlock;
$Block['identified'] = true;
}
if (method_exists($this, 'block'.$blockType.'Continue')) {
$Block['incomplete'] = true;
}
$CurrentBlock = $Block;
continue 2;
}
}
# ~
if (isset($CurrentBlock) and ! isset($CurrentBlock['type']) and ! isset($CurrentBlock['interrupted'])) {
$CurrentBlock['element']['text'] .= "\n".$text;
} else {
$Blocks []= $CurrentBlock;
$CurrentBlock = $this->paragraph($Line);
$CurrentBlock['identified'] = true;
}
}
# ~
if (isset($CurrentBlock['incomplete']) and method_exists($this, 'block'.$CurrentBlock['type'].'Complete')) {
$CurrentBlock = $this->{'block'.$CurrentBlock['type'].'Complete'}($CurrentBlock);
}
# ~
$Blocks []= $CurrentBlock;
unset($Blocks[0]);
# ~
// $markup = '';
// foreach ($Blocks as $Block)
// {
// if (isset($Block['hidden']))
// {
// continue;
// }
// $markup .= "\n";
// $markup .= isset($Block['markup']) ? $Block['markup'] : $this->element($Block['element']);
// }
// $markup .= "\n";
return $Blocks;
}
}