Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* Add filter registration system, break out existing filters into sep…

…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...
commit bb25f47c74ca71a5db90aac76ac82a4cef25e3a1 1 parent 2f58e71
@bruce bruce authored
View
11 bin/webby
@@ -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
View
2  data/Rakefile
@@ -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
View
29 data/tasks/setup.rb
@@ -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}
@@ -31,4 +16,4 @@
Object.instance_eval {const_set "HAVE_#{lib.upcase}", try_require(lib)}
end
-# EOF
+# EOF
View
31 lib/webby.rb
@@ -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
View
90 lib/webby/filters.rb
@@ -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
View
9 lib/webby/filters/coderay.rb
@@ -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
@@ -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
View
6 lib/webby/filters/erb.rb
@@ -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
View
11 lib/webby/filters/graphviz.rb
@@ -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
@@ -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
View
7 lib/webby/filters/haml.rb
@@ -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
View
4 lib/webby/filters/markdown.rb
@@ -0,0 +1,4 @@
+# Render text via markdown using the BlueCloth library.
+Webby::Filters.register :markdown do |input|
+ BlueCloth.new(input).to_html
+end
View
5 lib/webby/filters/sass.rb
@@ -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
View
4 lib/webby/filters/textile.rb
@@ -0,0 +1,4 @@
+# Render text via textile using the RedCloth library.
+Webby::Filters.register :textile do |input|
+ RedCloth.new(input).to_html
+end
View
7 lib/webby/filters/tidy.rb
@@ -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
View
117 lib/webby/renderer.rb
@@ -40,7 +40,7 @@ def self.write( page )
break unless renderer.next_page
}
end
-
+
# call-seq:
# Renderer.new( page )
#
@@ -48,6 +48,7 @@ def self.write( page )
# desired filters to the _page_ (from the page's meta-data) and then
# render the filtered page into the desired layout.
#
+ attr_reader :page, :pages, :pager, :content
def initialize( page )
unless page.is_page?
raise ArgumentError,
@@ -79,10 +80,7 @@ def layout_page
break if lyt.nil?
@content, str = str, ::Webby::File.read(lyt.path)
-
- Array(lyt.filter).each do |filter|
- str = self.send(filter + '_filter', str)
- end
+ str = Filters.process(self, lyt, str)
@content, obj = nil, lyt
end
@@ -97,13 +95,7 @@ def layout_page
# determined from the page's meta-data.
#
def render_page
- str = ::Webby::File.read(@page.path)
-
- Array(@page.filter).each do |filter|
- str = self.send(filter + '_filter', str)
- end
-
- str
+ Filters.process(self, @page, ::Webby::File.read(@page.path))
end
# call-seq:
@@ -125,7 +117,7 @@ def paginate( items, count, &block )
items[offset,per_page]
end.first
- @pager.each &block
+ @pager.each(&block)
end
# call-seq:
@@ -149,105 +141,6 @@ def next_page
true
end
- # Render text via ERB using the built in ERB library.
- #
- def erb_filter( str )
- b = binding
- ERB.new(str, nil, '-').result(b)
- end
-
- # Render text via markdown using the BlueCloth library.
- #
- def markdown_filter( str )
- BlueCloth.new(str).to_html
- rescue NameError => err
- @log.error 'markdown filter failed (BlueCloth not installed?)'
- @log.debug err
- exit
- end
-
- # Render text via textile using the RedCloth library.
- #
- def textile_filter( str )
- RedCloth.new(str).to_html
- rescue NameError => err
- @log.error 'textile filter failed (RedCloth not installed?)'
- @log.debug err
- exit
- end
-
- # Render text via the CodeRay syntax highlighter library.
- #
- def coderay_filter( str )
- filters = nil
-
- if Array === @page.filter
- idx = @page.filter.index('coderay') + 1
- filters = @page.filter.slice(idx..-1)
- end
-
- Filters::CodeRay.new(str, filters).to_html
- rescue NameError => err
- @log.error 'coderay filter failed (CodeRay not installed?)'
- @log.debug err
- exit
- end
-
- # Render text into iamges via the Graphviz programs.
- #
- def graphviz_filter( str )
- filters = nil
-
- if Array === @page.filter
- idx = @page.filter.index('graphviz') + 1
- filters = @page.filter.slice(idx..-1)
- end
-
- Filters::Graphviz.new(str, filters).to_html
- rescue NameError => err
- @log.error 'graphviz filter failed (Graphviz not installed?)'
- @log.debug err
- exit
- rescue Filters::Graphviz::Error
- exit
- end
-
- # Render html into html/xhtml via the Tidy program
- #
- def tidy_filter( str )
- Filters::Tidy.new(str).process
- rescue NameError => err
- @log.error 'tidy filter failed (Tidy not installed?)'
- @log.debug err
- exit
- rescue Filters::Tidy::Error
- exit
- end
-
- # Render text via the Haml library
- #
- def haml_filter( str )
- opts = @page.haml_options || {}
- opts[:locals] ||= {}
- opts[:locals].merge!({:page => @page, :pages => @pages})
- Haml::Engine.new(str, opts).to_html
- rescue NameError => err
- @log.error 'haml filter failed (Haml not installed?)'
- @log.debug err
- exit
- end
-
- # Render text via the Sass library (part of Haml)
- #
- def sass_filter( str )
- opts = @page.sass_options || {}
- Sass::Engine.new(str, opts).render
- rescue NameError => err
- @log.error 'sass filter failed (Haml not installed?)'
- @log.debug err
- exit
- end
-
end # class Renderer
end # module Webby
Please sign in to comment.
Something went wrong with that request. Please try again.