/
Books_atom.cgi
executable file
·146 lines (120 loc) · 4.31 KB
/
Books_atom.cgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/perl
use POSIX qw(strftime);
use lib './lib';
require 'wiki.pl';
&PrintContentType();
&PrintDocumentHeader();
&PrintDocumentParts();
&PrintDocumentFooter();
sub PrintContentType {
if (!grep { /--no-headers/ } @ARGV) {
print "Content-type: application/atom+xml\n";
print "\n";
}
}
sub PrintDocumentHeader {
local ($document) = &GetWikiName();
local ($title) = &GetWikiTitle();
print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
print "\n";
print "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n";
print "\n";
print " <title>$title</title>\n";
print " <id>https://jeantessier.com/${document}.html</id>\n";
print " <link href=\"https://jeantessier.com/${document}.html\"/>\n";
print " <link href=\"https://jeantessier.com/${document}_atom.cgi\" rel=\"self\"/>\n";
print " <author>\n";
print " <name>Jean Tessier</name>\n";
print " <email>jean\@jeantessier.com</email>\n";
print " <uri>https://jeantessier.com/</uri>\n";
print " </author>\n";
print " <rights type=\"xhtml\"><div xmlns=\"http://www.w3.org/1999/xhtml\">Copyright (c) 2001, Jean Tessier</div></rights>\n";
}
sub PrintDocumentParts {
local ($document) = &GetWikiName();
local (@files) = &GetWikiFiles();
local ($max_mtime) = 0;
foreach $file (@files) {
my $mtime = (stat($file))[9];
if ($mtime > $max_mtime) {
$max_mtime = $mtime;
}
}
local ($updated) = strftime "%Y-%m-%dT%H:%M:%SZ", gmtime($max_mtime);
print " <updated>$updated</updated>\n";
foreach $file (reverse sort @files) {
&PrintDocumentPart($document, $file);
}
}
sub PrintDocumentPart {
local ($document, $filename) = @_;
local ($mtime) = (stat($filename))[9];
local ($updated) = strftime "%Y-%m-%dT%H:%M:%SZ", gmtime($mtime);
open(FILEHANDLE, $filename);
local (@lines) = <FILEHANDLE>;
close(FILEHANDLE);
local (%meta_data);
local ($line);
do {
$line = shift(@lines);
chomp $line;
if ($line =~ /(\w+):\s*(.*)/) {
local ($key, $value) = @{^CAPTURE};
if ($key eq "title") {
if ($value =~ /\[(.*)\]\(.*\)/) {
$meta_data{'title'} = $1 unless defined $meta_data{'title'};
} else {
$meta_data{'title'} = $value unless defined $meta_data{'title'};
}
} else {
$meta_data{$key} = $value unless defined $meta_data{$key};
}
}
} until ($line =~ /^\s*$/);
$meta_data{'title'} =~ s/à/à/g;
$meta_data{'title'} =~ s/è/è/g;
$meta_data{'title'} =~ s/é/é/g;
$meta_data{'title'} =~ s/ê/ê/g;
$meta_data{'title'} =~ s/ô/ô/g;
$meta_data{'title'} =~ s/ö/ö/g;
$meta_data{'title'} =~ s/ú/ú/g;
$meta_data{'title'} =~ s/ü/ü/g;
$filename =~ /(?<published_date>\d{4}-\d{2}-\d{2})/;
print "\n";
print " <entry>\n";
print " <title>" . $meta_data{'title'} . "</title>\n";
print " <id>https://jeantessier.com/${document}.html#" . $meta_data{'name'} . "</id>\n";
print " <link href=\"https://jeantessier.com/${document}.html#" . $meta_data{'name'} . "\"/>\n";
print " <published>$+{published_date}T00:00:00Z</published>\n";
print " <updated>$updated</updated>\n";
print " <content type=\"text/markdown\">\n";
&PrintWikiContents(@lines);
print "\n";
if ($meta_data{"start"} =~ /(?<start_date>\d{4}-\d{2}-\d{2})/) {
print "Started reading: `$+{start_date}` \n";
} else {
print "Started reading: _not started_ \n";
}
if (defined $meta_data{"start"}) {
if ($meta_data{"stop"} =~ /(?<stop_date>\d{4}-\d{2}-\d{2})/) {
print "Finished reading: `$+{stop_date}`\n";
} else {
print "Finished reading: _in progress_\n";
}
}
print " </content>\n";
print " </entry>\n";
}
sub PrintWikiContents {
local (@lines) = @_;
foreach $line (@lines) {
$line =~ s/&(?!amp|lt|gt)/&/g;
$line =~ s/</</g;
$line =~ s/>/>/g;
print $line;
}
}
sub PrintDocumentFooter {
print "\n";
print "</feed>\n";
}