Module: Tilt

Defined in:
lib/tilt.rb,
lib/tilt/csv.rb,
lib/tilt/erb.rb,
lib/tilt/haml.rb,
lib/tilt/sass.rb,
lib/tilt/yajl.rb,
lib/tilt/erubi.rb,
lib/tilt/prawn.rb,
lib/tilt/coffee.rb,
lib/tilt/erubis.rb,
lib/tilt/etanni.rb,
lib/tilt/liquid.rb,
lib/tilt/radius.rb,
lib/tilt/string.rb,
lib/tilt/builder.rb,
lib/tilt/mapping.rb,
lib/tilt/markaby.rb,
lib/tilt/nokogiri.rb,
lib/tilt/pipeline.rb,
lib/tilt/template.rb

Overview

Namespace for Tilt. This module is not intended to be included anywhere.

Defined Under Namespace

Modules: CLI, CompiledTemplates Classes: BuilderTemplate, CSVTemplate, Cache, CoffeeScriptLiterateTemplate, CoffeeScriptTemplate, ERBTemplate, ErubiTemplate, ErubisTemplate, EtanniTemplate, HamlTemplate, LiquidTemplate, Mapping, MarkabyTemplate, NokogiriTemplate, Pipeline, PrawnTemplate, RadiusTemplate, SassTemplate, ScssTemplate, StaticTemplate, StringTemplate, Template, YajlTemplate

Constant Summary collapse

VERSION =

Current version.

'2.4.0'
RDocTemplate =

RDoc template. See: github.com/ruby/rdoc

It’s suggested that your program run the following at load time when using this templae engine in a threaded environment:

require 'rdoc'
require 'rdoc/markup'
require 'rdoc/markup/to_html'
require 'rdoc/options'
Tilt::StaticTemplate.subclass do
  RDoc::Markup::ToHtml.new(RDoc::Options.new, nil).convert(@data).to_s
end
SlimTemplate =
Slim::Template
BabelTemplate =
Tilt::StaticTemplate.subclass(mime_type: 'application/javascript') do
  @options[:filename] ||= @file
  Babel::Transpiler.transform(@data)["code"]
end
PlainTemplate =

Raw text (no template functionality).

Tilt::StaticTemplate.subclass{@data}
CreoleTemplate =

Creole implementation. See: www.wikicreole.org/

Tilt::StaticTemplate.subclass do
  opts = {}
  allowed_opts.each do |k|
    opts[k] = @options[k] if @options[k]
  end
  Creole::Parser.new(@data, opts).to_html
end
MarukuTemplate =

Maruku markdown implementation. See: github.com/bhollis/maruku

Tilt::StaticTemplate.subclass do
  Maruku.new(@data, @options).to_html
end
PandocTemplate =

Pandoc markdown implementation. See: pandoc.org/

Tilt::StaticTemplate.subclass do
  # turn options hash into an array
  # Map tilt options to pandoc options
  # Replace hash keys with value true with symbol for key
  # Remove hash keys with value false
  # Leave other hash keys untouched
  pandoc_options = []
  from = "markdown"
  smart_extension = "-smart"
  @options.each do |k,v|
    case k
    when :smartypants
      smart_extension = "+smart" if v
    when :escape_html
      from = "markdown-raw_html" if v
    when :commonmark
      from = "commonmark" if v
    when :markdown_strict
      from = "markdown_strict" if v
    else
      case v
      when true
        pandoc_options << k
      when false
        # do nothing
      else
        pandoc_options << { k => v }
      end
    end
  end
  pandoc_options << { :f => from + smart_extension }

  PandocRuby.new(@data, *pandoc_options).to_html.strip
end
AsciidoctorTemplate =

Asciidoctor implementation for AsciiDoc see: asciidoctor.github.com/

Asciidoctor is an open source, pure-Ruby processor for converting AsciiDoc documents or strings into HTML 5, DocBook 4.5 and other formats.

Tilt::StaticTemplate.subclass do
  @options[:header_footer] = false if @options[:header_footer].nil?
  Asciidoctor.render(@data, @options)
end
KramdownTemplate =

Kramdown Markdown implementation. See: kramdown.gettalong.org/

Tilt::StaticTemplate.subclass do
  # dup as Krawmdown modifies the passed option with map!
  @options[:smart_quotes] = dumb_quotes.dup unless @options[:smartypants]

  Kramdown::Document.new(@data, @options).to_html
end
RedClothTemplate =

RedCloth implementation. See: github.com/jgarber/redcloth

Tilt::StaticTemplate.subclass do
  engine = RedCloth.new(@data)
  @options.each  do |k, v|
    m = :"#{k}="
    engine.send(m, v) if engine.respond_to? m
  end
  engine.to_html
end
TOPOBJECT =
CompiledTemplates
LOCK =
Mutex.new
RDiscountTemplate =

Discount Markdown implementation. See: github.com/rtomayko/rdiscount

RDiscount is a simple text filter. It does not support scope or locals. The :smart and :filter_html options may be set true to enable those flags on the underlying RDiscount object.

Tilt::StaticTemplate.subclass do
  flags = _flags.select { |flag| @options[flag] }.
    map! { |flag| aliases[flag] || flag }

  RDiscount.new(@data, *flags).to_html
