Module: Tilt

Defined in:
lib/vendor/tilt-1.4.1/lib/tilt/asciidoc.rb,
lib/vendor/tilt-1.4.1/lib/tilt.rb,
lib/vendor/tilt-1.4.1/lib/tilt/css.rb,
lib/vendor/tilt-1.4.1/lib/tilt/csv.rb,
lib/vendor/tilt-1.4.1/lib/tilt/erb.rb,
lib/vendor/tilt-1.4.1/lib/tilt/haml.rb,
lib/vendor/tilt-1.4.1/lib/tilt/rdoc.rb,
lib/vendor/tilt-1.4.1/lib/tilt/wiki.rb,
lib/vendor/tilt-1.4.1/lib/tilt/yajl.rb,
lib/vendor/tilt-1.4.1/lib/tilt/plain.rb,
lib/vendor/tilt-1.4.1/lib/tilt/coffee.rb,
lib/vendor/tilt-1.4.1/lib/tilt/etanni.rb,
lib/vendor/tilt-1.4.1/lib/tilt/liquid.rb,
lib/vendor/tilt-1.4.1/lib/tilt/radius.rb,
lib/vendor/tilt-1.4.1/lib/tilt/string.rb,
lib/vendor/tilt-1.4.1/lib/tilt/builder.rb,
lib/vendor/tilt-1.4.1/lib/tilt/markaby.rb,
lib/vendor/tilt-1.4.1/lib/tilt/textile.rb,
lib/vendor/tilt-1.4.1/lib/tilt/markdown.rb,
lib/vendor/tilt-1.4.1/lib/tilt/nokogiri.rb,
lib/vendor/tilt-1.4.1/lib/tilt/template.rb

Overview

AsciiDoc see: asciidoc.org/

Defined Under Namespace

Modules: CompileSite Classes: AsciidoctorTemplate, BlueClothTemplate, BuilderTemplate, CSVTemplate, Cache, CoffeeScriptTemplate, CreoleTemplate, ERBTemplate, ErubisTemplate, EtanniTemplate, HamlTemplate, KramdownTemplate, LessTemplate, LiquidTemplate, MarkabyTemplate, MarukuTemplate, NokogiriTemplate, PlainTemplate, RDiscountTemplate, RDocTemplate, RadiusTemplate, RedClothTemplate, RedcarpetTemplate, SassTemplate, ScssTemplate, StringTemplate, Template, WikiClothTemplate, YajlTemplate

Constant Summary collapse

VERSION =
'1.4.1'
TOPOBJECT =
Object.superclass || Object

Class Method Summary collapse

Class Method Details

.[](file) ⇒ Object

Lookup a template class for the given filename or file extension. Return nil when no implementation is found.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/vendor/tilt-1.4.1/lib/tilt.rb', line 69

def self.[](file)
  pattern = file.to_s.downcase
  until pattern.empty? || registered?(pattern)
    pattern = File.basename(pattern)
    pattern.sub!(/^[^.]*\.?/, '')
  end

  # Try to find a preferred engine.
  preferred_klass = @preferred_mappings[pattern]
  return preferred_klass if preferred_klass

  # Fall back to the general list of mappings.
  klasses = @template_mappings[pattern]

  # Try to find an engine which is already loaded.
  template = klasses.detect do |klass|
    if klass.respond_to?(:engine_initialized?)
      klass.engine_initialized?
    end
  end

  return template if template

  # Try each of the classes until one succeeds. If all of them fails,
  # we'll raise the error of the first class.
  first_failure = nil

  klasses.each do |klass|
    begin
      klass.new { '' }
    rescue Exception => ex
      first_failure ||= ex
      next
    else
      return klass
    end
  end

  raise first_failure if first_failure
end

.mappingsObject

Hash of template path pattern => template implementation class mappings.



8
9
10
# File 'lib/vendor/tilt-1.4.1/lib/tilt.rb', line 8

def self.mappings
  @template_mappings
end

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

Create a new template for the given file using the file’s extension to determine the the template mapping.



59
60
61
62
63
64
65
# File 'lib/vendor/tilt-1.4.1/lib/tilt.rb', line 59

def self.new(file, line=nil, options={}, &block)
  if template_class = self[file]
    template_class.new(file, line, options, &block)
  else
    fail "No template engine registered for #{File.basename(file)}"
  end
end

.normalize(ext) ⇒ Object



12
13
14
# File 'lib/vendor/tilt-1.4.1/lib/tilt.rb', line 12

def self.normalize(ext)
  ext.to_s.downcase.sub(/^\./, '')
end

.prefer(template_class, *extensions) ⇒ Object

Makes a template class preferred for the given file extensions. If you don’t provide any extensions, it will be preferred for all its already registered extensions:

# Prefer RDiscount for its registered file extensions:
Tilt.prefer(Tilt::RDiscountTemplate)

# Prefer RDiscount only for the .md extensions:
Tilt.prefer(Tilt::RDiscountTemplate, '.md')


38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vendor/tilt-1.4.1/lib/tilt.rb', line 38

def self.prefer(template_class, *extensions)
  if extensions.empty?
    mappings.each do |ext, klasses|
      @preferred_mappings[ext] = template_class if klasses.include? template_class
    end
  else
    extensions.each do |ext|
      ext = normalize(ext)
      register(template_class, ext)
      @preferred_mappings[ext] = template_class
    end
  end
end

.register(template_class, *extensions) ⇒ Object

Register a template implementation by file extension.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/vendor/tilt-1.4.1/lib/tilt.rb', line 17

def self.register(template_class, *extensions)
  if template_class.respond_to?(:to_str)
    # Support register(ext, template_class) too
    extensions, template_class = [template_class], extensions[0]
  end

  extensions.each do |ext|
    ext = normalize(ext)
    mappings[ext].unshift(template_class).uniq!
  end
end

.registered?(ext) ⇒ Boolean

Returns true when a template exists on an exact match of the provided file extension

Returns:

  • (Boolean)


53
54
55
# File 'lib/vendor/tilt-1.4.1/lib/tilt.rb', line 53

def self.registered?(ext)
  mappings.key?(ext.downcase) && !mappings[ext.downcase].empty?
end