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