aboutsummaryrefslogtreecommitdiff
path: root/src/guff/page-html-view.cr
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2016-03-06 02:13:07 -0500
committerPaul Duncan <pabs@pablotron.org>2016-03-06 02:13:07 -0500
commit7e24e13e887d441fd82a8d427913cfe36d5682da (patch)
tree52015789322efb6454a37c88771a038289ac5c29 /src/guff/page-html-view.cr
parent2421d5f15569515d9fda3c6f36edeaf7e5222b54 (diff)
downloadold-guff-7e24e13e887d441fd82a8d427913cfe36d5682da.tar.bz2
old-guff-7e24e13e887d441fd82a8d427913cfe36d5682da.zip
s/HTMLPageView/PageHTMLView/
Diffstat (limited to 'src/guff/page-html-view.cr')
-rw-r--r--src/guff/page-html-view.cr66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/guff/page-html-view.cr b/src/guff/page-html-view.cr
new file mode 100644
index 0000000..42ea454
--- /dev/null
+++ b/src/guff/page-html-view.cr
@@ -0,0 +1,66 @@
+require "ecr/macros"
+require "html"
+
+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)
+ end
+
+ def body_attrs
+ {
+ "id": @body_id,
+ "class": @body_class,
+ }.map { |k, v| attr(k, v) }.join(" ")
+ 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[:style] % [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
+
+ ECR.def_to_s("./src/guff/views/page.ecr")
+ end
+end