From bc86dbf55dd2c134fc2e5d0e6bf8cefbe6b1ceba Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Tue, 8 Mar 2016 17:21:09 -0500 Subject: refactor html views --- src/guff/api-docs-html-view.cr | 73 ---------------------------------------- src/guff/handlers/api-handler.cr | 3 +- src/guff/page-html-view.cr | 66 ------------------------------------ src/guff/views/html/api-docs.cr | 71 ++++++++++++++++++++++++++++++++++++++ src/guff/views/html/page.cr | 66 ++++++++++++++++++++++++++++++++++++ 5 files changed, 139 insertions(+), 140 deletions(-) delete mode 100644 src/guff/api-docs-html-view.cr delete mode 100644 src/guff/page-html-view.cr create mode 100644 src/guff/views/html/api-docs.cr create mode 100644 src/guff/views/html/page.cr diff --git a/src/guff/api-docs-html-view.cr b/src/guff/api-docs-html-view.cr deleted file mode 100644 index 3ee1e34..0000000 --- a/src/guff/api-docs-html-view.cr +++ /dev/null @@ -1,73 +0,0 @@ -require "html" -require "ecr/macros" -require "./page-html-view" -require "./api/methods" - -module Guff - class APIDocsHTMLView - TITLE = "Guff API Documentation" - - def self.run(context : HTTP::Server::Context) - page = PageHTMLView.new(TITLE, new.to_s) - context.response.content_type = page.content_type - context.response.puts page - end - - private def namespaces - API::Methods::METHODS.keys.sort - end - - private def methods( - namespace : String - ) - API::Methods::METHODS[namespace].keys.sort - end - - private def method_text( - namespace : String, - method : String - ) - API::Methods::METHODS[namespace][method][:text] as String - end - - private def method_args( - namespace : String, - method : String - ) - if method_has_args?(namespace, method) - args = API::Methods::METHODS[namespace][method][:args] as \ - Hash(String, Hash(Symbol, String|Symbol|Bool)) | Nil - args.keys.sort - else - [] of String - end - end - - private def arg_text( - namespace : String, - method : String, - name : String - ) : String - if method_has_args?(namespace, method) - arg = API::Methods::METHODS[namespace][method][:args][name] as\ - Hash(Symbol, String|Symbol|Bool) - arg[:text] as String - else - "" - end - end - - private def method_has_args?( - namespace : String, - method : String - ) - API::Methods::METHODS[namespace][method].has_key?(:args) - end - - def h(s : String) - HTML.escape(s || "") - end - - ECR.def_to_s("./src/guff/views/ecrs/api-docs.ecr") - end -end diff --git a/src/guff/handlers/api-handler.cr b/src/guff/handlers/api-handler.cr index f40a39d..77606ae 100644 --- a/src/guff/handlers/api-handler.cr +++ b/src/guff/handlers/api-handler.cr @@ -1,6 +1,7 @@ require "json" require "../handler" require "../api/*" +require "../views/html/api-docs" private macro define_method_calls(hash) case namespace @@ -142,6 +143,6 @@ class Guff::Handlers::APIHandler < Guff::Handler end private def do_docs(context : HTTP::Server::Context) - APIDocsHTMLView.run(context) + Guff::APIDocsHTMLView.run(context) end end diff --git a/src/guff/page-html-view.cr b/src/guff/page-html-view.cr deleted file mode 100644 index c300070..0000000 --- a/src/guff/page-html-view.cr +++ /dev/null @@ -1,66 +0,0 @@ -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: "", - style: "", - 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/ecrs/page.ecr") - end -end diff --git a/src/guff/views/html/api-docs.cr b/src/guff/views/html/api-docs.cr new file mode 100644 index 0000000..0b5aff3 --- /dev/null +++ b/src/guff/views/html/api-docs.cr @@ -0,0 +1,71 @@ +require "html" +require "ecr/macros" +require "../../api/methods" +require "./page" + +class Guff::APIDocsHTMLView + TITLE = "Guff API Documentation" + + def self.run(context : HTTP::Server::Context) + page = PageHTMLView.new(TITLE, new.to_s) + context.response.content_type = page.content_type + context.response.puts page + end + + private def namespaces + API::Methods::METHODS.keys.sort + end + + private def methods( + namespace : String + ) + API::Methods::METHODS[namespace].keys.sort + end + + private def method_text( + namespace : String, + method : String + ) + API::Methods::METHODS[namespace][method][:text] as String + end + + private def method_args( + namespace : String, + method : String + ) + if method_has_args?(namespace, method) + args = API::Methods::METHODS[namespace][method][:args] as \ + Hash(String, Hash(Symbol, String|Symbol|Bool)) | Nil + args.keys.sort + else + [] of String + end + end + + private def arg_text( + namespace : String, + method : String, + name : String + ) : String + if method_has_args?(namespace, method) + arg = API::Methods::METHODS[namespace][method][:args][name] as\ + Hash(Symbol, String|Symbol|Bool) + arg[:text] as String + else + "" + end + end + + private def method_has_args?( + namespace : String, + method : String + ) + API::Methods::METHODS[namespace][method].has_key?(:args) + end + + def h(s : String) + HTML.escape(s || "") + end + + ECR.def_to_s("./src/guff/views/ecrs/api-docs.ecr") +end diff --git a/src/guff/views/html/page.cr b/src/guff/views/html/page.cr new file mode 100644 index 0000000..c300070 --- /dev/null +++ b/src/guff/views/html/page.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: "", + style: "", + 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/ecrs/page.ecr") + end +end -- cgit v1.2.3