summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Lasseter <Nathan Lasseter nathan@bytemark.co.uk>2015-01-21 18:57:31 +0000
committerNathan Lasseter <Nathan Lasseter nathan@bytemark.co.uk>2015-01-21 18:57:31 +0000
commite1d758ddf72d7d8cd29bb0170abcf1397bd67f71 (patch)
treea0192b4dca3ebfdfb343fac5f4e969a5ffec7c2b
parentfd7ac7c1e6d143d74254c478d6a734da54e83b24 (diff)
Added support for scoped tags
-rw-r--r--.gitignore1
-rw-r--r--example.html.rb27
-rw-r--r--lib/tag.rb197
3 files changed, 198 insertions, 27 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..1377554
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+*.swp
diff --git a/example.html.rb b/example.html.rb
index ec1cd29..553ef0e 100644
--- a/example.html.rb
+++ b/example.html.rb
@@ -1,26 +1,33 @@
require './lib/virgil'
-include Virgil
+include Virgil::Tags
-puts Tag.doctype {
+puts doctype {
"html"
}
-puts Tag.html {
- Tag.head {
- Tag.title {
+puts comment {
+ "this is neat"
+}
+
+puts html {
+ head {
+ title {
"My Virgil site"
}
} +
- Tag.body {
- Tag.h1 {
+ body {
+ h1 {
"My Virgil site"
} +
- Tag.p({:class => "italic"}) {
+ p({:class => "italic"}) {
"some content"
} +
- Tag.img({:src => "img.jpg"}) +
- Tag.a({"href" => "/"}) {
+ img({:src => "img.jpg"}) +
+ a({"href" => "/"}) {
"A link home"
+ } +
+ map {
+ area {}
}
}
}
diff --git a/lib/tag.rb b/lib/tag.rb
index bad0d01..b401acb 100644
--- a/lib/tag.rb
+++ b/lib/tag.rb
@@ -1,27 +1,190 @@
module Virgil
- class Tag
- class << self
- def doctype
- "<!DOCTYPE " + yield + ">"
+ module Tag
+ class Doctype
+ def initialize
+ @tag = "<!DOCTYPE " + yield + ">"
end
- def common(attrs = {})
- "<#{__callee__}" + Utils.unfold(attrs) + ">" +
+ def to_s
+ @tag
+ end
+ end
+ class Comment
+ def initialize
+ @tag = "<!--" + yield + "-->"
+ end
+ def to_s
+ @tag
+ end
+ end
+ class Common
+ def initialize(tag, attrs)
+ @tag = "<#{tag}" + Utils.unfold(attrs) + ">" +
yield +
- "</#{__callee__}>"
+ "</#{tag}>"
end
- def common_single(attrs = {})
- "<#{__callee__}" + Utils.unfold(attrs) + " />"
+ def to_s
+ @tag
end
+ end
+ class Common_Single
+ def initialize(tag, attrs)
+ @tag = "<#{tag}" + Utils.unfold(attrs) + "/>"
+ end
+ def to_s
+ @tag
+ end
+ end
- alias :html :common
- alias :head :common
- alias :title :common
- alias :body :common
- alias :h1 :common
- alias :p :common
- alias :img :common_single
- alias :a :common
+ class Map < Common
+ def initialize(attrs, &block)
+ @tag = "<map" + Utils.unfold(attrs) + ">" +
+ self.instance_eval(&block) +
+ "</map>"
+ end
+ def area
+ "<area>" +
+ "</area>"
+ end
+ end
+ end
+ module Tags
+ def doctype
+ Tag::Doctype.new(&Proc.new).to_s
+ end
+ def comment
+ Tag::Comment.new(&Proc.new).to_s
+ end
+ def common(attrs = {})
+ Tag::Common.new(__callee__, attrs, &Proc.new).to_s
end
+ def common_single(attrs = {})
+ Tag::Common_Single.new(__callee__, attrs).to_s
+ end
+
+ def map(attrs = {})
+ Tag::Map.new(attrs, &Proc.new).to_s
+ end
+
+ alias :a :common
+ alias :abbr :common
+ alias :acronym :common
+ alias :address :common
+ alias :applet :common
+ alias :article :common
+ alias :aside :common
+ alias :audio :common
+ alias :b :common
+ alias :base :common
+ alias :basefont :common
+ alias :bdi :common
+ alias :bdo :common
+ alias :big :common
+ alias :blockquote :common
+ alias :body :common
+ alias :br :common
+ alias :button :common
+ alias :canvas :common
+ alias :caption :common
+ alias :center :common
+ alias :cite :common
+ alias :code :common
+ alias :col :common
+ alias :colgroup :common
+ alias :datalist :common
+ alias :dd :common
+ alias :del :common
+ alias :details :common
+ alias :dfn :common
+ alias :dialog :common
+ alias :dir :common
+ alias :div :common
+ alias :dl :common
+ alias :dt :common
+ alias :em :common
+ alias :embed :common
+ alias :fieldset :common
+ alias :figcaption :common
+ alias :figure :common
+ alias :font :common
+ alias :footer :common
+ alias :form :common
+ alias :frame :common
+ alias :frameset :common
+ alias :h1 :common
+ alias :h2 :common
+ alias :h3 :common
+ alias :h4 :common
+ alias :h5 :common
+ alias :h6 :common
+ alias :head :common
+ alias :header :common
+ alias :hgroup :common
+ alias :hr :common
+ alias :html :common
+ alias :i :common
+ alias :iframe :common
+ alias :img :common_single
+ alias :input :common
+ alias :ins :common
+ alias :kbd :common
+ alias :keygen :common
+ alias :label :common
+ alias :legend :common
+ alias :li :common
+ alias :link :common
+ alias :main :common
+ alias :mark :common
+ alias :menu :common
+ alias :menuitem :common
+ alias :meta :common
+ alias :meter :common
+ alias :nav :common
+ alias :noframes :common
+ alias :noscript :common
+ alias :object :common
+ alias :ol :common
+ alias :optgroup :common
+ alias :option :common
+ alias :output :common
+ alias :p :common
+ alias :param :common
+ alias :pre :common
+ alias :progress :common
+ alias :q :common
+ alias :rp :common
+ alias :rt :common
+ alias :ruby :common
+ alias :s :common
+ alias :samp :common
+ alias :script :common
+ alias :section :common
+ alias :select :common
+ alias :small :common
+ alias :source :common
+ alias :span :common
+ alias :strike :common
+ alias :strong :common
+ alias :style :common
+ alias :sub :common
+ alias :summary :common
+ alias :sup :common
+ alias :table :common
+ alias :tbody :common
+ alias :td :common
+ alias :textarea :common
+ alias :tfoot :common
+ alias :th :common
+ alias :thead :common
+ alias :time :common
+ alias :title :common
+ alias :tr :common
+ alias :track :common
+ alias :tt :common
+ alias :u :common
+ alias :ul :common
+ alias :var :common
+ alias :video :common
+ alias :wbr :common
end
end