34
35:- module(format_style,
36 [ element_css/3, 37 css_block_options/5, 38 css_inline_options/3, 39 attrs_classes/2, 40 style_css_attrs/2 41 ]). 42:- autoload(library(apply),[maplist/3,convlist/3]). 43:- autoload(library(lists),[append/2,list_to_set/2]). 44:- autoload(library(option),[option/3]). 45
46:- multifile
47 html_text:style/3.
51element_css(El, Attrs, CSS) :-
52 findall(CSSs, applicable_style(El, Attrs, CSSs), CssList),
53 CssList \== [],
54 append(CssList, CSS0),
55 list_to_set(CSS0, CSS).
56
57applicable_style(a, Attrs, [href(HREF)]) :- 58 memberchk(href=HREF, Attrs).
59applicable_style(_, Attrs, CSS) :-
60 memberchk(style=Style, Attrs),
61 style_css_attrs(Style, CSS0),
62 include(text_style, CSS0, CSS).
63applicable_style(El, Attrs, CSS) :-
64 html_text:style(El, Cond, CSS),
65 ( eval(Cond, Attrs)
66 -> true
67 ).
68
69eval(true, _).
70eval(class(Class), Attrs) :-
71 attrs_classes(Attrs, Classes),
72 memberchk(Class, Classes).
73
74attrs_classes(Attrs, Classes) :-
75 memberchk(class=Spec, Attrs),
76 split_string(Spec, " \t\r\n", " \t\r\n", ClassStrings),
77 maplist(atom_string, Classes, ClassStrings).
85style_css_attrs(Style, CSS) :-
86 split_string(Style, ";", " \t\r\n", Parts),
87 convlist(style_css_attr, Parts, CSS).
88
89style_css_attr(Style, CSS) :-
90 split_string(Style, ":", " \t\r\n", [NameS,ValueS]),
91 atom_string(Name, NameS),
92 atom_string(Value, ValueS),
93 CSS =.. [Name,Value].
94
95text_style(float(right)).
101css_block_options(CSS, Top0-Bottom0, Top-Bottom, ParOptions, Style) :-
102 option(margin_top(Top), CSS, Top0),
103 option(margin_bottom(Bottom), CSS, Bottom0),
104 convlist(par_option, CSS, ParOptions),
105 convlist(font_style, CSS, Style).
106
107par_option(text_align(Align), text_align(Align)).
108par_option(margin_left(Align), margin_left(Align)).
109par_option(margin_right(Align), margin_right(Align)).
110
111font_style(font_weight(bold), bold).
112font_style(font_weight(normal), normal).
113font_style(color(BC), hfg(C)) :- atom_concat(bright_, C, BC).
114font_style(color(C), fg(C)).
115font_style(background(BC), hbg(C)) :- atom_concat(bright_, C, BC).
116font_style(background(C), bg(C)).
117font_style(text_decoration(none), underline(false)).
123css_inline_options(CSS, Left-Right, Style) :-
124 option(margin_left(Left), CSS, 0),
125 option(margin_right(Right), CSS, 0),
126 convlist(inline_style, CSS, Style).
127
128inline_style(CSS, Style) :-
129 font_style(CSS, Style),
130 !.
131inline_style(float(right), float(right)).
132inline_style(href(HREF), href(HREF))