-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkorrekturen.php
313 lines (280 loc) · 7.93 KB
/
korrekturen.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
require_once('config.php');
// Seiten korrigieren
function korrBereich($s)
{
$i = 0;
$ret = '';
if(preg_match_all('/(\d+)[-,](\d+)/', $s, $matches)) {
while(isset($matches[1][$i])) {
if($matches[1][$i] == $matches[2][$i])
$ret .= ((int)$matches[1][$i]).',~';
else
$ret .= ((int)$matches[1][$i]).'--'.((int)$matches[2][$i]).',~';
$i++;
}
} else if(preg_match_all('/(\d+)/', $s, $matches)) {
while(isset($matches[1][$i])) {
$ret .= ((int)$matches[1][$i]).',~';
$i++;
}
} else {
return false;
}
return trim(substr($ret, 0, strlen($ret)-2));
}
function korrStringWiki($s, $doTrim=true)
{
//$s = preg_replace('/"([^"]+)"/', '"`$1"\'', $s); // Anfuehrungszeichen lassen sich nicht korrekt reparieren.
$s = str_replace(array(
'----',
'"',
'&',
'#',
'%',
'
',
'_',
'^',
'´',
'fi',
'¬',
'fl',
'→',
'°',
'−',
'',
'$',
'[',
']',
'~',
'‑',
'\.\.\.',
), array(
'',
'\textquotedbl{}',
'\&',
'\#',
'\%',
' ', // double whitespaces are ignored by LaTeX
'\_',
'\^',
'\'',
'fi',
' ',
'fl',
'\textrightarrow{}',
'o',
'--',
'-',
'\$',
'$[$',
'$]$',
'\~{}',
'--',
'\ldots',
), $s);
$s = korrDash($s);
$s = strip_tags($s);
if ($doTrim)
$s = trim($s);
return $s;
}
function korrString($s, $doTrim=true)
{
$result = '';
foreach (preg_split('/(\\\newline\s\\\begin{tabular}{[lr|]*}.*?\\\end{tabular})/s', $s, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
if (preg_match('/(\\\newline\s\\\begin{tabular}{[lr|]*})(.*?)\\\end{tabular}/s', $part, $match)) {
$rows = preg_split('/\\\[^a-z]/', $match[2]);
/* require_once('Logger.php');
Logger::dump($match[1]);
Logger::dump($rows); */
for ($i = 0; $i < count($rows); $i++) {
$columns = preg_split('/&/', $rows[$i]);
for ($j = 0; $j < count($columns); $j++) {
$columns[$j] = preg_replace(
'/\$\[\$\$\[\$Quelle:(.*?)\|(.*?)\$\]\$\$\]\$/es',
'"\href{http://de.vroniplag.wikia.com/wiki/" . urlToTex("$1") . "}{" . korrString("$1") . "}"',
$columns[$j]
);
}
$rows[$i] = "\n" . '\hline' . "\n" . implode('&', $columns);
}
$result .= '\newline' . $match[1] . implode('\\\\', $rows) . "\n" . '\hline' . "\n" . '\end{tabular}';
}
else {
foreach (preg_split('/(<math>.*?<\/math>)/s', $part, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
if (preg_match('/<math>(.*?)<\/math>/s', $part, $match)) {
$result .= '$'.korrMath($match[1]).'$';
}
else {
$r = str_replace(array(
'\\',
'{',
'}',
), array(
'\backslash ',
'\{',
'\}',
), $part);
$result .= korrStringWiki($r, false);
}
}
}
}
if($doTrim)
$result = trim($result);
return $result;
}
// wie korrString, aber externe Links in Anmerkung mit @url umfassen
function korrStringWithLinks($s, $doTrim=true, $stuffIntoFootnotes=false, $enableRef = false)
{
$result = '';
$prots = 'http|https|ftp';
$schemeRegex = '(?:(?:'.$prots.'):\/\/)';
$fragmentRegex = NAME_PREFIX.'\/Fragment[_ ]\d\d\d[_ ]\d\d';
$refRegex = $enableRef ? '|<ref>.*?<\/ref>' : '';
//if ($enableRef) print "enableRef\n";
foreach(preg_split('/(\[\[.+?\]\]|\['.$schemeRegex.'[^][{}<>"\\x00-\\x08\\x0a-\\x1F]+\]|'.$schemeRegex.'[^][{}<>"\\x00-\\x20\\x7F]+'.$refRegex.')/s', $s, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
if(preg_match('/^\[\[([^|]+)\]\]$/', $part, $match)) {
// interne Links ohne Linktext
if(preg_match('/'.$fragmentRegex.'/', $match[1])) {
// Link auf Fragment intern handhaben
$result .= '\hyperlink{'.titleToKey($match[1]).'}{'.korrString($match[1]).'}';
} else {
$result .= '\url{http://de.vroniplag.wikia.com/wiki/'.urlToTex($match[1]).'}';
}
} else if(preg_match('/^\[\[(.+?)\|(.+)\]\]$/', $part, $match)) {
// interne Links mit Linktext
if(preg_match('/'.$fragmentRegex.'/', $match[1])) {
$result .= '\hyperlink{'.titleToKey($match[1]).'}{'.korrString($match[2]).'}';
} else {
if($stuffIntoFootnotes) {
$result .= korrString($match[2]).'\footnote{\url{http://de.vroniplag.wikia.com/wiki/'.urlToTex($match[1]).'}}';
} else {
$result .= '\href{http://de.vroniplag.wikia.com/wiki/'.urlToTex($match[1]).'}{'.korrString($match[2]).'}';
}
}
} else if(preg_match('/^'.$schemeRegex.'/s', $part, $match)) {
// externe Links ohne Linktext
$result .= '\url{'.urlToTex($part).'}';
} else if(preg_match('/^\[('.$schemeRegex.'[^][{}<>"\\x00-\x20\\x7F]+) *([^\]\\x00-\\x08\\x0A-\\x1F]*)?\]$/s', $part, $match)) {
if(isset($match[2]) && trim($match[2])) {
// externe Links mit Linktext
if($stuffIntoFootnotes) {
$result .= korrString($match[2]).'\footnote{\url{'.urlToTex($match[1]).'}}';
} else {
$result .= '\href{'.urlToTex($match[1]).'}{'.korrString($match[2]).'}';
}
} else {
$result .= '\url{'.urlToTex($match[1]).'}';
}
} else if($enableRef && preg_match('/^<ref>(.*)<\/ref>$/s', $part, $match)) {
// <ref>...</ref>
$result .= '\footnote{' . korrStringWithLinks($match[1], false, false, false) . '}';
} else {
$result .= korrString($part, false);
}
}
if ($doTrim)
$result = trim($result);
return $result;
}
// konvertiert Wiki-Formatierung ('''...''', ''...'', <u>...</u>, <i>...</i>, <b>...</b>) nach LaTeX
function korrWikiFontStyles($s)
{
$s = preg_replace('/\'\'\'(.*?)\'\'\'/s', '\textbf{$1}', $s);
$s = preg_replace('/\'\'(.*?)\'\'/s', '\textsl{$1}', $s);
$s = preg_replace(';<u>(.*?)</u>;s', '\underline{$1}', $s);
$s = preg_replace(';<i>(.*?)</i>;s', '\textsl{$1}', $s);
$s = preg_replace(';<b>(.*?)</b>;s', '\textbf{$1}', $s);
return $s;
}
// convert to LaTeX math
function korrMath($s)
{
$s = preg_replace(';<math>(.*?)</math>;s', '$$1$', $s);
return $s;
}
// {{highlight|...|...}} entfernen
function removeHighlights($s)
{
$s = preg_replace('/{{highlight\|[^\|]*\|([^}]*)}}/', '$1', $s);
return $s;
}
// Dissertation und Original eines Fragments korrigieren
function korrFragmentText($s)
{
$s = trim($s);
$s = removeHighlights($s);
$s = korrString($s);
$s = korrWikiFontStyles($s);
return ($s != '') ? $s : '--';
}
// Anmerkung eines Fragments korrigieren
function korrFragmentAnmerkung($s)
{
$s = trim($s);
$s = preg_replace('/\{\{Fragmentsichter[^}]*\}\}/i', '', $s);
$s = korrStringWithLinks($s);
return $s;
}
// Grossbuchstaben in Titel und Sammlung vor bibtex schuetzen
function korrVersalien($s)
{
return preg_replace('/([A-Z])/', '{$1}', $s);
}
// - durch -- ersetzen, wenn es passt
function korrDash($s)
{
return str_replace(' - ', ' -- ', $s);
}
// & durch \& ersetzen
function korrAmpersand($s)
{
return str_replace('&', '\&', $s);
}
// , durch and ersetzen in den Autoren (aber nicht in geschweiften Klammern)
function korrAnd($s)
{
$depth = 0;
for($i = 0; $i < strlen($s); ++$i) {
if($s[$i] == ',' && $depth <= 0)
$s = substr($s, 0, $i) . ' and ' . substr($s, $i+1);
else if($s[$i] == '{')
$depth++;
else if($s[$i] == '}')
$depth--;
}
return $s;
}
// u.a. durch and others ersetzen in den Autoren
function korrEtAl($s)
{
return preg_replace('/\[\s*u\.\s*a\.\s*\]\s*$|u\.\s*a\.\s*$/', ' and others', $s);
}
// aeussere eckige Klammern entfernen
function korrBracket($s)
{
return preg_replace('/^\s*\[(.*)\]\s*$/', '$1', $s);
}
// Hack fuer [http:// Linktext]-Links im URL-Feld
// wird wegen umbruch von zu breiten floats im Wiki (IE/Chrome) benoetigt
function korrUrlForBibliography($s)
{
$prots = 'http|https|ftp';
$schemeRegex = '(?:(?:'.$prots.'):\/\/)';
return preg_replace('/^\[('.$schemeRegex.'[^][{}<>"\\x00-\x20\\x7F]+) *([^\]\\x00-\\x08\\x0A-\\x1F]*)?\]$/s', '$1', $s);
}
function titleToKey($title)
{
$title = str_replace('Kategorie:', '', $title);
$title = str_replace(' ', '-', $title);
$title = preg_replace('/[^a-zA-Z0-9]/', '-', $title);
return $title;
}
// % und # in URLs mit Backslash escapen
function urlToTex($s)
{
return str_replace(array('%', '#'), array('\\%', '\\#'), $s);
}