|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace itsjavi\Flatdown\Markdown; |
|
4
|
|
|
|
|
5
|
|
|
use cebe\markdown\GithubMarkdown; |
|
6
|
|
|
use cebe\markdown\Markdown; |
|
7
|
|
|
use cebe\markdown\MarkdownExtra; |
|
8
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
9
|
|
|
|
|
10
|
|
|
class MarkdownFileParser |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Parses the given Markdown file |
|
14
|
|
|
* |
|
15
|
|
|
* @param string $filename |
|
16
|
|
|
* @param array $options |
|
17
|
|
|
* |
|
18
|
|
|
* @return MarkdownFile |
|
19
|
|
|
*/ |
|
20
|
|
|
public function parse($filename, array $options) |
|
21
|
|
|
{ |
|
22
|
|
|
$file = new MarkdownFile($filename); |
|
23
|
|
|
|
|
24
|
|
|
$content = $file->load()->content; |
|
25
|
|
|
|
|
26
|
|
|
$file->metadata = $this->parseMetadata($content, $options); |
|
|
|
|
|
|
27
|
|
|
$file->html = trim($this->createFlavoredParser($options)->parse($content)); |
|
28
|
|
|
$parsedTitle = $this->parseTitle($file->html); |
|
29
|
|
|
$file->title = (isset($file->metadata['title']) && strlen($file->metadata['title'])) |
|
30
|
|
|
? $file->metadata['title'] : $parsedTitle; |
|
31
|
|
|
|
|
32
|
|
|
return $file; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $content |
|
37
|
|
|
* @param array $options |
|
38
|
|
|
* |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
|
|
private function parseMetadata(&$content, array $options) |
|
42
|
|
|
{ |
|
43
|
|
|
$metadataLines = []; |
|
44
|
|
|
$contentLines = explode("\n", $content); |
|
45
|
|
|
$delimiter = isset($options['metadataDelimiter']) ? $options['metadataDelimiter'] : '---'; |
|
46
|
|
|
$indentation = isset($options['metadataIndentation']) ? $options['metadataIndentation'] : 4; |
|
47
|
|
|
|
|
48
|
|
|
if (empty($contentLines) || ($contentLines[0] !== $delimiter)) { |
|
49
|
|
|
return []; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
array_shift($contentLines); |
|
53
|
|
|
|
|
54
|
|
|
$line = 0; |
|
55
|
|
|
while (isset($contentLines[$line]) && ($contentLines[$line] != $delimiter)) { |
|
56
|
|
|
$metadataLines[] = $indentation ? |
|
57
|
|
|
preg_replace('/^(\s{' . $indentation . '})/', '', $contentLines[$line]) : |
|
58
|
|
|
$contentLines[$line]; |
|
59
|
|
|
$line++; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if ($contentLines[$line] == $delimiter) { |
|
63
|
|
|
$line++; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$content = []; |
|
67
|
|
|
|
|
68
|
|
|
for ($i = $line; $i < count($contentLines); $i++) { |
|
|
|
|
|
|
69
|
|
|
$content[] = $contentLines[$i]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$content = implode("\n", $content); |
|
73
|
|
|
$metadata = implode("\n", $metadataLines); |
|
74
|
|
|
|
|
75
|
|
|
return Yaml::parse($metadata); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string $html |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
private function parseTitle(&$html) |
|
84
|
|
|
{ |
|
85
|
|
|
$parts = explode('</h1>', $html, 2); |
|
86
|
|
|
|
|
87
|
|
|
if (count($parts) != 2) { |
|
88
|
|
|
return ''; |
|
89
|
|
|
} |
|
90
|
|
|
$title = preg_replace('/(.*)(\<h1.*\>)(.*)/', '$3', $parts[0]); |
|
91
|
|
|
$html = preg_replace('/(.*)(\<h1.*)/', '$1', $parts[0]) . $parts[1]; |
|
92
|
|
|
|
|
93
|
|
|
return $title; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param array $options |
|
98
|
|
|
* |
|
99
|
|
|
* @return Markdown |
|
100
|
|
|
*/ |
|
101
|
|
|
private function createFlavoredParser(array $options) |
|
102
|
|
|
{ |
|
103
|
|
|
$parser = null; |
|
|
|
|
|
|
104
|
|
|
$flavor = isset($options['flavor']) ? $options['flavor'] : 'extra'; |
|
105
|
|
|
|
|
106
|
|
|
switch ($flavor) { |
|
107
|
|
|
case 'default': |
|
108
|
|
|
$parser = new Markdown(); |
|
109
|
|
|
break; |
|
110
|
|
|
case 'extra': |
|
111
|
|
|
$parser = new MarkdownExtra(); |
|
112
|
|
|
break; |
|
113
|
|
|
case 'github': |
|
114
|
|
|
$parser = new GithubMarkdown(); |
|
115
|
|
|
break; |
|
116
|
|
|
default: |
|
117
|
|
|
throw new \InvalidArgumentException('Flavor not supported: ' . $flavor); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
foreach ($options as $optName => $optValue) { |
|
121
|
|
|
$parser->$optName = $optValue; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $parser; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.