Skip to content
Browse files

Rails3 support - specifically support for html_safe output

  • Loading branch information...
1 parent f98f0c7 commit 8dab12fd1e3dcd8dcf955b8a2bfa032626960d95 @ahoward committed Jan 31, 2011
Showing with 195 additions and 45 deletions.
  1. +150 −19 Rakefile
  2. +28 −19 lib/tagz.rb
  3. +8 −0 lib/tagz/rails.rb
  4. +7 −7 tagz.gemspec
  5. +2 −0 test/{tagz.rb → tagz_test.rb}
View
169 Rakefile
@@ -1,19 +1,67 @@
-
This.rubyforge_project = 'codeforpeople'
This.author = "Ara T. Howard"
This.email = "ara.t.howard@gmail.com"
-This.homepage = "http://github.com/ahoward/#{ This.lib }/tree/master"
+This.homepage = "http://github.com/ahoward/#{ This.lib }"
task :default do
- puts(Rake::Task.tasks.map{|task| task.name} - ['default'])
+ puts((Rake::Task.tasks.map{|task| task.name.gsub(/::/,':')} - ['default']).sort)
+end
+
+task :test do
+ run_tests!
+end
+
+namespace :test do
+ task(:unit){ run_tests!(:unit) }
+ task(:functional){ run_tests!(:functional) }
+ task(:integration){ run_tests!(:integration) }
+end
+
+def run_tests!(which = nil)
+ which ||= '**'
+ test_dir = File.join(This.dir, "test")
+ test_glob ||= File.join(test_dir, "#{ which }/**_test.rb")
+ test_rbs = Dir.glob(test_glob).sort
+
+ div = ('=' * 119)
+ line = ('-' * 119)
+ helper = "-r ./test/helper.rb" if test(?e, "./test/helper.rb")
+
+ test_rbs.each_with_index do |test_rb, index|
+ testno = index + 1
+ command = "#{ This.ruby } -I ./lib -I ./test/lib #{ helper } #{ test_rb }"
+
+ puts
+ say(div, :color => :cyan, :bold => true)
+ say("@#{ testno } => ", :bold => true, :method => :print)
+ say(command, :color => :cyan, :bold => true)
+ say(line, :color => :cyan, :bold => true)
+
+ system(command)
+
+ say(line, :color => :cyan, :bold => true)
+
+ status = $?.exitstatus
+
+ if status.zero?
+ say("@#{ testno } <= ", :bold => true, :color => :white, :method => :print)
+ say("SUCCESS", :color => :green, :bold => true)
+ else
+ say("@#{ testno } <= ", :bold => true, :color => :white, :method => :print)
+ say("FAILURE", :color => :red, :bold => true)
+ end
+ say(line, :color => :cyan, :bold => true)
+
+ exit(status) unless status.zero?
+ end
end
task :gemspec do
ignore_extensions = 'git', 'svn', 'tmp', /sw./, 'bak', 'gem'
- ignore_directories = 'pkg'
- ignore_files = 'test/log'
+ ignore_directories = %w[ pkg ]
+ ignore_files = %w[ test/log ]
shiteless =
lambda do |list|
@@ -44,8 +92,9 @@ task :gemspec do
summary = object.respond_to?(:summary) ? object.summary : "summary: #{ lib } kicks the ass"
description = object.respond_to?(:description) ? object.description : "description: #{ lib } kicks the ass"
- extensions = This.extensions
- if extensions.nil?
+ if This.extensions.nil?
+ This.extensions = []
+ extensions = This.extensions
%w( Makefile configure extconf.rb ).each do |ext|
extensions << ext if File.exists?(ext)
end
@@ -75,8 +124,8 @@ task :gemspec do
spec.has_rdoc = #{ has_rdoc.inspect }
spec.test_files = #{ test_files.inspect }
- #spec.add_dependency 'lib', '>= version'
- #spec.add_dependency 'fattr'
+
+ # spec.add_dependency 'lib', '>= version'
spec.extensions.push(*#{ extensions.inspect })
@@ -89,12 +138,13 @@ task :gemspec do
}
end
- open("#{ lib }.gemspec", "w"){|fd| fd.puts template}
- This.gemspec = "#{ lib }.gemspec"
+ Fu.mkdir_p(This.pkgdir)
+ This.gemspec = File.join(This.dir, "#{ This.lib }.gemspec") #File.join(This.pkgdir, "gemspec.rb")
+ open("#{ This.gemspec }", "w"){|fd| fd.puts(template)}
end
task :gem => [:clean, :gemspec] do
- Fu.mkdir_p This.pkgdir
+ Fu.mkdir_p(This.pkgdir)
before = Dir['*.gem']
cmd = "gem build #{ This.gemspec }"
`#{ cmd }`
@@ -160,33 +210,47 @@ task :release => [:clean, :gemspec, :gem] do
cmd = "rubyforge login && rubyforge add_release #{ This.rubyforge_project } #{ This.lib } #{ This.version } #{ This.pkgdir }/#{ This.gem }"
puts cmd
system cmd
+ cmd = "gem push #{ This.pkgdir }/#{ This.gem }"
+ puts cmd
+ system cmd
end
BEGIN {
+# support for this rakefile
+#
$VERBOSE = nil
require 'ostruct'
require 'erb'
require 'fileutils'
+ require 'rbconfig'
+# fu shortcut
+#
Fu = FileUtils
+# cache a bunch of stuff about this rakefile/environment
+#
This = OpenStruct.new
This.file = File.expand_path(__FILE__)
This.dir = File.dirname(This.file)
This.pkgdir = File.join(This.dir, 'pkg')
+# grok lib
+#
lib = ENV['LIB']
unless lib
- lib = File.basename(Dir.pwd)
+ lib = File.basename(Dir.pwd).sub(/[-].*$/, '')
end
This.lib = lib
+# grok version
+#
version = ENV['VERSION']
unless version
require "./lib/#{ This.lib }"
@@ -196,9 +260,22 @@ BEGIN {
end
This.version = version
+# we need to know the name of the lib an it's version
+#
abort('no lib') unless This.lib
abort('no version') unless This.version
+# discover full path to this ruby executable
+#
+ c = Config::CONFIG
+ bindir = c["bindir"] || c['BINDIR']
+ ruby_install_name = c['ruby_install_name'] || c['RUBY_INSTALL_NAME'] || 'ruby'
+ ruby_ext = c['EXEEXT'] || ''
+ ruby = File.join(bindir, (ruby_install_name + ruby_ext))
+ This.ruby = ruby
+
+# some utils
+#
module Util
def indent(s, n = 2)
s = unindent(s)
@@ -208,18 +285,20 @@ BEGIN {
def unindent(s)
indent = nil
- s.each do |line|
- next if line =~ %r/^\s*$/
- indent = line[%r/^\s*/] and break
+ s.each_line do |line|
+ next if line =~ %r/^\s*$/
+ indent = line[%r/^\s*/] and break
+ end
+ indent ? s.gsub(%r/^#{ indent }/, "") : s
end
- indent ? s.gsub(%r/^#{ indent }/, "") : s
- end
extend self
end
+# template support
+#
class Template
def initialize(&block)
- @block = block
+ @block = block.binding
@template = block.call.to_s
end
def expand(b=nil)
@@ -229,5 +308,57 @@ BEGIN {
end
def Template(*args, &block) Template.new(*args, &block) end
+# colored console output support
+#
+ This.ansi = {
+ :clear => "\e[0m",
+ :reset => "\e[0m",
+ :erase_line => "\e[K",
+ :erase_char => "\e[P",
+ :bold => "\e[1m",
+ :dark => "\e[2m",
+ :underline => "\e[4m",
+ :underscore => "\e[4m",
+ :blink => "\e[5m",
+ :reverse => "\e[7m",
+ :concealed => "\e[8m",
+ :black => "\e[30m",
+ :red => "\e[31m",
+ :green => "\e[32m",
+ :yellow => "\e[33m",
+ :blue => "\e[34m",
+ :magenta => "\e[35m",
+ :cyan => "\e[36m",
+ :white => "\e[37m",
+ :on_black => "\e[40m",
+ :on_red => "\e[41m",
+ :on_green => "\e[42m",
+ :on_yellow => "\e[43m",
+ :on_blue => "\e[44m",
+ :on_magenta => "\e[45m",
+ :on_cyan => "\e[46m",
+ :on_white => "\e[47m"
+ }
+ def say(phrase, *args)
+ options = args.last.is_a?(Hash) ? args.pop : {}
+ options[:color] = args.shift.to_s.to_sym unless args.empty?
+ keys = options.keys
+ keys.each{|key| options[key.to_s.to_sym] = options.delete(key)}
+
+ color = options[:color]
+ bold = options.has_key?(:bold)
+
+ parts = [phrase]
+ parts.unshift(This.ansi[color]) if color
+ parts.unshift(This.ansi[:bold]) if bold
+ parts.push(This.ansi[:clear]) if parts.size > 1
+
+ method = options[:method] || :puts
+
+ Kernel.send(method, parts.join)
+ end
+
+# always run out of the project dir
+#
Dir.chdir(This.dir)
}
View
47 lib/tagz.rb
@@ -4,11 +4,12 @@
#
module Tagz
def Tagz.version()
- '7.2.2'
+ '8.1.0'
end
- def Tagz.description()
- <<-__
+ def Tagz.description
+ <<-____
+
tagz.rb is generates html, xml, or any sgml variant like a small ninja
running across the backs of a herd of giraffes swatting of heads like
a mark-up weedwacker. weighing in at less than 300 lines of code
@@ -18,13 +19,14 @@ def Tagz.description()
that generate html to be able to do so easily in any context without
heavyweight syntax or scoping issues, like a ninja sword through
butter.
- __
+
+ ____
end
private
# access tagz doc and enclose tagz operations
#
- def tagz document = nil, &block
+ def tagz(document = nil, &block)
@tagz ||= nil ## shut wornings up
previous = @tagz
@@ -43,9 +45,10 @@ def tagz document = nil, &block
end
end
+
# open_tag
#
- def tagz__ name, *argv, &block
+ def tagz__(name, *argv, &block)
options = argv.last.is_a?(Hash) ? argv.pop : {}
content = argv
@@ -101,13 +104,13 @@ def tagz__ name, *argv, &block
# close_tag
#
- def __tagz tag, *a, &b
+ def __tagz(tag, *a, &b)
tagz.push "</#{ tag }>"
end
# catch special tagz methods
#
- def method_missing m, *a, &b
+ def method_missing(m, *a, &b)
strategy =
case m.to_s
when %r/^(.*[^_])_(!)?$/o
@@ -135,11 +138,14 @@ def method_missing m, *a, &b
m, bang = $1, $2
b ||= lambda{} if bang
tagz{ tagz__(m, *a, &b) }
+
when :close_tag
m = $1
tagz{ __tagz(m, *a, &b) }
+
when :element
Tagz.element.new(*a, &b)
+
when :puts
tagz do
tagz.push("\n")
@@ -205,7 +211,8 @@ def << string
end
self
end
- def concat string
+
+ def concat(string)
self << string
end
#alias_method 'concat', '<<'
@@ -215,7 +222,7 @@ def escape(*strings)
end
alias_method 'h', 'escape'
- def puts string
+ def puts(string)
write "#{ string }\n"
end
@@ -235,11 +242,19 @@ def to_s
def to_str
self
end
+
+ def html_safe
+ self
+ end
+
+ def html_safe?
+ true
+ end
end
Tagz.singleton_class{ define_method(:document){ Tagz.namespace(:Document) } }
class Element < ::String
- def Element.attributes options
+ def Element.attributes(options)
unless options.empty?
' ' <<
options.map do |key, value|
@@ -261,7 +276,7 @@ def Element.attributes options
attr 'name'
- def initialize name, *argv, &block
+ def initialize(name, *argv, &block)
options = {}
content = []
@@ -447,11 +462,5 @@ def Tagz *argv, &block
(argv.empty? and block.nil?) ? ::Tagz : Tagz.tagz(*argv, &block)
end
- if defined?(Rails)
- _=ActionView,ActionView::Base,ActionController,ActionController::Base
- ActionView::Base.send(:include, Tagz.globally)
- ActionController::Base.send(:include, Tagz)
- end
-
- Tagz.html_mode!
+ Tagz.xml_mode!
end
View
8 lib/tagz/rails.rb
@@ -0,0 +1,8 @@
+if defined?(Rails)
+ #_ = ActionView, ActionView::Base, ActionController, ActionController::Base
+ #ActionView::Base.send(:include, Tagz.globally)
+ #ActionController::Base.send(:include, Tagz)
+
+ unloadable(Tagz)
+ Tagz.xml_mode!
+end
View
14 tagz.gemspec
@@ -3,25 +3,25 @@
Gem::Specification::new do |spec|
spec.name = "tagz"
- spec.version = "7.2.1"
+ spec.version = "8.1.0"
spec.platform = Gem::Platform::RUBY
spec.summary = "tagz"
- spec.description = " tagz.rb is generates html, xml, or any sgml variant like a small ninja\n running across the backs of a herd of giraffes swatting of heads like\n a mark-up weedwacker. weighing in at less than 300 lines of code\n tagz.rb adds an html/xml/sgml syntax to ruby that is both unobtrusive,\n safe, and available globally to objects without the need for any\n builder or superfluous objects. tagz.rb is designed for applications\n that generate html to be able to do so easily in any context without\n heavyweight syntax or scoping issues, like a ninja sword through\n butter.\n"
+ spec.description = "\n tagz.rb is generates html, xml, or any sgml variant like a small ninja\n running across the backs of a herd of giraffes swatting of heads like\n a mark-up weedwacker. weighing in at less than 300 lines of code\n tagz.rb adds an html/xml/sgml syntax to ruby that is both unobtrusive,\n safe, and available globally to objects without the need for any\n builder or superfluous objects. tagz.rb is designed for applications\n that generate html to be able to do so easily in any context without\n heavyweight syntax or scoping issues, like a ninja sword through\n butter.\n\n"
- spec.files = ["lib", "lib/tagz.rb", "Rakefile", "README", "readme.erb", "samples", "samples/a.rb", "samples/b.rb", "samples/c.rb", "samples/d.rb", "samples/e.rb", "samples/f.rb", "samples/g.rb", "tagz.gemspec", "test", "test/tagz.rb"]
+ spec.files = ["lib", "lib/tagz", "lib/tagz/rails.rb", "lib/tagz.rb", "Rakefile", "README", "readme.erb", "samples", "samples/a.rb", "samples/b.rb", "samples/c.rb", "samples/d.rb", "samples/e.rb", "samples/f.rb", "samples/g.rb", "tagz.gemspec", "test", "test/tagz_test.rb"]
spec.executables = []
spec.require_path = "lib"
spec.has_rdoc = true
- spec.test_files = "test/tagz.rb"
- #spec.add_dependency 'lib', '>= version'
- #spec.add_dependency 'fattr'
+ spec.test_files = nil
+
+# spec.add_dependency 'lib', '>= version'
spec.extensions.push(*[])
spec.rubyforge_project = "codeforpeople"
spec.author = "Ara T. Howard"
spec.email = "ara.t.howard@gmail.com"
- spec.homepage = "http://github.com/ahoward/tagz/tree/master"
+ spec.homepage = "http://github.com/ahoward/tagz"
end
View
2 test/tagz.rb → test/tagz_test.rb
@@ -10,6 +10,8 @@
require 'tagz'
+Tagz.html_mode!
+
class TagzTest < Test::Unit::TestCase
include Tagz

0 comments on commit 8dab12f

Please sign in to comment.
Something went wrong with that request. Please try again.