Class: Henshin::Gen
- Inherits:
-
Object
- Object
- Henshin::Gen
- Defined in:
- lib/henshin/gen.rb
Overview
This is the main class for all pages, posts, sass, etc, that need to be run through a plugin
Direct Known Subclasses
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#content ⇒ Object
Returns the value of attribute content.
-
#data ⇒ Object
Returns the value of attribute data.
-
#date ⇒ Object
Returns the value of attribute date.
-
#extension ⇒ Object
Returns the value of attribute extension.
-
#layout ⇒ Object
Returns the value of attribute layout.
-
#output ⇒ Object
Returns the value of attribute output.
-
#path ⇒ Object
Returns the value of attribute path.
-
#renderer ⇒ Object
Returns the value of attribute renderer.
-
#site ⇒ Object
Returns the value of attribute site.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
-
#<=>(val) ⇒ Object
Needed to sort the posts by date, newest first.
-
#initialize(path, site, data = nil) ⇒ Gen
constructor
A new instance of Gen.
- #inspect ⇒ Object
-
#override(override) ⇒ Object
Uses the loaded data to override settings.
-
#payload ⇒ Hash
Creates the data to be sent to the layout engine.
-
#permalink ⇒ Object
Returns the permalink for the gen.
-
#process ⇒ Object
Processes the file.
-
#read_yaml ⇒ Object
Reads the files yaml frontmatter and uses it to override some settings, then grabs content.
-
#render ⇒ Object
Renders the files content.
-
#to_hash ⇒ Hash
Turns all of the post data into a hash.
-
#url ⇒ Object
Returns the (pretty) url for the gen.
-
#write ⇒ Object
Writes the file to the correct place.
Constructor Details
#initialize(path, site, data = nil) ⇒ Gen
Returns a new instance of Gen.
9 10 11 12 13 14 15 |
# File 'lib/henshin/gen.rb', line 9 def initialize( path, site, data=nil ) @path = path @site = site @config = site.config @extension = path.extension @data = data end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
7 8 9 |
# File 'lib/henshin/gen.rb', line 7 def config @config end |
#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.
7 8 9 |
# File 'lib/henshin/gen.rb', line 7 def data @data end |
#date ⇒ Object
Returns the value of attribute date.
6 7 8 |
# File 'lib/henshin/gen.rb', line 6 def date @date end |
#extension ⇒ Object
Returns the value of attribute extension.
6 7 8 |
# File 'lib/henshin/gen.rb', line 6 def extension @extension end |
#layout ⇒ Object
Returns the value of attribute layout.
6 7 8 |
# File 'lib/henshin/gen.rb', line 6 def layout @layout end |
#output ⇒ Object
Returns the value of attribute output.
7 8 9 |
# File 'lib/henshin/gen.rb', line 7 def output @output end |
#path ⇒ Object
Returns the value of attribute path.
6 7 8 |
# File 'lib/henshin/gen.rb', line 6 def path @path end |
#renderer ⇒ Object
Returns the value of attribute renderer.
7 8 9 |
# File 'lib/henshin/gen.rb', line 7 def renderer @renderer end |
#site ⇒ Object
Returns the value of attribute site.
7 8 9 |
# File 'lib/henshin/gen.rb', line 7 def site @site end |
#title ⇒ Object
Returns the value of attribute title.
6 7 8 |
# File 'lib/henshin/gen.rb', line 6 def title @title end |
Instance Method Details
#<=>(val) ⇒ Object
Needed to sort the posts by date, newest first
125 126 127 128 129 130 131 132 |
# File 'lib/henshin/gen.rb', line 125 def <=>( val ) s = self.date <=> val.date if s == 0 return self.permalink <=> val.permalink else return -1 * s end end |
#inspect ⇒ Object
134 135 136 |
# File 'lib/henshin/gen.rb', line 134 def inspect "#<Gen:#{@path}>" end |
#override(override) ⇒ Object
Uses the loaded data to override settings
40 41 42 43 44 |
# File 'lib/henshin/gen.rb', line 40 def override( override ) @title = override[:title] if override[:title] @layout = @site.layouts[ override[:layout] ] if override[:layout] @date = Time.parse( override[:date].to_s ) if override[:date] end |
#payload ⇒ Hash
Creates the data to be sent to the layout engine. Adds optional data if available
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/henshin/gen.rb', line 71 def payload hash = { 'yield' => @content, 'gen' => self.to_hash, 'site' => @site.payload['site'], } hash[ @data[:name] ] = @data[:payload] if @data hash end |
#permalink ⇒ Object
Returns the permalink for the gen
110 111 112 |
# File 'lib/henshin/gen.rb', line 110 def permalink @path[config[:root].size..-1] end |
#process ⇒ Object
Processes the file
20 21 22 |
# File 'lib/henshin/gen.rb', line 20 def process self.read_yaml end |
#read_yaml ⇒ Object
Reads the files yaml frontmatter and uses it to override some settings, then grabs content
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/henshin/gen.rb', line 25 def read_yaml file = File.read(self.path) if file =~ /^(---\s*\n.*?\n?^---\s*$\n?)/m override = YAML.load_file(@path). self.override(override) @content = file[$1.size..-1] else @content = file end end |
#render ⇒ Object
Renders the files content
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/henshin/gen.rb', line 49 def render ignore_layout = false if config[:plugins][:generators].has_key? @extension plugin = config[:plugins][:generators][@extension] @content = plugin.generate( @content ) @output = plugin.extensions[:output] ignore_layout = true if plugin.config[:ignore_layouts] end @layout ||= site.layouts[ site.config[:layout] ] unless ignore_layout || @layout.nil? config[:plugins][:layout_parsers].each do |plugin| @content = plugin.generate( @layout, self.payload ) end end end |
#to_hash ⇒ Hash
Turns all of the post data into a hash
85 86 87 88 89 90 91 92 93 |
# File 'lib/henshin/gen.rb', line 85 def to_hash { 'title' => @title, 'permalink' => self.permalink, 'url' => self.url, 'date' => @date, 'content' => @content } end |
#url ⇒ Object
Returns the (pretty) url for the gen
115 116 117 118 119 120 121 |
# File 'lib/henshin/gen.rb', line 115 def url if config[:permalink].include? "/index.html" self.permalink[0..-11] else self.permalink end end |
#write ⇒ Object
Writes the file to the correct place
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/henshin/gen.rb', line 98 def write write_path = File.join( config[:root], config[:target], @path[config[:root].size..-1] ) # change extension if necessary write_path.gsub!(".#{@extension}", ".#{@output}") if @output FileUtils.mkdir_p File.join( write_path.directory ) file = File.new( File.join( write_path ), "w" ) file.puts( @content ) end |