Module: Texas::Template

Defined in:
lib/texas/template.rb,
lib/texas/template/helper.rb,
lib/texas/template/runner.rb,
lib/texas/template/helper/md.rb,
lib/texas/template/helper/tex.rb,
lib/texas/template/helper/info.rb,
lib/texas/template/helper/base.rb

Defined Under Namespace

Modules: Helper, Runner

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.handlersObject

Returns a map of file extensions to handlers


5
6
7
# File 'lib/texas/template.rb', line 5

def handlers
  @handlers
end

.known_extensionsObject

Returns all known template extensions


8
9
10
# File 'lib/texas/template.rb', line 8

def known_extensions
  @known_extensions
end

Class Method Details

.basename(filename) ⇒ Object

Returns the filename without the template extension

Example:

Template.basename("/home/rene/github/sample_project/tmp/build/chapter-01/contents.md.erb")
# => "/home/rene/github/sample_project/tmp/build/chapter-01/contents"

68
69
70
71
72
73
74
# File 'lib/texas/template.rb', line 68

def basename(filename)
  value = filename
  known_extensions.each do |str|
    value = value.gsub(/\.#{str}$/, '')
  end
  value
end

.create(filename, build) ⇒ Object

Returns a Template runner for the given filename

Example:

Template.create("some_file.tex.erb", build)
# => #<Texas::Template::Runner::TeX ...>

58
59
60
# File 'lib/texas/template.rb', line 58

def create(filename, build)
  handler(filename).new(filename, build)
end

.handler(filename) ⇒ Object

Returns a template handler for the given filename

Example:

Template.handler("some_file.tex.erb")
# => Template::Runner::TeX

16
17
18
19
20
21
22
# File 'lib/texas/template.rb', line 16

def handler(filename)
  Template.handlers.each do |pattern, klass|
    return klass if filename =~ pattern
  end
  warning { "No template handler found for: #{filename}" }
  Template::Runner::Base
end

.register_handler(extensions, handler_class) ⇒ Object

Registers a handler for the given template extensions

Example:

Template.register_handler %w(tex tex.erb), Template::Runner::TeX

29
30
31
32
33
34
# File 'lib/texas/template.rb', line 29

def register_handler(extensions, handler_class)
  extensions.each do |ext|
    handlers[/\.#{ext}$/i] = handler_class
  end
  known_extensions.concat extensions
end

.register_helper(klass) ⇒ Object

Registers the methods defined in a module to use them in templates.

Example:

module Foo
  def bar; "foobar"; end
end
Template.register_helper Foo

# afterwards, this prints "foobar" in any template:
<%= bar %>

48
49
50
# File 'lib/texas/template.rb', line 48

def register_helper(klass)
  Template::Runner::Base.__send__ :include, klass
end