forked from SWI-Prolog/packages-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml_quasiquotations.pl
103 lines (86 loc) · 3.7 KB
/
html_quasiquotations.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
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 2013, 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., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301 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_quasi_quotations, [ html/4 ]).
:- use_module(library(sgml)).
:- use_module(library(apply)).
:- use_module(library(error)).
:- use_module(library(lists)).
:- use_module(library(quasi_quotations)).
/** <module> HTML quasi quotations
@ingroup http
This module implements quasi quotations for HTML. Together with
library(http/html_write), this allows for inclusion of long HTML
fragments in the Prolog source code while replacing attributes and
content with variable that come from the surrounding Prolog clause.
This module is included and re-exported from library(http/html_write).
@see library(http/js_write) provides quasi quotation for JavaScript.
*/
%% html(+Content, +Vars, +VarDict, -DOM) is det.
%
% The predicate html/4 implements HTML quasi quotations. These
% quotations produce a DOM term that is suitable for html//1 and
% other predicates that are declared to consume this format. The
% quasi quoter only accepts valid, but possibly partial HTML
% documents. The document *must* begin with a tag. The quoter
% replaces attributes or content whose value is a Prolog variable
% that appears in the argument list of the =html= indicator. If
% the variable defines content, it must be the only content. Here
% is an example, replacing both a content element and an
% attribute. Note that the document is valid HTML.
%
% ==
% html({|html(Name, URL)||
% <p>Dear <span class="name">Name</span>,
%
% <p>You can <a href="URL">download</a> the requested
% article now.
% |}
% ==
:- quasi_quotation_syntax(html).
html(Content, Vars, Dict, DOM) :-
must_be(list, Dict),
include(qq_var(Vars), Dict, QQDict),
with_quasi_quotation_input(
Content, In,
load_html(In, DOM0,
[ max_errors(0),
case_sensitive_attributes(true)
])),
xml_content(QQDict, DOM0, DOM).
qq_var(Vars, _=Var) :- member(V, Vars), V == Var, !.
xml_content(Dict, [Name], [Var]) :-
atom(Name),
memberchk(Name=Var, Dict), !.
xml_content(Dict, Content0, Content) :-
maplist(xml_content_element(Dict), Content0, Content).
xml_content_element(Dict,
element(Tag, Attrs0, Content0),
element(Tag, Attrs, Content)) :- !,
maplist(xml_attribute(Dict), Attrs0, Attrs),
xml_content(Dict, Content0, Content).
xml_content_element(_, Element, Element).
xml_attribute(Dict, Attr=Name, Attr=Var) :-
memberchk(Name=Var, Dict), !.
xml_attribute(_, Attr, Attr).