Permalink
Browse files

Initial import

  • Loading branch information...
0 parents commit f24aaa1442cb8794c64574bbbceb88c5e3edbf72 _why committed Jan 6, 2006
Showing with 279 additions and 0 deletions.
  1. +37 −0 README
  2. +6 −0 init.rb
  3. +7 −0 lib/markaby.rb
  4. +82 −0 lib/markaby/builder.rb
  5. +29 −0 lib/markaby/cssproxy.rb
  6. +16 −0 lib/markaby/metaid.rb
  7. +30 −0 lib/markaby/tags.rb
  8. +12 −0 lib/markaby/template.rb
  9. +10 −0 lib/markaby/view.rb
  10. +50 −0 test/example.rb
37 README
@@ -0,0 +1,37 @@
+
+ |- Markaby (Markup as Ruby) -|
+
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+Write Rails templates in pure Ruby. Example layout:
+
+ html do
+ head do
+ title 'Products: ' + action_name
+ stylesheet_link_tag 'scaffold'
+ end
+
+ body do
+ p flash[:notice], :style => "color: green"
+
+ self << content_for_layout
+ end
+ end
+
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+Output defaults to XHTML 1.0 Transitional. To do XHTML 1.0 Strict,
+try this:
+
+ xhtml_strict do
+ # innerds
+ end
+
+Classes may be added by hooking methods onto container elements:
+
+ div.entry do
+ h2.entryTitle 'Son of WebPage'
+ div.entrySection %{by Anthony}
+ div.entryContent 'Okay, once again, the idea here is ...'
+ end
+
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+Markaby by Tim Fletcher and why the lucky stiff
6 init.rb
@@ -0,0 +1,6 @@
+$:.unshift File.expand_path(File.join(File.dirname(__FILE__), 'lib'))
+
+require 'markaby'
+require 'markaby/view'
+
+ActionView::Base::register_template_handler :mab, Markaby::View
7 lib/markaby.rb
@@ -0,0 +1,7 @@
+$:.unshift File.expand_path(File.dirname(__FILE__))
+
+require 'markaby/builder'
+require 'markaby/cssproxy'
+require 'markaby/metaid'
+require 'markaby/tags'
+require 'markaby/template'
82 lib/markaby/builder.rb
@@ -0,0 +1,82 @@
+module Markaby
+ class Builder
+
+ def initialize(assigns, helpers, &block)
+ @builder, @helpers = ::Builder::XmlMarkup.new(:indent => 2), helpers
+ for iv in helpers.instance_variables
+ instance_variable_set(iv, helpers.instance_variable_get(iv))
+ end
+ for iv, val in assigns
+ instance_variable_set("@#{iv}", val)
+ end
+
+ if block
+ instance_eval &block
+ end
+ end
+
+ def to_s
+ @builder.target!
+ end
+
+ def text(string)
+ @builder << string.to_s
+ nil
+ end
+ alias_method :<<, :text
+
+ def tag!(tag, *args, &block)
+ @builder.method_missing(tag, *args, &block)
+ end
+
+ def method_missing(tag, *args, &block)
+ if (TAGS + BIG_TAGS).include?(tag)
+ if args.empty? and block.nil?
+ return CssProxy.new do |args, block|
+ tag!(tag, *args, &block)
+ end
+ end
+ end
+
+ if TAGS.include?(tag)
+ tag!(tag, *args, &block)
+ elsif BIG_TAGS.include?(tag)
+ tag!(tag, *args, &block)
+ elsif SELF_CLOSING_TAGS.include?(tag)
+ tag!(tag, *args)
+ elsif instance_variable_get("@#{tag}")
+ instance_variable_get("@#{tag}")
+ elsif @helpers.respond_to?(tag)
+ text @helpers.send(tag, *args, &block)
+ else
+ tag!(tag, *args, &block)
+ end
+ end
+
+ def p(*args, &block)
+ method_missing(:p, *args, &block)
+ end
+
+ def head
+ tag!(:head) do
+ tag!(:meta, 'http-equiv' => 'Content-Type', 'content' => 'text/html; charset=utf-8')
+ yield
+ end
+ end
+
+ def html(*args, &block)
+ if args.empty?
+ args = ["-//W3C//DTD XHTML 1.0 Transitional//EN", "DTD/xhtml1-transitional.dtd"]
+ end
+ @builder.instruct!
+ @builder.declare!(:DOCTYPE, :html, :PUBLIC, *args)
+ tag!(:html, :xmlns => "http://www.w3.org/1999/xhtml",
+ "xml:lang" => "en", :lang => "en", &block)
+ end
+ alias_method :xhtml_transitional, :html
+
+ def xhtml_strict(&block)
+ html("-//W3C//DTD XHTML 1.0 Transitional//EN", "DTD/xhtml1-transitional.dtd", &block)
+ end
+ end
+end
29 lib/markaby/cssproxy.rb
@@ -0,0 +1,29 @@
+module Markaby
+ class CssProxy
+
+ def initialize(opts = {}, &blk)
+ @opts = opts
+ @blk = blk
+ end
+
+ def method_missing(id_or_class, *args, &blk)
+ idc = id_or_class.to_s
+ case idc
+ when /!$/
+ @opts[:id] = $`
+ else
+ @opts[:class] ||= ""
+ @opts[:class] += "#{idc} "
+ end
+ if args.empty? and blk.nil?
+ self
+ else
+ if args.last.respond_to? :to_hash
+ @opts.merge!(args.pop.to_hash)
+ end
+ args.push @opts
+ @blk.call(args, blk)
+ end
+ end
+ end
+end
16 lib/markaby/metaid.rb
@@ -0,0 +1,16 @@
+# metaprogramming assistant -- metaid.rb
+class Object
+ # The hidden singleton lurks behind everyone
+ def metaclass; class << self; self; end; end
+ def meta_eval &blk; metaclass.instance_eval &blk; end
+
+ # Adds methods to a metaclass
+ def meta_def name, &blk
+ meta_eval { define_method name, &blk }
+ end
+
+ # Defines an instance method within a class
+ def class_def name, &blk
+ class_eval { define_method name, &blk }
+ end
+end
30 lib/markaby/tags.rb
@@ -0,0 +1,30 @@
+module Markaby
+
+ TAGS = [
+ :a, :abbr, :acronym, :span, :b, :caption, :del, :cite, :code, :col,
+ :colgroup, :dd, :dfn, :dt, :em, :fieldset, :i, :img, :ins, :kbd, :p,
+ :label, :legend, :li, :optgroup, :option, :select, :small, :span, :strong,
+ :sub, :sup, :tbody, :td, :textarea, :thead, :title, :th, :tr, :tfoot, :tt
+ ]
+
+ BIG_TAGS = [
+ :address, :blockquote, :body, :div, :dl, :form, :h1, :h2, :h3, :head,
+ :noscript, :object, :ol, :pre, :q, :samp, :script, :style, :table, :ul
+ ]
+
+ SELF_CLOSING_TAGS = [
+ :hr, :br, :link, :meta, :input
+ ]
+
+ # tag categories
+ # --------------
+ #
+ # inline
+ # self_closing
+ #
+ # block
+ # cdata
+ # large (seperated by extra space)
+ #
+
+end
12 lib/markaby/template.rb
@@ -0,0 +1,12 @@
+module Markaby
+ class Template
+ def initialize(template)
+ @template = template
+ end
+ def render(*args)
+ output = Builder.new(*args)
+ output.instance_eval @template
+ return output.to_s
+ end
+ end
+end
10 lib/markaby/view.rb
@@ -0,0 +1,10 @@
+module Markaby
+ class View
+ def initialize(action_view)
+ @action_view = action_view
+ end
+ def render(template, local_assigns = {})
+ Template.new(template).render(@action_view.assigns, @action_view)
+ end
+ end
+end
50 test/example.rb
@@ -0,0 +1,50 @@
+$:.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
+
+require 'builder'
+require 'markaby'
+
+
+module Helpers
+ def self.link_to(person)
+ %{<a href="/person/#{person.id}">#{capitalize(person.name)}</a>}
+ end
+ def self.capitalize(text)
+ text.sub(/([a-z])/) { $1.upcase }
+ end
+ def self.p(text)
+ "<p>#{text}</p>"
+ end
+end
+
+class Person < Struct.new(:id, :name)
+ def self.all
+ @@people ||= []
+ end
+ def self.<<(name)
+ all << new(name)
+ end
+ def initialize(name)
+ super(self.class.all.size + 1, name)
+ end
+end
+
+Person << 'tim'
+Person << 'sophie'
+
+template = <<-EOT
+
+ link :rel => 'stylesheet'
+
+ ul do
+ people.each do |person|
+ li link_to(person)
+ end
+ end
+
+ hr
+
+ div self.p("foo")
+
+EOT
+
+puts Markaby::Template.new(template).render({'people' => Person.all}, Helpers)

0 comments on commit f24aaa1

Please sign in to comment.