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

raise_errors[RW]

Public Class Methods

gem_root() click to toggle source

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!() click to toggle source

Reset the state of flatrack and its configuration For testing @private

# File lib/flatrack.rb, line 59
def reset!
  @delegate_instance = nil
end
site=(instance) click to toggle source
# File lib/flatrack.rb, line 63
def site=(instance)
  @delegate_instance = instance
end
site_root() click to toggle source

The site root @!attribute [r] site_root @return [String]

# File lib/flatrack.rb, line 52
def site_root
  Dir.pwd
end

Private Class Methods

delegate_instance() click to toggle source
# File lib/flatrack.rb, line 69
def delegate_instance
  @delegate_instance ||= new
end
method_missing(m, *args, &block) click to toggle source
# File lib/flatrack.rb, line 73
def method_missing(m, *args, &block)
  delegate_instance.public_method(m).call(*args, &block)
end

Public Instance Methods

Redirector(source, opts={}) click to toggle source
Calls superclass method
# 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
Rewriter(source, opts={}) click to toggle source
Calls superclass method
# 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
assets() click to toggle source

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
config() { |self| ... } click to toggle source

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
middleware() click to toggle source

The middleware stack for flatrack @!attribute [r] middleware @return [Hash]

# File lib/flatrack.rb, line 116
def middleware
  @middleware ||= []
end
mock_env_for(url, opts={}) click to toggle source

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(source, to: nil, type: :permanent) click to toggle source

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
redirects() click to toggle source

The redirects @!attribute [r] redirects @return [Hash]

# File lib/flatrack.rb, line 158
def redirects
  @redirects ||= {}
end
register_format(ext, mime) click to toggle source

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(source, to: nil) click to toggle source

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
rewrites() click to toggle source

The rewrites @!attribute [r] rewrites @return [Hash]

# File lib/flatrack.rb, line 151
def rewrites
  @rewrites ||= {}
end
site() click to toggle source

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
site_root() click to toggle source

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
site_root=(path) click to toggle source
# File lib/flatrack.rb, line 169
def site_root=(path)
  path = File.expand_path path
  Template.register_path path
  @site_root = path
end
use(middleware, options = nil) click to toggle source

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

builder() click to toggle source
# 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
env_extensions() click to toggle source
# File lib/flatrack.rb, line 210
def env_extensions
  { 'flatrack.config' => self.config }
end