Class: HtmlMockup::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/html_mockup/template.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, options = {}) ⇒ Template

Returns a new instance of Template.

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :source_path (String, Pathname)

    The path to the source of the template being processed

  • :layouts_path (String, Pathname)

    The path to where all layouts reside

  • :partials_path (String, Pathname)

    The path to where all partials reside



38
39
40
41
42
43
44
45
46
47
# File 'lib/html_mockup/template.rb', line 38

def initialize(source, options = {})
  @options = options
  self.source_path = options[:source_path]
  self.data, self.source = extract_front_matter(source)
  self.template = Tilt.new(self.source_path.to_s){ self.source }
  
  if self.data[:layout] && layout_template_path = self.find_template(self.data[:layout], :layouts_path)
    @layout_template = Tilt.new(layout_template_path.to_s)
  end
end

Instance Attribute Details

#dataObject

Store the frontmatter



19
20
21
# File 'lib/html_mockup/template.rb', line 19

def data
  @data
end

#sourceObject

The source



16
17
18
# File 'lib/html_mockup/template.rb', line 16

def source
  @source
end

#source_pathObject

The path to the source file for this template



25
26
27
# File 'lib/html_mockup/template.rb', line 25

def source_path
  @source_path
end

#templateObject

The actual Tilt template



22
23
24
# File 'lib/html_mockup/template.rb', line 22

def template
  @template
end

Class Method Details

.open(path, options = {}) ⇒ Object



28
29
30
31
# File 'lib/html_mockup/template.rb', line 28

def open(path, options = {})
  raise "Unknown file #{path}" unless File.exist?(path)
  self.new(File.read(path), options.update(:source_path => path))
end

Instance Method Details

#find_template(name, path_type) ⇒ Object

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
74
75
76
# File 'lib/html_mockup/template.rb', line 67

def find_template(name, path_type)
  raise(ArgumentError, "path_type must be one of :partials_path or :layouts_path") unless [:partials_path, :layouts_path].include?(path_type)

  return nil unless @options[path_type]

  @resolvers ||= {}        
  @resolvers[path_type] ||= Resolver.new(@options[path_type])
  
  @resolvers[path_type].find_template(name, :preferred_extension => self.target_extension)
end

#render(env = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/html_mockup/template.rb', line 49

def render(env = {})
  context = TemplateContext.new(self, env)
  
  if @layout_template
    content_for_layout = self.template.render(context, {}) # yields
    
    @layout_template.render(context, {}) do |content_for|
      if content_for
        context._content_for_blocks[content_for]
      else
        content_for_layout
      end
    end
  else
    self.template.render(context, {})
  end
end

#source_extensionObject



94
95
96
97
98
99
100
101
# File 'lib/html_mockup/template.rb', line 94

def source_extension
  parts = File.basename(File.basename(self.source_path.to_s)).split(".")
  if parts.size > 2
    parts[-2..-1].join(".")
  else
    File.extname(self.source_path.to_s).sub(/^\./, "")
  end
end

#target_extensionObject

Try to infer the final extension of the output file.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/html_mockup/template.rb', line 79

def target_extension
  return @target_extension if @target_extension

  if type = MIME::Types[self.target_mime_type].first
    # Dirty little hack to enforce the use of .html instead of .htm
    if type.sub_type == "html"
      @target_extension = "html"
    else
      @target_extension = type.extensions.first
    end
  else
    @target_extension = File.extname(self.source_path.to_s).sub(/^\./, "")
  end
end

#target_mime_typeObject

Try to figure out the mime type based on the Tilt class and if that doesn’t work we try to infer the type by looking at extensions (needed for .erb)



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/html_mockup/template.rb', line 105

def target_mime_type
  mime = self.template.class.default_mime_type
  return mime if mime

  path = File.basename(self.source_path.to_s)
  mime = MIME::Types.type_for(path).first
  return mime.to_s if mime

  parts = File.basename(path).split(".")
  if parts.size > 2
    mime = MIME::Types.type_for(parts[0..-2].join(".")).first
    return mime.to_s if mime
  else
    nil
  end
end