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