From 4e0218895efd84adeb613a9b068bc52989ad518e Mon Sep 17 00:00:00 2001 From: Nathan Lasseter Date: Fri, 23 Jan 2015 17:17:30 +0000 Subject: Really working in the tree structure for tags. aiming to remove all of the aliases in favour of the new style. --- example.html.rb | 1 + lib/tag.rb | 59 +++++++++++++++++++++++++++------------------------------ lib/tags.rb | 16 +++++----------- lib/utils.rb | 30 +++++++++++++++++++++++++++-- 4 files changed, 62 insertions(+), 44 deletions(-) diff --git a/example.html.rb b/example.html.rb index 553ef0e..159f996 100644 --- a/example.html.rb +++ b/example.html.rb @@ -26,6 +26,7 @@ puts html { a({"href" => "/"}) { "A link home" } + + input({"webkit-speech-recognition" => nil}) {} + map { area {} } diff --git a/lib/tag.rb b/lib/tag.rb index 0144bb0..b0d5a84 100644 --- a/lib/tag.rb +++ b/lib/tag.rb @@ -21,42 +21,39 @@ module Virgil end end - class Common - def initialize(tag, attrs, &block) - @tag = "<#{tag}" + - Utils.unfold(attrs) + - ">" + - self.instance_eval(&block) + - "" - end - def to_s - @tag + class Html < Utils::Generic + def head(attrs = {}) + Head.new(attrs, &Proc.new).to_s end - def subtag(attrs = {}) - Common.new(__callee__, attrs, &Proc.new).to_s - end - def subtagsingle(attrs = {}) - Common_Single.new(__callee__, attrs).to_s - end - end - class Common_Single - def initialize(tag, attrs) - @tag = "<#{tag}" + - Utils.unfold(attrs) + - ">" + def body(attrs = {}) + Body.new(attrs, &Proc.new).to_s end - def to_s - @tag + + class Body < Html + def map(attrs = {}) + Map.new(attrs, &Proc.new).to_s + end + + class Map < Utils::Generic + def area(attrs = {}) + Area.new(attrs).to_s + end + + class Area < Utils::GenericSingle ; end + end end - end - class Map < Common - alias :area :subtagsingle - end + class Head < Html + def link(attrs = {}) + Link.new(attrs).to_s + end + class Link < Utils::GenericSingle ; end - class Head < Common - alias :link :subtagsingle - alias :title :subtag + def title(attrs = {}) + Title.new(attrs, &Proc.new).to_s + end + class Title < Utils::Generic ; end + end end end end diff --git a/lib/tags.rb b/lib/tags.rb index e148a77..1746451 100644 --- a/lib/tags.rb +++ b/lib/tags.rb @@ -7,25 +7,20 @@ module Virgil Tag::Comment.new(&Proc.new).to_s end def common(attrs = {}) - Tag::Common.new(__callee__, attrs, &Proc.new).to_s + Utils::Generic.new(attrs, __callee__, &Proc.new).to_s end def common_single(attrs = {}) - Tag::Common_Single.new(__callee__, attrs).to_s + Utils::GenericSingle.new(attrs, __callee__).to_s end - def map(attrs = {}) - Tag::Map.new("map", attrs, &Proc.new).to_s - end - - def head(attrs = {}) - Tag::Head.new("head", attrs, &Proc.new).to_s + def html(attrs = {}) + Tag::Html.new(attrs, &Proc.new).to_s end alias :a :common alias :b :common alias :br :common_single alias :blockquote :common - alias :body :common alias :center :common alias :code :common alias :h1 :common @@ -35,9 +30,9 @@ module Virgil alias :h5 :common alias :h6 :common alias :hr :common_single - alias :html :common alias :i :common alias :img :common_single + alias :input :common_single alias :li :common alias :ol :common alias :p :common @@ -89,7 +84,6 @@ module Virgil # alias :header :common # alias :hgroup :common # alias :iframe :common -# alias :input :common # alias :ins :common # alias :kbd :common # alias :keygen :common diff --git a/lib/utils.rb b/lib/utils.rb index e3b96a9..1df8305 100644 --- a/lib/utils.rb +++ b/lib/utils.rb @@ -1,11 +1,37 @@ module Virgil - class Utils + module Utils def self.unfold(attrs) ret = "" attrs.each do |key, value| - ret += " #{key.to_s}=\"#{value}\"" + if value.nil? then + ret += " #{key.to_s}" + else + ret += " #{key.to_s}=\"#{value}\"" + end end ret end + + class Generic + def initialize(attrs, tag = nil, &block) + tag = self.class.name.split('::').last.downcase if tag.nil? + @tag = "<#{tag}" + + Utils::unfold(attrs) + + ">" + + self.instance_eval(&block) + + "" + end + def to_s + @tag + end + end + class GenericSingle < Generic + def initialize(attrs, tag = nil) + tag = self.class.name.split('::').last.downcase if tag.nil? + @tag = "<#{tag}" + + Utils::unfold(attrs) + + ">" + end + end end end -- cgit v1.2.1