-
Notifications
You must be signed in to change notification settings - Fork 10
/
wiki.pl
369 lines (311 loc) · 10.1 KB
/
wiki.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
362
363
364
365
366
367
368
369
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 2009-2015, VU University Amsterdam
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
As a special exception, if you link this library with other files,
compiled with a Free Software compiler, to produce an executable, this
library does not by itself cause the resulting executable to be covered
by the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
:- module(plweb_wiki,
[ wiki_file_to_dom/2, % +File, -DOM
wiki_file_codes_to_dom/3, % +Codes, +File, -DOM
wiki_page_title/2, % +Location, -Title
index_wiki_pages/0, %
update_wiki_page_title/1, % +Location
wiki_extension/1, % ?Extension
file//2, % +File, +Options
include//3, % +Object, +Type, +Options
extract_title/3, % +DOM0, -Title, -DOM
title_text/2, % +Title, -Text:atom
safe_file_name/1 % +Name
]).
:- reexport(library(pldoc/doc_html),
except([ file//2,
include//3
])).
:- use_module(library(pldoc/doc_wiki)).
:- use_module(library(http/html_write)).
:- use_module(library(http/http_wrapper)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(readutil)).
:- use_module(library(option)).
:- use_module(library(apply)).
:- use_module(library(lists)).
:- use_module(library(filesex)).
:- use_module(wiki_edit).
:- predicate_options(file//2, 2,
[ absolute_path(atom),
label(any)
]).
:- predicate_options(include//3, 3,
[pass_to(pldoc_html:include/5, 3)]).
%% wiki_file_to_dom(+File, +DOM) is det.
%
% DOM is the HTML dom representation for the content of File.
wiki_file_to_dom(File, DOM) :-
read_file_to_codes(File, String, []),
wiki_file_codes_to_dom(String, File, DOM).
%% wiki_codes_to_dom(+Codes, +File, -DOM)
%
% DOM is the HTML dom representation for Codes that originate from
% File.
wiki_file_codes_to_dom(String, File, DOM) :-
( nb_current(pldoc_file, OrgFile)
-> setup_call_cleanup(
b_setval(pldoc_file, File),
wiki_codes_to_dom(String, [], DOM),
b_setval(pldoc_file, OrgFile))
; setup_call_cleanup(
b_setval(pldoc_file, File),
wiki_codes_to_dom(String, [], DOM),
nb_delete(pldoc_file))
).
/*******************************
* RENDERING *
*******************************/
%% include(+Object, +Type, +Options)//
include(Object, Type, Options) -->
pldoc_html:include(Object, Type,
[ map_extension([txt-html])
| Options
]).
%% file(+Path, Options)//
%
% Trap translation of \file(+Path, Options)
file(Path, Options) -->
{ \+ option(label(_), Options),
file_base_name(Path, File),
file_name_extension(Label, txt, File), !,
file_href(Options, Options1)
},
pldoc_html:file(Path,
[ label(Label),
map_extension([txt-html]),
edit_handler(wiki_edit)
| Options1
]).
file(File, Options) -->
{ file_href(Options, Options1)
},
pldoc_html:file(File,
[ map_extension([txt-html]),
edit_handler(wiki_edit)
| Options1
]).
file_href(Options0, Options) :-
\+ ( nb_current(pldoc_file, CFile),
CFile \== []
),
option(absolute_path(Path), Options0),
absolute_file_name(document_root(.),
DocRoot,
[ file_type(directory),
access(read)
]),
atom_concat(DocRoot, DocLocal, Path), !,
ensure_leading_slash(DocLocal, HREF),
Options = [ href(HREF) | Options0 ].
file_href(Options, Options).
ensure_leading_slash(Path, SlashPath) :-
( sub_atom(Path, 0, _, _, /)
-> SlashPath = Path
; atom_concat(/, Path, SlashPath)
).
/*******************************
* OBJECT INTEGRATION *
*******************************/
:- multifile
prolog:doc_object_summary/4,
prolog:doc_object_link//2,
prolog:doc_object_page//2,
prolog:doc_category/3,
prolog:doc_file_index_header//2.
prolog:doc_object_summary(wiki(Location), wiki, wiki, Summary) :-
wiki_page_title(Location, Summary).
:- dynamic
wiki_page_title_cache/3, % Location, Title, Time
wiki_pages_indexed/1.
%% wiki_page_title(?Location, ?Title) is nondet.
%
% True when Title is the title of the wiki page at Location.
wiki_page_title(Location, Title) :-
wiki_pages_indexed(_), !,
wiki_page_title_cache(Location, Title, _).
wiki_page_title(Location, Title) :-
nonvar(Location), !,
( wiki_page_title_cache(Location, TitleRaw, _)
-> Title = TitleRaw
; extract_wiki_page_title(Location, File, TitleRaw)
-> time_file(File, Modified),
assertz(wiki_page_title_cache(Location, TitleRaw, Modified)),
Title = TitleRaw
; print_message(warning, wiki(no_title(Location))),
Title = 'No title'
).
wiki_page_title(Location, Title) :-
index_wiki_pages,
wiki_page_title(Location, Title).
update_wiki_title_cache :-
wiki_locations(Pages),
maplist(update_wiki_page_title, Pages).
%% update_wiki_page_title(Location) is det.
%
% Update the cached information about a wiki file.
update_wiki_page_title(Location) :-
wiki_page_title_cache(Location, _, Time), !,
location_wiki_file(Location, File),
time_file(File, Modified),
( abs(Time-Modified) < 1
-> true
; extract_wiki_page_title(Location, File, Title),
retractall(wiki_page_title_cache(Location, _, _)),
assertz(wiki_page_title_cache(Location, Title, Modified))
).
update_wiki_page_title(Location) :-
extract_wiki_page_title(Location, File, Title),
time_file(File, Modified),
assertz(wiki_page_title_cache(Location, Title, Modified)).
extract_wiki_page_title(Location, File, Title) :-
( var(File)
-> location_wiki_file(Location, File, read)
; true
),
( catch(wiki_file_to_dom(File, DOM), E,
( print_message(warning, E),
fail
)),
dom_title(DOM, Title)
-> true
; format(atom(Title), 'Wiki page at "~w"', Location)
).
%% dom_title(+DOM, -Title) is semidet.
%
% Get the title as an atom from a parsed wiki page.
%
% @tbd Currently assumes no markup in the title.
dom_title([h1(_, TitleList)|_], Title) :-
maplist(to_atom, TitleList, TitleList2),
atomic_list_concat(TitleList2, Title).
to_atom(Atomic, Atomic) :- atomic(Atomic).
to_atom(predref(Name/Arity), Label) :-
atomic_list_concat([Name,/,Arity], Label).
prolog:doc_object_link(wiki(Location), _Options) -->
{ wiki_page_title(Location, Title) },
html([ '[wiki] ', Title ]).
prolog:doc_object_page(wiki(Location), _Options) -->
{ http_current_request(Request),
http_redirect(see_other, root(Location), Request)
}.
prolog:doc_category(wiki, 60, 'Wiki pages').
prolog:doc_file_index_header(wiki, _) --> [].
%% index_wiki_pages
%
% Create a (title) index of the available wiki pages. This is
% started from server/1 in a background thread.
index_wiki_pages :-
wiki_pages_indexed(_), !.
index_wiki_pages :-
with_mutex(index_wiki_pages,
index_wiki_pages_sync).
index_wiki_pages_sync :-
wiki_pages_indexed(_).
index_wiki_pages_sync :-
wiki_locations(Locations),
maplist(wiki_page_title, Locations, _Titles),
get_time(Now),
asserta(wiki_pages_indexed(Now)).
%% wiki_locations(-Locations) is det.
%
% True when Files is a list of all .txt files on the site.
wiki_locations(Files) :-
findall(Dir, absolute_file_name(
document_root(.), Dir,
[ access(read),
file_type(directory),
solutions(all)
]),
RootDirs),
maplist(wiki_locations, RootDirs, NestedFiles),
append(NestedFiles, Files).
wiki_locations(Dir, Files) :-
phrase(wiki_locations(Dir, Dir), Files).
wiki_locations([], _) --> !.
wiki_locations([H|T], Root) --> !,
wiki_locations(H, Root),
wiki_locations(T, Root).
wiki_locations(CurrentDir, Root) -->
{ exists_directory(CurrentDir), !,
directory_files(CurrentDir, Members),
exclude(special, Members, Members2),
maplist(directory_file_path(CurrentDir), Members2, MemberPaths)
},
wiki_locations(MemberPaths, Root).
wiki_locations(Entry, Root) -->
{ file_name_extension(_, Ext, Entry),
wiki_extension(Ext), !,
directory_file_path(Root, Wiki, Entry)
},
[Wiki].
wiki_locations(_, _) --> [].
wiki_extension(txt).
wiki_extension(md).
special(.).
special(..).
%! extract_title(+DOM0, -Title, -DOM) is det.
%
% Extract the title from a wiki page. The title is considered
% to be the first h<N> element.
extract_title([H|T], Title, T) :-
title(H, Title), !.
extract_title(DOM, 'SWI-Prolog', DOM).
title(h1(_Attrs, Title), Title).
title(h2(_Attrs, Title), Title).
title(h3(_Attrs, Title), Title).
title(h4(_Attrs, Title), Title).
%! title_text(+Title, -Text:atom) is det.
%
% Turn the title, represented as an argument to html//1 into a
% plain string. Turns it into HTML, then parses the HTML and
% finally extracts the string. First clause avoids this for the
% common normal case.
title_text(Title, Text) :-
maplist(atomic, Title), !,
atomics_to_string(Title, Text).
title_text(Title, Text) :-
phrase(html(Title), Tokens),
with_output_to(string(HTML), print_html(Tokens)),
setup_call_cleanup(
open_string(HTML, In),
load_html(In, DOM, []),
close(In)),
xpath(element(div, [], DOM), /('*'(text)), Text).
%! safe_file_name(+Name)
%
% True when Name is a file without references to parent
% directories.
safe_file_name(Name) :-
must_be(atom, Name),
prolog_to_os_filename(FileName, Name),
\+ unsafe_name(FileName),
!.
safe_file_name(Name) :-
permission_error(read, file, Name).
unsafe_name(Name) :- Name == '..'.
unsafe_name(Name) :- sub_atom(Name, 0, _, _, '../').
unsafe_name(Name) :- sub_atom(Name, _, _, _, '/../').
unsafe_name(Name) :- sub_atom(Name, _, _, 0, '/..').