|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Cecil. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Arnaud Ligny <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Cecil\Collection\Page; |
|
15
|
|
|
|
|
16
|
|
|
use Cecil\Exception\RuntimeException; |
|
17
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Parser class. |
|
21
|
|
|
* |
|
22
|
|
|
* Parses the front matter and body of a file, extracting metadata and content. |
|
23
|
|
|
*/ |
|
24
|
|
|
class Parser |
|
25
|
|
|
{ |
|
26
|
|
|
// https://regex101.com/r/uZ79Gu/1, https://regex101.com/r/0w5ZvL/1, https://regex101.com/r/GLnaKs/1 |
|
27
|
|
|
//public const PATTERN = '^\s*(?:<!--|---|\+\+\+){1}[\n\r]+(.*?)[\n\r]+(?:-->|---|\+\+\+){1}[\n\r\s]+(.*)$'; |
|
28
|
|
|
public const PATTERN = '^\s*(?:<!--|---|\+\+\+){1}[\n\r\s]*(.*?)[\n\r\s]*(?:-->|---|\+\+\+){1}[\n\r\s]*(.*)$'; |
|
29
|
|
|
|
|
30
|
|
|
/** @var SplFileInfo */ |
|
31
|
|
|
protected $file; |
|
32
|
|
|
|
|
33
|
|
|
/** @var string */ |
|
34
|
|
|
protected $frontmatter; |
|
35
|
|
|
|
|
36
|
|
|
/** @var string */ |
|
37
|
|
|
protected $body; |
|
38
|
|
|
|
|
39
|
1 |
|
public function __construct(SplFileInfo $file) |
|
40
|
|
|
{ |
|
41
|
1 |
|
$this->file = $file; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Parse the contents of the file. |
|
46
|
|
|
* |
|
47
|
|
|
* Example: |
|
48
|
|
|
* --- |
|
49
|
|
|
* title: Title |
|
50
|
|
|
* date: 2016-07-29 |
|
51
|
|
|
* --- |
|
52
|
|
|
* Lorem Ipsum. |
|
53
|
|
|
* |
|
54
|
|
|
* @throws RuntimeException |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function parse(): self |
|
57
|
|
|
{ |
|
58
|
1 |
|
if ($this->file->isFile()) { |
|
59
|
1 |
|
if (!$this->file->isReadable()) { |
|
60
|
|
|
throw new RuntimeException('Cannot read file.'); |
|
61
|
|
|
} |
|
62
|
1 |
|
preg_match( |
|
63
|
1 |
|
'/' . self::PATTERN . '/s', |
|
64
|
1 |
|
$this->file->getContents(), |
|
65
|
1 |
|
$matches |
|
66
|
1 |
|
); |
|
67
|
|
|
// if there is not front matter, set body only |
|
68
|
1 |
|
if (empty($matches)) { |
|
69
|
1 |
|
$this->body = $this->file->getContents(); |
|
70
|
|
|
|
|
71
|
1 |
|
return $this; |
|
72
|
|
|
} |
|
73
|
1 |
|
$this->frontmatter = trim($matches[1]); |
|
74
|
1 |
|
$this->body = trim($matches[2]); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get front matter. |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function getFrontmatter(): ?string |
|
84
|
|
|
{ |
|
85
|
1 |
|
return $this->frontmatter; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get body. |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function getBody(): string |
|
92
|
|
|
{ |
|
93
|
1 |
|
return $this->body; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|