-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.php
264 lines (247 loc) · 10 KB
/
config.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
<?php
use Illuminate\Support\Str;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Translation\FileLoader;
use Illuminate\Translation\Translator;
use TightenCo\Jigsaw\Collection\CollectionItem;
use Symfony\Component\Yaml\Yaml;
$LANGUAGES;
function getLanguages()
{
global $LANGUAGES;
if (is_null($LANGUAGES)) {
$global_config = Yaml::parseFile("global-config.yaml");
$LANGUAGES = $global_config['languages'];
}
return $LANGUAGES;
}
/**
* Get all collections in the desired languages
*
* @return array
*/
function getMultilangCollections()
{
$collections = [];
$languages = getLanguages();
foreach ($languages as $langKey => $lang) {
// posts
$collections['posts_' . $lang] = [
'author' => 'Tim Bernhard', // Default author, if not provided in a post
'comments' => true,
'sort' => '-date',
'language' => $lang,
'extends' => '_layout.post',
'path' => 'blog/' . $lang . '/{date|Y}/{slug}',
'filter' => function ($post) {
return !$post->draft;
},
'webmentions' => function (CollectionItem $post) {
$path = $post->getUrl();
$path = str_replace(['https://', 'www.genieblog.ch', 'index.html'], "", $path);
$path = trim($path, '/');
$commentsFile = __DIR__ . '/source/data/webmentions/' . $path . '.json';
$counts = ['all' => 0, 'in-reply-to' => 0, 'like-of' => 0, 'repost-of' => 0, 'bookmark-of' => 0, 'mention-of' => 0, 'rsvp' => 0];
$data = [];
if (file_exists($commentsFile)) {
$data = json_decode(file_get_contents($commentsFile), true);
if ($data === null) {
throw new Exception("Data in comments file " . $commentsFile . " is emtpy and/or JSON malformed.");
$data = [];
}
// process data minimally
foreach ($data as $key => $value) {
$counts[$value['wm-property']] += 1;
}
$counts['all'] = count($data);
}
return [
'count' => $counts,
'raw' => $data
];
},
/**
* Function to check whether a post is old.
*/
'isOld' => function (CollectionItem $post, $dayDiff = 730) {
/**
* Check the date the post was updated:
* If its update date is declared, take that one
*/
if (property_exists($post, 'updated')) {
$postDate = $post->updated;
} else {
/**
* If not though, check if we can use the files modification date.
* Fall back to the default date of the post
*/
$sourceFile = $post->getSource() . '/' . $post->getFilename() . '.md';
if (file_exists($sourceFile)) {
$postDate = filemtime($sourceFile);
} else {
$postDate = $post->date;
}
}
/**
* Finally, convert the dates before checking the difference
*/
if (!$postDate instanceof \DateTime) {
if (is_int($postDate)) {
$timestamp = $postDate;
$postDate = new \DateTime();
$postDate->setTimestamp($timestamp);
} else {
$postDate = new \DateTime($postDate);
}
}
$now = new \DateTime();
return $postDate->diff($now)->days > $dayDiff;
}
];
// categories
$collections['categories_' . $lang] = [
'path' => 'blog/' . $lang . '/categories/{_filename}',
'language' => $lang,
'translations' => false, // by default, we do not translate categories
'posts' => function ($page, $allPosts) use ($lang) {
if (!$allPosts) {
// echo "AllPosts = " + $allPosts;
return [];
}
return $allPosts->filter(function ($post) use ($page, $lang) {
if ($post->categories) {
return in_array($page->title, $post->categories, true) && $post->language === $lang;
}
return false;
});
},
];
// pages
$collections['pages_' . $lang] = [
'author' => 'Tim Bernhard',
'language' => $lang,
'sort' => '-filename',
'extends' => '_layout.page',
'path' => 'pages/' . $lang . '/{slug}'
// 'path' => 'attempt/' . $lang . '/{filename}'
];
}
// $collections['posts'] = $collections['posts_en'];
return $collections;
}
return [
'baseUrl' => '',
'production' => false,
'requiresCanonical' => true,
'siteName' => 'genieblog.ch',
'siteDescription' => 'A genius for a genius',
'siteAuthor' => 'Tim Bernhard',
'languages' => getLanguages(),
'language' => 'en', // default language, if undefined for some reason
// might be bool or array: if array, have keys the languages, as values the path to the other language
// TODO: don't rely on URL/path, as it might be different for different environments
'translations' => true,
// a pseudo-mechanism to hide certain posts from search & sitemap.
// currently, they are still listed in the blog pagination though
'findable' => true,
// collections
'collections' => getMultilangCollections(),
// helpers
'translateUrl' => function ($page, $targetLanguage) {
$url = $page->getUrl();
if ($page->language === $targetLanguage) {
return $url;
}
if (is_array($page->translations)) {
// if there is a '/' in the translation, it is an URL/path
if (Str::contains($page->translations[$targetLanguage], '/')) {
return $page->baseUrl . $page->translations[$targetLanguage];
}
// otherwise, it is the translated slug
// in which case we have to rebuild the URL.
// For a blog post, e.g., according to the scheme defined above:
// blog/' . $lang . '/{date|Y}/{slug}'
// the replacement here is rather opinionated:
// expects /$lang/ as well as slug being last part of URL.
// First, replace the language as needed,
$rep1 = str_replace("/$page->language/", "/$targetLanguage/", $url);
// then, make sure the URL ends in slug: replace some possibly distracting stuff
$rep2 = preg_replace('/index.html$/', '', $rep1);
$rep3 = rtrim($rep2, '/');
// finally, assemble new
return dirname($rep3) . '/' . $page->translations[$targetLanguage] . '/';
}
// replace all possible occurences of the language in the file name/path
$rep1 = str_replace("/" . $page->language . "/", "/" . $targetLanguage . "/", $url);
$rep2 = str_replace("." . $page->language . ".", "." . $targetLanguage . ".", $rep1);
$rep3 = str_replace("index." . $page->language, "index." . $targetLanguage, $rep2);
return str_replace("." . $page->language . "/", "." . $targetLanguage . "/", $rep3);
},
'hasTranslation' => function ($page, $targetLanguage) {
// check whether a certain translation is existing/available/accessible
if ($page->language == $targetLanguage) {
return true;
}
if ($page->translations === false) {
return false;
}
if (is_array($page->translations)) {
return array_key_exists($targetLanguage, $page->translations);
}
return true;
},
'translate' => function ($page, $key, $locale = null) {
if (is_null($locale)) {
$locale = $page->language;
}
// Prepare the FileLoader
$loader = new FileLoader(new Filesystem(), './source/_assets/translations');
// Register the Translator
$translator = new Translator($loader, $locale);
return $translator->get($key);
},
'getDate' => function ($page) {
return Datetime::createFromFormat('U', $page->date);
},
'getExcerpt' => function ($page, $length = 255) {
if ($page->excerpt) {
return $page->excerpt;
}
$content = preg_split('/<!-- more -->/m', $page->getContent(), 2);
$cleaned = trim(
strip_tags(
preg_replace(['/<pre>[\w\W]*?<\/pre>/', '/<h\d>[\w\W]*?<\/h\d>/'], '', $content[0]),
'<code>'
)
);
if (count($content) > 1) {
return $content[0];
}
$truncated = substr($cleaned, 0, $length);
if (substr_count($truncated, '<code>') > substr_count($truncated, '</code>')) {
$truncated .= '</code>';
}
return strlen($cleaned) > $length
? preg_replace('/\s+?(\S+)?$/', '', $truncated) . '...'
: $cleaned;
},
'getShortTitle' => function ($page, $limit = 32, $break = " ", $pad = "...") {
$string = $page->title;
// return with no change if string is shorter than $limit
if (strlen($string) <= $limit) {
return $string;
}
$string = substr($string, 0, $limit);
if (false !== ($breakpoint = strrpos($string, $break))) {
$string = substr($string, 0, $breakpoint);
}
return $string . $pad;
},
'isActive' => function ($page, $path) {
return Str::endsWith(trimPath($page->getPath()), trimPath($path));
},
'underscorify' => function ($page, $path) {
$rep1 = str_replace(" ", "_", strtolower($path));
return str_replace("-", "_", $rep1);
}
];