-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki.php
119 lines (104 loc) · 4.17 KB
/
wiki.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
<?php
define('DEBUG', 0);
function findBaseType($match) {
$match = str_replace('Link', '', $match);
return strtolower($match);
}
function makeLowerCaseLink($string) {
return "[[$string|".strtolower($string).']]';
}
function generateVariants(&$replacements, $type, $value) {
switch($type) {
case 'icon':
$replacements[$type] = basename($value, '.DDS').'.png';
break;
case 'recipes':
$recipes = '';
foreach($value as $category => $item) {
switch($category) {
case 'Craft':
foreach($item as $recipe) {
$temp = '{{PoC-Craft|';
foreach($recipe['ingredients'] as $ingredient) {
$temp .= $ingredient['name'].','.$ingredient['quantity'].';';
}
$temp .= $recipe['quantity'].($recipe['unlock'] ? '|blueprint=yes' : '').'}}';
}
$recipes .= $recipes ? "\n".$temp : $temp;
break;
case 'Refine':
$temp = '{{PoC-Refine';
foreach($item as $recipe) {
$temp .= '|';
foreach($recipe['ingredients'] as $ingredient) {
$temp .= $ingredient['name'].','.$ingredient['quantity'].';';
}
$temp .= $recipe['quantity'].';'.$recipe['time'].'%'.$recipe['name'];
}
$temp .= '}}';
$recipes .= $recipes ? "\n".$temp : $temp;
break;
case 'Cook':
$temp = '{{PoC-Cook';
foreach($item as $recipe) {
$temp .= '|';
foreach($recipe['ingredients'] as $ingredient) {
$temp .= $ingredient['name'].','.$ingredient['quantity'].';';
}
$temp .= $recipe['quantity'].';'.$recipe['time'].'%'.$recipe['name'];
}
$temp .= '}}';
$recipes .= $recipes ? "\n".$temp : $temp;
break;
default:
die('Unknown recipe action: '.$recipe['action']);
}
}
$replacements[$type] = rtrim($recipes);
break;
case 'type':
case 'subtitle':
$replacements[ucfirst($type)] = $value;
$replacements[$type] = strtolower($value);
$replacements[$type.'Link'] = makeLowerCaseLink($value);
break;
default:
$replacements[$type] = $value;
}
}
$conf = json_decode(file_get_contents('wikiConf.json'), TRUE);
$items = json_decode(file_get_contents('items.json'), TRUE);
$template = file_get_contents('template.wiki');
$pattern = '`µ£(?<tag>.+?)£µ`';
if(preg_match_all($pattern, $template, $matches) === FALSE) die("Error: Failed to match patterns in template file\n");
$types = [];
foreach($matches['tag'] as $match) {
$types[] = findBaseType($match);
}
$types = array_unique($types);
if(!file_exists('pages')) mkdir('pages');
$generated = [];
foreach($items as $id => $item) {
$replacements = [];
foreach($item as $key => $value) {
if(!in_array($key, $types, TRUE)) continue;
if(is_string($value) && isset($conf[$value])) $value = $conf[$value];
generateVariants($replacements, $key, $value);
}
if(DEBUG) var_dump($replacements);
if(!isset($replacements['name'])) die("No name key for item $id");
$output = preg_replace_callback($pattern,
function ($matches) use (&$replacements) {
return $replacements[$matches['tag']];
}
, $template);
$name = $item['name']."_$id";
file_put_contents("pages/$name", $output);
echo "Generated $name\n";
if(DEBUG) var_dump($output);
$generated[] = $name;
}
echo 'Output '.count($generated)." items:\n";
sort($generated);
var_dump($generated);
?>