-
Notifications
You must be signed in to change notification settings - Fork 10
/
messages.pl
146 lines (125 loc) · 4.87 KB
/
messages.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
/* Part of ClioPatria
Author: Jan Wielemaker
E-mail: J.Wielemaker@cs.vu.nl
WWW: http://www.swi-prolog.org
Copyright (C): 2010, 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(html_messages,
[ call_showing_messages/2
]).
:- use_module(library(http/html_write)).
:- use_module(library(http/html_head)).
:- use_module(library(option)).
/** <module> Run goals that produce messages
This module allows executing (long running) Prolog goals and see the
messages appear in the browser.
*/
:- meta_predicate
call_showing_messages(0, +).
%% call_showing_messages(:Goal, +Options) is det.
%
% Execute Goal, showing the feedback in the browser. This
% predicate builds a default application page with a placeholder
% for the messages. It then sends all HTML upto the placeholder
% and flushes the output to the browser. During execution, all
% output from Goal emitted through print_message/2 is caught in
% the message-box. After completion of Goal the page is completed.
%
% This predicate is intended for action such as loading RDF files,
% while providing feedback on files loaded and possible error
% messages. Note that this call creates a complete page.
%
% @bug This call uses =chunked= transfer encoding to send the
% page in parts. Not all browsers support this and not
% all browsers update the page incrementally.
:- create_prolog_flag(html_messages, false, [type(boolean)]).
assert_message_hook :-
Head = message_hook(_Term, Level, Lines),
Body = send_message(Level, Lines),
( clause(user:Head, Body)
-> true
; asserta((user:Head :- Body))
).
:- initialization
assert_message_hook.
call_showing_messages(Goal, Options) :-
option(style(Style), Options, default),
option(head(Head), Options, title('SWI-Prolog -- make')),
option(header(Header), Options,
div(class(msg_header),
h4('Messages ...'))),
( option(footer(Footer), Options)
-> true
; ( option(return_to(ReturnURI), Options)
-> FooterRest = [ p(['Go ', a(href(ReturnURI), 'back'),
' to the previous page']) ]
; FooterRest = []
),
Footer = div(class(msg_footer), [ h4('Done') | FooterRest ])
),
format('Content-Type: text/html~n'),
format('Transfer-Encoding: chunked~n~n'),
header(Style, Head, Header, Footer, FooterTokens),
setup_call_cleanup(
set_prolog_flag(html_messages, true),
catch(Goal, E, print_message(error, E)),
set_prolog_flag(html_messages, false)), !,
footer(FooterTokens).
send_message(Level, Lines) :-
current_prolog_flag(html_messages, true),
level_css_class(Level, Class),
phrase(html(pre(class(Class), \html_message_lines(Lines))), Tokens),
with_mutex(html_messages, print_html(Tokens)),
flush_output,
fail.
level_css_class(informational, msg_informational).
level_css_class(warning, msg_warning).
level_css_class(error, msg_error).
html_message_lines([]) -->
[].
html_message_lines([nl|T]) --> !,
html('\n'), % we are in a <pre> environment
html_message_lines(T).
html_message_lines([flush]) -->
[].
html_message_lines([H|T]) --> !,
html(H),
html_message_lines(T).
%% header(+Style, +Head, +Header, +Footer, -FooterTokens)
%
% Emit all tokens upto the placeholder for the actual messages and
% return the remaining page-tokens in FooterTokens. Style and Head
% are passed
header(Style, Head, Header, Footer, FooterTokens) :-
Magic = '$$$MAGIC$$$',
Body = [ Header,
\(html_messages:html_requires(css('messages.css'))),
div(class(messages), Magic),
Footer
],
phrase(html_write:page(Style, Head, Body), Tokens),
html_write:mailman(Tokens),
append(HeaderTokens, [Magic|FooterTokens], Tokens), !,
current_output(Out),
html_write:write_html(HeaderTokens, Out),
flush_output(Out).
footer(Footer) :-
current_output(Out),
html_write:write_html(Footer, Out).