Class: Pixiurge::Middleware::Tilt

Inherits:
Object
  • Object
show all
Defined in:
lib/pixiurge/config_ru.rb

Overview

The Tilt middleware serves Tilt (template) directories. See https://github.com/rtomayko/tilt for the list of supported template engines.

Since:

  • 0.1.0

Constant Summary

MIME_TYPE_EXTENSION_MAP =

Since:

  • 0.1.0

{
}

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Tilt

Constructor. If you supply a list of engines, you'll need to make sure the appropriate gems are included for them. For instance if you supply "haml" as an engine, make sure to include the "haml" gem for your game.

This can be used to serve Ruby template files like Erb or Haml, but also Markdown, CoffeeScript, TypeScript, LiveScript, Less, Sass/Scss and more with the right gems. See https://github.com/rtomayko/tilt for the list of supported template engines.

For templating libraries like Erb that can handle any file type, this middleware will strip extensions. That means something ending in ".html.erb" will only be served if you ask for the same with ".html". You can do the same with something like CoffeeScript, so you could name your files whatever_filename.js.coffee if you want them to be converted to JS and served with a .js extension. But the middleware will also try to use sane extensions (convert .coffee to .js, etc) where it can. If you have trouble, try manually using the "stripped" extension trick (e.g. my_file.js.coffee or my_file.html.haml).

Note that multiple instances don't keep the extensions separate, so it may be hard to keep from serving Markdown files out of your Haml directory... Don't keep files in your asset directories if you're worried about your users seeing them, in general.

In production it will be faster to "bake" these directories into static files of the appropriate type instead of converting on the server.

Parameters:

  • app (Rack::App)

    The next innermost Rack app

  • options (Hash) (defaults to: {})

    Options to this middleware

Options Hash (options):

  • :urls (String, Array<String>)

    A root or list of URL roots to serve TMX files from

  • :root (String)

    The file system root to serve from (default: Dir.pwd)

  • :engines (String, Array<String>)

    List of Tilt-supported template engines to use; default: [ "erubis" ]

  • :encoding (String)

    The string encoding for Tilt to use (default: 'utf8')

  • :scope (Object)

    The 'scope' object for Tilt evaluation - can define methods that will be available

  • :locals (Hash)

    Local variables to be defined inside Tilt evaluations

Since:

  • 0.1.0



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/pixiurge/config_ru.rb', line 311

def initialize(app, options = {})
  @app = app
  @urls = [(options[:urls] || "/tmx")].flatten
  @root = options[:root] || Dir.pwd

  @tilt_scope = options[:scope] || Object.new
  @tilt_locals = options[:locals] || {}

  engines = []
  if options[:engines]
    engines = [options[:engines]].flatten
  else
    engines = [ "erubis" ]
  end

  engines.each { |engine_name| require engine_name }
  bad_engines = engines.select { |eng| ::Tilt[eng].nil? }
  raise("No Tilt templating engine for engine(s): #{bad_engines.inspect}!") unless bad_engines.empty?
  tilt_engines = engines.map { |engine_name| ::Tilt[engine_name] }
  # We just want the list of extensions. We're going to use Tilt's
  # default preference priorities for a given extension. If
  # somebody wants different, they're allowed to call Tilt.prefer,
  # likely from config.ru.
  @extensions = tilt_engines.flat_map { |eng| ::Tilt.default_mapping.extensions_for(eng) }.uniq

  @mapped_extensions = {}
  tilt_engines.each do |engine|
    engine.default_mime_type
  end

  @encoding = options[:encoding] || 'utf8'
end

Instance Method Details

#call(env) ⇒ Object

The Rack .call method for middleware.

Parameters:

  • env (Hash)

    The Rack environment hash.

Since:

  • 0.1.0



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/pixiurge/config_ru.rb', line 348

def call(env)
  # If no Tilt path is matched, forward the call to the next middleware
  call_root = @urls.detect { |u| env["PATH_INFO"].index(u) == 0 }
  return @app.call(env) unless call_root

  # A directory was matched. Let's see if we have any file which,
  # when we strip the extension, would give us the requested
  # file.
  local_path = File.join(@root, env["PATH_INFO"])
  local_extension = @extensions.detect { |ext| File.exist?(local_path + "." + ext) }
  # No match? 404.
  unless local_extension
    return [404, {}, [""]]
  end

  local_template_file = local_path + "." + local_extension

  # Caching these is faster than not. You know what's faster yet?
  # Rendering them into a nice static file and serving that via
  # NGinX. The easiest way to do that for nontrivial cases is
  # probably using 'curl' to get these from the server.
  template = ::Tilt.new local_template_file
  return [200, {}, [ template.render(@tilt_scope, @tilt_locals) ]]
end