-
Notifications
You must be signed in to change notification settings - Fork 14
/
doc_htmlsrc.pl
571 lines (498 loc) · 17.5 KB
/
doc_htmlsrc.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2006-2017, University of Amsterdam
VU University Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(pldoc_htmlsrc,
[ source_to_html/3 % +Source, +Out, +Options
]).
:- use_module(library(apply)).
:- use_module(library(option)).
:- use_module(library(debug)).
:- use_module(library(lists)).
:- use_module(library(prolog_colour)).
:- use_module(doc_colour).
:- use_module(doc_html).
:- use_module(doc_wiki).
:- use_module(doc_modes).
:- use_module(doc_process).
:- use_module(library(http/html_write)).
:- use_module(library(http/http_path)).
:- use_module(library(prolog_xref)).
:- meta_predicate
source_to_html(+, +, :).
/** <module> HTML source pretty-printer
This module colourises Prolog source using HTML+CSS using the same
cross-reference based technology as used by PceEmacs.
@tbd Create hyper-links to documentation and definitions.
@author Jan Wielemaker
*/
:- predicate_options(source_to_html/3, 3,
[ format_comments(boolean),
header(boolean),
skin(callable),
stylesheets(list),
title(atom)
]).
:- thread_local
lineno/0, % print line-no on next output
nonl/0, % previous tag implies nl (block level)
id/1. % Emitted ids
%! source_to_html(+In:filename, +Out, :Options) is det.
%
% Colourise Prolog source as HTML. The idea is to first create a
% sequence of fragments and then to apply these to the code.
% Options are:
%
% * format_comments(+Boolean)
% If =true= (default), use PlDoc formatting for structured
% comments.
%
% Other options are passed to the following predicates:
%
% * print_html_head/2
% * print_html_footer/2.
% * html_fragments/6
%
% @arg In A filename. Can also be an abstract name,
% which is subject to library(prolog_source)
% abstract file handling. See
% prolog_open_source/2. Note that this cannot
% be a stream as we need to read the file three
% times: (1) xref, (2) assign colours and (3)
% generate HTML.
% @arg Out Term stream(Stream) or filename specification
source_to_html(Src, stream(Out), MOptions) :-
!,
meta_options(is_meta, MOptions, Options),
( option(title(_), Options)
-> HeadOptions = Options
; file_base_name(Src, Title),
HeadOptions = [title(Title)|Options]
),
retractall(lineno), % play safe
retractall(nonl), % play safe
retractall(id(_)),
colour_fragments(Src, Fragments),
setup_call_cleanup(
( open_source(Src, In),
asserta(user:thread_message_hook(_,_,_), Ref)
),
( print_html_head(Out, HeadOptions),
html_fragments(Fragments, In, Out, [], State, Options),
copy_rest(In, Out, State, State1),
pop_state(State1, Out, In)
),
( erase(Ref),
close(In)
)),
print_html_footer(Out, Options).
source_to_html(Src, FileSpec, Options) :-
absolute_file_name(FileSpec, OutFile, [access(write)]),
setup_call_cleanup(
open(OutFile, write, Out, [encoding(utf8)]),
source_to_html(Src, stream(Out), Options),
close(Out)).
open_source(Id, Stream) :-
prolog:xref_open_source(Id, Stream),
!.
open_source(File, Stream) :-
open(File, read, Stream).
is_meta(skin).
%! print_html_head(+Out:stream, +Options) is det.
%
% Print the =DOCTYPE= line and HTML header. Options:
%
% * header(Bool)
% Only print the header if Bool is not =false=
%
% * title(Title)
% Title of the HTML document
%
% * stylesheets(List)
% Reference to the CSS style-sheets.
%
% * format_comments(Bool)
% If =true= (default), format structured comments.
%
% * skin(Closure)
% Called using call(Closure, Where, Out), where Where
% is one of =header= or =footer=. These calls are made
% just after opening =body= and before closing =body=.
print_html_head(Out, Options) :-
option(header(true), Options, true),
!,
option(title(Title), Options, 'Prolog source'),
http_absolute_location(pldoc_resource('pldoc.css'), PlDocCSS, []),
http_absolute_location(pldoc_resource('pllisting.css'), PlListingCSS, []),
option(stylesheets(Sheets), Options, [PlListingCSS, PlDocCSS]),
format(Out, '<!DOCTYPE html>~n', []),
format(Out, '<html>~n', []),
format(Out, ' <head>~n', []),
format(Out, ' <title>~w</title>~n', [Title]),
forall(member(Sheet, Sheets),
format(Out, ' <link rel="stylesheet" type="text/css" href="~w">~n', [Sheet])),
format(Out, ' </head>~n', []),
format(Out, '<body>~n', []),
skin_hook(Out, header, Options).
print_html_head(Out, Options) :-
skin_hook(Out, header, Options).
print_html_footer(Out, Options) :-
option(header(true), Options, true),
!,
skin_hook(Out, footer, Options),
format(Out, '~N</body>~n', []),
format(Out, '</html>', []).
print_html_footer(Out, Options) :-
skin_hook(Out, footer, Options).
skin_hook(Out, Where, Options) :-
option(skin(Skin), Options),
call(Skin, Where, Out),
!.
skin_hook(_, _, _).
%! html_fragments(+Fragments, +In, +Out, +State, +Options) is det.
%
% Copy In to Out, inserting HTML elements using Fragments.
html_fragments([], _, _, State, State, _).
html_fragments([H|T], In, Out, State0, State, Options) :-
html_fragment(H, In, Out, State0, State1, Options),
html_fragments(T, In, Out, State1, State, Options).
%! html_fragment(+Fragment, +In, +Out,
%! +StateIn, -StateOut, +Options) is det.
%
% Print from current position upto the end of Fragment. First
% clause deals with structured comments.
html_fragment(fragment(Start, End, comment(structured), []),
In, Out, State0, [], Options) :-
option(format_comments(true), Options, true),
!,
copy_without_trailing_white_lines(In, Start, Out, State0, State1),
pop_state(State1, Out, In),
Len is End - Start,
read_n_codes(In, Len, Comment),
is_structured_comment(Comment, Prefix),
indented_lines(Comment, Prefix, Lines0),
( section_comment_header(Lines0, Header, Lines1)
-> wiki_lines_to_dom(Lines1, [], DOM),
phrase(pldoc_html:html(div(class(comment),
[Header|DOM])), Tokens),
print_html(Out, Tokens)
; stream_property(In, file_name(File)),
line_count(In, Line),
( xref_module(File, Module)
-> true
; Module = user
),
process_modes(Lines0, Module, File:Line, Modes, Args, Lines1),
maplist(assert_seen_mode, Modes),
DOM = [\pred_dt(Modes, pubdef, []), dd(class=defbody, DOM1)],
wiki_lines_to_dom(Lines1, Args, DOM0),
strip_leading_par(DOM0, DOM1),
phrase(pldoc_html:html(DOM), Tokens), % HACK
format(Out, '<dl class="comment">~n', []),
print_html(Out, Tokens),
format(Out, '</dl>~n', [])
).
html_fragment(fragment(Start, End, structured_comment, []),
In, Out, State0, State, _Options) :-
!,
copy_to(In, Start, Out, State0, State1),
line_count(In, StartLine),
Len is End - Start,
read_n_codes(In, Len, Comment),
is_structured_comment(Comment, Prefix),
indented_lines(Comment, Prefix, Lines),
( section_comment_header(Lines, _Header, _RestSectionLines)
-> true
; stream_property(In, file_name(File)),
line_count(In, Line),
( xref_module(File, Module)
-> true
; Module = user
),
process_modes(Lines, Module, File:Line, Modes, _Args, _Lines1),
maplist(mode_anchor(Out), Modes)
),
start_fragment(structured_comment, In, Out, State1, State2),
copy_codes(Comment, StartLine, Out, State2, State3),
end_fragment(Out, In, State3, State).
html_fragment(fragment(Start, End, Class, Sub),
In, Out, State0, State, Options) :-
copy_to(In, Start, Out, State0, State1),
start_fragment(Class, In, Out, State1, State2),
html_fragments(Sub, In, Out, State2, State3, Options),
copy_to(In, End, Out, State3, State4), % TBD: pop-to?
end_fragment(Out, In, State4, State).
start_fragment(atom, In, Out, State0, State) :-
!,
( peek_code(In, C),
C == 39
-> start_fragment(quoted_atom, In, Out, State0, State)
; State = [nop|State0]
).
start_fragment(Class, _, Out, State, [Push|State]) :-
element(Class, Tag, CSSClass),
!,
Push =.. [Tag,class(CSSClass)],
( anchor(Class, ID)
-> format(Out, '<~w id="~w" class="~w">', [Tag, ID, CSSClass])
; format(Out, '<~w class="~w">', [Tag, CSSClass])
).
start_fragment(Class, _, Out, State, [span(class(SpanClass))|State]) :-
functor(Class, SpanClass, _),
format(Out, '<span class="~w">', [SpanClass]).
end_fragment(_, _, [nop|State], State) :- !.
end_fragment(Out, In, [span(class(directive))|State], State) :-
!,
copy_full_stop(In, Out),
format(Out, '</span>', []),
( peek_code(In, 10),
\+ nonl
-> assert(nonl)
; true
).
end_fragment(Out, _, [Open|State], State) :-
retractall(nonl),
functor(Open, Element, _),
format(Out, '</~w>', [Element]).
pop_state([], _, _) :- !.
pop_state(State, Out, In) :-
end_fragment(Out, In, State, State1),
pop_state(State1, Out, In).
%! anchor(+Class, -Label) is semidet.
%
% True when Label is the =id= we must assign to the fragment of
% class Class. This that the first definition of a head with
% the id _name/arity_.
anchor(head(_, Head), Id) :-
callable(Head),
functor(Head, Name, Arity),
format(atom(Id), '~w/~w', [Name, Arity]),
( id(Id)
-> fail
; assertz(id(Id))
).
mode_anchor(Out, Mode) :-
mode_anchor_name(Mode, Id),
( id(Id)
-> true
; format(Out, '<span id="~w"><span>', [Id]),
assertz(id(Id))
).
assert_seen_mode(Mode) :-
mode_anchor_name(Mode, Id),
( id(Id)
-> true
; assertz(id(Id))
).
%! copy_to(+In:stream, +End:int, +Out:stream, +State) is det.
%
% Copy data from In to Out upto character-position End. Inserts
% HTML entities for HTML the reserved characters =|<&>|=. If State
% does not include a =pre= environment, create one and skip all
% leading blank lines.
copy_to(In, End, Out, State, State) :-
member(pre(_), State),
!,
copy_to(In, End, Out).
copy_to(In, End, Out, State, [pre(class(listing))|State]) :-
format(Out, '<pre class="listing">~n', []),
line_count(In, Line0),
read_to(In, End, Codes0),
delete_leading_white_lines(Codes0, Codes, Line0, Line),
assert(lineno),
write_codes(Codes, Line, Out).
copy_codes(Codes, Line, Out, State, State) :-
member(pre(_), State),
!,
write_codes(Codes, Line, Out).
copy_codes(Codes0, Line0, Out, State, State) :-
format(Out, '<pre class="listing">~n', []),
delete_leading_white_lines(Codes0, Codes, Line0, Line),
assert(lineno),
write_codes(Codes, Line, Out).
%! copy_full_stop(+In, +Out) is det.
%
% Copy upto and including the .
copy_full_stop(In, Out) :-
get_code(In, C0),
copy_full_stop(C0, In, Out).
copy_full_stop(0'., _, Out) :-
!,
put_code(Out, 0'.).
copy_full_stop(C, In, Out) :-
put_code(Out, C),
get_code(In, C2),
copy_full_stop(C2, In, Out).
%! delete_leading_white_lines(+CodesIn, -CodesOut, +LineIn, -Line) is det.
%
% Delete leading white lines. Used after structured comments. The
% last two arguments update the start-line number of the <pre>
% block that is normally created.
delete_leading_white_lines(Codes0, Codes, Line0, Line) :-
append(LineCodes, [10|Rest], Codes0),
all_spaces(LineCodes),
!,
Line1 is Line0 + 1,
delete_leading_white_lines(Rest, Codes, Line1, Line).
delete_leading_white_lines(Codes, Codes, Line, Line).
%! copy_without_trailing_white_lines(+In, +End, +StateIn, -StateOut) is det.
%
% Copy input, but skip trailing white-lines. Used to copy the text
% leading to a structured comment.
copy_without_trailing_white_lines(In, End, Out, State, State) :-
member(pre(_), State),
!,
line_count(In, Line),
read_to(In, End, Codes0),
delete_trailing_white_lines(Codes0, Codes),
write_codes(Codes, Line, Out).
copy_without_trailing_white_lines(In, End, Out, State0, State) :-
copy_to(In, End, Out, State0, State).
delete_trailing_white_lines(Codes0, []) :-
all_spaces(Codes0),
!.
delete_trailing_white_lines(Codes0, Codes) :-
append(Codes, Tail, [10|Rest], Codes0),
!,
delete_trailing_white_lines(Rest, Tail).
delete_trailing_white_lines(Codes, Codes).
%! append(-First, -FirstTail, ?Rest, +List) is nondet.
%
% Split List. First part is the difference-list First-FirstTail.
append(T, T, L, L).
append([H|T0], Tail, L, [H|T]) :-
append(T0, Tail, L, T).
all_spaces([]).
all_spaces([H|T]) :-
code_type(H, space),
all_spaces(T).
copy_to(In, End, Out) :-
line_count(In, Line),
read_to(In, End, Codes),
( debugging(htmlsrc)
-> length(Codes, Count),
debug(htmlsrc, 'Copy ~D chars: ~s', [Count, Codes])
; true
),
write_codes(Codes, Line, Out).
read_to(In, End, Codes) :-
character_count(In, Here),
Len is End - Here,
read_n_codes(In, Len, Codes).
%! write_codes(+Codes, +Line, +Out) is det.
%
% Write codes that have been read starting at Line.
write_codes([], _, _).
write_codes([H|T], L0, Out) :-
content_escape(H, Out, L0, L1),
write_codes(T, L1, Out).
%! content_escape(+Code, +Out, +Line0, -Line) is det
%
% Write Code to Out, while taking care of.
%
% * Use HTML entities for =|<&>|=
% * If a line-no-tag is requested, write it
% * On \n, post a line-no request. If nonl/0 is set,
% do _not_ emit a newline as it is implied by the
% closed environment.
content_escape(_, Out, L, _) :-
( lineno
-> retractall(lineno),
write_line_no(L, Out),
fail
; fail
).
content_escape(0'\n, Out, L0, L) :-
!,
L is L0 + 1,
( retract(nonl)
-> true
; nl(Out)
),
assert(lineno).
content_escape(0'<, Out, L, L) :-
!,
format(Out, '<', []).
content_escape(0'>, Out, L, L) :-
!,
format(Out, '>', []).
content_escape(0'&, Out, L, L) :-
!,
format(Out, '&', []).
content_escape(C, Out, L, L) :-
put_code(Out, C).
write_line_no(LineNo, Out) :-
format(Out, '<span class="line-no">~|~t~d~5+</span>', [LineNo]).
%! copy_rest(+In, +Out, +StateIn, -StateOut) is det.
%
% Copy upto the end of the input In.
copy_rest(In, Out, State0, State) :-
copy_to(In, -1, Out, State0, State).
%! read_n_codes(+In, +N, -Codes)
%
% Read the next N codes from In as a list of codes. If N < 0, read
% upto the end of stream In.
read_n_codes(_, N, Codes) :-
N =< 0,
!,
Codes = [].
read_n_codes(In, N, Codes) :-
get_code(In, C0),
read_n_codes(N, C0, In, Codes).
read_n_codes(_, -1, _, []) :- !.
read_n_codes(1, C, _, [C]) :- !.
read_n_codes(N, C, In, [C|T]) :-
get_code(In, C2),
N2 is N - 1,
read_n_codes(N2, C2, In, T).
%! element(+Class, -HTMLElement, -CSSClass) is nondet.
%
% Map classified objects to an HTML element and CSS class. The
% actual clauses are created from the 1st argument of
% prolog_src_style/2.
term_expansion(element(_,_,_), Clauses) :-
findall(C, element_clause(C), Clauses).
%element_tag(directive, div) :- !.
element_tag(_, span).
element_clause(element(Term, Tag, CSS)) :-
span_term(Term, CSS),
element_tag(Term, Tag).
span_term(Classification, Class) :-
syntax_colour(Classification, _Attributes),
css_class(Classification, Class).
css_class(Class, Class) :-
atom(Class),
!.
css_class(Term, Class) :-
Term =.. [P1,A|_],
( var(A)
-> Class = P1
; css_class(A, P2),
atomic_list_concat([P1, -, P2], Class)
).
element(_,_,_). % term expanded