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