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
|
/* $Id$
Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: wielemak@science.uva.nl
WWW: http://www.swi-prolog.org
Copyright (C): 1985-2007, University of 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(pldoc_htmlsrc,
[ source_to_html/3 % +Source, +OutStream, +Options
]).
:- use_module(library(option)).
:- use_module(library(lists)).
:- 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(prolog_xref)).
/** <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
*/
:- thread_local
lineno/0, % print line-no on next output
nonl/0. % previous tag implies nl (block level)
%% 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.
%
% @param In A filename
% @param Out Term stream(Stream) or file-name specification
source_to_html(Src, stream(Out), Options) :- !,
retractall(lineno), % play safe
retractall(nonl), % play safe
colour_fragments(Src, Fragments),
open(Src, read, In),
asserta(user:message_hook(_,_,_), Ref),
call_cleanup((file_base_name(Src, Base),
print_html_head(Out, [title(Base), Options]),
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)]),
open(OutFile, write, Out, [encoding(utf8)]),
call_cleanup(source_to_html(Src, stream(Out), Options),
close(Out)).
%% 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.
print_html_head(Out, Options) :-
option(header(true), Options, true), !,
option(title(Title), Options, 'Prolog source'),
option(stylesheets(Sheets), Options, ['pllisting.css', 'pldoc.css']),
format(Out,
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" \
"http://www.w3.org/TR/html4/strict.dtd">~n~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', []).
print_html_head(_, _).
print_html_footer(Out, Options) :-
option(header(true), Options, true), !,
format(Out, '~N</body>~n', []),
format(Out, '</html>', []).
print_html_footer(_, _).
%% 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, structured_comment, []),
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),
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', [Out]),
print_html(Out, Tokens),
format(Out, '</dl>~n', [Out])
).
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)],
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) :- !,
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).
%% 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', [Out]),
line_count(In, Line0),
read_to(In, End, Codes0),
delete_leading_white_lines(Codes0, Codes, Line0, Line),
assert(lineno),
write_codes(Codes, Line, 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),
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, _) :-
retract(lineno),
write_line_no(L, Out),
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~4+</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(_, 0, []) :- !.
read_n_codes(In, N, Codes) :-
get_code(In, C0),
read_n_codes(N, C0, In, Codes).
read_n_codes(1, C, _, [C]) :- !.
read_n_codes(_, -1, _, []) :- !.
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/3, 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) :-
prolog_src_style(Classification, _Style),
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/3.
|