/
ParsePage.php
181 lines (139 loc) · 4.19 KB
/
ParsePage.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php namespace App;
use \Michelf\MarkdownExtra as Markdown;
/**
* This class is used to take raw pages (markdown files) and prepare them for public consumption.
*
* @author jbelelieu
* @date 7/3/15
* @package Banana Dance Lite
* @link http://www.bananadance.org/
* @license GPL-3.0
* @link http://www.opensource.org/licenses/gpl-3.0.html
*/
class ParsePage {
/**
* @var string
*/
private $internalHeadings = array();
/**
* @var string
*/
private $content = '';
/**
* @var string
*/
private $rawData = '';
/**
* @var Markdown Class that contains the tool for formatting Markdown.
* Thank you to Michel Fortin (michelf.ca)!
*/
private $markdown;
/**
*
*/
public function __construct()
{
$this->markdown = new Markdown();
}
/**
* Get the formatted contents of the page. Should be called after
* a process() request.
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Get the internal links for a page in the form of a formatted <ul>.
*
* @return string
*/
public function getHeadings()
{
return $this->internalHeadings;
}
/**
* This is the data from the raw markdown wiki file.
*
* @param string $data
*
* @return $this
*/
public function setRawData($data)
{
$this->rawData = $data;
return $this;
}
/**
* Process everything we need to do to make the raw file usable.
*
* @return $this
*/
public function process()
{
$this->content = $this->markdown->transform($this->rawData);
$this->standardReplacements();
$this->content = $this->parseInnerHeadings();
return $this;
}
/**
* Custom replacements available on all pages. Establish standard
* replacements within the "config/custom_replacements.php" file.
*
* @return bool
*/
private function standardReplacements()
{
$replacements = include dirname(dirname(__FILE__)) . '/wiki/config/custom_replacements.php';
$lang = \App\getLanguage();
$useArray = $replacements[$lang];
// If the user requested a language other than english but there
// are no entries for that language, try to default to english.
if (empty($useArray) && $lang != BD_DEFAULT_LANGUAGE) {
if (empty($replacements[BD_DEFAULT_LANGUAGE])) return false;
$useArray = $replacements[BD_DEFAULT_LANGUAGE];
}
$keys = array_keys($useArray);
$values = array_values($useArray);
$this->content = str_replace($keys, $values, $this->content);
return true;
}
/**
* Builds a list of page headings for easy access and internal linking.
*
* @return string Final page with added <a name> tags.
*/
private function parseInnerHeadings()
{
$exp = explode("\n", $this->content);
$newContent = '';
foreach ($exp as $line) {
$check = substr(trim($line), 0, 4);
if ($check == '<h1>' || $check == '<h2>' || $check == '<h3>' || $check == '<h4>') {
switch ($check) {
case '<h2>':
$class = 'h2'; break;
case '<h3>':
$class = 'h3'; break;
case '<h4>':
$class = 'h4'; break;
default:
$class = 'h1';
}
$aName = str_replace(' ', '_', strtolower(preg_replace("/[^A-Za-z0-9 ]/", '', strip_tags($line))));
$this->internalHeadings[$aName] = array(
'class' => $class,
'name' => trim(substr($line, 4)),
);
// Add the anchor to the page's content.
$newContent .= '<a class="anchor" name="' . $aName . '"></a>' . "\n" . $line . "\n";
}
else {
$newContent .= $line . "\n";
}
}
return $newContent;
}
}