29
30:- module(html_basics,
31 [ hidden//2, 32 form_input//2, 33 form_submit//1, 34 n//2, 35 nc//2, 36 nc//3, 37 odd_even_row//3, 38 sort_th//3, 39 insert_html_file//1 40 ]). 41:- use_module(library(http/html_write)). 42:- use_module(library(sgml)). 43:- use_module(library(lists)). 44:- use_module(library(option)). 45:- use_module(library(occurs)). 46:- use_module(library(http/http_dispatch)). 47:- use_module(library(http/http_wrapper)). 48
49:- html_meta((
50 form_input(html, html, ?, ?),
51 odd_even_row(+, -, html, ?, ?),
52 sort_th(+, +, html, ?, ?))).
57
65hidden(Name, Value) -->
66 html(input([ type(hidden),
67 name(Name),
68 value(Value)
69 ])).
79form_input(Label, Input) -->
80 html(tr([ th(class(label), Label),
81 td(Input)
82 ])).
83
84
85form_submit(Label) -->
86 html(tr(class(buttons),
87 [ th([align(right), colspan(2)],
88 input([ type(submit),
89 value(Label)
90 ]))
91 ])).
92
93
94
111nc(Fmt, Value) -->
112 nc(Fmt, Value, []).
113
114nc(Fmt, Value, Options) -->
115 { class(Value, Class),
116 merge_options(Options,
117 [ align(right),
118 class(Class)
119 ], Opts),
120 number_html(Fmt, Value, HTML)
121 },
122 html(td(Opts, HTML)).
123
124class(Value, Class) :-
125 ( integer(Value)
126 -> Class = int
127 ; float(Value)
128 -> Class = float
129 ; Class = value
130 ).
137odd_even_row(Row, Next, Content) -->
138 { ( Row mod 2 =:= 0
139 -> Class = even
140 ; Class = odd
141 ),
142 Next is Row+1
143 },
144 html(tr(class(Class), Content)).
161sort_th(Name, Name, Label) -->
162 html(th(a([class(sorted)], Label))).
163sort_th(Name, _By, Label) -->
164 { http_current_request(Request),
165 http_reload_with_parameters(Request, [sort_by(Name)], HREF)
166 },
167 html(th(a([href(HREF), class(resort)], Label))).
168
169
170
180n(Fmt, Value) -->
181 { number_html(Fmt, Value, HTML) },
182 html(HTML).
183
184number_html(human, Value, HTML) :-
185 integer(Value), !,
186 human_count(Value, HTML).
187number_html(Fmt, Value, HTML) :-
188 number(Value), !,
189 HTML = Fmt-[Value].
190number_html(_, Value, '~p'-[Value]).
191
192
193human_count(Number, HTML) :-
194 Number < 1024, !,
195 HTML = '~d'-[Number].
196human_count(Number, HTML) :-
197 Number < 1024*1024, !,
198 KB is Number/1024,
199 digits(KB, N),
200 HTML = '~*fK'-[N, KB].
201human_count(Number, HTML) :-
202 Number < 1024*1024*1024, !,
203 MB is Number/(1024*1024),
204 digits(MB, N),
205 HTML = '~*fM'-[N, MB].
206human_count(Number, HTML) :-
207 TB is Number/(1024*1024*1024),
208 digits(TB, N),
209 HTML = '~*fG'-[N, TB].
210
211digits(Count, N) :-
212 ( Count < 100
213 -> N = 1
214 ; N = 0
215 ).
216
217
218
227insert_html_file(Alias) -->
228 { absolute_file_name(Alias, Page, [access(read)]),
229 load_html_file(Page, DOM),
230 contains_term(element(body, _, Body), DOM),
231 Style = element(style, _, _),
232 findall(Style, sub_term(Style, DOM), Styles),
233 append(Styles, Body, Content)
234 },
235 html(Content)
Simple Small HTML components
*/