-
Notifications
You must be signed in to change notification settings - Fork 1
/
plog.pl
361 lines (299 loc) · 11.5 KB
/
plog.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
% vim: set expandtab syntax=prolog :
:- use_module(library(error)).
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_error)).
:- use_module(library(http/http_log)).
:- use_module(library(http/js_write)).
:- use_module(library(http/http_parameters)).
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- use_module(library(readutil)).
% http_reply_from_files is here
:- use_module(library(http/http_files)).
:- use_module(library(http/http_authenticate)).
:- use_module(library(http/http_dirindex)).
% for the DCG stuff, I think
:- use_module(library(http/html_write)).
% html_resource
:- use_module(library(http/html_head)).
:- use_module(library(lists)).
:- use_module(parse_post).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% :- use_module(content/about).
:- use_module(fileinfo).
:- use_module(rss).
:- use_module(toc_reader).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:- multifile http:location/3.
:- dynamic http:location/3.
:- multifile user:body//2.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This predicate starts the server in the background, and returns
% to the toplevel, so that you can reload and debug the code, etc.
server(Port) :-
http_server(http_dispatch, [port(Port)]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
http:location(css, '/css', []).
http:location(posts, '/posts', []).
http:location(info, '/info', []).
http:location(img, '/img', []).
http:location(data, '/data', []).
http:location(tags, '/tags', []).
http:location(static, '/static', []).
http:location(feed, '/feed', []).
http:location(sitemap, '/sitemap.xml', []).
http:location(notfound, '/', [priority(-9)]).
http:location(bing, '/BingSiteAuth.xml', []).
http:location(scripts, '/scripts', []).
user:file_search_path(css, './content/css').
user:file_search_path(posts, './content/posts').
user:file_search_path(info, './content/info').
user:file_search_path(img, './content/img').
user:file_search_path(data, './content/data').
user:file_search_path(static, './content/static').
user:file_search_path(scripts, './scripts'). % not in content/
%%%
% Handlers
%%
%:- http_handler(root(.), serve_404, [prefix]).
:- http_handler(css(.), file_reply(css(/), []), [prefix]).
:- http_handler(img(.), file_reply(img(/), []), [prefix]).
:- http_handler(data(.), file_reply(data(/), []), [prefix]).
:- http_handler(static(.), file_reply(static(/), []), [prefix]).
:- http_handler(scripts(.), file_reply(scripts(/), []), [prefix]).
:- http_handler(posts(.), serve_post_markdown, [prefix]).
:- http_handler(info(.), serve_info_markdown, [prefix]).
:- http_handler(tags(.), display_tags, []).
:- http_handler(tags(.), display_toc, [prefix]).
:- http_handler('/favicon.ico',
http_reply_file(img('favicon.ico'), []),
[]).
:- http_handler('/robots.txt',
http_reply_file(info('robots.txt'), []),
[]).
:- http_handler(feed(.), serve_rss, []).
:- http_handler(sitemap(.), serve_sitemap, []).
:- http_handler(bing(.), http_reply_file(info('BingSiteAuth.xml'), []), []).
%:- http_handler(notfound(.), serve_404, [prefix]).
:- http_handler(root(.), display_toc, []).
file_reply(Dir, Opt, Request) :-
http_reply_from_files(Dir, Opt, Request).
file_reply(_, _, Request) :-
serve_404(Request).
serve_rss(_Request) :-
make_rss(RSS),
format('Content-Type: application/xml~n~n~s~n', RSS).
serve_sitemap(_Request) :-
make_sitemap(Sitemap),
format('Content-Type: application/xml~n~n~s~n', Sitemap).
validate_file(Path) :-
exists_file(Path),
http_safe_file(Path, []).
path_of_request([path_info(PathInfo) | _], PathInfo).
path_of_request([_ | Tail], PathInfo) :- path_of_request(Tail, PathInfo).
serve_post_markdown(Request) :- serve_markdown(Request, posts).
serve_info_markdown(Request) :- serve_markdown(Request, info).
% (+File, +Location, -Blocks, -Commit, -Entry)
get_content(File, Location, Blocks, Commit, Entry) :-
user:file_search_path(Location, PostDir),
atomic_list_concat([PostDir, '/', File], Path),
validate_file(Path),
git_hash_of_file(Location, File, Commit),
parse_post_with_meta(Path, Blocks, Entry),
!.
% (+File, +Location, -Blocks, -Commit)
get_content(File, Location, Blocks, Commit) :-
user:file_search_path(Location, PostDir),
atomic_list_concat([PostDir, '/', File], Path),
validate_file(Path),
git_hash_of_file(Location, File, Commit),
%format(user_error, 'Commit for ~s/~s: ~s\n', [Location, File, Commit]),
%format(user_error, 'Path: ~s\n', Path),
parse_post(Path, Blocks),
!.
make_footer(uncommitted,
[hr(class=footer_hr),
'Last Commit: uncommitted',
hr(class=footer_hr)]).
make_footer(Commit, [Bar, FooterDiv, Bar]) :-
about:repo(Repo),
Bar = hr(class=footer_hr),
format(atom(CommitUrl), '~s/commit/~s', [Repo, Commit]),
FooterDiv = div(class=footer, ['Last Commit: ', a(href=CommitUrl, Commit)]),
!.
make_header(Basename, Entry, Header) :-
dissect_entry(Entry,_Filename,Title,Author,_Abstract,Tags,WordCount,Date),
prettify_date(Date, PrettyDate),
format(atom(DateLine), 'Posted: ~s', [PrettyDate]),
(memberchk(draft, Tags)
-> TitleLine = ['DRAFT: ', Title]
; TitleLine = [Title]),
last_build_date_of_file(posts, Basename, RevisedDate),
prettify_date(RevisedDate, PrettyRevisedDate),
memberchk(editedby(Editors), Entry),
format(atom(RevisedDateLine), 'Edited: ~s (~s)', [PrettyRevisedDate, Editors]),
format(atom(WordLine), '~d words', WordCount),
maplist(make_tag, Tags, TagLine),
Header = [
h1(TitleLine),
div(class=post_frontmatter,
[
hr(class=title_hr),
div(class=byline, ['Author: ', Author]),
div(class=dateline, DateLine),
div(class=dateline, RevisedDateLine),
div(class=header_wordcount, ['Length: ', WordLine]),
div(class=tagline, ['Tags: ' | TagLine]),
hr(class=title_hr)
])
],
!.
serve_markdown(Request, posts) :-
memberchk(path(Path), Request), %path_of_request(Request, Basename),
safe_base_and_parent_name(Path, Basename, posts),
%format(user_error, "[serve_markdown] Path = ~s Basename = ~s~n", [Path, Basename]),
file_name_extension(_, md, Basename),
get_content(Basename, posts, PostBlocks, Commit, Entry),
make_header(Basename, Entry, Header),
memberchk(abstract(Abstract), Entry),
memberchk(title(Title), Entry),
format(atom(Description), 'name="description", content="~s"', Abstract),
make_footer(Commit, Footer),
reply_html_page(my_style,
[title(Title),
meta(Description)
],
[div(class=header, Header),
div(class=post, PostBlocks),
div(class=footer, Footer)]).
serve_markdown(Request, Location) :-
Location \= posts,
path_of_request(Request, Basename),
file_name_extension(_, md, Basename),
get_content(Basename, Location, PostBlocks, Commit),
make_footer(Commit, Footer),
format(atom(Title), '~s', [Basename]),
format(atom(Description), 'name="description", content="~s"', [Basename]),
reply_html_page(my_style,
[title(Title),
meta(Description)],
[div(class=post, PostBlocks),
div(class=footer, Footer)]).
serve_markdown(Request, Location) :-
user:file_search_path(Location, PostDir),
file_reply(PostDir, [], Request).
serve_404(Request) :-
memberchk(path(Path), Request),
format('Status: 404~n'),
Message = div(['The requested URL ', code(Path), ' could not be found on this server.']),
reply_html_page(my_style,
[title('Not Found'),
meta('name="description", content="resource not found"')],
[h1(class=error, 'Not Found'),
div(id=post, Message)]).
%%
% The "secret" parameter that's required in order to display the
% drafts is a pretty low-security secret. It's passed in the GET
% request, and can easily be snooped. But the stakes are low, so
% I'm letting it be. Don't put anything in your drafts that you
% are afraid of the world seeing.
%%
get_secret(Secret) :-
getenv('PLOG_SECRET', Secret),
!.
get_secret(opensesame).
get_tag_from_request(Request, draft) :-
memberchk(path(Path), Request),
atom_concat('/tags/', draft, Path),
get_secret(Secret),
catch(http:http_parameters(Request, [secret(Secret, [] )]),
error(existence_error(http_parameter, secret), _),
false).
get_tag_from_request(Request, Tag) :-
memberchk(path(Path), Request),
atom_concat('/tags/', Tag, Path),
Tag \= draft.
get_tag_from_request(_, everything).
% if the Tag is 'draft', then check for the magic word
% in the parameter.
display_toc(Request) :-
make, %% KLUDGE - rebuild the page when reloading %%
get_tag_from_request(Request, Tag),
user:file_search_path(posts, PostDir),
make_toc(PostDir, ToC, Tag),
content:about:title(Title),
reply_html_page(
my_style,
[title(Title)],
[
\toc_page_content(ToC)
]).
display_tags(_Request) :-
content:about:title(Title),
user:file_search_path(posts, PostDir),
extract_tags_from_toc(PostDir, Tags),
reply_html_page(
my_style,
[title(Title)],
[\tag_page_content(Tags)]
).
toc_page_content(ToC) -->
html([div(class(toc), ul(class(toc), ToC))]).
tag_page_content(Tags) -->
html([h1('Tags'), div(class(tag_list), Tags)]).
get_motto(Motto) :-
content:about:motto(MottoA),
% Check if the motto is a valid file path, relative to ./content/, and if it is, choose a random line from it.
( ( format(atom(MottoPath), './content/~s', [MottoA]),
exists_file(MottoPath) )
-> read_file_to_string(MottoPath, String, []),
split_string(String, "\n", "", Lines),
% filter out any empty lines
exclude(=(""), Lines, NonEmptyLines),
random_member(Line, NonEmptyLines),
format(atom(Motto), '~s', [Line])
; format(atom(Motto), '~s', [MottoA])
).
user:body(my_style, Body) -->
{
content:about:title(Title),
get_motto(Motto)
},
html(body([div(class(container),
[div(id(main),
[h1(Title),
hr(_),
p(class(motto), Motto),
\nav_bar,
hr(_),
br(_),
Body])])])).
user:head(my_style, Head) -->
html(head([meta([name=viewport,
content='width=device-width, initial-scale=1.0']),
\html_requires(scripts('es5/tex-mml-chtml.js')),
\html_requires(scripts('MathJaxConfig.js')),
\html_requires(css('stylesheet.css')),
Head])).
nav('Home', /).
nav('About', '/info/about.md').
nav('Tags', '/tags').
nav('Links', '/info/links.md').
nav('FAQ', '/info/FAQ.md').
nav('RSS', '/feed').
nav('P\'log', 'https://github.com/oblivia-simplex/plog').
% nav('Storage', '/data/'). % why list when dir listing is blocked?
nav_bar -->
{
findall(Name, nav(Name, _), ButtonNames),
maplist(as_top_nav, ButtonNames, TopButtons)
},
html(TopButtons).
as_top_nav(Name, span([a([href=HREF, class=topnav], Name), &(nbsp), ' '])) :-
nav(Name, HREF).
%%%%% Entry point %%%%%
start :-
about:bind(Address),
about:port(Port),
server(Address:Port).