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