forked from koreapyj/php-namumark
/
NamuMarkExtra.php
189 lines (156 loc) · 6.37 KB
/
NamuMarkExtra.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
182
183
184
185
186
187
<?php
class NamuMarkExtra {
function __construct($text, $title) {
$this->text = $text;
$this->title = $title;
}
public function indent() {
if(preg_match_all('/^( +)([^* ][^\n]*)$/m', $this->text, $indent, PREG_SET_ORDER)) {
foreach ($indent as $each_indent) {
if (!preg_match('/^(1\.|A\.|I\.)/i', $each_indent[2])) {
if(preg_match('/^>/', $each_indent[2]))
$each_indent[1] = str_replace(' ', '', $each_indent[1]);
else
$each_indent[1] = str_replace(' ', ':', $each_indent[1]);
$each_indent[0] = '/^'.preg_quote($each_indent[0], '/').'$/m';
$this->text = preg_replace($each_indent[0], $each_indent[1].$each_indent[2], $this->text);
}
}
}
}
public function title() {
$this->text = preg_replace("/^(=+ .* =+)(\s+)?$/m", "\n$1", $this->text);
}
public function external() {
$this->text = preg_replace(
'/\[(<a rel="nofollow" target="_blank" class=".*?" href=".*?">.*?<\/a>)\]/',
'$1',
$this->text
);
}
public function imageurl() {
preg_match_all('/<a rel="nofollow" target="_blank" class="external free" href="(.*?)">.*?<\/a>/', $this->text, $exurlarr, PREG_SET_ORDER);
foreach ($exurlarr as $image_path) {
$n = 0;
if (preg_match('/(.*)(\.jpeg|\.jpg|\.png|\.gif)(.*)/', $image_path[1], $image_url)) {
$this->text = str_replace($image_path[0], '<img src="'.$image_url[1].$image_url[2].'"><br>', $this->text);
$n = 1;
}
if ($n == '0') {
$header = @get_headers($image_path[1], 1);
}
if(isset($header['Content-Type']) && !is_array($header['Content-Type']) && preg_match('/image/', $header['Content-Type'])) {
$this->text = str_replace("$image_path[0]", "<img src=\"$image_path[1]\"><br>\n", $this->text);
}
}
}
public function getTemplateParameter() {
if(preg_match_all('/\{\{(.*?)\}\}/', $this->text, $includes, PREG_SET_ORDER)) {
foreach($includes as $include) {
$GLOBALS['template'] = explode('|', $include[1]);
}
}
if(preg_match_all('/\{\{(.*?)\}\}/s', $this->text, $includes, PREG_SET_ORDER)) {
foreach($includes as $include) {
$GLOBALS['template'] = explode('|', $include[1]);
}
}
}
public function printTemplateParameter() {
if(isset($GLOBALS['template'])) {
$properties = $GLOBALS['template'];
$i = 1;
if (is_array($properties)) {
foreach ($properties as $property) {
if ($property == $properties[0])
continue;
$property = explode('=', $property);
$property[0] = trim($property[0]);
if (isset($property[1])) {
$this->text = str_replace('@' . $property[0] . '@', $property[1], $this->text);
} else {
$this->text = str_replace('@' . $i . '@', $property[0], $this->text);
$i++;
}
}
}
}
}
public function table() {
$this->text = preg_replace('/^\|([^\|\}]+?)\|(.*?\|\|)$/m', '||<table caption=$1>$2', $this->text);
if(preg_match_all('/^(\|\|.*?\|\|)\s*$/sm', $this->text, $tables)) {
foreach ($tables[1] as $table) {
$newtable = preg_replace('/^((?:\|\|)+)(<table.*?\>)((?:\|\|)+)(.+)/im', '$1$3$2$4', $table);
if(preg_match('/\{\{\{(?:\+|-)\d$/m', $newtable))
$this->text = str_replace($table, $newtable, $this->text);
else
$this->text = str_replace($table, str_replace("\n", '<br />', $newtable), $this->text);
}
}
$this->text = preg_replace('/\n\|\|$/', '||', $this->text);
if(preg_match_all('/(\{\{\{(?:\+|-)\d\n(\|\|.*)\}\}\}) *?\|\|/s', $this->text, $tables, PREG_SET_ORDER)) {
foreach($tables as $n => $table) {
if(preg_match('/(\{\{\{(?:\+|-)\d\n(\|\|.*)\}\}\}) *?\|\|/s', $table[2], $sub_table)) {
$tEngine = new NamuMark1($sub_table[1], null);
$newtable = str_replace("\n", '', $tEngine->toHtml());
$table[1] = str_replace($sub_table[1], $newtable, $table[1]);
}
$tEngine = new NamuMark1($table[1], null);
$newtable = str_replace("\n", '', $tEngine->toHtml());
$this->text = str_replace($tables[$n][1], $newtable, $this->text);
}
}
}
public function enter() {
$lines = explode("\n", $this->text);
$this->text = '';
foreach($lines as $line_n => $line) {
$line = preg_replace('/^\s*/', '', $line);
if(preg_match('@<pre>@i', $line)) {
// pre 태그 시작
$this->text .= $line . "\n";
$is_pre = true;
continue;
} elseif(isset($is_pre) && $is_pre === true && preg_match('@</pre>@i', $line)) {
// pre 태그 끝
$is_pre = false;
} elseif(isset($is_pre) && $is_pre === true) {
$this->text .= $line . "\n";
continue;
}
if(!isset($lines[$line_n - 1])) {
$this->text .= $line . "\n";
continue;
} else {
$prev_line = preg_replace('/^\s*/', '', $lines[$line_n - 1]);
}
if( $line === '' || $prev_line === '' || preg_match('/^<(?!img)[^>]*>$/', $line) || preg_match('/^<(?!img)[^>]*>$/', $prev_line) || preg_match('@(</li>|</div>|</ul>|</h\d>|</table>|<br/>|<br>|<br />|</dl>|<ol.*>|</ol>|</blockquote>|</t.>)$@i', $prev_line) || preg_match('@^(</?p>|<a onclick|<dl>|<dd>|<ul>|<li>|<ol>)@i', $line) )
$this->text .= $line . "\n";
else
$this->text .= '<br>' . $line . "\n";
}
}
public function cutMediawikiTable() {
preg_match_all('/(?<!\{)\{\|(.*?)\|\}(?!\})/s', $this->text, $matches);
$mediawikiTable = array();
foreach($matches[0] as $key => $match) {
$mediawikiTable[$key] = $match;
$this->text = str_replace($match, '<preserved type=mediawikiTable no='.$key.'>', $this->text);
}
return $mediawikiTable;
}
public function pasteMediawikiTable($mediawikiTable) {
preg_match_all('/<preserved type=mediawikiTable no=(\S*?)>/', $this->text, $matches, PREG_SET_ORDER);
foreach($matches as $match) {
$contents = preg_split('/(?:\|-|\|\+)/', $mediawikiTable[$match[1]], -1);
foreach($contents as $contents_key => $each_contents) {
if($contents_key == 0)
continue;
$wEngine = new NamuMark3($each_contents, $this->title);
$m_each_contents = $wEngine->toHtml();
$mediawikiTable[$match[1]] = str_replace($each_contents, $m_each_contents, $mediawikiTable[$match[1]]);
}
$this->text = str_replace($match[0], $mediawikiTable[$match[1]], $this->text);
}
}
}