class Flatrack
A static site generator with a little sprinkle of ruby magic
Version
Constants
- FORMATS
@private
- FileNotFound
@private
- MAPPING
@private
- TemplateNotFound
@private
- VERSION
@private
Attributes
Public Class Methods
The root of the flatrack gem @!attribute [r] gem_root
@return [String]
# File lib/flatrack.rb, line 45 def gem_root File.expand_path File.join __FILE__, '..' end
Reset the state of flatrack and its configuration For testing @private
# File lib/flatrack.rb, line 59 def reset! @delegate_instance = nil end
# File lib/flatrack.rb, line 63 def site=(instance) @delegate_instance = instance end
The site root @!attribute [r] site_root
@return [String]
# File lib/flatrack.rb, line 52 def site_root Dir.pwd end
Private Class Methods
# File lib/flatrack.rb, line 69 def delegate_instance @delegate_instance ||= new end
# File lib/flatrack.rb, line 73 def method_missing(m, *args, &block) delegate_instance.public_method(m).call(*args, &block) end
Public Instance Methods
# File lib/flatrack/redirector.rb, line 2 def Redirector(source, opts={}) to = opts.delete(:to) type = opts.delete(:type) || :permanent klass = Class.new(Redirector) klass.send(:define_method, :initialize) do |app, mw_opts| mapping = { source => Redirector::Redirect.new(to, type) } super app, mapping, mw_opts end klass end
# File lib/flatrack/rewriter.rb, line 2 def Rewriter(source, opts={}) to = opts.delete(:to) klass = Class.new(Rewriter) klass.send(:define_method, :initialize) do |app, mw_opts| mapping = { source => to } super app, mapping, mw_opts end klass end
The flatrack sprockets environment @!attribute [r] assets @return [Sprockets::Environment]
# File lib/flatrack.rb, line 92 def assets @assets ||= begin Sass.load_paths << File.join(site_root, 'assets/stylesheets') Sprockets::Environment.new.tap do |environment| environment.register_engine '.sass', Sprockets::Sass::SassTemplate environment.register_engine '.scss', Sprockets::Sass::ScssTemplate environment.append_path File.join site_root, 'assets/images' environment.append_path File.join site_root, 'assets/javascripts' environment.append_path File.join site_root, 'assets/stylesheets' environment.context_class.class_eval { include AssetExtensions } end end end
Configure the flatrack instance @yield [Flatrack] configuration for the flatrack instance @return [Flatrack]
# File lib/flatrack.rb, line 81 def config yield self if block_given? OpenStruct.new( site_root: site_root, raise_errors: raise_errors ).freeze end
The middleware stack for flatrack @!attribute [r] middleware @return [Hash]
# File lib/flatrack.rb, line 116 def middleware @middleware ||= [] end
This is for testing @private
# File lib/flatrack.rb, line 122 def mock_env_for(url, opts={}) env = Rack::MockRequest.env_for url, opts env.merge! env_extensions end
redirect a path @param source [String] @param to [String] @param type [Symbol]
# File lib/flatrack.rb, line 141 def redirect(source, to: nil, type: :permanent) unless [source, to].all? { |path| path.is_a? String } raise ArgumentError, 'mappings must be strings' end redirects.merge! source => Redirector::Redirect.new(to, type) end
The redirects @!attribute [r] redirects @return [Hash]
# File lib/flatrack.rb, line 158 def redirects @redirects ||= {} end
register a format extension by its mime type @param ext [String] the extension @param mime [String] the mime type
# File lib/flatrack.rb, line 109 def register_format(ext, mime) FORMATS[ext.to_s] = mime end
Rewrite a path @param source [String] @param to [String]
# File lib/flatrack.rb, line 130 def rewrite(source, to: nil) unless [source, to].all? { |path| path.is_a? String } raise ArgumentError, 'mappings must be strings' end rewrites.merge! source => to end
The rewrites @!attribute [r] rewrites @return [Hash]
# File lib/flatrack.rb, line 151 def rewrites @rewrites ||= {} end
Returns the site lambda @return [Proc]
# File lib/flatrack.rb, line 184 def site lambda { |env| env.merge! env_extensions Request.new(env).response } end
The site root @!attribute [r] site_root
@return [String]
# File lib/flatrack.rb, line 178 def site_root @site_root || (self.site_root = self.class.site_root) end
# File lib/flatrack.rb, line 169 def site_root=(path) path = File.expand_path path Template.register_path path @site_root = path end
Insert a rack middleware at the end of the stack @param middleware [Class] the middleware class @param options [Hash] the options for the middleware
# File lib/flatrack.rb, line 165 def use(middleware, options = nil) self.middleware << [middleware, options].compact end
Private Instance Methods
# File lib/flatrack.rb, line 193 def builder @builder ||= begin this = self Rack::Builder.app do use Rack::Cookies use DomainParser use Flatrack::Rewriter, this.rewrites if this.rewrites.present? use Flatrack::Redirector, this.redirects if this.redirects.present? use Rack::Static, urls: ['/favicon.ico', 'assets'], root: File.join(this.site_root, 'public') this.middleware.each { |mw| use *mw } MAPPING.each { |path, app| map(path) { run this.send(app) } } end end end
# File lib/flatrack.rb, line 210 def env_extensions { 'flatrack.config' => self.config } end