-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdparser.php
More file actions
50 lines (39 loc) · 1.33 KB
/
mdparser.php
File metadata and controls
50 lines (39 loc) · 1.33 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
<?php
use \Michelf\MarkdownExtra;
require 'phpmd/Michelf/MarkdownExtra.inc.php';
/**
* Analyze and parse a MD file
*
* This class breaks the file in paragraphs, ando parse them individually. This
* is for (in the future) generating the ToC from the titles, and also write them
* as links.
*/
class MDparser extends MarkdownExtra
{
public static function parse($filename) {
$variables = [];
$state = 'variables';
$body = '';
$data = explode ( "\n\n", file_get_contents ($filename) );
// Analizamos primero los metadatos
$metadata = array_shift ($data);
foreach ( explode ( "\n", $metadata ) as $line ) {
list($var, $val) = explode(':', $line, 2);
$variables[trim($var)] = trim($val);
}
// Luego los demás párrafos
while ($line = array_shift($data)) {
// Code?
if (substr($line, 0, 3) == '```') {
while (substr($line, -3) != '```') {
$line .= PHP_EOL . PHP_EOL . array_shift($data);
}
}
$md = new self;
$paragraph = $md->transform($line);
$body .= $paragraph . PHP_EOL;
}
$variables ['body'] = $body;
return $variables;
}
}