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