Class: Usmu::Layout
- Inherits:
-
StaticFile
- Object
- StaticFile
- Usmu::Layout
- 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
Instance Attribute Summary collapse
-
#content_path ⇒ string
readonly
protected
Returns the base path to the files used by this class.
-
#input_path ⇒ String
readonly
The full path to the file in the source directory.
-
#metadata ⇒ Hash
readonly
Returns the metadata associated with this layout.
-
#output_extension ⇒ String
readonly
The extension to use with the output file.
-
#output_filename ⇒ String
readonly
Returns the filename to use for the output directory with any modifications to the input filename required.
-
#provider_name ⇒ String
readonly
protected
Returns the Tilt template engine's name for this layout.
-
#template_class ⇒ Tilt::Template
readonly
protected
The Tilt template engine for this layout.
-
#type ⇒ String
readonly
The type of file this is.
Attributes inherited from StaticFile
Class Method Summary collapse
-
.find_layout(configuration, name) ⇒ Usmu::Layout
Static method to create a layout for a given configuration by it's name if it exists.
-
.is_valid_file?(folder_type, name) ⇒ Boolean
Tests if a given file is a valid Tilt template based on the filename.
Instance Method Summary collapse
-
#get_variables(variables) ⇒ Hash
private
Utility function which collates variables to pass to the template engine.
-
#initialize(configuration, name, type = nil, content = nil, metadata = nil) ⇒ Layout
constructor
A new instance of Layout.
-
#render(variables = {}) ⇒ String
Renders the file with any templating language required and returns the result.
Constructor Details
#initialize(configuration, name, type = nil, content = nil, metadata = nil) ⇒ Layout
Returns a new instance of Layout.
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? = "#{path}.meta.yml" = if File.exist? YAML.load_file() else {} end end @metadata = @parent = nil @parent = Layout.find_layout(configuration, self.['layout']) end |
Instance Attribute Details
#content_path ⇒ string (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.
176 177 178 |
# File 'lib/usmu/layout.rb', line 176 def content_path @configuration.layouts_path end |
#input_path ⇒ String (readonly)
Returns 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 |
#metadata ⇒ Hash (readonly)
Returns the metadata associated with this layout.
This will include any metadata from parent templates and default metadata
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_extension ⇒ String (readonly)
Returns 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_filename ⇒ String (readonly)
Returns the filename to use for the output directory with any modifications to the input filename required.
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_name ⇒ String (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.
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_class ⇒ Tilt::Template (readonly, protected)
Returns 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 |
#type ⇒ String (readonly)
Returns 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
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.
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.
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
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 |