end
RedcarpetTemplate =
Tilt::StaticTemplate.subclass do
  aliases.each do |opt, aka|
    if options.key?(aka) || !@options.key?(opt)
      @options[opt] = @options.delete(aka)
    end
  end

  # only raise an exception if someone is trying to enable :escape_html
  @options.delete(:escape_html) unless @options[:escape_html]

  renderer = @options.delete(:renderer) || ::Redcarpet::Render::HTML.new(@options)
  if options.delete(:smartypants) && !(renderer.is_a?(Class) && renderer <= ::Redcarpet::Render::SmartyPants)
    renderer = if renderer == ::Redcarpet::Render::XHTML
      ::Redcarpet::Render::SmartyHTML.new(:xhtml => true)
    elsif renderer == ::Redcarpet::Render::HTML
      ::Redcarpet::Render::SmartyHTML
    elsif renderer.is_a? Class
      Class.new(renderer) { include ::Redcarpet::Render::SmartyPants }
    else
      renderer.extend ::Redcarpet::Render::SmartyPants
    end
  end

  Redcarpet::Markdown.new(renderer, @options).render(@data)
end
WikiClothTemplate =

WikiCloth implementation. See: github.com/nricciar/wikicloth

Tilt::StaticTemplate.subclass do
  parser = @options.delete(:parser) || WikiCloth::Parser
  @options[:data] = @data
  parser.new(@options).to_html
end
LiveScriptTemplate =

LiveScript template implementation. See: livescript.net/

LiveScript templates do not support object scopes, locals, or yield.

Tilt::StaticTemplate.subclass(mime_type: 'application/javascript') do
  LiveScript.compile(@data, @options)
end
RstPandocTemplate =

Pandoc reStructuredText implementation. See: # pandoc.org/

Tilt::StaticTemplate.subclass do
  PandocRuby.new(@data, rst).to_html.strip
end
TypeScriptTemplate =
Tilt::StaticTemplate.subclass(mime_type: 'application/javascript') do
  option_args = []

  @options.each do |key, value|
    next unless value

    option_args << "--#{key}"

    if value != true
      option_args << value.to_s
    end
  end

  TypeScript::Node.compile(@data, *option_args)
end
CommonMarkerTemplate =
Tilt::StaticTemplate.subclass do
  extensions = exts.select do |extension|
    @options[extension]
  end

  parse_options, render_options = [parse_opts, render_opts].map do |opts|
    opts = opts.select do |option|
      @options[option]
    end.map! do |option|
      aliases[option] || option
    end

    opts = :DEFAULT unless opts.any?
    opts
  end

  CommonMarker.render_doc(@data, parse_options, extensions).to_html(render_options, extensions)
end

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.default_mappingTilt::Mapping (readonly)

Returns the main mapping object.

Returns:



87
88
89
# File 'lib/tilt.rb', line 87

def default_mapping
  @default_mapping
end

Class Method Details

.[](file) ⇒ Object



71
72
73
# File 'lib/tilt.rb', line 71

def self.[](file)
  @default_mapping[file]
end

.finalize!Object

Replace the default mapping with a finalized version of the default mapping. This can be done to improve performance after the template libraries you desire to use have already been loaded. Once this is is called, all attempts to modify the default mapping will fail. This also freezes Tilt itself.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tilt.rb', line 20

def self.finalize!
  return self if @default_mapping.is_a?(FinalizedMapping)

  class << self
    prepend(Module.new do
      def lazy_map(*)
        raise "Tilt.#{__callee__} not supported after Tilt.finalize! has been called"
      end
      alias register lazy_map
      alias register_lazy lazy_map
      alias register_pipeline lazy_map
      alias prefer lazy_map
    end)
  end

  @default_mapping = @default_mapping.finalized

  freeze
end

.lazy_mapObject



41
42
43
# File 'lib/tilt.rb', line 41

def self.lazy_map
  @default_mapping.lazy_map
end

.new(file, line = nil, options = nil, &block) ⇒ Object



66
67
68
# File 'lib/tilt.rb', line 66

def self.new(file, line=nil, options=nil, &block)
  @default_mapping.new(file, line, options, &block)
end

.register(template_class, *extensions) ⇒ Object Also known as: prefer



46
47
48
# File 'lib/tilt.rb', line 46

def self.register(template_class, *extensions)
  @default_mapping.register(template_class, *extensions)
end

.register_lazy(class_name, file, *extensions) ⇒ Object



51
52
53
# File 'lib/tilt.rb', line 51

def self.register_lazy(class_name, file, *extensions)
  @default_mapping.register_lazy(class_name, file, *extensions)
end

.register_pipeline(ext, options = EMPTY_HASH) ⇒ Object



56
57
58
# File 'lib/tilt.rb', line 56

def self.register_pipeline(ext, options=EMPTY_HASH)
  @default_mapping.register_pipeline(ext, options)
end

.registered?(ext) ⇒ Boolean

Returns:

  • (Boolean)

See Also:



61
62
63
# File 'lib/tilt.rb', line 61

def self.registered?(ext)
  @default_mapping.registered?(ext)
end

.template_for(file) ⇒ Object



76
77
78
# File 'lib/tilt.rb', line 76

def self.template_for(file)
  @default_mapping.template_for(file)
end

.templates_for(file) ⇒ Object



81
82
83
# File 'lib/tilt.rb', line 81

def self.templates_for(file)
  @default_mapping.templates_for(file)
end