Class: Henshin::Gen
- Inherits:
-
Object
- Object
- Henshin::Gen
- Defined in:
- lib/henshin/gen.rb
Overview
This is the main class for files which need to be rendered with plugins
Direct Known Subclasses
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#data ⇒ Object
Returns the value of attribute data.
-
#generators ⇒ Object
Returns the value of attribute generators.
-
#path ⇒ Object
Returns the value of attribute path.
-
#site ⇒ Object
Returns the value of attribute site.
-
#to_inject ⇒ Object
Returns the value of attribute to_inject.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Sorts gens based on permalink only.
-
#get_generators ⇒ Object
Finds the correct plugins to render this gen and sets output.
-
#get_layout ⇒ Object
Gets the correct layout for the gen, or the default if none exists.
-
#initialize(path, site, to_inject = nil) ⇒ Gen
constructor
Creates a new instance of Gen.
- #inspect ⇒ Object
-
#payload ⇒ Hash
Creates the data to be sent to the layout engine.
-
#permalink ⇒ String
The permalink of the gen.
-
#read ⇒ Object
Reads the file if it exists, if not gets generators and layout, then cleans up @data.
-
#read_file ⇒ Object
Opens the file and reads the yaml frontmatter if any exists, and also gets the contents of the file.
-
#render ⇒ Object
Renders the files content using the generators from #get_generators and all layout parsers.
-
#to_hash ⇒ Hash
Turns all of the gens data into a hash.
-
#url ⇒ String
The pretty url for the gen.
-
#write ⇒ Object
Writes the file to the correct place.
-
#write_path ⇒ Pathname
Path to write the file to.
Constructor Details
#initialize(path, site, to_inject = nil) ⇒ Gen
Creates a new instance of Gen
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/henshin/gen.rb', line 13 def initialize(path, site, to_inject=nil) @path = path @site = site @data = {} @content = '' @to_inject = to_inject @generators = [] @data['input'] = @path.extension end |
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
6 7 8 |
# File 'lib/henshin/gen.rb', line 6 def content @content end |
#data ⇒ Object
Returns the value of attribute data.
6 7 8 |
# File 'lib/henshin/gen.rb', line 6 def data @data end |
#generators ⇒ Object
Returns the value of attribute generators.
6 7 8 |
# File 'lib/henshin/gen.rb', line 6 def generators @generators end |
#path ⇒ Object
Returns the value of attribute path.
6 7 8 |
# File 'lib/henshin/gen.rb', line 6 def path @path end |
#site ⇒ Object
Returns the value of attribute site.
6 7 8 |
# File 'lib/henshin/gen.rb', line 6 def site @site end |
#to_inject ⇒ Object
Returns the value of attribute to_inject.
6 7 8 |
# File 'lib/henshin/gen.rb', line 6 def to_inject @to_inject end |
Instance Method Details
#<=>(other) ⇒ Object
Sorts gens based on permalink only
146 147 148 |
# File 'lib/henshin/gen.rb', line 146 def <=>( other ) self.permalink <=> other.permalink end |
#get_generators ⇒ Object
Finds the correct plugins to render this gen and sets output
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/henshin/gen.rb', line 52 def get_generators @site.plugins[:generators].each do |k, v| if k == @data['input'] || k == '*' @generators << v @data['output'] ||= v.extensions[:output] @data['ignore_layout'] ||= (v.config['ignore_layouts'] ? true : false) end end @generators.sort! end |
#get_layout ⇒ Object
Gets the correct layout for the gen, or the default if none exists. It gets the default layout from options.yaml or looks for one called ‘main’ or ‘default’.
66 67 68 69 70 71 72 73 |
# File 'lib/henshin/gen.rb', line 66 def get_layout if @data['layout'] @data['layout'] = site.layouts[ @data['layout'] ] else # get default layout @data['layout'] = site.layouts[(site.config['layout'] || 'main' || 'default')] end end |
#inspect ⇒ Object
150 151 152 |
# File 'lib/henshin/gen.rb', line 150 def inspect "#<Gen:#{@path}>" end |
#payload ⇒ Hash
Creates the data to be sent to the layout engine. Adds optional data if available.
95 96 97 98 99 100 101 102 103 |
# File 'lib/henshin/gen.rb', line 95 def payload hash = { 'yield' => @content, 'gen' => self.to_hash, 'site' => @site.payload['site'] } hash[ @to_inject[:name] ] = @to_inject[:payload] if @to_inject hash end |
#permalink ⇒ String
Returns the permalink of the gen.
125 126 127 128 129 |
# File 'lib/henshin/gen.rb', line 125 def permalink rel = @path.relative_path_from(@site.root).to_s rel.gsub!(".#{@data['input']}", ".#{@data['output']}") File.join(@site.base, rel) end |
#read ⇒ Object
Reads the file if it exists, if not gets generators and layout, then cleans up @data
27 28 29 30 31 32 33 34 35 |
# File 'lib/henshin/gen.rb', line 27 def read self.read_file if @path.exist? self.get_generators self.get_layout # tidy up data @data['output'] ||= @data['input'] self end |
#read_file ⇒ Object
Opens the file and reads the yaml frontmatter if any exists, and also gets the contents of the file.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/henshin/gen.rb', line 39 def read_file file = @path.read if file =~ /^(---\s*\n.*?\n?^---\s*$\n?)/m override = YAML.load_file(@path) @data = @data.merge(override) @content = file[$1.size..-1] else @content = file end end |
#render ⇒ Object
Renders the files content using the generators from #get_generators and all layout parsers. Passed through layout parser twice so that markup in the gen is processed.
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/henshin/gen.rb', line 78 def render @generators.each do |plugin| @content = plugin.generate(@content) end unless @data['ignore_layout'] || @data['layout'].nil? @site.plugins[:layoutors].each do |plugin| @content = plugin.generate(@data['layout'], self.payload) @content = plugin.generate(@content, self.payload) end end end |
#to_hash ⇒ Hash
Turns all of the gens data into a hash.
108 109 110 111 112 113 |
# File 'lib/henshin/gen.rb', line 108 def to_hash @data['content'] = @content @data['url'] = self.url @data['permalink'] = self.permalink @data end |
#url ⇒ String
Returns the pretty url for the gen.
132 133 134 135 136 137 138 |
# File 'lib/henshin/gen.rb', line 132 def url if @site.config['permalink'].include?("/index.html") && @data['output'] == 'html' self.permalink.to_p.dirname.to_s else self.permalink end end |
#write ⇒ Object
Writes the file to the correct place
118 119 120 121 122 |
# File 'lib/henshin/gen.rb', line 118 def write FileUtils.mkdir_p(self.write_path.dirname) file = File.new(self.write_path, "w") file.puts(@content) end |
#write_path ⇒ Pathname
Returns path to write the file to.
141 142 143 |
# File 'lib/henshin/gen.rb', line 141 def write_path @site.target + self.permalink[1..-1] end |