Move off-by-default extensions into middleman-more, make middleman-more itself an extension

This commit is contained in:
Thomas Reynolds 2012-05-24 16:14:17 -07:00
parent 182ad87ad3
commit 1cad726c81
92 changed files with 902 additions and 758 deletions

View file

@ -111,10 +111,6 @@ module Middleman
# @return [Boolean]
set :show_exceptions, true
# Automatically loaded extensions
# @return [Array<Symbol>]
set :default_extensions, [ :lorem ]
# Default layout name
# @return [String, Symbold]
set :layout, :_auto_layout
@ -162,37 +158,6 @@ module Middleman
# i18n
register Middleman::CoreExtensions::I18n
# Built-in Extensions
# Provide Apache-style index.html files for directories
Middleman::Extensions.register(:directory_indexes) do
require "middleman-core/extensions/directory_indexes"
Middleman::Extensions::DirectoryIndexes
end
# Lorem provides a handful of helpful prototyping methods to generate
# words, paragraphs, fake images, names and email addresses.
Middleman::Extensions.register(:lorem) do
require "middleman-core/extensions/lorem"
Middleman::Extensions::Lorem
end
# AutomaticImageSizes inspects the images used in your dynamic templates
# and automatically adds width and height attributes to their HTML
# elements.
Middleman::Extensions.register(:automatic_image_sizes) do
require "middleman-core/extensions/automatic_image_sizes"
Middleman::Extensions::AutomaticImageSizes
end
# AssetHost allows you to setup multiple domains to host your static
# assets. Calls to asset paths in dynamic templates will then rotate
# through each of the asset servers to better spread the load.
Middleman::Extensions.register(:asset_host) do
require "middleman-core/extensions/asset_host"
Middleman::Extensions::AssetHost
end
# Initialize the Middleman project
def initialize(&block)
# Current path defaults to nil, used in views.

View file

@ -40,7 +40,6 @@ module Middleman
# Using for version parsing
require "rubygems"
# app.set :default_extensions, []
app.define_hook :after_configuration
app.define_hook :before_configuration
app.define_hook :build_config
@ -140,11 +139,6 @@ module Middleman
run_hook :after_configuration
# Add in defaults
default_extensions.each do |ext|
activate ext
end
if logging?
self.class.extensions.each do |ext|
puts "== Extension: #{ext}"

View file

@ -1,47 +0,0 @@
# Extensions namespace
module Middleman::Extensions
# Asset Host module
module AssetHost
# Setup extension
class << self
# Once registered
def registered(app)
# Default to no host
app.set :asset_host, false
# Include methods
app.send :include, InstanceMethods
end
alias :included :registered
end
# Asset Host Instance Methods
module InstanceMethods
# Override default asset url helper to include asset hosts
#
# @param [String] path
# @param [String] prefix
# @return [String]
def asset_url(path, prefix="")
original_output = super
return original_output unless asset_host
asset_prefix = if asset_host.is_a?(Proc)
asset_host.call(original_output)
elsif asset_host.is_a?(String)
asset_host
end
File.join(asset_prefix, original_output)
end
end
end
# Register the extension
register :asset_host, AssetHost
end

View file

@ -1,57 +0,0 @@
# Extensions namespace
module Middleman::Extensions
# Automatic Image Sizes extension
module AutomaticImageSizes
# Setup extension
class << self
# Once registered
def registered(app)
# Include 3rd-party fastimage library
require "middleman-core/extensions/automatic_image_sizes/fastimage"
# Include methods
app.send :include, InstanceMethods
end
alias :included :registered
end
# Automatic Image Sizes Instance Methods
module InstanceMethods
# Override default image_tag helper to automatically calculate and include
# image dimensions.
#
# @param [String] path
# @param [Hash] params
# @return [String]
def image_tag(path, params={})
if !params.has_key?(:width) && !params.has_key?(:height) && !path.include?("://")
params[:alt] ||= ""
http_prefix = http_images_path rescue images_dir
begin
real_path = File.join(source, images_dir, path)
full_path = File.expand_path(real_path, root)
http_prefix = http_images_path rescue images_dir
if File.exists? full_path
dimensions = ::FastImage.size(full_path, :raise_on_failure => true)
params[:width] = dimensions[0]
params[:height] = dimensions[1]
end
rescue
# $stderr.puts params.inspect
end
end
super(path, params)
end
end
end
# Register the extension
register :automatic_image_sizes, AutomaticImageSizes
end

