-
Notifications
You must be signed in to change notification settings - Fork 1
/
importWiki.php
68 lines (52 loc) · 1.64 KB
/
importWiki.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
<?php
require_once('korrekturen.php');
function getCommonPrefix($s1, $s2) {
$max = min(strlen($s1), strlen($s2));
for ($i = 0; $i < $max && $s1[$i] == $s2[$i]; ++$i)
;
return substr($s1, 0, $i);
}
setlocale(LC_ALL, 'de_DE');
if(!file_exists('cache')) {
print "Fehler: Cache existiert nicht! 'make cache' ausgefuehrt?\n";
exit(1);
}
$cache = unserialize(file_get_contents('cache'));
$content = $cache['static'];
$content = preg_replace('/.*BEGIN_ABSCHLUSSBERICHT/s', '', $content);
// references
$content = korrStringWithLinks($content, true, true, true);
$content = preg_replace('/===\s*([^=]+?)\s*===/s', '\section{$1}', $content);
$content = preg_replace('/==\s*([^=]+?)\s*==/s', '\chapter{$1}', $content);
$content = korrWikiFontStyles($content);
$arr = explode("\n", $content);
$arr[] = ''; // for ensuring itemize/enumerate are closed properly
$i = 0;
$inEnum = '';
foreach($arr as $a) {
$new[$i] = '';
preg_match('/^([\*#]*)(.*)$/', $a, $match);
$enumPrefix = $match[1];
$enumSuffix = $match[2];
$commonEnumPrefix = getCommonPrefix($enumPrefix, $inEnum);
while(strlen($inEnum) > strlen($commonEnumPrefix)) {
if($inEnum[strlen($inEnum)-1] == '#')
$new[$i] .= '\end{enumerate}'."\n";
else
$new[$i] .= '\end{itemize}'."\n";
$inEnum = substr($inEnum, 0, strlen($inEnum)-1);
}
while(strlen($inEnum) < strlen($enumPrefix)) {
if($enumPrefix[strlen($inEnum)] == '#')
$new[$i] .= '\begin{enumerate}'."\n";
else
$new[$i] .= '\begin{itemize}'."\n";
$inEnum .= $enumPrefix[strlen($inEnum)];
}
if(!empty($enumPrefix))
$new[$i] .= '\item ';
$new[$i] .= $enumSuffix."\n";
$i++;
}
$content = implode("\n", $new);
print($content);