%-----------------------------------------------------------------------------%
% Copyright (C) 1997 The University of Melbourne.
% This file may only be copied under the terms of the GNU Library General
% Public License - see the file COPYING.LIB in the Mercury distribution.
%-----------------------------------------------------------------------------%
% File: html.m.
% Author: fjh.
% This module provides a strongly-typed, quite declarative method
% for representing and outputting arbitrary HTML text.
% It is intended for use in CGI scripts.
%
% Basically all the predicates here are wrappers around io__write_string.
% However, the types defined here let you indicate the structure of your
% HTML text in the structure of the Mercury terms used to represent it.
%-----------------------------------------------------------------------------%
:- module html.
:- interface.
:- import_module bool, io, string, std_util.
%-----------------------------------------------------------------------------%
:- type html
---> html(
header,
body
).
:- type header == list(header_item).
:- type header_item
---> title(markup)
; header_item(string) % String can contain any HTML markup.
% This is a general "catch-all" for
% anything not covered by the above
% cases.
.
:- type body == markup.
% XXX add anchors
:- type markup
---> heading(int, markup)
; style(style, markup) % a.k.a. logical style
; font(font, markup) % a.k.a. physical style
; text(string)
; definition_list(list(pair(markup)))
; list(list_type, list(markup))
; form(string, markup) % actionURL, form contents
; field(string, field) % name, field type
; address(markup)
; np % new paragraph
; br % line break
; hr % horizontal_rule
; markup(string) % String can contain any HTML markup.
% This is a general "catch-all" for
% anything not covered by the above
% cases.
; ','(markup, markup)
.
:- type list_type
---> ordered
; unordered
; menu
; directory
.
:- type style
---> emph
; strong
; samp
; code
; keyboard
; cite
; var
.
:- type font
---> italics
; bold
; underline
; typewriter % typewriter fixed-width font
.
% XXX add maps
:- type field
---> text(
int, % size (display width in characters)
int, % maxlength
string % initial (default) value
)
; password(
int, % size
int, % maxlength
string % initial (default) value
)
; textarea(
int, int, % rows, columns
string % initial (default) value
)
; checkbox(
bool, % initial (default) value
string % value sent, if checkbox set
)
; radio(
bool, % initial (default) value
string % value sent, if button set
)
; select(
int, % size,
bool, % allow multiple selections?
list(pair(
string, % selection text
bool % selected?
))
)
; submit(
string % text on the pushbutton
)
; reset(
string % text on the pushbutton
)
; hidden(
string % value
)
.
:- pred output_content_type_html(state, state).
:- mode output_content_type_html(di, uo) is det.
:- pred output_html(html, state, state).
:- mode output_html(in, di, uo) is det.
:- pred output_header(header, state, state).
:- mode output_header(in, di, uo) is det.
:- pred output_header_item(header_item, state, state).
:- mode output_header_item(in, di, uo) is det.
:- pred output_body(body, state, state).
:- mode output_body(in, di, uo) is det.
:- pred output_markup(markup, state, state).
:- mode output_markup(in, di, uo) is det.
:- pred output_field(string, field, io__state, io__state).
:- mode output_field(in, in, di, uo) is det.
:- pred output_form_start(string::in, io__state::di, io__state::uo) is det.
:- pred output_form_end(io__state::di, io__state::uo) is det.
% convert any special characters in a HTML markup string into
% appropriate HTML escapes
:- func escape_html_string(string) = string.
:- pred escape_html_string(string::in, string::out) is det.
% convert any special characters in a HTML attribute value string
% into appropriate HTML escapes
:- func escape_attr_string(string) = string.
:- pred escape_attr_string(string::in, string::out) is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module int, char, list.
%-----------------------------------------------------------------------------%
:- func list_type_name(list_type) = string.
list_type_name(ordered) = "ol".
list_type_name(unordered) = "ul".
list_type_name(menu) = "menu".
list_type_name(directory) = "dir".
:- func style_name(style) = string.
style_name(emph) = "em".
style_name(strong) = "strong".
style_name(samp) = "samp".
style_name(code) = "code".
style_name(keyboard) = "kbd".
style_name(cite) = "cite".
style_name(var) = "var".
:- func font_name(font) = string.
font_name(italics) = "it".
font_name(bold) = "b".
font_name(underline) = "u".
font_name(typewriter) = "tt".
%-----------------------------------------------------------------------------%
output_content_type_html -->
write_string("Content-type: text/html\n\n").
output_html(html(Head, Body)) -->
output_header(Head),
nl,
output_body(Body).
output_header(HeaderItems) -->
output_markup_scope("head",
output_list(output_header_item, HeaderItems)).
output_header_item(title(Title)) -->
output_markup_scope("title",
output_markup(Title)).
output_header_item(header_item(Markup)) -->
write_string(Markup).
output_body(Body) -->
output_markup_scope("body",
output_markup(Body)).
output_markup((Markup1, Markup2)) -->
output_markup(Markup1),
output_markup(Markup2).
output_markup(address(Address)) -->
output_markup_scope("address",
output_markup(Address)).
output_markup(heading(Level, Heading)) -->
format("
\n").
output_markup(br) -->
write_string("
\n").
output_markup(hr) -->
write_string("