Class: Henshin::Gen

Inherits:
Object
  • Object
show all
Defined in:
lib/henshin/gen.rb

Overview

This is the main class for files which need to be rendered with plugins

Direct Known Subclasses

Post

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, site, to_inject = nil) ⇒ Gen

Creates a new instance of Gen

Parameters:

  • path (Pathname)

    to the file

  • the (Site)

    site the gen belongs to

  • an (Hash)

    optional payload to add when rendered



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

#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.



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

def data
  @data
end

#generatorsObject

Returns the value of attribute generators.



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

def generators
  @generators
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#siteObject

Returns the value of attribute site.



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

def site
  @site
end

#to_injectObject

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_generatorsObject

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_layoutObject

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

#inspectObject



150
151
152
# File 'lib/henshin/gen.rb', line 150

def inspect
  "#<Gen:#{@path}>"
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



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

Returns the permalink of the gen.

Returns:

  • (String)

    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

#readObject

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_fileObject

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

#renderObject

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_hashHash

Turns all of the gens data into a hash.

Returns:



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

#urlString

Returns the pretty url for the gen.

Returns:

  • (String)

    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

#writeObject

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_pathPathname

Returns path to write the file to.

Returns:

  • (Pathname)

    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