Skip to content

Commit

Permalink
remove Rails dependency
Browse files Browse the repository at this point in the history
update documentation with instructions for Sinatra
include alternate content_tag and image_tag to remove ActionView dependency
  • Loading branch information
audionerd committed Nov 14, 2009
1 parent 45fc4ed commit b8992f4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
23 changes: 22 additions & 1 deletion README.markdown
Expand Up @@ -139,7 +139,28 @@ You can optionally define a global configuration in `config/jitterbug.yml`. The

Jitterbug uses [Imagemagick](http://www.imagemagick.org/script/index.php) to build the header images. It needs to be installed on your development and production machines, as do any fonts that you're using. The default location for fonts is `/lib/fonts` in your project.

We've only used Jitterbug in Rails projects thus far, so currently there are a few Rails dependencies: `RAILS_ROOT`, `content_tag` and `image_tag`. If anyone adapts Jitterbug for use in [Sinatra](http://github.com/sinatra/sinatra/), [Merb](http://github.com/merb/merb) or elsewhere, please send us your patches.
## Using Jitterbug with Sinatra

Jitterbug doesn't require Rails, provided you override the default root path and environment variables:

require 'sinatra'

require 'jitterbug'
Jitterbug::root = File.dirname(__FILE__)
Jitterbug::environment = Sinatra::Application::environment.to_s

helpers do
include Jitterbug
end

`Jitterbug::root` is the root path, which should include folders like `public/`, `lib/` and `config/`.
`Jitterbug::environment` is the a string representing the current environment (e.g.: `"development"` or `"production"`).

These default to `RAILS_ROOT` and `RAILS_ENV`, which require Rails.

Jitterbug will provide its own `content_tag` and `image_tag` if ActionView isn't included.

I've only tested this with Sinatra so far. Patches for Merb and other non-Rails systems would be very welcome.

## Compatibility and Font Types

Expand Down
37 changes: 32 additions & 5 deletions lib/jitterbug.rb
@@ -1,6 +1,6 @@
require 'jitterbug/config'
require 'jitterbug/css'
require 'jitterbug/fonts'
require File.expand_path(File.join(File.dirname(__FILE__), 'jitterbug', 'config'))
require File.expand_path(File.join(File.dirname(__FILE__), 'jitterbug', 'css'))
require File.expand_path(File.join(File.dirname(__FILE__), 'jitterbug', 'fonts'))
require 'md5'

module Jitterbug
Expand All @@ -11,17 +11,19 @@ def self.included(base)

def jitterbug(label = '<jitterbug>', options = {})
options = Jitterbug::Config.settings.merge(options)
root = Jitterbug::root
hash = MD5.new("#{label}#{options.sort {|a, b| a[0].to_s <=> b[0].to_s}.to_s}")
path = "#{RAILS_ROOT}/public/#{options[:img_path]}/#{hash}.#{options[:format]}".gsub('//', '/')
path = "#{root}/public/#{options[:img_path]}/#{hash}.#{options[:format]}".gsub('//', '/')
unless File.exist?(path)
caption = options[:width] ? "-size #{options[:width]}x caption:'#{label}'" : "label:'#{label}'"
`mkdir -p #{"#{RAILS_ROOT}/public/#{options[:img_path]}".gsub('//', '/')}`
`mkdir -p #{"#{root}/public/#{options[:img_path]}".gsub('//', '/')}`
`convert -background #{options[:background]} -fill "#{options[:color]}" \
-font #{Jitterbug::Fonts.find(options[:font_dir], options[:font])} \
-pointsize #{options[:size]} -blur 0x.3 #{caption} #{path}`
end
img_src = "/#{options[:img_path]}/#{hash}.#{options[:format]}".gsub('//', '/')
img_class = (['jitterbug'] << options[:class]).compact.join(' ')

if options[:tag]
content_tag(options[:tag], label, :class => img_class, :style => Jitterbug::Css.tag(img_src, options))
elsif options[:fat]
Expand All @@ -31,4 +33,29 @@ def jitterbug(label = '<jitterbug>', options = {})
end
end

# add content_tag and image_tag if ActionView isn't included
unless (respond_to?('content_tag'))
def content_tag(tag, label, options)
"<#{tag} class=\"#{options[:class]}\" style=\"#{options[:style]}\">#{label}</#{tag}>"
end
end
unless (respond_to?('image_tag'))
def image_tag(img_src, options)
"<img src=\"#{img_src}\" alt=\"#{options[:alt]}\" title=\"#{options[:alt]}\" class=\"#{options[:class]}\"></img>"
end
end

# accessor methods, with defaults for Rails
def self.root
@@root ||= RAILS_ROOT
end
def self.root=(value)
@@root = value
end
def self.environment
@@environment ||= RAILS_ENV
end
def self.environment=(value)
@@environment = value
end
end
10 changes: 5 additions & 5 deletions lib/jitterbug/config.rb
@@ -1,6 +1,6 @@
module Jitterbug
module Config

@@settings = { :background => 'transparent',
:color => 'black',
:font => '*',
Expand All @@ -9,16 +9,16 @@ module Config
:img_path => '/content/jitterbug/',
:size => 16 }

def self.read
config = "#{RAILS_ROOT}/config/jitterbug.yml"
def self.read
config = "#{Jitterbug::root}/config/jitterbug.yml"
if File.exist?(config)
YAML.load_file(config)[RAILS_ENV].each {|key, value| @@settings[key.to_sym] = value}
YAML.load_file(config)[Jitterbug::environment].each {|key, value| @@settings[key.to_sym] = value}
end
end

def self.settings
@@settings
end

end
end
2 changes: 1 addition & 1 deletion lib/jitterbug/fonts.rb
Expand Up @@ -2,7 +2,7 @@ module Jitterbug
module Fonts

def self.find(_path, _font)
path = "#{RAILS_ROOT}/#{_path}/*#{_font}*".gsub('//', '/')
path = "#{Jitterbug::root}/#{_path}/*#{_font}*".gsub('//', '/')
font = Dir.glob(path)
case font.size
when 0: raise "*** Jitterbug Error: Font '#{_font}' could not be found in #{_path}"
Expand Down

0 comments on commit b8992f4

Please sign in to comment.