View file

@ -1,287 +0,0 @@
# FastImage finds the size or type of an image given its uri.
# It is careful to only fetch and parse as much of the image as is needed to determine the result.
# It does this by using a feature of Net::HTTP that yields strings from the resource being fetched
# as soon as the packets arrive.
#
# No external libraries such as ImageMagick are used here, this is a very lightweight solution to
# finding image information.
#
# FastImage knows about GIF, JPEG, BMP and PNG files.
#
# FastImage can also read files from the local filesystem by supplying the path instead of a uri.
# In this case FastImage uses the open-uri library to read the file in chunks of 256 bytes until
# it has enough. This is possibly a useful bandwidth-saving feature if the file is on a network
# attached disk rather than truly local.
#
# === Examples
# require 'fastimage'
#
# FastImage.size("http://stephensykes.com/images/ss.com_x.gif")
# => [266, 56]
# FastImage.type("http://stephensykes.com/images/pngimage")
# => :png
# FastImage.type("/some/local/file.gif")
# => :gif
#
# === References
# * http://snippets.dzone.com/posts/show/805
# * http://www.anttikupila.com/flash/getting-jpg-dimensions-with-as3-without-loading-the-entire-file/
# * http://pennysmalls.com/2008/08/19/find-jpeg-dimensions-fast-in-ruby/
# * http://imagesize.rubyforge.org/
#
require 'net/https'
require 'open-uri'
class FastImage
attr_reader :size, :type
class FastImageException < StandardError # :nodoc:
end
class MoreCharsNeeded < FastImageException # :nodoc:
end
class UnknownImageType < FastImageException # :nodoc:
end
class ImageFetchFailure < FastImageException # :nodoc:
end
class SizeNotFound < FastImageException # :nodoc:
end
DefaultTimeout = 2
LocalFileChunkSize = 256
# Returns an array containing the width and height of the image.
# It will return nil if the image could not be fetched, or if the image type was not recognised.
#
# By default there is a timeout of 2 seconds for opening and reading from a remote server.
# This can be changed by passing a :timeout => number_of_seconds in the options.
#
# If you wish FastImage to raise if it cannot size the image for any reason, then pass
# :raise_on_failure => true in the options.
#
# FastImage knows about GIF, JPEG, BMP and PNG files.
#
# === Example
#
# require 'fastimage'
#
# FastImage.size("http://stephensykes.com/images/ss.com_x.gif")
# => [266, 56]
# FastImage.size("http://stephensykes.com/images/pngimage")
# => [16, 16]
# FastImage.size("http://farm4.static.flickr.com/3023/3047236863_9dce98b836.jpg")
# => [500, 375]
# FastImage.size("http://www-ece.rice.edu/~wakin/images/lena512.bmp")
# => [512, 512]
# FastImage.size("test/fixtures/test.jpg")
# => [882, 470]
# FastImage.size("http://pennysmalls.com/does_not_exist")
# => nil
# FastImage.size("http://pennysmalls.com/does_not_exist", :raise_on_failure=>true)
# => raises FastImage::ImageFetchFailure
# FastImage.size("http://stephensykes.com/favicon.ico", :raise_on_failure=>true)
# => raises FastImage::UnknownImageType
# FastImage.size("http://stephensykes.com/favicon.ico", :raise_on_failure=>true, :timeout=>0.01)
# => raises FastImage::ImageFetchFailure
# FastImage.size("http://stephensykes.com/images/faulty.jpg", :raise_on_failure=>true)
# => raises FastImage::SizeNotFound
#
# === Supported options
# [:timeout]
# Overrides the default timeout of 2 seconds. Applies both to reading from and opening the http connection.
# [:raise_on_failure]
# If set to true causes an exception to be raised if the image size cannot be found for any reason.
#
def self.size(uri, options={})
new(uri, options).size
end
# Returns an symbol indicating the image type fetched from a uri.
# It will return nil if the image could not be fetched, or if the image type was not recognised.
#
# By default there is a timeout of 2 seconds for opening and reading from a remote server.
# This can be changed by passing a :timeout => number_of_seconds in the options.
#
# If you wish FastImage to raise if it cannot find the type of the image for any reason, then pass
# :raise_on_failure => true in the options.
#
# === Example
#
# require 'fastimage'
#
# FastImage.type("http://stephensykes.com/images/ss.com_x.gif")
# => :gif
# FastImage.type("http://stephensykes.com/images/pngimage")
# => :png
# FastImage.type("http://farm4.static.flickr.com/3023/3047236863_9dce98b836.jpg")
# => :jpeg
# FastImage.type("http://www-ece.rice.edu/~wakin/images/lena512.bmp")
# => :bmp
# FastImage.type("test/fixtures/test.jpg")
# => :jpeg
# FastImage.type("http://pennysmalls.com/does_not_exist")
# => nil
#
# === Supported options
# [:timeout]
# Overrides the default timeout of 2 seconds. Applies both to reading from and opening the http connection.
# [:raise_on_failure]
# If set to true causes an exception to be raised if the image type cannot be found for any reason.
#
def self.type(uri, options={})
new(uri, options.merge(:type_only=>true)).type
end
def initialize(uri, options={})
@property = options[:type_only] ? :type : :size
@timeout = options[:timeout] || DefaultTimeout
@uri = uri
begin
@parsed_uri = URI.parse(uri)
rescue URI::InvalidURIError
fetch_using_open_uri
else
if @parsed_uri.scheme == "http" || @parsed_uri.scheme == "https"
fetch_using_http
else
fetch_using_open_uri
end
end
raise SizeNotFound if options[:raise_on_failure] && @property == :size && !@size
rescue Timeout::Error, SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ECONNRESET,
ImageFetchFailure, Net::HTTPBadResponse, EOFError, Errno::ENOENT
raise ImageFetchFailure if options[:raise_on_failure]
rescue NoMethodError # 1.8.7p248 can raise this due to a net/http bug
raise ImageFetchFailure if options[:raise_on_failure]
rescue UnknownImageType
raise UnknownImageType if options[:raise_on_failure]
end
private
def fetch_using_http
setup_http
@http.request_get(@parsed_uri.request_uri) do |res|
raise ImageFetchFailure unless res.is_a?(Net::HTTPSuccess)
res.read_body do |str|
break if parse_packet(str)
end
end
end
def setup_http
@http = Net::HTTP.new(@parsed_uri.host, @parsed_uri.port)
@http.use_ssl = (@parsed_uri.scheme == "https")
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
@http.open_timeout = @timeout
@http.read_timeout = @timeout
end
def fetch_using_open_uri
open(@uri) do |s|
while str = s.read(LocalFileChunkSize)
break if parse_packet(str)
end
end
end
# returns true once result is achieved
#
def parse_packet(str)
@str = (@unused_str || "") + str
@strpos = 0
begin
result = send("parse_#{@property}")
if result
instance_variable_set("@#{@property}", result)
true
end
rescue MoreCharsNeeded
false
end
end
def parse_size
@type = parse_type unless @type
@strpos = 0
send("parse_size_for_#{@type}")
end
def get_chars(n)
if @strpos + n - 1 >= @str.size
@unused_str = @str[@strpos..-1]
raise MoreCharsNeeded
else
result = @str[@strpos..(@strpos + n - 1)]
@strpos += n
result
end
end
def get_byte
get_chars(1).unpack("C")[0]
end
def read_int(str)
size_bytes = str.unpack("CC")
(size_bytes[0] << 8) + size_bytes[1]
end
def parse_type
case get_chars(2)
when "BM"
:bmp
when "GI"
:gif
when 0xff.chr + 0xd8.chr
:jpeg
when 0x89.chr + "P"
:png
else
raise UnknownImageType
end
end
def parse_size_for_gif
get_chars(11)[6..10].unpack('SS')
end
def parse_size_for_png
get_chars(25)[16..24].unpack('NN')
end
def parse_size_for_jpeg
loop do
@state = case @state
when nil
get_chars(2)
:started
when :started
get_byte == 0xFF ? :sof : :started
when :sof
c = get_byte
if (0xe0..0xef).include?(c)
:skipframe
elsif [0xC0..0xC3, 0xC5..0xC7, 0xC9..0xCB, 0xCD..0xCF].detect {|r| r.include? c}
:readsize
else
:skipframe
end
when :skipframe
@skip_chars = read_int(get_chars(2)) - 2
:do_skip
when :do_skip
get_chars(@skip_chars)
:started
when :readsize
s = get_chars(7)
return [read_int(s[5..6]), read_int(s[3..4])]
end
end
end
def parse_size_for_bmp
d = get_chars(29)[14..28]
d.unpack("C")[0] == 40 ? d[4..-1].unpack('LL') : d[4..8].unpack('SS')
end
end

