Class: Usmu::Layout

Inherits:
StaticFile show all
Defined in:
lib/usmu/layout.rb

Overview

Class to represent files templated with a Tilt library. Most of the custom rendering logic is contained here.

Direct Known Subclasses

Page

Instance Attribute Summary collapse

Attributes inherited from StaticFile

#name

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration, name, type = nil, content = nil, metadata = nil) ⇒ Layout

Returns a new instance of Layout.

Parameters:

  • configuration (Usmu::Configuration)

    The configuration for the website we're generating.

  • name (String)

    The name of the file in the source directory.

  • type (String) (defaults to: nil)

    The type of template to use with the file. Used for testing purposes.

  • content (String) (defaults to: nil)

    The content of the file. Used for testing purposes.

  • metadata (String) (defaults to: nil)

    The metadata for the file. Used for testing purposes.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/usmu/layout.rb', line 17

def initialize(configuration, name, type = nil, content = nil,  = nil)
  super(configuration, name)

  if type.nil?
    type = name.split('.').last
    unless ::Tilt.default_mapping[type]
      raise "Templates of type '#{type}' aren't currently supported by Tilt. " +
            'Do you have the required gem installed?'
    end
  end
  @type = type
  path = File.join("#{content_path}", "#{name[0, name.length - type.length - 1]}")

  if content.nil?
    content = File.read("#{path}.#{type}")
  end
  @content = content

  if .nil?
    meta_file = "#{path}.meta.yml"
     = if File.exist? meta_file
      YAML.load_file(meta_file)
    else
      {}
    end
  end
  @metadata = 

  @parent = nil
  @parent = Layout.find_layout(configuration, self.['layout'])
end

Instance Attribute Details

#content_pathstring (readonly, protected)

Returns the base path to the files used by this class.

This folder should be the parent folder for the file named by the name attribute.

Returns:

  • (string)

    the base path to the files used by this class.

See Also:



176
177
178
# File 'lib/usmu/layout.rb', line 176

def content_path
  @configuration.layouts_path
end

#input_pathString (readonly)

Returns the full path to the file in the source directory.

Returns:

  • (String)

    the full path to the file in the source directory



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

def input_path
  File.join(content_path, @name)
end

#metadataHash (readonly)

Returns the metadata associated with this layout.

This will include any metadata from parent templates and default metadata

Returns:

  • (Hash)

    the metadata associated with this layout.



55
56
57
58
59
60
61
# File 'lib/usmu/layout.rb', line 55

def 
  if @parent.nil?
    (@configuration['default meta'] || {}).dup.deep_merge!(@metadata)
  else
    @parent..deep_merge!(@metadata)
  end
end

#output_extensionString (readonly)

Returns the extension to use with the output file.

Returns:

  • (String)

    the extension to use with the output file.



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/usmu/layout.rb', line 87

def output_extension
  case @type
    when 'erb', 'rhtml', 'erubis', 'liquid'
      nil
    when 'coffee'
      'js'
    when 'less', 'sass', 'scss'
      'css'
    else
      'html'
  end
end

#output_filenameString (readonly)

Returns the filename to use for the output directory with any modifications to the input filename required.

Returns:

  • (String)

    the filename to use in the output directory.



104
105
106
107
108
109
110
# File 'lib/usmu/layout.rb', line 104

def output_filename
  if output_extension
    @name[0..@name.rindex('.')] + output_extension
  else
    @name[0..@name.rindex('.') - 1]
  end
end

#provider_nameString (readonly, protected)

Returns the Tilt template engine's name for this layout.

This is used to determine which settings to use from the configuration file.

Returns:

  • (String)

    the Tilt template engine's name for this layout



164
165
166
# File 'lib/usmu/layout.rb', line 164

def provider_name
  Tilt.default_mapping.lazy_map[@type].select {|x| x[0] == template_class.name }.first[1].split('/').last
end

#template_classTilt::Template (readonly, protected)

Returns the Tilt template engine for this layout.

Returns:

  • (Tilt::Template)

    the Tilt template engine for this layout



154
155
156
# File 'lib/usmu/layout.rb', line 154

def template_class
  @template_class ||= ::Tilt.default_mapping[@type]
end

#typeString (readonly)

Returns the type of file this is. This is used to determine which template engine to use.

Returns:

  • (String)

    the type of file this is. This is used to determine which template engine to use.



10
11
12
# File 'lib/usmu/layout.rb', line 10

def type
  @type
end

Class Method Details

.find_layout(configuration, name) ⇒ Usmu::Layout

Static method to create a layout for a given configuration by it's name if it exists. This differs from #initialise in that it allows different types of values to be supplied as the name and will not fail if name is nil

Parameters:

  • configuration (Usmu::Configuration)

    The configuration to use for the search

  • name (String)

    If name is a string then search for a template with that name. Name here should not include file extension, eg. body not body.slim. If name is not a string then it will be returned verbatim. This means that name is nilable and can also be passed in as an Usmu::Layout already for testing purposes.

Returns:



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/usmu/layout.rb', line 122

def self.find_layout(configuration, name)
  if name === 'none'
    nil
  elsif name.class.name == 'String'
    Dir["#{configuration.layouts_path}/#{name}.*"].each do |f|
      filename = File.basename(f)
      if filename != "#{name}.meta.yml"
        return new(configuration, f[(configuration.layouts_path.length + 1)..f.length])
      end
    end
    nil
  else
    name
  end
end

.is_valid_file?(folder_type, name) ⇒ Boolean

Tests if a given file is a valid Tilt template based on the filename.

Parameters:

  • folder_type (String)

    One of "source" or "layout" depending on where the template is in the source tree. Not used by Usmu::Layout directly but intended to be available for future API.

  • name (String)

    The filename to be tested.

Returns:

  • (Boolean)


145
146
147
148
# File 'lib/usmu/layout.rb', line 145

def self.is_valid_file?(folder_type, name)
  type = name.split('.').last
  ::Tilt.default_mapping[type] ? true : false
end

Instance Method Details

#get_variables(variables) ⇒ Hash (private)

Utility function which collates variables to pass to the template engine.

Returns:

  • (Hash)


185
186
187
# File 'lib/usmu/layout.rb', line 185

def get_variables(variables)
  {site: @configuration}.deep_merge!().deep_merge!(variables)
end

#render(variables = {}) ⇒ String

Renders the file with any templating language required and returns the result

Parameters:

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

    Variables to be used in the template.

Returns:

  • (String)

    The rendered file.



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/usmu/layout.rb', line 67

def render(variables = {})
  content = template_class.new("#{@name}.#{@type}", 1, @configuration[provider_name]) { @content }.
      render(nil, get_variables(variables))
  has_cr = content.index("\r")
  content += (has_cr ? "\r\n" : "\n") if content[-1] != "\n"
  if @parent.nil?
    content
  else
    @parent.render({'content' => content})
  end
end