1#!/usr/bin/perl -CSD
2use warnings;
3use strict;
4use v5.22;
5
6use File::Path qw(make_path remove_tree);
7use DateTime;
8use DateTime::Format::ISO8601;
9use File::pushd;
10use Text::VimColor;
11
12my %output_dir = (
13 'nekodev' => '../nekodev',
14 'blog.nekodev' => '../blog.nekodev',
15 'code.nekodev' => '../code.nekodev',
16 '' => '../tmp',
17 'tmpdir' => '../tmp'
18);
19
20my %src_dirs = (
21 'code-tmp' => 'code-tmp',
22 'blog' => 'blog',
23 'web' => 'web'
24);
25
26
27my $blog_index = $output_dir{'tmpdir'}.'/blog_index';
28
29sub make {
30 foreach(@_) {
31 system($^X, '-CSD', "../webcc", $_) if (-f $_);
32 #`perl -d webcc $_` if (-f $_);
33 if (-d $_) {
34 print "Processing directory $_\n";
35
36 my $dirGlob = $_.'/*';
37 make(glob $dirGlob);
38 }
39 }
40}
41
42sub make_code_highlight {
43
44 foreach(@_) {
45 if (-f $_) {
46 #$highlighter->syntax_mark_file($_);
47 # need to overwrite vim options, as nvim doesn't provide -X flag
48 my @vim_opts = [qw( -RZ -i NONE -u NONE -N -n ), "+set nomodeline"];
49 my @extra_vim_opts = ['+setg number', '+set printoptions=number:y'];
50 my $highlighter = Text::VimColor->new(
51 file => $_,
52 all_syntax_groups => 1,
53 vim_command => 'nvim',
54 vim_options => @vim_opts,
55 extra_vim_options => @extra_vim_opts
56 );
57
58 my $modDate = DateTime->from_epoch( epoch => (stat $_)[9])->iso8601();
59 my $outFileName = $_ =~ s/^code\//code-tmp\//r;
60 $outFileName .= ".code";
61 my ($dir,$saneFileName) = $_ =~ /(.*)\/(.*)/;
62 make_path($dir);
63 print "outputting file $outFileName\n";
64
65 open my $out_file, '>', $outFileName or die "Can't open out file $outFileName: $!";
66 print {$out_file} "[title]\n$saneFileName\n[date]\n$modDate\n[content]\n<pre>\n";
67
68 $saneFileName =~ s/\./_/g;
69 my $lineNr = 1;
70 foreach(split /^/, $highlighter->html) {
71 my $lineId = 'L'.$lineNr.$saneFileName;
72 print {$out_file} "<span id=\"$lineId\" class=\"synLineNr\">$lineNr </span>".$_;
73 $lineNr++;
74 }
75 print {$out_file} "</pre>\n";
76 close $out_file;
77 }
78
79 if (-d $_) {
80 print "Processing directory $_\n";
81 my $dirGlob = $_.'/*';
82 make_code_highlight(glob $dirGlob);
83 }
84 }
85}
86
87
88# setup tmpdirs
89{
90 my $dir = pushd($src_dirs{'blog'});
91 remove_tree($output_dir{'tmpdir'});
92 make_path($output_dir{'tmpdir'});
93}
94
95make_code_highlight('code');
96
97foreach(values %src_dirs)
98{
99 my $dir = pushd($_);
100 make(glob '*');
101}
102
103
104#make(@ARGV);
105
106# build blog.nekodex index.html
107{
108my $dir = pushd('tmp');
109my %post2title;
110my %post2date;
111
112open (my $FH, '<', $blog_index) or die "can't open blog_index file, no blog post or serious error: $!";
113while( my $post = <$FH> ) {
114 chomp $post;
115
116
117 chomp( my $title = <$FH>);
118 chomp( my $date = <$FH>);
119
120 if( $date ne '' and $title ne '' )
121 {
122 $post2title{$post} = $title;
123 $post2date{$post} = $date;
124 }
125}
126close $FH;
127
128# traverse by date
129my $date_index = <<EOF;
130[title]
131Blogposts nach Datum sortiert
132[content]
133EOF
134
135my $lastMonth;
136foreach my $post (sort { $post2date{$a} cmp $post2date{$b} } keys %post2date) {
137 my $date_str = $post2date{$post};
138 my $date = DateTime::Format::ISO8601->parse_datetime( $date_str );
139 # format with locale dependent text
140 $date->set_locale('de-DE');
141 my $localeDate_str = $date->strftime('%A, %d. %B %Y, %H:%M');
142
143 my $curMonth = $date->strftime('%B %Y');
144 if (defined $lastMonth) {
145 if ($curMonth ne $lastMonth) {
146 $date_index .= "</ul><li><time datetime=<time datetime=$date_str>$curMonth</time></li><ul>\n";
147 $lastMonth = $curMonth;
148 }
149 } else {
150 $date_index .= "<ul><li><time datetime=<time datetime=$date_str>$curMonth</time></li><ul>\n";
151 $lastMonth = $curMonth;
152 }
153
154 my $pattern = $output_dir{'blog.nekodev'}."\/";
155 my $linkPath = $post;
156 $linkPath =~ s/$pattern//;
157 $date_index .= "<li><a href=\"$linkPath\">$post2title{$post} | <time datetime=$date_str>$localeDate_str</time></a></li>\n";
158}
159$date_index .= "</ul></ul>\n";
160
161# We're in tmpdir
162my $tmp_blog_index = 'date_index.blog';
163
164open (my $date_index_fh, '>', $tmp_blog_index) or die "can't open date_index file: $!";
165print {$date_index_fh} $date_index;
166close $date_index_fh;
167
168system($^X, '-CSD', "../webcc", $tmp_blog_index);
169
170
171# build alphabeticat index of posts
172my $alphaIndex =<<EOF;
173[title]
174Blogposts alphabetisch sortiert
175[content]
176<ul>
177EOF
178
179foreach my $post (sort { $post2title{$a} cmp $post2title{$b} } keys %post2title) {
180 my $pattern = $output_dir{'blog.nekodev'}."\/";
181 my $linkPath = $post;
182 $linkPath =~ s/$pattern//;
183 $alphaIndex .= "<li><a href=\"$linkPath\">$post2title{$post}</li>\n";
184}
185$alphaIndex .= "</ul>\n";
186
187
188# We're in tmpdir
189my $tmp_alpha_index = 'alpha_index.blog';
190
191open (my $alpha_index_fh, '>', $tmp_alpha_index) or die "can't open alpha_index file: $!";
192print {$alpha_index_fh} $alphaIndex;
193close $alpha_index_fh;
194
195system($^X, '-CSD', "../webcc", $tmp_alpha_index);
196}