View file

@ -1,54 +0,0 @@
# Extensions namespace
module Middleman::Extensions
# Directory Indexes extension
module DirectoryIndexes
# Setup extension
class << self
# Once registered
def registered(app)
app.ready do
sitemap.register_resource_list_manipulator(
:directory_indexes,
DirectoryIndexManager.new(self)
)
end
end
alias :included :registered
end
class DirectoryIndexManager
def initialize(app)
@app = app
end
# Update the main sitemap resource list
# @return [void]
def manipulate_resource_list(resources)
index_file = @app.index_file
new_index_path = "/#{index_file}"
resources.each do |resource|
# Check if it would be pointless to reroute
next if resource.path == index_file ||
resource.path.end_with?(new_index_path) ||
File.extname(index_file) != resource.ext
# Check if frontmatter turns directory_index off
d = resource.data
next if d && d["directory_index"] == false
# Check if file metadata (options set by "page" in config.rb) turns directory_index off
if resource.metadata[:options] && resource.metadata[:options][:directory_index] == false
next
end
resource.destination_path = resource.destination_path.chomp(File.extname(index_file)) + new_index_path
end
end
end
end
end

View file

@ -1,195 +0,0 @@
# Extension namespace
module Middleman::Extensions
# Lorem helper
module Lorem
# Setup extension
class << self
# Once registered
def registered(app)
# Include methods
app.send :include, InstanceMethods
end
alias :included :registered
end
# Lorem extension instance methods
module InstanceMethods
# Access to the Lorem object
# @return [Middleman::Extensions::Lorem::LoremObject]
def lorem
@_lorem ||= LoremObject.new
end
# Return a placeholder image using placekitten.com
#
# @param [String] size
# @param [Hash] options
# @return [String]
def placekitten(size, options={})
options[:domain] = "http://placekitten.com"
lorem.image(size, options)
end
end
# Adapted from Frank:
# https://github.com/blahed/frank/
# Copyright (c) 2010 Travis Dunn
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
class LoremObject
# Words for use in lorem text
WORDS = %w(alias consequatur aut perferendis sit voluptatem accusantium doloremque aperiam eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo aspernatur aut odit aut fugit sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt neque dolorem ipsum quia dolor sit amet consectetur adipisci velit sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem ut enim ad minima veniam quis nostrum exercitationem ullam corporis nemo enim ipsam voluptatem quia voluptas sit suscipit laboriosam nisi ut aliquid ex ea commodi consequatur quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae et iusto odio dignissimos ducimus qui blanditiis praesentium laudantium totam rem voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident sed ut perspiciatis unde omnis iste natus error similique sunt in culpa qui officia deserunt mollitia animi id est laborum et dolorum fuga et harum quidem rerum facilis est et expedita distinctio nam libero tempore cum soluta nobis est eligendi optio cumque nihil impedit quo porro quisquam est qui minus id quod maxime placeat facere possimus omnis voluptas assumenda est omnis dolor repellendus temporibus autem quibusdam et aut consequatur vel illum qui dolorem eum fugiat quo voluptas nulla pariatur at vero eos et accusamus officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae itaque earum rerum hic tenetur a sapiente delectus ut aut reiciendis voluptatibus maiores doloribus asperiores repellat)
# Get one placeholder word
# @return [String]
def word
words(1)
end
# Get some number of placeholder words
# @param [Fixnum] total
# @return [String]
def words(total)
(1..total).map do
randm(WORDS)
end.join(' ')
end
# Get one placeholder sentence
# @return [String]
def sentence
sentences(1)
end
# Get some number of placeholder sentences
# @param [Fixnum] total
# @return [String]
def sentences(total)
(1..total).map do
words(randm(4..15)).capitalize
end.join('. ')
end
# Get one placeholder paragraph
# @return [String]
def paragraph
paragraphs(1)
end
# Get some number of placeholder paragraphs
# @param [Fixnum] total
# @return [String]
def paragraphs(total)
(1..total).map do
sentences(randm(3..7)).capitalize
end.join("\n\n")
end
# Get a placeholder date
# @param [String] fmt
# @return [String]
def date(fmt = '%a %b %d, %Y')
y = rand(20) + 1990
m = rand(12) + 1
d = rand(31) + 1
Time.local(y,m,d).strftime(fmt)
end
# Get a placeholder name
# @return [String]
def name
"#{first_name} #{last_name}"
end
# Get a placeholder first name
# @return [String]
def first_name
names = "Judith Angelo Margarita Kerry Elaine Lorenzo Justice Doris Raul Liliana Kerry Elise Ciaran Johnny Moses Davion Penny Mohammed Harvey Sheryl Hudson Brendan Brooklynn Denis Sadie Trisha Jacquelyn Virgil Cindy Alexa Marianne Giselle Casey Alondra Angela Katherine Skyler Kyleigh Carly Abel Adrianna Luis Dominick Eoin Noel Ciara Roberto Skylar Brock Earl Dwayne Jackie Hamish Sienna Nolan Daren Jean Shirley Connor Geraldine Niall Kristi Monty Yvonne Tammie Zachariah Fatima Ruby Nadia Anahi Calum Peggy Alfredo Marybeth Bonnie Gordon Cara John Staci Samuel Carmen Rylee Yehudi Colm Beth Dulce Darius inley Javon Jason Perla Wayne Laila Kaleigh Maggie Don Quinn Collin Aniya Zoe Isabel Clint Leland Esmeralda Emma Madeline Byron Courtney Vanessa Terry Antoinette George Constance Preston Rolando Caleb Kenneth Lynette Carley Francesca Johnnie Jordyn Arturo Camila Skye Guy Ana Kaylin Nia Colton Bart Brendon Alvin Daryl Dirk Mya Pete Joann Uriel Alonzo Agnes Chris Alyson Paola Dora Elias Allen Jackie Eric Bonita Kelvin Emiliano Ashton Kyra Kailey Sonja Alberto Ty Summer Brayden Lori Kelly Tomas Joey Billie Katie Stephanie Danielle Alexis Jamal Kieran Lucinda Eliza Allyson Melinda Alma Piper Deana Harriet Bryce Eli Jadyn Rogelio Orlaith Janet Randal Toby Carla Lorie Caitlyn Annika Isabelle inn Ewan Maisie Michelle Grady Ida Reid Emely Tricia Beau Reese Vance Dalton Lexi Rafael Makenzie Mitzi Clinton Xena Angelina Kendrick Leslie Teddy Jerald Noelle Neil Marsha Gayle Omar Abigail Alexandra Phil Andre Billy Brenden Bianca Jared Gretchen Patrick Antonio Josephine Kyla Manuel Freya Kellie Tonia Jamie Sydney Andres Ruben Harrison Hector Clyde Wendell Kaden Ian Tracy Cathleen Shawn".split(" ")
names[rand(names.size)]
end
# Get a placeholder last name
# @return [String]
def last_name
names = "Chung Chen Melton Hill Puckett Song Hamilton Bender Wagner McLaughlin McNamara Raynor Moon Woodard Desai Wallace Lawrence Griffin Dougherty Powers May Steele Teague Vick Gallagher Solomon Walsh Monroe Connolly Hawkins Middleton Goldstein Watts Johnston Weeks Wilkerson Barton Walton Hall Ross Chung Bender Woods Mangum Joseph Rosenthal Bowden Barton Underwood Jones Baker Merritt Cross Cooper Holmes Sharpe Morgan Hoyle Allen Rich Rich Grant Proctor Diaz Graham Watkins Hinton Marsh Hewitt Branch Walton O'Brien Case Watts Christensen Parks Hardin Lucas Eason Davidson Whitehead Rose Sparks Moore Pearson Rodgers Graves Scarborough Sutton Sinclair Bowman Olsen Love McLean Christian Lamb James Chandler Stout Cowan Golden Bowling Beasley Clapp Abrams Tilley Morse Boykin Sumner Cassidy Davidson Heath Blanchard McAllister McKenzie Byrne Schroeder Griffin Gross Perkins Robertson Palmer Brady Rowe Zhang Hodge Li Bowling Justice Glass Willis Hester Floyd Graves Fischer Norman Chan Hunt Byrd Lane Kaplan Heller May Jennings Hanna Locklear Holloway Jones Glover Vick O'Donnell Goldman McKenna Starr Stone McClure Watson Monroe Abbott Singer Hall Farrell Lucas Norman Atkins Monroe Robertson Sykes Reid Chandler Finch Hobbs Adkins Kinney Whitaker Alexander Conner Waters Becker Rollins Love Adkins Black Fox Hatcher Wu Lloyd Joyce Welch Matthews Chappell MacDonald Kane Butler Pickett Bowman Barton Kennedy Branch Thornton McNeill Weinstein Middleton Moss Lucas Rich Carlton Brady Schultz Nichols Harvey Stevenson Houston Dunn West O'Brien Barr Snyder Cain Heath Boswell Olsen Pittman Weiner Petersen Davis Coleman Terrell Norman Burch Weiner Parrott Henry Gray Chang McLean Eason Weeks Siegel Puckett Heath Hoyle Garrett Neal Baker Goldman Shaffer Choi Carver".split(" ")
names[rand(names.size)]
end
# Get a placeholder 140 character tweet about Philip the Purple Otter
# Via http://www.kevadamson.com/talking-of-design/article/140-alternative-characters-to-lorem-ipsum
# @return [String]
def tweet
tweets = [ 'Far away, in a forest next to a river beneath the mountains, there lived a small purple otter called Philip. Philip likes sausages. The End.',
'He liked the quality sausages from Marks & Spencer but due to the recession he had been forced to shop in a less desirable supermarket. End.',
'He awoke one day to find his pile of sausages missing. Roger the greedy boar with human eyes, had skateboarded into the forest & eaten them!']
tweets[rand(tweets.size)]
end
# Get a placeholder email address
# @return [String]
def email
delimiters = [ '_', '-', '' ]
domains = %w(gmail.com yahoo.com hotmail.com email.com live.com me.com mac.com aol.com fastmail.com mail.com)
username = name.gsub(/[^\w]/, delimiters[rand(delimiters.size)])
"#{username}@#{domains[rand(domains.size)]}".downcase
end
# Get a placeholder image, using placehold.it by default
# @param [String] size
# @param [Hash] options
# @return [String]
def image(size, options={})
domain = options[:domain] || "http://placehold.it"
src = "#{domain}/#{size}"
hex = %w[a b c d e f 0 1 2 3 4 5 6 7 8 9]
background_color = options[:background_color]
color = options[:color]
if options[:random_color]
background_color = hex.shuffle[0...6].join
color = hex.shuffle[0...6].join
end
src << "/#{background_color.sub(/^#/, '')}" if background_color
src << "/ccc" if background_color.nil? && color
src << "/#{color.sub(/^#/, '')}" if color
src << "&text=#{Rack::Utils::escape(options[:text])}" if options[:text]
src
end
private
# Pick a random item from a given range
# @param [Range] range
# @return [Object]
def randm(range)
a = range.to_a
a[rand(a.length)]
end
end
end
end