Class: Henshin::Gen

Inherits:
Object
  • Object
show all
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

Post

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/henshin/gen.rb', line 7

def config
  @config
end

#contentObject

Returns the value of attribute content.



6
7
8
# File 'lib/henshin/gen.rb', line 6

def content
  @content
end

#dataObject

Returns the value of attribute data.



7
8
9
# File 'lib/henshin/gen.rb', line 7

def data
  @data
end

#dateObject

Returns the value of attribute date.



6
7
8
# File 'lib/henshin/gen.rb', line 6

def date
  @date
end

#extensionObject

Returns the value of attribute extension.



6
7
8
# File 'lib/henshin/gen.rb', line 6

def extension
  @extension
end

#layoutObject

Returns the value of attribute layout.



6
7
8
# File 'lib/henshin/gen.rb', line 6

def layout
  @layout
end

#outputObject

Returns the value of attribute output.



7
8
9
# File 'lib/henshin/gen.rb', line 7

def output
  @output
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/henshin/gen.rb', line 6

def path
  @path
end

#rendererObject

Returns the value of attribute renderer.



7
8
9
# File 'lib/henshin/gen.rb', line 7

def renderer
  @renderer
end

#siteObject

Returns the value of attribute site.



7
8
9
# File 'lib/henshin/gen.rb', line 7

def site
  @site
end

#titleObject

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

#inspectObject



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

Parameters:

  • override (Hash)

    data to override settings with



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

#payloadHash

Creates the data to be sent to the layout engine. Adds optional data if available

Returns:

  • (Hash)

    the payload for the layout engine



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

Returns the permalink for the gen



110
111
112
# File 'lib/henshin/gen.rb', line 110

def permalink
  @path[config[:root].size..-1]
end

#processObject

Processes the file



20
21
22
# File 'lib/henshin/gen.rb', line 20

def process
  self.read_yaml
end

#read_yamlObject

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).to_options
    self.override(override)
    @content = file[$1.size..-1]
  else
    @content = file
  end  
end

#renderObject

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_hashHash

Turns all of the post data into a hash

Returns:



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

#urlObject

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

#writeObject

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