Class: Pandocomatic::Template
- Inherits:
-
Object
- Object
- Pandocomatic::Template
- Defined in:
- lib/pandocomatic/template.rb
Overview
A pandocomatic template
Constant Summary collapse
- EXTENDS =
The name of the ‘extends’ section in a template
'extends'- GLOB =
The name of the ‘glob’ section in a template
'glob'- SETUP =
The name of the ‘setup’ section in a template
'setup'- PREPROCESSORS =
The name of the ‘preprocessors’ section in a template
'preprocessors'- METADATA =
The name of the ‘metadata’ section in a template
'metadata'- PANDOC =
The name of the ‘pandoc’ section in a template
'pandoc'- POSTPROCESSORS =
The name of the ‘postprocessors’ section in a template
'postprocessors'- CLEANUP =
The name of the ‘cleanup’ section in a template
'cleanup'- SECTIONS =
List of the sections a template can contain
[EXTENDS, GLOB, SETUP, PREPROCESSORS, METADATA, PANDOC, POSTPROCESSORS, CLEANUP].freeze
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
-
.clone(template) ⇒ Template
Deep copy template.
-
.extend_value(current, parent) ⇒ Object
Extend the current value with the parent value.
Instance Method Summary collapse
-
#extends ⇒ Array<String>
List of template names that this template extends.
-
#external? ⇒ Bool
Is this an external template?.
-
#initialize(name, template_hash = {}, path = nil) ⇒ Template
constructor
Create a new template based on a template hash.
-
#internal? ⇒ Bool
Is this an internal template?.
-
#merge!(other) ⇒ Object
Merge another template into this one.
-
#to_h ⇒ Hash
Create Hash representation of this template.
Constructor Details
#initialize(name, template_hash = {}, path = nil) ⇒ Template
Create a new template based on a template hash
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/pandocomatic/template.rb', line 69 def initialize(name, template_hash = {}, path = nil) @name = name @path = path @data = { EXTENDS => [], GLOB => [], SETUP => [], PREPROCESSORS => [], METADATA => {}, PANDOC => {}, POSTPROCESSORS => [], CLEANUP => [] } @data.merge! template_hash end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
24 25 26 |
# File 'lib/pandocomatic/template.rb', line 24 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
24 25 26 |
# File 'lib/pandocomatic/template.rb', line 24 def path @path end |
Class Method Details
.clone(template) ⇒ Template
Deep copy template
91 92 93 |
# File 'lib/pandocomatic/template.rb', line 91 def self.clone(template) Template.new(template.name, Marshal.load(Marshal.dump(template.to_h)), template.path) end |
.extend_value(current, parent) ⇒ Object
Extend the current value with the parent value. Depending on the value and type of the current and parent values, the extension differs.
For simple values, the current value takes precedence over the parent value
For Hash values, each parent value’s property is extended as well
For Arrays, the current overwrites and adds to parent value’s items unless the current value is a Hash with a ‘remove’ and ‘add’ property. Then the ‘add’ items are added to the parent value and the ‘remove’ items are removed from the parent value.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/pandocomatic/template.rb', line 163 def self.extend_value(current, parent) if parent.nil? # If no parent value is specified, the current takes # precedence current elsif current.nil? nil # Current nil removes value of parent; follows YAML spec. # Note. take care to actually remove this value from a # Hash. (Like it is done in the next case) else case parent when Hash if current.is_a? Hash # Mixin current and parent values parent.each_pair do |property, value| if current.key? property extended_value = extend_value(current[property], value) if extended_value.nil? current.delete property else current[property] = extended_value end else current[property] = value end end end current when Array case current when Hash if current.key? 'remove' to_remove = current['remove'] if to_remove.is_a? Array parent.delete_if { |v| current['remove'].include? v } else parent.delete to_remove end end if current.key? 'add' to_add = current['add'] if to_add.is_a? Array parent = current['add'].concat(parent).uniq else parent.push(to_add).uniq end end parent when Array # Just combine parent and current arrays, current # values take precedence current.concat(parent).uniq else # Unknown what to do, assuming current should take # precedence current end else # Simple values: current replaces parent current end end end |
Instance Method Details
#extends ⇒ Array<String>
List of template names that this template extends.
112 113 114 115 116 117 118 |
# File 'lib/pandocomatic/template.rb', line 112 def extends # Overwriting automatically generated method with more specific # behavior to_extend = section(EXTENDS) to_extend = [to_extend] if to_extend.is_a? String to_extend end |
#external? ⇒ Bool
Is this an external template?
105 106 107 |
# File 'lib/pandocomatic/template.rb', line 105 def external? !internal? end |
#internal? ⇒ Bool
Is this an internal template?
98 99 100 |
# File 'lib/pandocomatic/template.rb', line 98 def internal? @path.nil? end |
#merge!(other) ⇒ Object
Merge another template into this one.
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/pandocomatic/template.rb', line 123 def merge!(other) SECTIONS.each do |section_name| current_section = section(section_name) other_section = other.send section_name extended_section = Template.extend_value other_section, current_section if extended_section.nil? @data.delete section_name else @data[section_name] = extended_section end end end |
#to_h ⇒ Hash
Create Hash representation of this template
140 141 142 |
# File 'lib/pandocomatic/template.rb', line 140 def to_h @data end |