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/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, EtanniTemplate, HamlTemplate, LiquidTemplate, Mapping, MarkabyTemplate, NokogiriTemplate, Pipeline, PrawnTemplate, RadiusTemplate, SassTemplate, ScssTemplate, StaticTemplate, StringTemplate, Template, YajlTemplate

Constant Summary collapse

VERSION =

Current version.

'2.6.1'
RDocTemplate =

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

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
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
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:



91
92
93
# File 'lib/tilt.rb', line 91

def default_mapping
  @default_mapping
end

.extract_fixed_localsObject

Whether to extract fixed locals from templates by scanning the template content.



95
96
97
# File 'lib/tilt.rb', line 95

def extract_fixed_locals
  @extract_fixed_locals
end

Class Method Details

.[](file) ⇒ Object



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

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.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tilt.rb', line 24

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



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

def self.lazy_map
  @default_mapping.lazy_map
end

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



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

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



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

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

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



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

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

.register_pipeline(ext, options = EMPTY_HASH) ⇒ Object



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

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

.registered?(ext) ⇒ Boolean

Returns:

  • (Boolean)

See Also:



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

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

.template_for(file) ⇒ Object



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

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

.templates_for(file) ⇒ Object



85
86
87
# File 'lib/tilt.rb', line 85

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