-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebsite_html.pro
85 lines (68 loc) · 2.34 KB
/
website_html.pro
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
/** <module> HTML abstract syntax tree
This enhances the usability and documentation of library(http/html_write).
Actions:
- render_ast/1
Relations:
- ast_string/2
*/
:- module(website_html, [
code_html/2
, codes_html/2
, string_html/2
, ast_string/2
, render_ast/1
]).
:- use_module(library(http/html_write)).
:- consult_unregistered("html_escape.pro").
/** render_ast(+Ast)
Render Ast to current_output/1.
Ast follows the syntax of page//1 or page//2 in library(http/html_write):
- Ast has the shape page(ElemList) or page(Head, Body).
It represents the entire HTML document.
- =Head= is an =ElemList=. It represents the contents of the =head= tag.
- =Body= is an =ElemList=. It represents the contents of the =body= tag.
- An =ElemList= is a list of =Elem=s.
- An =Elem= has the shape =|Tag(AttrList, ElemList)|=.
It represents the HTML fragment =|<Tag AttrList>ElemList</Tag>|=.
- =AttrList= is a list of =Attr=.
- An =Attr= has the shape =|Attr(Val)|=.
It represents the HTML attribute =|Attr=Val|=.
- =ElemList= is as defined above. Thus it is recursive.
Example:
```
% Using page//1.
render_ast(
page([
head([title('Foo')]),
body([
p('First paragraph.')
, p(class(foo), 'Second paragraph with class.')
, p([align(left), class(foo)], 'Third paragraph with many attributes.')
])
])).
% Using page//2.
render_ast(
page(
[title('Foo')],
[
p('First paragraph.')
, p(class(foo), 'Second paragraph with class.')
, p([align(left), class(foo)], 'Third paragraph with many attributes.')
]
)).
```
*/
render_ast(Ast) :- ast_tokens(Ast, Toks), print_tokens(Toks).
/** ast_string(+Ast, -Str:string)
"Rendering Ast produces the string Str."
Ast has the shape documented in render_ast/1.
*/
ast_string(Ast, Str) :- ast_tokens(Ast, Toks), tokens_string(Toks, Str).
/** ast_tokens(+Ast, -Ren:string)
"Rendering Ast produces the token list Ren."
*/
ast_tokens(page(C), Ren) :- !, phrase(page(C), Ren).
ast_tokens(page(H, B), Ren) :- !, phrase(page(H, B), Ren).
ast_tokens(A, _) :- throw(error(invalid_page_ast(A), _)).
tokens_string(Toks, Str) :- with_output_to(string(Str), print_tokens(Toks)).
print_tokens(Toks) :- print_html(Toks).