Author(s): Daniel Cabeza, Manuel Hermenegildo, Sacha Varma.
Version: 1.10#7 (2006/4/26, 19:22:13 CEST)
Version of last change: 1.9#351 (2004/6/23, 18:37:20 CEST)
This module implements the predicates of the PiLLoW package related to HTML/ XML generation and parsing, CGI and form handlers programming, and in general all the predicates which do not imply the use of the HTTP protocol.
html):- use_module(library(html)).
output_html/1,
html2terms/2,
xml2terms/2,
html_template/3,
html_report_error/1,
get_form_input/1,
get_form_value/3,
form_empty_value/1,
form_default/3,
set_cookie/2,
get_cookies/1,
url_query/2,
url_query_amp/2,
url_query_values/2,
my_url/1,
url_info/2,
url_info_relative/3,
form_request_method/1,
icon_address/2,
html_protect/1,
http_lines/3.
define_flag/3,
html_expansion/2.
html)
output_html(HTMLTerm)
Outputs HTMLTerm, interpreted as an
html_term/1, to current output stream.
html2terms(String, Terms)
String is a character list containing HTML code and Terms is its prolog structured representation.
Usage 1: html2terms(-string, +html_term)
Usage 2: html2terms(+string, ?canonic_html_term)
xml2terms(String, Terms)
String is a character list containing XML code and Terms is its prolog structured representation.
Usage 1: xml2terms(-string, +html_term)
Usage 2: xml2terms(+string, ?canonic_xml_term)
html_template(Chars, Terms, Dict)
Interprets Chars as an HTML template returning in Terms the corresponding structured HTML-term, which includes variables, and unifying Dict with a dictionary of those variables (an incomplete list of name=Var pairs). An HTML template is standard HTML code, but in which "slots" can be defined and given an identifier. These slots represent parts of the HTML code in which other HTML code can be inserted, and are represented in the HTML-term as free variables. There are two kinds of variables in templates:
<V>name</V>.
_, followed by its name, which must be formed by alphabetic characters.
As an example, suposse the following HTML template:
<html> <body bgcolor=_bgcolor> <v>content</v> </body> </html>
The following query in the Ciao toplevel shows how the template is parsed, and the dictionary returned:
?- file_to_string('template.html',_S), html_template(_S,Terms,Dict).
Dict = [bgcolor=_A,content=_B|_],
Terms = [env(html,[],["
",env(body,[bgcolor=_A],["
",_B,"
"]),"
"]),"
"] ?
yes
If a dictionary with values is supplied at call time, then variables are unified accordingly inside the template:
?- file_to_string('template.html',_S),
html_template(_S,Terms,[content=b("hello world!"),bgcolor="white"]).
Terms = [env(html,[],["
",env(body,[bgcolor="white"],["
",b("hello world!"),"
"]),"
"]),"
"] ?
yes
Usage: html_report_error(Error)
Error as a standard HTML page.
get_form_input(Dict)
Translates input from the form (with either the POST or GET methods, and even with CONTENT_TYPE multipart/form-data) to a dictionary Dict of attribute=value pairs. If the flag raw_form_values is off (which is the default state), it translates empty values (which indicate only the presence of an attribute) to the atom '$empty', values with more than one line (from text areas or files) to a list of lines as strings, the rest to atoms or numbers (using
name/2). If the flag on, it gives all values as atoms, without translations.
get_form_value(Dict, Var, Val)
Unifies Val with the value for attribute Var in dictionary Dict. Does not fail: value is " if not found (this simplifies the programming of form handlers when they can be accessed directly).
Usage: form_empty_value(Term)
Term, a value comming from a text area is empty (can have spaces, newlines and linefeeds).
Usage: form_default(+Val, +Default, -NewVal)
Val is empty then NewVal=Default, else NewVal=Val.
set_cookie(Name, Value)
Sets a cookie of name Name and value Value. Must be invoked before outputting any data, including the cgi_reply html-term.
get_cookies(Cookies)
Unifies Cookies with a dictionary of attribute=value pairs of the active cookies for this URL. If the flag raw_form_values is on, values are always atoms even if they could be interpreted as numbers.
url_query(Dict, URLArgs)
(Deprecated, see
url_query_values/2) Translates a dictionary Dict of parameter values into a string URLArgs for appending to a URL pointing to a form handler.
url_query_amp(Dict, URLArgs)
Translates a dictionary Dict of parameter values into a string URLArgs for appending to a URL pointing to a form handler to be used in the href of a link (uses & instead of &).
url_query_values(Dict, URLArgs)
Dict is a dictionary of parameter values and URLArgs is the URL-encoded string of those assignments, which may appear after an URL pointing to a CGI script preceded by a '?'. Dict is computed according to the raw_form_values flag. The use of this predicate is reversible.
my_url(URL)
Unifies URL with the Uniform Resource Locator (WWW address) of this cgi executable.
url_info(URL, URLTerm)
Translates a URL URL to a Prolog structure URLTerm which details its various components, and vice-versa. For now non-HTTP URLs make the predicate fail.
url_info_relative(URL, BaseURLTerm, URLTerm)
Translates a relative URL URL which appears in the HTML page refered to by BaseURLTerm into URLTerm, a Prolog structure containing its absolute parameters. Absolute URLs are translated as with
url_info/2. E.g.
url_info_relative("dadu.html",
http('www.foo.com',80,"/bar/scoob.html"), Info)
gives Info = http('www.foo.com',80,"/bar/dadu.html").
Usage: form_request_method(Method)
Method with the method of invocation of the form handler (GET or POST).
Method is an atom.
(basic_props:atm/1)
icon_address(Img, IAddress)
The PiLLoW image Img has URL IAddress.
html_protect(Goal)
Calls Goal. If an error occurs during its execution, or it fails, an HTML page is output informing about the incident. Normaly the whole execution of a CGI is protected thus.
Meta-predicate with arguments: html_protect(goal).
Usage:
Goal is a term which represents a goal, i.e., an atom or a structure.
(basic_props:callable/1)
Usage: http_lines(Lines, String, Tail)
Lines is a list of the lines with occur in String until Tail. The lines may end UNIX-style or DOS-style in String, in Lines they have not end of line characters. Suitable to be used in DCGs.
Lines is a list of strings.
(basic_props:list/2)
String is a string (a list of character codes).
(basic_props:string/1)
Tail is a string (a list of character codes).
(basic_props:string/1)
html)Defines a flag as follows:
define_flag(raw_form_values,[on,off],off).
(See section Changing system behaviour and various flags).
If flag is on, values returned by
get_form_input/1 are always atoms, unchanged from its original value.
The predicate is multifile.
The predicate is multifile.
Usage: html_expansion(Term, Expansion)
html)The code uses input from from L. Naish's forms and F. Bueno's previous Chat interface. Other people who have contributed are (please inform us if we leave out anybody): Markus Fromherz, Samir Genaim.
Go to the first, previous, next, last section, table of contents.