Skip to content

Commit

Permalink
* Add filter registration system, break out existing filters into sep…
Browse files Browse the repository at this point in the history
…arate files under filters/

* bin/webby loads webby from relative path
* SITE defaults in a single place (the library's webby.rb vs webby.rb AND setup.rb); to be overridden in project Rakefile
* Support automatically loading vendor/webby when loading rake in projects
* Support automatically loading vendor/filters when loading rake in projects
* Very little documentation and no testing; YMMV
  • Loading branch information
Bruce Williams committed Dec 8, 2007
1 parent 2f58e71 commit bb25f47
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 161 deletions.
11 changes: 1 addition & 10 deletions bin/webby
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
#!/usr/bin/env ruby

begin
require 'rubygems'
require 'webby'
rescue LoadError
path = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
raise if $:.include? path
$: << path
retry
end

require File.dirname(__FILE__) << "/../lib/webby"
::Webby::Main.run ARGV

# EOF
2 changes: 1 addition & 1 deletion data/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load 'tasks/setup.rb'

task :default => :build

desc 'depoloy the site to the webserver'
desc 'deploy the site to the webserver'
task :deploy => [:build, 'deploy:rsync']

# EOF
29 changes: 7 additions & 22 deletions data/tasks/setup.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@

# Load webby from vendor/webby if available
$:.unshift File.dirname(__FILE__) + '/../vendor/webby/lib'
require 'webby'

SITE = Webby.site

# Webby defaults
SITE.content_dir = 'content'
SITE.output_dir = 'output'
SITE.layout_dir = 'layouts'
SITE.template_dir = 'templates'
SITE.exclude = %w[tmp$ bak$ ~$ CVS \.svn]

SITE.page_defaults = {
'extension' => 'html',
'layout' => 'default'
}
# Load vendor/filters/*.rb
Webby.require_all_libs_relative_to File.dirname(__FILE__) + '/../vendor/filters'

# Items used to deploy the webiste
SITE.host = 'user@hostname.tld'
SITE.remote_dir = '/not/a/valid/dir'
SITE.rsync_args = %w(-av --delete)

# Options passed to the 'tidy' program when the tidy filter is used
SITE.tidy_options = '-indent -wrap 80'
# Note: Override SITE defaults in Rakefile
SITE = Webby.site

# Load up the other rake tasks
FileList['tasks/*.rake'].each {|task| import task}
Expand All @@ -31,4 +16,4 @@
Object.instance_eval {const_set "HAVE_#{lib.upcase}", try_require(lib)}
end

# EOF
# EOF
31 changes: 18 additions & 13 deletions lib/webby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,24 @@ def self.require_all_libs_relative_to( fname, dir = nil )
#
def self.site
return @site if defined? @site

@site = OpenStruct.new
@site.output_dir = 'output'
@site.content_dir = 'content'
@site.layout_dir = 'layouts'
@site.template_dir = 'templates'
@site.exclude = %w(tmp$ bak$ ~$ CVS \.svn)
@site.page_defaults = {
'extension' => 'html',
'layout' => 'default'
}

@site
@site = OpenStruct.new(
:output_dir => 'output',
:content_dir => 'content',
:output_dir => 'output',
:layout_dir => 'layouts',
:template_dir => 'templates',
:exclude => %w[tmp$ bak$ ~$ CVS \.svn],
:page_defaults => {
'extension' => 'html',
'layout' => 'default'
},
# Items used to deploy the webiste
:host => 'user@hostname.tld',
:remote_dir => '/not/a/valid/dir',
:rsync_args => %w(-av --delete),
# Options passed to the 'tidy' program when the tidy filter is used
:tidy_options => '-indent -wrap 80'
)
end

# call-seq
Expand Down
90 changes: 90 additions & 0 deletions lib/webby/filters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@

module Webby

module Filters

class << self

# Register a handler for a filter
def register(filter, &block)
handlers[filter.to_s] = block
end

# Process input through filters
def process(renderer, page, input)
# Start a new cursor for this page
Cursor.new(renderer, page).start_for(input)
end

# Access a filter handler
def [](name)
handlers[name]
end

#######
private
#######

# The registered filter handlers
def handlers
@handlers ||= {}
end

# Instances of this class handle processing a set of filters
# for a given renderer and page.
# Note: The instance is passed as the second argument to filters
# that require two parameters and can be used to access
# information on the renderer, page, or filters being
# processed.
class Cursor

attr_reader :renderer, :page, :filters, :log
def initialize(renderer, page)
@renderer, @page = renderer, page
@filters = Array(page.filter)
@log = Logging::Logger[self]
@processed = 0
end

def start_for(input)
filters.inject(input) do |result, filter|
handler = Filters[filter]
args = [result, self][0, handler.arity]
handle(filter, handler, *args)
end
end

# The list of filters yet to be processed
def remaining_filters
filters[@processed..-1]
end

# The name of the current filter
def current_filter
filters[@processed]
end

#######
private
#######

# Process arguments through a single filter
def handle(filter, handler, *args)
result = handler.call(*args)
@processed += 1
result
rescue NameError => e
log.fatal "Name error in filter `#{filter}' (missing dependency?): #{e.message}"
exit 1
rescue => e
log.fatal "Error in filter `#{filter}': #{e.message}"
exit 1
end

end

end

end

end
9 changes: 8 additions & 1 deletion lib/webby/filters/coderay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
try_require 'coderay'

module Webby
module Filters

module Filters

# The CodeRay applies syntax highlighting to source code embedded in a
# webpage. The CodeRay highlighting engine is used for the HTML markup of
Expand Down Expand Up @@ -86,6 +87,12 @@ def to_html
end

end # class CodeRay

# Render text via the CodeRay syntax highlighter library.
register :coderay do |input, cursor|
Filters::CodeRay.new(input, cursor.remaining_filters).to_html
end

end # module Filters
end # module Webby

Expand Down
6 changes: 6 additions & 0 deletions lib/webby/filters/erb.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Render text via ERB using the built in ERB library.
Webby::Filters.register :erb do |input, cursor|
@page = cursor.page
@content = cursor.renderer.content
ERB.new(input, nil, '-').result(binding)
end
11 changes: 9 additions & 2 deletions lib/webby/filters/graphviz.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ module Filters
# }
# </graphviz>
#
# If the DOT script contains *URL* or *href* statements on any of the nodes
# If the DOT script contains *URL* or *href* cursorments on any of the nodes
# or edges, then an image map will be generated and the image will be
# "clikcable" in the webpage. If *URL* or *href* statements do not appear in
# "clikcable" in the webpage. If *URL* or *href* cursorments do not appear in
# the DOT script, then a regular image will be inserted into the webpage.
#
# The image is inserted into the page using an HTML <img /> tag. A
Expand Down Expand Up @@ -169,6 +169,13 @@ def error_check
end

end # class Graphviz

# Render text into iamges via the Graphviz programs.
#
register :graphviz do |input, cursor|
Filters::Graphviz.new(input, cursor.remaining_filters).to_html
end

end # module Filters
end # module Webby

Expand Down
7 changes: 7 additions & 0 deletions lib/webby/filters/haml.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Render text via the Haml library
Webby::Filters.register :haml do |input, cursor|
opts = cursor.page.haml_options || {}
opts[:locals] ||= {}
opts[:locals].merge!(:page => cursor.renderer.page, :pages => cursor.renderer.pages)
Haml::Engine.new(input, opts).to_html
end
4 changes: 4 additions & 0 deletions lib/webby/filters/markdown.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Render text via markdown using the BlueCloth library.
Webby::Filters.register :markdown do |input|
BlueCloth.new(input).to_html
end
5 changes: 5 additions & 0 deletions lib/webby/filters/sass.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Render text via the Sass library (part of Haml)
Webby::Filters.register :sass do |input, cursor|
opts = cursor.page.sass_options || {}
Sass::Engine.new(input, opts).render
end
4 changes: 4 additions & 0 deletions lib/webby/filters/textile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Render text via textile using the RedCloth library.
Webby::Filters.register :textile do |input|
RedCloth.new(input).to_html
end
7 changes: 7 additions & 0 deletions lib/webby/filters/tidy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ def process
end

end # class Tidy

# Render html into html/xhtml via the Tidy program
#
register :tidy do |input|
Filters::Tidy.new(input).process
end

end # module Filters
end # module Webby

Expand Down
Loading

0 comments on commit bb25f47

Please sign in to comment.