aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2016-03-05 20:13:53 -0500
committerPaul Duncan <pabs@pablotron.org>2016-03-05 20:13:53 -0500
commit58bbed61ddec922fec53a39ec1472f2a5c480a0b (patch)
treea8baf43f337e974ae84d1fac4cdada85bf1e5aad
parent2feff82cf98724bb1f8249d681f116459241c929 (diff)
downloadold-guff-58bbed61ddec922fec53a39ec1472f2a5c480a0b.tar.bz2
old-guff-58bbed61ddec922fec53a39ec1472f2a5c480a0b.zip
refactor page
-rw-r--r--src/guff.cr8
-rw-r--r--src/guff/html-page-view.cr43
-rw-r--r--src/guff/views/page.ecr5
3 files changed, 45 insertions, 11 deletions
diff --git a/src/guff.cr b/src/guff.cr
index b2794b6..533a10f 100644
--- a/src/guff.cr
+++ b/src/guff.cr
@@ -18,8 +18,12 @@ module Guff
md.to_s
]
- context.response.content_type = "text/html; charset=utf-8"
- context.response.puts HTMLPageView.new(title, "asdf")
+ page = HTMLPageView.new(title, "asdf")
+ page.styles << "foo/bar.css"
+
+ # TODO: refactor this
+ context.response.content_type = page.content_type
+ context.response.puts page
else
call_next(context)
end
diff --git a/src/guff/html-page-view.cr b/src/guff/html-page-view.cr
index 3ec33fe..e1f69a7 100644
--- a/src/guff/html-page-view.cr
+++ b/src/guff/html-page-view.cr
@@ -3,6 +3,8 @@ require "html"
module Guff
class HTMLPageView
+ property :charset
+ property :lang
property :title
property :body
property :scripts
@@ -11,26 +13,53 @@ module Guff
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
- # TODO
- ""
+ (@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
- # TODO
- ""
+ @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
diff --git a/src/guff/views/page.ecr b/src/guff/views/page.ecr
index 505731a..e009ac1 100644
--- a/src/guff/views/page.ecr
+++ b/src/guff/views/page.ecr
@@ -1,11 +1,12 @@
<!DOCTYPE html>
-<html lang='en-US'>
+<html lang='<%= h(@lang) %>'>
<head>
+ <meta charset="<%= h(@charset) %>"/>
<title><%= h(@title) %></title>
<%= page_headers %>
</head>
- <body id='<%= @body_id %>' class='<%= body_class %>'><%=
+ <body <%= body_attrs %>><%=
@body
%></body>