-
Notifications
You must be signed in to change notification settings - Fork 4
/
gensite.pl
executable file
·420 lines (354 loc) · 10.4 KB
/
gensite.pl
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/usr/bin/perl
=for comment
Copyright 2021 Jesse Allen
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
=cut
use warnings;
use strict;
use lib 'lib';
use Cwd;
use File::Basename;
use Text::Template;
use Text::Markdown 'markdown';
use YAML::Tiny;
use GreyMatterExtractor;
my $outdir = "out";
my $STATIC_FOLDER = "src/static";
my $TEMPLATE_FOLDER = "src/templates";
my $CONTENT_FOLDER = "src/content";
my @STATIC_FOLDER_ASSETS = ("css", "img", "fonts");
mkdir($outdir);
foreach my $folder (@STATIC_FOLDER_ASSETS) {
symlink("../$STATIC_FOLDER/$folder","$outdir/$folder");
}
my $template_file;
my $template;
my $article_in;
my $outfile;
my $article_folder;
my %vars;
my %default_vars = (
title => 'Seven Kindoms Ancient Adversaries',
description => 'Seven Kingdoms Ancient Adversaries is the real-time strategy game designed by Trevor Chan, now freely available and maintained by the community.',
base_path => '/',
forum_path => '/',
game_version => '2.15.6',
);
if (@ARGV && $ARGV[0] eq '-local') {
$default_vars{base_path} = "file://" . getcwd . "/out/";
}
my @articles = ();
gen_download_index();
gen_news_index();
my @content_folder = get_content_directories($CONTENT_FOLDER);
my @content_main_files = get_content_files($CONTENT_FOLDER);
for (@content_main_files){
#print "content_main_files : is $_\n";{}
if($_ eq "index.html"){
next;
}
my $output_name = replace_extension_to_html($_);
#print ("$string \n");
push(@articles, ["{title}", $_, $output_name] );
}
for (@content_folder){
#print "\$_ is $_\n";
my $curent_folder = $_;
my @content_folder_files = get_content_files("$CONTENT_FOLDER/$_");
for (@content_folder_files){
my $output_name = replace_extension_to_html($_);
push(@articles, ["{title}", "$curent_folder/$_", "$curent_folder/$output_name", $curent_folder, $curent_folder] );
if(! -d "$outdir/$curent_folder"){
mkdir("$outdir/$curent_folder")
}
#print "\t\t\ content_folder_files -> \$_ is $_ and output_name : $curent_folder/$output_name\n";
}
}
init_index();
write_page();
init_article();
while(next_article()) {
write_page();
}
exit 0;
sub init_vars {
return %default_vars;
}
sub init_partial {
my ($partial_name, %template_variables) = @_;
my $template_file = "$TEMPLATE_FOLDER/$partial_name";
my $template = Text::Template->new(TYPE => 'FILE', SOURCE => $template_file);
return $template->fill_in(HASH => \%template_variables);
}
sub init_partial_from_string {
my ($template_content, %template_variables) = @_;
my $template = Text::Template->new(TYPE => 'STRING', SOURCE => $template_content);
return $template->fill_in(HASH => \%template_variables);
}
sub prepare_view {
my ($view_name, $is_home) = @_;
$template_file = "$TEMPLATE_FOLDER/$view_name";
$template = Text::Template->new(TYPE => 'FILE', SOURCE => $template_file);
%vars = init_vars();
$vars{menu} = init_partial("_menu.html", (init_vars(), home => $is_home));
}
sub init_index {
prepare_view("index.html", 1);
$vars{title} = "Seven Kingdoms Ancient Adversaries";
$vars{latest_news} = init_partial("_news_index.html", (news_list => gen_news_card("src/content/news", 3)));
$article_in = "$CONTENT_FOLDER/index.md";
$outfile = "$outdir/index.html";
}
sub init_article {
prepare_view("article.html", 0);
$article_in = "";
$outfile = "";
}
sub next_article {
if (!@articles) {
return 0;
}
my $params = shift(@articles);
#TODO: Remove, now from GrayMatter header
$vars{title} = $params->[0];
$article_in = "$CONTENT_FOLDER/$params->[1]";
$outfile = "$outdir/$params->[2]";
$article_folder = $params->[3];
$vars{category} = '';
if( defined $article_folder ) {
$vars{category} = $article_folder;
}
# if(defined $article_folder){
# print("[INFO] FOLDER : $article_folder\n");
# print("[INFO] Preparing MENU on article \n");
# $vars{menu} = init_partial("_menu.html", (home => 0, relative_folder=>$article_folder) );
# }
return 1;
}
sub gen_download_index {
my $fh;
if (!open($fh, ">", "src/content/download/index.md")) {
return;
}
my $latest_version_file = "v$default_vars{game_version}";
my $latest_download_content = '';
my %latest_download_data = process_content_file("src/content/download/$latest_version_file.md");
$latest_download_content = $latest_download_content.init_partial("_download_entry.html", (spread_hash($latest_download_data{header}), content => $latest_download_data{body}));
print $fh <<EOF;
---
title: 'Get the latest version of the game'
---
## Download seven kingdoms for Windows, Linux and MacOS, and start play with your friends (or foes)
${latest_download_content}
### Not the version you are looking for?
<details>
<summary>Older versions</summary>
EOF
foreach my $name (sort{$b cmp $a} map{basename($_,'.md')} glob("src/content/download/*.md")) {
# if ($name eq 'index' || $name eq 'all') {
if ($name eq 'index' or $name eq $latest_version_file or $name eq 'all') {
next;
}
print $fh "[$name]($name.html)<br>\n";
}
print $fh "\n[Links to all files](all.html)\n";
print $fh <<EOF;
</details>
EOF
close($fh);
}
sub gen_news_index {
my $news_base_path = "src/content/news";
my $fh;
if (!open($fh, ">", "$news_base_path/index.md")) {
return;
}
print $fh <<EOF;
---
title: 'News'
---
EOF
my $news_list = gen_news_card($news_base_path);
print $fh init_partial("_news_index.html", (news_list => $news_list));
close($fh);
}
sub gen_news_card {
my ($path, $limit_count) = @_;
my @news_files = get_sorted_files_by_name($path, ".md", "*.md");
my $limit_to = $#news_files;
my $iteration = 0;
if(!defined $limit_count){
$limit_count = 0;
}
if($limit_count > 0 ){
$limit_to = $limit_count;
}
my $news_list = '';
foreach my $name (@news_files) {
if ($name eq 'index') {
next;
}
last if($limit_to == $iteration );
my %news_entry = process_content_file("$path/$name.md");
$news_list = $news_list.init_partial("_news_card.html", (spread_hash($news_entry{header}), content => $news_entry{body}));
$iteration++;
}
return $news_list;
}
sub get_sorted_files_by_name {
my ($path, $filename, $filepattern) = @_;
return (sort{$b cmp $a} map{basename($_,'.md')} glob("$path/*.md"));
}
sub is_file_newer {
my $a = (stat($_[0]))[9];
my $b = (stat($_[1]))[9];
return $a > $b;
}
sub is_up_to_date {
if (! -e $outfile) {
return 0;
}
if ($article_in ne "") {
if (! -e $article_in) {
die "The file $article_in does not exists";
}
if (!is_file_newer($outfile, $article_in)) {
return 0;
}
}
return is_file_newer($outfile, $template_file);
}
sub slurp {
my $file = $_[0];
my $fh;
if (!open($fh, '<', $file)) {
print "Unable to read temp file";
return "";
}
local $/ = undef;
my $cont = <$fh>;
close $fh;
return $cont;
}
sub write_page {
if ($outfile eq "") {
die;
}
if (is_up_to_date($outfile)) {
return;
}
if ($article_in ne "") {
my %article_processed = process_content_file($article_in);
$vars{article} = init_partial_from_string($article_processed{body}, %vars);
my $config = $article_processed{header};
# TODO: How to add automatically the values of the hash to the vars hash?
if ( defined $config->{title}){
#print "# YAML HEADER \n $config->{title}";
$vars{title} = $config->{title};
$vars{slug} = $config->{slug};
## TODO: Find a better way to draw per page custom variables
if ( defined $vars{category} && $vars{category} eq 'news' ){
$vars{date} = $config->{date};
$vars{author} = $config->{author};
} else {
$vars{date} = undef;
$vars{author} = undef;
}
}else{
print "\t[II] No header Info (YAML) for $article_in\n"
}
}
$vars{head} = init_partial("_head.html", %vars);
my $fh;
if (!open($fh, ">", $outfile)) {
return;
}
print $fh $template->fill_in(HASH => \%vars);
close($fh);
#print "\t--> OK\n";
return;
}
#** @function get_content_directories ($directory_to_check)
# @brief Try to read the directory and return only the directories in it
#
# @params $directory_to_check The directory to check
# @retval @detected_directories All the directories readed (not recursive)
#*
sub get_content_directories {
my ($directory_to_check) = @_;
my @detected_directories = ();
opendir(my $dir_handler, $directory_to_check) || die "Can't open directory: $!\n";
while (my $dir_entry = readdir($dir_handler)) {
next if should_skip_current_prev_dir($dir_entry);
if(-d "$directory_to_check/$dir_entry" ){
push(@detected_directories, $dir_entry );
}
}
closedir($dir_handler);
return @detected_directories;
}
sub get_content_files {
my ($directory_to_check) = @_;
my @detected_files = ();
opendir(my $dir_handler, $directory_to_check) || die "Can't open directory: $!\n";
while (my $dir_entry = readdir($dir_handler)) {
next if should_skip_current_prev_dir($dir_entry);
if(-f "$directory_to_check/$dir_entry" ){
push(@detected_files, $dir_entry );
}
}
closedir($dir_handler);
return @detected_files;
}
sub should_skip_current_prev_dir {
my ($directory_to_check) = @_;
return $directory_to_check eq "." || $directory_to_check eq "..";
}
sub replace_extension_to_html {
my ($output_name) = @_;
if(! defined $output_name){
return @_;
}
# Try match and replace the last ".extension "
$output_name =~ s/(\.\w.$)\b/.html/io;
return $output_name;
}
sub process_content_file {
my ($input) = @_;
if ($input eq ""){
print "\t[WW] Unable to read $input\n";
return;
}
my %input_matter = read_header($input);
my $yamls = undef;
# Prevent die thanks to https://stackoverflow.com/a/451236
eval { $yamls = YAML::Tiny->read_string( $input_matter{Header} ) }; warn "\t[WW] Unable to read YAML for $input\n" if $@;
if( !defined $yamls )
{
print "\t[II] YAML Header is not defined\n";
}
my $testBody = $input_matter{Body};
my $config = $yamls->[0];
if ( !defined $config ){
print "\t[II] No header Info (YAML) for $input\n";
}
# TODO: Read format from original file (ex. could be a markdown or html)
return (
"header" => $config,
"body" => markdown($testBody),
);
}
sub spread_hash {
# TODO: Validate hash
my ($input) = @_;
return %$input;
}