-
Notifications
You must be signed in to change notification settings - Fork 1
/
rss.pl
177 lines (158 loc) · 5.84 KB
/
rss.pl
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
% vim: set expandtab syntax=prolog :
:- module(rss, [make_rss/1, make_sitemap/1]).
:- use_module('content/about').
:- use_module(fileinfo).
:- use_module(toc_reader).
escape_char([], []).
% & --> &
escape_char([0x26 | Tail], [[0x26, 0x61, 0x6d, 0x70, 0x3b] | Esc]) :-
escape_char(Tail, Esc).
% < --> <
escape_char([0x3c | Tail], [[0x26, 0x6c, 0x74, 0x3b] | Esc]) :-
escape_char(Tail, Esc).
% > --> >
escape_char([0x3e | Tail], [[0x26, 0x67, 0x74, 0x3b] | Esc]) :-
escape_char(Tail, Esc).
% " --> "
escape_char([0x22 | Tail], [[0x26, 0x71, 0x75, 0x6f, 0x74, 0x3b] | Esc]) :-
escape_char(Tail, Esc).
% ' --> '
escape_char([0x27 | Tail], [[0x26, 0x61, 0x70, 0x6f, 0x73, 0x3b] | Esc]) :-
escape_char(Tail, Esc).
% and the rest is unchanged
escape_char([A | Tail], [A | Esc]) :- escape_char(Tail, Esc).
escape_xml_atom(Atom, Escaped) :-
name(Atom, Code),
escape_char(Code, Chunks),
flatten(Chunks, EscapedCode),
name(Escaped, EscapedCode).
rss_xml_header([
'<?xml version="1.0" encoding="utf-8"?>',
'<rss version="2.0">',
'<channel>',
'<title>', Title, '</title>',
'<link>', Link, '</link>',
'<description>', Description, '</description>',
'<lastBuildDate>', LastBuildDate, '</lastBuildDate>'
]) :-
content:about:title(UnescTitle),
content:about:domain(UnescDomain),
content:about:motto(UnescDesc),
escape_xml_atom(UnescTitle, Title),
escape_xml_atom(UnescDomain, Domain),
escape_xml_atom(UnescDesc, Description),
atomic_list_concat(['//', Domain, '/'], Link),
rfc2822_build_date(LastBuildDate).
rss_xml_footer(['</channel>', '</rss>']).
rss_item(Entry,
[
'<item>',
'<title>', Title, '</title>',
'<link>', Link, '</link>',
'<guid>', Guid, '</guid>',
'<description>', Description, '</description>',
'<pubDate>', PubDate, '</pubDate>',
'</item>'
]) :-
dissect_entry(Entry, UnescBasename, UnescTitle, _Author, UnescDesc, _Tags, _WordCount, IsoDate),
rfc2822_date(IsoDate, PubDate),
escape_xml_atom(UnescBasename, Basename),
escape_xml_atom(UnescTitle, Title),
escape_xml_atom(UnescDesc, Description),
content:about:domain(Domain),
atomic_list_concat(['http://', Domain, '/posts/', Basename], Link),
Guid = Link.
% Drafts are excluded from the RSS feed.
%% make_rss(RSS) :-
%% nb_current(rss, RSS). % cache
make_rss(RSS) :-
rss_xml_header(Header),
assemble_toc('./content/posts/', Entries),
%write(user_error, Entries),
filter_toc(Entries, ToC),
maplist(rss_item, ToC, ItemList),
flatten(ItemList, Items),
rss_xml_footer(Footer),
append(Header, Items, R),
append(R, Footer, RssList),
atomic_list_concat(RssList, RSS),
nb_setval(rss_feed, RSS).
% The sitemap for a blog will be pretty similar to its RSS feed, so let's handle
% that here, too.
get_url(Dir, Basename, Url) :-
content:about:domain(Domain),
atomic_list_concat(['http://', Domain, '/', Dir, '/', Basename], Url).
sitemap_info(Basename,
[
'<url>',
'<loc>', Location, '</loc>',
'<lastmod>', LastMod, '</lastmod>',
'<changefreq>', 'weekly', '</changefreq>',
'<priority>', '0.4', '</priority>',
'</url>'
]) :-
get_url(info, Basename, Location),
(
last_build_date_of_file(info, Basename, BuildIsoDate);
atom_concat('./content/info/', Basename, Path),
file_mod_date(Path, BuildIsoDate)
),
sitemap_date(BuildIsoDate, LastMod).
sitemap_home([
'<url>',
'<loc>', HomeUrl, '</loc>',
'<lastmod>', TocLastMod, '</lastmod>',
'<changefreq>', 'daily', '</changefreq>',
'<priority>', '0.6', '</priority>',
'</url>'
]) :-
content:about:domain(Domain),
atom_concat('http://', Domain, HomeUrl),
(
last_build_date_of_file('.', 'toc.data', BuildIsoDate);
file_mod_date('content/toc.data', BuildIsoDate)
),
sitemap_date(BuildIsoDate, TocLastMod).
sitemap_item(Entry,
[
'<url>',
'<loc>', Location, '</loc>',
'<lastmod>', LastMod, '</lastmod>',
'<changefreq>', 'weekly', '</changefreq>',
'<priority>', '1.0', '</priority>',
'</url>'
]) :-
dissect_entry(Entry, UnescBasename, _UnescTitle,
_Author, _UnescDesc, _Tags, _WordCount, _IsoDate),
(
last_build_date_of_file(posts, UnescBasename, BuildIsoDate);
atom_concat('./content/posts/', UnescBasename, Path),
file_mod_date(Path, BuildIsoDate)
),
sitemap_date(BuildIsoDate, LastMod),
get_url(posts, UnescBasename, UnescLocation),
escape_xml_atom(UnescLocation, Location).
make_sitemap(Sitemap) :-
nb_current(sitemap, Sitemap).
make_sitemap(Sitemap) :-
sitemap_xml_header(Header),
assemble_toc('./content/posts/', Entries),
filter_toc(Entries, ToC),
maplist(sitemap_item, ToC, ItemList),
sitemap_home(Home),
sitemap_info('about.md', About),
sitemap_info('links.md', Links),
sitemap_info('license.md', License),
WithInfo = [Home, About, Links, License | ItemList],
flatten(WithInfo, Items),
sitemap_xml_footer(Footer),
append(Header, Items, S),
append(S, Footer, SitemapList),
atomic_list_concat(SitemapList, Sitemap),
nb_setval(sitemap, Sitemap).
sitemap_xml_header(
[
'<?xml version="1.0" encoding="UTF-8"?>',
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
]).
sitemap_xml_footer(['</urlset>']).