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
|
require "ecr/macros"
require "html"
require "../../page-features"
module Guff
class PageHTMLView
property :charset
property :lang
property :title
property :body
property :scripts
property :styles
property :metas
property :body_id
property :body_class
FORMATS = {
attr: "%s='%s'",
meta: "<meta %s/>",
style: "<link rel='stylesheet' type='text/css' href='%s'/>",
script: "<script type='text/javascript' src='%s'></script>",
}
def initialize(@title : String = "", @body : String = "")
@charset = "utf-8"
@lang = "en-US"
@scripts = [] of String
@styles = [] of String
@metas = [] of Hash(String, String)
@features = {} of String => Bool
end
def body_attrs
r = {
"id": @body_id,
"class": @body_class,
}.map { |k, v| attr(k, v) }.join(" ").strip
# prefix with space if content
" " + r if r.size > 0
end
def page_headers : String
(@metas.map { |meta|
FORMATS[:meta] % [meta.map { |k, v| attr(k, v) }.join(" ")]
} + @styles.map { |path|
FORMATS[:style] % [h(path)]
}).join("")
end
def page_footers : String
@scripts.map { |path|
FORMATS[:script] % [h(path)]
}.join("")
end
def h(s : String?) : String
s ? HTML.escape(s) : ""
end
def attr(k : String, v : String?) : String
v ? FORMATS[:attr] % [k, h(v)] : ""
end
def content_type
"text/html; charset=%s" % [@charset]
end
def add_feature(key : String)
unless @features.has_key?(key)
Guff::PageFeatures.add(key, self)
@features[key] = true
end
end
def add_features(features : Array(String))
features.each { |key| add_feature(key) }
end
ECR.def_to_s("./src/guff/views/ecrs/page.ecr")
end
end
|