author | Pranshu Sharma <pranshu@bauherren.ovh> |
Tue, 17 Dec 2024 01:49:39 +1000 | |
changeset 8 | ee9b57cbbefc |
parent 7 | f00ed34eca17 |
child 9 | 58b72fea234c |
permissions | -rwxr-xr-x |
0 | 1 |
#!/usr/bin/perl |
1
0b7be2e78d3b
Added more documentation
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
0
diff
changeset
|
2 |
# Author: Pranshu Sharma <pranshu@bauherren.ovh> |
0 | 3 |
|
4 |
use v5.36.0; |
|
5 |
use strict; |
|
6 |
use warnings; |
|
7 |
||
8 |
use XML::LibXML; |
|
9 |
use File::Find; |
|
10 |
use File::Copy qw(copy move); |
|
11 |
use Cwd; |
|
12 |
use experimental qw(declared_refs); |
|
13 |
use Data::Dumper; |
|
5 | 14 |
use POSIX; |
7 | 15 |
# !! Add seperatio between title and identifier |
0 | 16 |
|
17 |
# Fix up CLI interface |
|
18 |
# Add option ro set cache to 0 through command line |
|
19 |
# Print required elisp code it is in $req_config |
|
20 |
# Add config option for $max_rss and $max_cat |
|
21 |
# Sort cataogirs by entires |
|
8 | 22 |
# Document link to file x from file x no work |
0 | 23 |
# Variable for map_css |
4
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
24 |
# Remove the desc |
0 | 25 |
# Domain thing as well |
4
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
26 |
# 2 articles 1 day better sorting |
7 | 27 |
# cataogry linking |
28 |
# reinforce slash ending for $oudir |
|
0 | 29 |
my $cache = 1; |
30 |
||
31 |
my $config_file = "genorg-conf"; |
|
32 |
my $dir = "blog"; |
|
33 |
my $outdir = "out/"; |
|
34 |
my $emacs = "emacs"; |
|
8 | 35 |
my $art_css = "astyle.css"; |
36 |
my $cat_css ="cstyle.css"; |
|
0 | 37 |
my $no_name_dir = "c"; |
4
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
38 |
my $cache_file = "genorg-cache/cache.xml"; |
0 | 39 |
my $domain = "p.bauherren.ovh"; |
40 |
my $max_rss = 30; |
|
41 |
# The first page has one less article. Think of it as a feature. |
|
5 | 42 |
my $max_cat = 7; |
8 | 43 |
my $main_title= ""; |
44 |
my $blog_title = "Pranshu's blog"; |
|
0 | 45 |
|
46 |
my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid ($<); |
|
47 |
my $template = <<"END"; |
|
48 |
#+title: Test document |
|
49 |
#+subtitle: This is the subtitle |
|
50 |
#+author: $username |
|
51 |
#+keywords: tag thing | related |
|
52 |
#+options: html-link-use-abs-url:nil html-postamble:auto |
|
53 |
#+options: html-preamble:t html-scripts:nil html-style:t |
|
54 |
#+options: html5-fancy:nil tex:t |
|
55 |
#+options: tex:mathjax |
|
56 |
#+html_doctype: html5 |
|
57 |
#+html_container: div |
|
58 |
#+html_content_class: content |
|
59 |
#+html_link_home: |
|
60 |
#+html_link_up: |
|
61 |
#+html_mathjax: |
|
62 |
#+html_equation_reference_format: \eqref{%s} |
|
63 |
#+html_head: |
|
64 |
#+html_head_extra: |
|
65 |
#+infojs_opt: |
|
66 |
#+creator: <a href="https://www.gnu.org/software/emacs/">Emacs</a> 31.0.50 (<a href="https://orgmode.org">Org</a> mode 9.7.11) |
|
67 |
#+latex_header: |
|
68 |
END |
|
69 |
||
70 |
my $req_config = <<"END"; |
|
6
c576e88fef13
It's starting to feel messy now
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
5
diff
changeset
|
71 |
(require 'org) |
c576e88fef13
It's starting to feel messy now
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
5
diff
changeset
|
72 |
(require 'ob) |
c576e88fef13
It's starting to feel messy now
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
5
diff
changeset
|
73 |
(org-link-set-parameters "genorg" |
0 | 74 |
:follow nil |
75 |
:export |
|
76 |
#'(lambda (link desc _ _) |
|
6
c576e88fef13
It's starting to feel messy now
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
5
diff
changeset
|
77 |
(format "<a class=\"rakim\" href=\"%s\">%s</a>" desc link)) |
c576e88fef13
It's starting to feel messy now
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
5
diff
changeset
|
78 |
:store nil) |
0 | 79 |
END |
80 |
||
6
c576e88fef13
It's starting to feel messy now
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
5
diff
changeset
|
81 |
my $custom_code = "(list 1)"; |
5 | 82 |
|
8 | 83 |
my $navbar = <<"END" =~ s/\n\s+//gr; |
84 |
<ul> |
|
85 |
<li> Home |
|
86 |
<li> Catagories |
|
87 |
<li> About |
|
88 |
</ul> |
|
89 |
END |
|
90 |
||
0 | 91 |
my $elisp_code = <<"END" =~ s/\n\s+//gr; |
92 |
(let ((ls '(LIST))) |
|
93 |
(require 'org) |
|
94 |
(mapc |
|
95 |
(lambda (b) |
|
96 |
(with-current-buffer b |
|
97 |
(when (and (not (string-match "^ " (buffer-name b))) |
|
98 |
(eq major-mode 'org-mode)) |
|
99 |
(org-export-to-file 'html (pop ls))))) |
|
100 |
(buffer-list)) |
|
6
c576e88fef13
It's starting to feel messy now
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
5
diff
changeset
|
101 |
(kill-emacs) |
c576e88fef13
It's starting to feel messy now
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
5
diff
changeset
|
102 |
) |
0 | 103 |
END |
104 |
||
105 |
||
106 |
my ($h_intro, $h_c_intro, $h_cat_li, $h_chap, $h_en_cat, $h_en, $h_end) |
|
107 |
= split /SPLIT/ ,<<"END" =~ s/\n\s+//gr; |
|
108 |
<!DOCTYPE html> |
|
109 |
<html lang="en"> |
|
110 |
<head> |
|
111 |
<meta charset="UTF-8"> |
|
112 |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
113 |
<title>TITLE</title> |
|
8 | 114 |
<link rel="stylesheet" href="CSS"> |
0 | 115 |
</head> |
8 | 116 |
<body> |
117 |
NAV |
|
118 |
<h1> BTIT </h1> |
|
0 | 119 |
SPLIT |
120 |
<ul id="cataogries"> |
|
121 |
SPLIT |
|
122 |
<li> <a href="LINK"> CATAG (NUMBER)</a> |
|
123 |
SPLIT |
|
124 |
</ul> |
|
125 |
<div> |
|
8 | 126 |
<h2> All articles <a id="rss" href="rss.xml">(rss)</span> </h2> <hr> |
0 | 127 |
SPLIT |
128 |
<a href="LINK"> |
|
8 | 129 |
<strong> TITLE </strong> <span>DATE</span> <i>CATAG</i> |
0 | 130 |
<hr> |
131 |
</a> |
|
132 |
SPLIT |
|
133 |
<a href="LINK"> |
|
134 |
<strong> TITLE </strong> <span>DATE</span> |
|
135 |
<hr> |
|
136 |
</a> |
|
137 |
SPLIT |
|
138 |
</div> |
|
139 |
</body> |
|
140 |
</html> |
|
141 |
END |
|
142 |
||
143 |
my $help = <<"END"; |
|
144 |
genorg [option] [dir|file] |
|
145 |
Option can be: |
|
146 |
-t FILE : Make a template org file at FILE |
|
147 |
-h : Help |
|
148 |
-c FILE : sepcify a new config file instead of genorg-conf |
|
149 |
-r : unconditionally remake org files |
|
150 |
||
151 |
If no option is set, accept a directory to generate. |
|
152 |
||
153 |
$config_file follow the syntax of: |
|
154 |
VAR VALUE |
|
155 |
where VAR can be: |
|
156 |
emacs-path : The value should be path of emacs. 'emacs' by default. |
|
157 |
css : The path of the css file to use. |
|
158 |
in_dir : The directory that contains the files. |
|
159 |
out_dir : the output directory. |
|
160 |
no_name_dir: The directory for generate files. This means in_dir/no_name_dir |
|
161 |
cannot exist. |
|
162 |
END |
|
163 |
||
164 |
||
165 |
my $rss_entry = <<"END" =~ s/\n\s+//gr; |
|
166 |
<?xml version=\"1.0\" encoding=\"UTF-8\" ?> |
|
167 |
<rss version=\"2.0\"> |
|
168 |
<channel> |
|
169 |
<title>W3Schools Home Page</title> |
|
170 |
<link>https://www.w3schools.com</link> |
|
171 |
<description>Free web building tutorials</description>" |
|
172 |
END |
|
173 |
||
174 |
my %config_vars = |
|
175 |
( |
|
176 |
'emacs-path' => \$emacs, |
|
8 | 177 |
css => \$art_css, |
0 | 178 |
indir => \$dir, |
179 |
outdir => \$outdir, |
|
180 |
no_name_dir => \$no_name_dir |
|
181 |
); |
|
182 |
||
6
c576e88fef13
It's starting to feel messy now
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
5
diff
changeset
|
183 |
|
0 | 184 |
sub read_config ($path) { |
185 |
open my $fh, '<', $path or die "Couldn't open config file\n"; |
|
186 |
while (<$fh>) { |
|
187 |
next if /^\s+$/; |
|
6
c576e88fef13
It's starting to feel messy now
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
5
diff
changeset
|
188 |
if (/^>>---+/) { |
c576e88fef13
It's starting to feel messy now
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
5
diff
changeset
|
189 |
local $/; |
c576e88fef13
It's starting to feel messy now
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
5
diff
changeset
|
190 |
$custom_code = <$fh>; |
c576e88fef13
It's starting to feel messy now
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
5
diff
changeset
|
191 |
return; |
c576e88fef13
It's starting to feel messy now
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
5
diff
changeset
|
192 |
} |
0 | 193 |
chomp; |
194 |
my ($directive, $rest) = split /\s+/, $_, 2; |
|
195 |
if (exists($config_vars{$directive})) { |
|
196 |
${$config_vars{$directive}} = $rest; |
|
197 |
} else { |
|
198 |
print "Unknown variable $directive\n"; |
|
199 |
} |
|
200 |
} |
|
201 |
close $fh; |
|
202 |
} |
|
203 |
||
204 |
sub help_and_bye { |
|
205 |
print $help; |
|
206 |
exit; |
|
207 |
} |
|
208 |
||
209 |
sub template ($file) { |
|
210 |
$file // die "-t needs an argument.\n"; |
|
211 |
$file =~ s/\.org$//; |
|
212 |
open my $fh, '>', $file . ".org" or die $file . " couldn't be opened\n"; |
|
213 |
print $fh $template; |
|
214 |
close $fh; |
|
215 |
} |
|
216 |
||
217 |
# Reletive file from the perspect of file1, to file2 |
|
218 |
sub prel_path ($from, $to) { |
|
219 |
return '.' if $from eq $to; |
|
220 |
my \(@f1, @f2) = map [m{/[^/]+}g], ($from, $to); |
|
221 |
# return substr($f1[$#f1], 1) if $from =~ $to; |
|
222 |
while (@f1 && @f2 && $f1[0] =~ $f2[0]) { |
|
223 |
shift @f1; |
|
224 |
shift @f2; |
|
225 |
} |
|
226 |
my $p = @f1-1; |
|
227 |
local $" = ""; |
|
228 |
"../" x ($p > 0 ? $p : 0) . substr("@f2", 1); |
|
229 |
} |
|
230 |
||
231 |
my @org_exps; |
|
232 |
my @files_to_make; |
|
7 | 233 |
my @files_to_move; |
0 | 234 |
|
235 |
sub wanted { |
|
236 |
my $n_path = $outdir . prel_path ($dir, "$File::Find::name/"); |
|
237 |
# We need to mirror the direcotry structure |
|
238 |
if (-d) { |
|
239 |
push @files_to_make, $n_path || die "Couldn't make directory $n_path\n" |
|
240 |
unless -d $n_path; |
|
241 |
return; |
|
242 |
} |
|
243 |
if (/\.org$/) { |
|
244 |
push @org_exps, substr $n_path, length($outdir); |
|
245 |
} else { |
|
7 | 246 |
push @files_to_move, $File::Find::name |
0 | 247 |
} |
248 |
} |
|
249 |
||
3 | 250 |
my @modified_files; |
0 | 251 |
|
252 |
# Export @org_exps |
|
253 |
sub conv_files { |
|
254 |
my %changed_files; |
|
255 |
# Remove files that are already converted |
|
256 |
for my $rel_file (@org_exps) { |
|
257 |
# Emacs need the full file path |
|
258 |
my ($file, $to) = map { getcwd . "/$_" . $rel_file} ($dir, $outdir); |
|
259 |
s/org$/html/ for ($to, $rel_file); |
|
260 |
unless ($cache && -f $to && (stat($to))[9] > (stat($file))[9]) { |
|
261 |
$changed_files{$file} = $to; |
|
3 | 262 |
push @modified_files, $rel_file; |
0 | 263 |
} |
264 |
} |
|
265 |
%changed_files // exit; |
|
266 |
$elisp_code =~ s/LIST/join '', map "\"$_\" ", values %changed_files /e; |
|
6
c576e88fef13
It's starting to feel messy now
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
5
diff
changeset
|
267 |
open my $fh, '-|', ($emacs, '-Q','--eval', "(progn $custom_code)", keys %changed_files, "--eval" , "$elisp_code"); |
0 | 268 |
close $fh; |
269 |
\%changed_files |
|
270 |
} |
|
271 |
||
272 |
my $cdir; |
|
273 |
||
274 |
$ARGV[0] // die "No arguments provided\n"; |
|
275 |
while ($_ = shift @ARGV) { |
|
6
c576e88fef13
It's starting to feel messy now
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
5
diff
changeset
|
276 |
/^-c$/ && do { print "$req_config\n"; exit }; |
0 | 277 |
/^(-h|--?help)$/ && help_and_bye; |
278 |
/^-t$/ && do { template shift @ARGV ; exit }; |
|
279 |
/^-c$/ && do { $config_file = shift @ARGV; next }; |
|
280 |
/^-d$/ && do { $cache = 0; next }; |
|
281 |
$cdir = $_; |
|
282 |
} |
|
283 |
$cdir || die "directory not provided\n"; |
|
284 |
main(); |
|
285 |
||
286 |
my %catags; |
|
287 |
my @arts; |
|
3 | 288 |
my $cache_dom; |
0 | 289 |
|
290 |
sub main { |
|
8 | 291 |
$outdir =~ s!/*$!/!; |
292 |
-d $cdir or help_and_bye; |
|
293 |
mkdir $outdir; |
|
294 |
chdir $cdir or die "Couldn't access $cdir\n"; |
|
295 |
read_config $config_file; |
|
296 |
-d $dir or die "$dir doesn't exist \n"; |
|
297 |
find (\&wanted, $dir); |
|
298 |
for (@files_to_move) { |
|
299 |
copy $_, s#$dir/?#$outdir#er; |
|
300 |
} |
|
301 |
mkdir for @files_to_make; |
|
302 |
values %{conv_files()}; |
|
303 |
$cache_dom = XML::LibXML->load_xml(location => $cache_file) if $cache && -f $cache_file; |
|
304 |
chdir $outdir; |
|
305 |
@arts = sort { $b->{date} <=> $a->{date}} map { html_fixup($_) } @org_exps; |
|
306 |
finalise_html(); |
|
307 |
chdir '..'; |
|
308 |
populate_cache(); |
|
3 | 309 |
} |
310 |
||
311 |
sub populate_cache { |
|
312 |
my $dom = XML::LibXML::Document->new('1.0', 'UTF-8'); |
|
4
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
313 |
my $root = $dom->createElement('root'); |
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
314 |
my %c_els; |
3 | 315 |
for my $art (@arts) { |
4
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
316 |
# We need the: related, file, date |
8 | 317 |
my ($art_el, $date, $title, $ca, $related, $utitle) = |
318 |
map {$dom->createElement($_)} ("art", "date", "title", "catag", "related", "utitle"); |
|
4
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
319 |
$art_el->{path} = $art->{file}; |
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
320 |
$ca->appendText($art->{catag}); |
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
321 |
$title->appendText($art->{title}); |
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
322 |
$date->appendText($art->{date}->text_easy); |
8 | 323 |
$utitle->appendText($art->{utitle}); |
7 | 324 |
my @ll = @{$art->{links}}; |
325 |
if (@ll) { |
|
326 |
my $links = $dom->createElement('links'); |
|
327 |
for (@ll) { |
|
328 |
my $link = $dom->createElement("link"); |
|
329 |
$link->{catag} = $_->[0]; |
|
330 |
$link->appendText($_->[1]); |
|
331 |
$links->appendChild($link); |
|
332 |
} |
|
333 |
$art_el->appendChild($links); |
|
334 |
} |
|
4
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
335 |
my $rel_str; |
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
336 |
for my $thing ($art->{related}) { |
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
337 |
for (@{$thing}) { |
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
338 |
my ($catag, $artt) = @{$_}; |
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
339 |
my $rel_str .= "\"$catag\"" . ($artt && "->\"$artt\""); |
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
340 |
$related->appendText("\"$catag\"" . ($artt && "->\"$artt\" ")); |
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
341 |
} |
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
342 |
} |
8 | 343 |
$art_el->appendChild($_) for ($date, $ca, $related, $title, $utitle); |
4
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
344 |
$root->appendChild($art_el); |
3 | 345 |
} |
4
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
346 |
$root->appendChild($_) for values %c_els; |
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
347 |
$dom->setDocumentElement($root); |
5 | 348 |
open my $fh, '>' , $cache_file; |
4
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
349 |
print $fh ($dom->toString(1) =~ s/(.+)/$1\n<!-- Nicley formated :() -->/r); |
3 | 350 |
close $fh; |
0 | 351 |
} |
352 |
||
353 |
sub entry { |
|
354 |
my ($data, $catagory) = @_; |
|
355 |
my $str = defined $catagory |
|
356 |
? ($h_en_cat =~ s/CATAG/$data->{catag}/er) : $h_en; |
|
357 |
my $prel_path = "/$no_name_dir/" . (defined $catagory ? "c" : "c/b"); |
|
358 |
$str =~ s/DATE/$data->{date}->fmt/er |
|
8 | 359 |
=~ s/TITLE/$data->{utitle}/er |
0 | 360 |
=~ s|LINK|prel_path($prel_path, '/'. $data->{file})|er |
361 |
} |
|
362 |
||
363 |
sub rss_en ($data) { |
|
364 |
my $thing = <<"END" =~ s/\n\s+//gr; |
|
5 | 365 |
<item> |
0 | 366 |
<title>TITLE</title> |
367 |
<link>LINK</link> |
|
368 |
<description>DESC</description> |
|
369 |
</item> |
|
370 |
END |
|
8 | 371 |
$thing |
372 |
=~ s|LINK|"$domain/" . $data->{file}|er |
|
373 |
=~ s/TITLE/$data->{utitle}/er; |
|
0 | 374 |
} |
375 |
||
376 |
sub min ($x, $y) { |
|
377 |
($x, $y)[$x > $y] |
|
378 |
} |
|
379 |
||
2
c4ca65113229
Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
1
diff
changeset
|
380 |
# @_ has to be in format of [STRING, LINK] |
c4ca65113229
Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
1
diff
changeset
|
381 |
sub list2paginaiton { |
c4ca65113229
Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
1
diff
changeset
|
382 |
my $page = shift; |
c4ca65113229
Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
1
diff
changeset
|
383 |
'<ul>' . join ("", map { |
c4ca65113229
Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
1
diff
changeset
|
384 |
my ($str, $file) = @{$_}; |
8 | 385 |
$str = "<span class=\"psel\">$str</span>" if $file && ($file == $page); |
2
c4ca65113229
Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
1
diff
changeset
|
386 |
defined $file ? |
c4ca65113229
Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
1
diff
changeset
|
387 |
"<li><a href=\"$file.html\">$str</a>" : $str |
c4ca65113229
Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
1
diff
changeset
|
388 |
} ((["←", $page - 1]) x!! ($page - 1), |
c4ca65113229
Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
1
diff
changeset
|
389 |
@_, |
c4ca65113229
Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
1
diff
changeset
|
390 |
(["→", $page + 1]) x ($page != $_[$#_]->[1]))) . '</ul>' |
c4ca65113229
Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
1
diff
changeset
|
391 |
} |
c4ca65113229
Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
1
diff
changeset
|
392 |
|
c4ca65113229
Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
1
diff
changeset
|
393 |
sub move_on_if_neccasary ($fh, $art_num, $total_articles, $path) { |
c4ca65113229
Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
1
diff
changeset
|
394 |
return $fh if $art_num == 0 || $art_num % $max_cat; |
c4ca65113229
Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
1
diff
changeset
|
395 |
my $cpage = $art_num / $max_cat; |
5 | 396 |
print $fh panigation($cpage, ceil($total_articles / $max_cat), $path); |
2
c4ca65113229
Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
1
diff
changeset
|
397 |
open $fh, '>', $path . '/' . ($cpage + 1).'.html'; |
c4ca65113229
Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
1
diff
changeset
|
398 |
$fh; |
0 | 399 |
} |
400 |
||
3 | 401 |
sub panigation ($page, $no_pages, $dir) { |
402 |
if ($no_pages < 9) { |
|
403 |
list2paginaiton ($page, (map [$_, $_], (1..$no_pages))) |
|
404 |
} elsif (4 < $page <= ($no_pages - 4)) { |
|
405 |
list2paginaiton |
|
406 |
($page, |
|
407 |
[1, 1], |
|
408 |
["<li>..."], |
|
409 |
(map [$_, $_], (($page-2)..($page+2))), |
|
410 |
["<li>..."], |
|
411 |
[($no_pages) x 2]) |
|
412 |
} elsif ($page < 6) { |
|
413 |
list2paginaiton $page, |
|
414 |
((map [$_, $_], (1..5)), |
|
415 |
["<li>..."], |
|
416 |
[($no_pages) x 2]) |
|
417 |
} else { |
|
418 |
list2paginaiton $page, |
|
419 |
([1,1], |
|
420 |
["<li>..."], |
|
421 |
(map [$_, $_], (($no_pages - 5)..$no_pages))) |
|
422 |
} |
|
423 |
} |
|
424 |
||
7 | 425 |
sub get_art($cat, $id) { |
426 |
for (@arts) { |
|
427 |
return $_ if $_->{catag} eq $cat && $_->{title} eq $id |
|
428 |
} |
|
5 | 429 |
} |
430 |
||
8 | 431 |
sub cssbs($s,$t,$p){ |
432 |
$s =~ s|CSS|prel_path("/$p","/$cat_css")|er |
|
433 |
=~ s/TITLE|BTIT/$t/ger |
|
434 |
=~ s/NAV/$navbar/er; |
|
435 |
} |
|
436 |
||
0 | 437 |
sub finalise_html { |
438 |
mkdir $no_name_dir; |
|
439 |
open my $fh, '>', "$no_name_dir/1.html"; |
|
440 |
open my $mrss, '>', "$no_name_dir/rss.xml"; # The master rss file |
|
441 |
print $mrss $rss_entry; |
|
8 | 442 |
print $fh cssbs($h_intro . $h_c_intro, $blog_title, "a/c"); |
0 | 443 |
my (%c_files, %r_files); # Cataogry files and rss |
8 | 444 |
|
445 |
for my $cat (sort { $catags{$b} <=> $catags{$a} } keys(%catags)) { |
|
446 |
my $num = $catags{$cat}; |
|
0 | 447 |
# ($h_intro, $h_c_intro, $h_cat_li, $h_chap, $h_en_cat, $h_en, $h_end) |
448 |
print $fh $h_cat_li =~ s/NUMBER/$num/re |
|
3 | 449 |
=~ s/CATAG/$cat/er |
450 |
=~ s=LINK="$cat/1.html"=er; |
|
0 | 451 |
mkdir "$no_name_dir/$cat"; |
452 |
open $c_files{$cat}, '>', "$no_name_dir/$cat/1.html"; |
|
453 |
open $r_files{$cat}, '>', "$no_name_dir/$cat/rss.xml"; |
|
454 |
print {$r_files{$cat}} $rss_entry; |
|
8 | 455 |
print {$c_files{$cat}} |
456 |
cssbs($h_intro, "$cat <a id=\"rss\" href=\"rss.xml\">(rss)</span> ", "a/b/c"); |
|
0 | 457 |
} |
458 |
print $fh $h_chap; |
|
459 |
# :-) |
|
460 |
my $total_articles = () = map {(1)x$_} values %catags; |
|
461 |
# Now we iterate through the articles and add their index |
|
462 |
my ($n_fh, $n_mrss, %n_rss) = (0,0); |
|
463 |
my %n_cat; |
|
464 |
for my $art (@arts) { |
|
8 | 465 |
# TODO do we need $title? |
0 | 466 |
my ($catag, $title) = @{$art}{qw(catag title)}; |
467 |
my $rentry = rss_en $art; |
|
468 |
if (($n_rss{$catag} // 0) < $max_rss) { |
|
469 |
print {$r_files{$catag}} $rentry; |
|
470 |
++$n_rss{$catag}; |
|
471 |
} |
|
472 |
if ($n_mrss < $max_rss) { |
|
473 |
print $mrss $rentry; |
|
474 |
++$n_mrss |
|
475 |
} |
|
2
c4ca65113229
Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
1
diff
changeset
|
476 |
# ($fh, $art_num, $total_articles, $path) |
c4ca65113229
Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
1
diff
changeset
|
477 |
$fh = move_on_if_neccasary $fh, $n_fh, $total_articles, $no_name_dir; |
0 | 478 |
$n_fh++; |
479 |
print $fh entry $art, 1; |
|
3 | 480 |
my $ncat = \$n_cat{$catag}; |
481 |
++$$ncat; |
|
482 |
my $cfh = \$c_files{$catag}; |
|
483 |
$$cfh = move_on_if_neccasary $$cfh, $$ncat, $catags{$catag}, "$no_name_dir/$catag"; |
|
484 |
print {$$cfh} entry $art; |
|
485 |
my $a_file = $art->{file}; |
|
7 | 486 |
if (1 or grep /^$a_file$/, @modified_files) { |
487 |
my $doom = linkify($art); |
|
3 | 488 |
open my $h_file, '>', $a_file; |
7 | 489 |
print $h_file $doom->toStringHTML; |
3 | 490 |
close $h_file; |
491 |
} |
|
0 | 492 |
} |
5 | 493 |
print $fh panigation ((ceil $n_fh / $max_cat) x 2, $no_name_dir) if $max_cat < $total_articles; |
0 | 494 |
print $fh $h_end; |
495 |
close $fh; |
|
496 |
for (values %c_files) { |
|
497 |
print $_ $h_end; |
|
7 | 498 |
close $_; |
0 | 499 |
} |
500 |
print $mrss '</channel> </rss>'; |
|
501 |
for (values %r_files) { |
|
502 |
print $_ '</channel> </rss>';; |
|
7 | 503 |
close $_; |
0 | 504 |
} |
505 |
} |
|
506 |
||
7 | 507 |
sub linkify ($art) { |
508 |
# if $art->{dom} is undefined, we must sadly parse again |
|
509 |
my @links; |
|
510 |
my @olinks = @{$art->{links} //[]}; |
|
511 |
my $bom = $art->{dom} // XML::LibXML->load_html(location => $art->{file}); |
|
512 |
for ($bom->findnodes('//a[@class="rakim"]')) { |
|
513 |
my $key; |
|
514 |
my $link_text; |
|
515 |
if (@olinks) { |
|
516 |
$key = pop @olinks; |
|
517 |
} else { |
|
518 |
($key) = parse_keywords($_->to_literal); |
|
519 |
$link_text = $_->{href}; |
|
520 |
$_->removeChild($_->firstChild()); |
|
521 |
$_->appendText($link_text); |
|
522 |
} |
|
523 |
push @links, $key; |
|
524 |
my $l = get_art(@{$key})->{file} or die "Link not found in $art->{filename}\n"; |
|
525 |
$_->{href} = prel_path("/".$art->{file}, "/$l"); |
|
526 |
} |
|
527 |
$art->{links} = \@links; |
|
528 |
return $bom |
|
529 |
} |
|
530 |
||
4
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
531 |
# I don't want to prepopulate, as it is not known which ones are |
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
532 |
# usless and outdated, and it would be a waste of cycles parsing |
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
533 |
# something to know it is not needed |
5 | 534 |
sub getcache ($fn) { |
4
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
535 |
$cache_dom || return 0; |
5 | 536 |
for my $d ($cache_dom->findnodes("/root/art[\@path=\"$fn\"]")) { |
537 |
# TODO related |
|
538 |
my %dome = ('file' => $fn); |
|
8 | 539 |
(@dome{'title', 'catag', 'utitle'}, my $date) = |
5 | 540 |
map |
541 |
{ $d->getChildrenByTagName($_)->[0]->to_literal } |
|
8 | 542 |
qw[title catag utitle date]; |
7 | 543 |
for ($d->getChildrenByTagName("links")) { |
544 |
my @ll = map [$_->{catag}, $_->to_literal], |
|
545 |
($_->getChildrenByTagName('link')); |
|
546 |
$dome{links} = \@ll; |
|
547 |
} |
|
5 | 548 |
$catags{$dome{catag}}++; |
549 |
$dome{date} = PDate->new(split /-/, $date); |
|
550 |
return \%dome; |
|
551 |
} |
|
4
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
552 |
return 0; |
3 | 553 |
} |
554 |
||
0 | 555 |
sub html_fixup ($filename) { |
8 | 556 |
if ($cache && !grep /^$filename$/, @modified_files) { |
4
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
557 |
for (getcache($filename)) { |
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
558 |
return $_ if $_ |
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
559 |
} |
3 | 560 |
} |
0 | 561 |
my $dom = |
8 | 562 |
XML::LibXML->load_html(location => $filename); |
0 | 563 |
my %data = ('file', $filename); |
564 |
||
565 |
# We don't need style or table of contents heading (if exists) |
|
566 |
for my $xpath ('/html/head/style', '/html/body/div/div[@id="table-of-contents"]/h2') { |
|
567 |
$_->parentNode->removeChild($_) for $dom->findnodes($xpath) |
|
568 |
} |
|
569 |
||
570 |
# Info we need |
|
571 |
||
572 |
# Date |
|
573 |
for ($dom->findnodes('/html/body/div[@id="postamble"]/p[@class="date"]')) { |
|
574 |
my $text = $_->to_literal; |
|
575 |
if ($text =~ /^Date: (\d+)-(\d+)-(\d+)/) { |
|
576 |
my $date = PDate->new($1, $2, $3); |
|
577 |
$data{date} = $date; |
|
578 |
my $p = $_->parentNode; |
|
579 |
$p->parentNode->removeChild($p); |
|
580 |
} |
|
581 |
} |
|
8 | 582 |
# Title |
583 |
say "HELLO"; |
|
0 | 584 |
for ($dom->findnodes('/html/head/title')) { |
8 | 585 |
say "HERE"; |
586 |
$data{utitle} = $_->to_literal; |
|
0 | 587 |
} |
588 |
# keywords/catogry |
|
589 |
for my $node ($dom->findnodes('/html/head/meta')) { |
|
590 |
(my $cont = $node->getAttribute('content')) || next; |
|
591 |
for ($node->getAttribute('name')) { |
|
5 | 592 |
if (/keywords/) { |
0 | 593 |
my ($key, $rest) = parse_keywords($cont); |
594 |
@data{qw[catag title]} = @{$key}; |
|
595 |
$catags{$data{catag}}++; |
|
596 |
$rest =~ s/\s+\|\s+//; |
|
597 |
while ($rest) { |
|
598 |
(my $t,$rest) = parse_keywords($rest); |
|
599 |
push @{$data{related}}, $t; |
|
600 |
} |
|
601 |
} else { |
|
602 |
next; |
|
603 |
} |
|
604 |
$node->parentNode->removeChild($node); |
|
605 |
} |
|
606 |
} |
|
3 | 607 |
$data{dom} = $dom; |
0 | 608 |
\%data; |
609 |
} |
|
610 |
||
611 |
sub parse_keywords ($str) { |
|
612 |
if ($str =~ s/\s*(?:"(.+?)"|(\w+))\s*->(?:\s*(?:"(.+?)"|(\w+)))//) { |
|
613 |
([$1 // $2, $3 // $4], $str); |
|
614 |
} else { |
|
615 |
die "Keywords not arranged properly\n" |
|
616 |
} |
|
617 |
} |
|
618 |
||
619 |
# I wonder if sean combs has made a similar class |
|
620 |
{ |
|
621 |
package PDate; |
|
622 |
||
623 |
sub new { |
|
624 |
my $class = shift; |
|
625 |
my $self = { year => 0 + shift, |
|
626 |
month => 0 + shift, |
|
627 |
day => 0 + shift, |
|
628 |
}; |
|
629 |
bless $self, $class; |
|
630 |
return $self; |
|
631 |
} |
|
632 |
||
633 |
# $d1 is greater than $d2 |
|
634 |
sub cmp { |
|
635 |
my ($d1, $d2) = @_; |
|
636 |
for ($d1->{year} <=> $d2->{year}, |
|
637 |
$d1->{month} <=> $d2->{month}, |
|
638 |
$d1->{day} <=> $d2->{day}) { |
|
639 |
return $_ unless $_ == 0 |
|
640 |
} |
|
641 |
0 |
|
642 |
} |
|
643 |
use overload '<=>' => \&cmp; |
|
644 |
||
645 |
sub fmt { |
|
646 |
my $self = shift; |
|
647 |
my @months = |
|
648 |
qw(January Febuary March April May June July August September November October December); |
|
649 |
my $n = $self->{day}; |
|
650 |
if ($n == 1) { $n = '1st' } |
|
651 |
elsif (($n - 2) % 10 == 0) { $n = "${n}nd" } |
|
652 |
elsif (($n - 3) % 10 == 0) { $n = "${n}rd" } |
|
5 | 653 |
else { $n = "${n}th" } |
0 | 654 |
$months[$self->{month} - 1] . " $n, " . $self->{year} |
655 |
} |
|
656 |
||
657 |
sub short_fmt { |
|
658 |
my $self = shift; |
|
659 |
join "-", ($self->{year}, $self->{month}, $self->{day}); |
|
660 |
} |
|
4
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
661 |
sub text_easy { |
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
662 |
my $self = shift; |
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
663 |
join "-", ($self->{year}, $self->{month}, $self->{day}); |
c98c7c32ab46
Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
3
diff
changeset
|
664 |
} |
0 | 665 |
} |
666 |
||
6
c576e88fef13
It's starting to feel messy now
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
5
diff
changeset
|
667 |