-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentry-to-md.php
141 lines (118 loc) · 3.89 KB
/
entry-to-md.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
<?php
class Parser
{
private $baseFilename = '';
private $texFilename = '';
private $mdFilename = '';
private $htmlFilename = '';
private $contents = '';
private $output = '';
public function __construct($entry)
{
$this->baseFilename = 'entries/'.$entry[0].'/'.$entry;
$this->texFilename = $this->baseFilename.'.tex';
$this->mdFilename = $this->baseFilename.'.md';
$this->htmlFilename = $this->baseFilename.'.html';
$this->contents = file_get_contents($this->texFilename);
}
public function parse()
{
$output = $this->contents;
$links = array();
$footnotes = array();
$changes = array();
/*** HTML SPECIAL CHARACTERS ***/
$output = str_replace(
array(
'<', '>', '``', '\'\'', '`', '\'', '\\dots', '{[}',
'\\begin{inparaenum}', '\\end{inparaenum}',
),
array(
'<', '>', '“', '”', '‘', '’',
'…', '[', '', '',
),
$output
);
/*** ENTRY HEADERS ***/
$output = preg_replace('/\\\\mainentry\\{(.*?)\\}/s', "**\\1**", $output);
/*** ITALICISED TEXT ***/
$output = preg_replace('/\\\\textit\\{(.*?)\\}/s', '*\\1*', $output);
/*** CITATIONS ***/
$output = preg_replace_callback(
'/\\\\cite(?:entry|appendix)\\{(?<text>.*?)\\}/s',
function ($matches) use (&$links) {
$text = $matches['text'];
$id = getId($text);
$links[$id] = $text;
return "[$text][$id]";
},
$output
);
/*** CHANGES ***/
$output = preg_replace_callback(
'/\\\\Changes\\{(?<text>.*?)\\}/s',
function ($matches) use (&$changes) {
$id = count($changes);
$changes[] = $matches['text'];
$output = ' <span class="changes" data-id="'.$id.'">Changes'.
'</span>';
return $output;
},
$output
);
/*** FOOTNOTES ***/
$output = preg_replace_callback(
'/\\\\footnote\\{(?<text>.*?)\\}/s',
function ($matches) use (&$footnotes) {
$footnotes[] = $matches['text'];
$output = '<sup>'.count($footnotes).'</sup>';
return $output;
},
$output
);
if (count($footnotes)) {
$output .= "\n";
}
foreach ($footnotes as $index => $text) {
$output .= ($index+1).'. '.$text."\n";
}
/*** PARAGRAPH ENUM ITEMS ***/
$output = str_replace('\\item', '1. ', $output);
/*** CHANGES TEXT ***/
if (count($changes)) {
$output .= "\n";
}
foreach ($changes as $index => $text) {
$output .= '<div class="changes-text" data-id="'.$index.'">'.$text.
'</div>'."\n";
}
/*** FOOTER ***/
if (file_exists('footer.md')) {
$output .= "\n".file_get_contents('footer.md');
};
/*** CITED ENTRY LINKS ***/
foreach ($links as $text) {
$linkId = getId($text);
$linkLetter = $linkId[0];
$output .= "[$linkId]: ../$linkLetter/$linkId.html\n";
}
file_put_contents($this->mdFilename, $output);
exec(
'perl Markdown.pl '.escapeshellarg($this->mdFilename).' > '.
escapeshellarg($this->htmlFilename)
);
return $output;
}
}
$entry = $argv[1];
$parser = new Parser($entry);
$output = $parser->parse();
print "$output\n";
function getId($entry)
{
return strtolower(str_replace(
array(' ', "\n"),
array('-', '-'),
$entry
));
}