Module: Henshin

Defined in:
lib/henshin.rb,
lib/henshin/gen.rb,
lib/henshin/post.rb,
lib/henshin/site.rb,
lib/henshin/tags.rb,
lib/henshin/plugin.rb,
lib/henshin/static.rb,
lib/henshin/archive.rb,
lib/henshin/categories.rb

Defined Under Namespace

Classes: Archive, Category, Gen, Generator, LayoutParser, Post, Site, StandardPlugin, Static, Tag

Constant Summary collapse

Defaults =

Default options for configuration

{:title => 'A site',
:description => 'No description',
:time_zone => 'GMT',
:author => '',
:layout => '',
:file_name => '<{category}/>{title-with-dashes}.{extension}',
:permalink => '/{year}/{month}/{date}/{title}.html',
:plugins => ['maruku', 'liquid'],
:root => '.',
:target => '_site',
:plugin_options => {},
:exclude => [] }

Class Method Summary collapse

Class Method Details

.configure(override = {}) ⇒ Hash

Creates the configuration hash by merging defaults, supplied options and options read from the ‘options.yaml’ file. Then loads the plugins and sorts them

Parameters:

  • override (Hash) (defaults to: {})

    to override other set options

Returns:

  • (Hash)

    the merged configuration hash



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/henshin.rb', line 46

def self.configure( override={} )  
  config_file = File.join((override[:root] || Defaults[:root]), '/options.yaml')
  
  begin
    config = YAML.load_file(config_file).to_options
    settings = Defaults.merge(config).merge(override)
  rescue => e
    $stderr.puts "\nCould not read configuration, falling back to defaults..."
    $stderr.puts "-> #{e.to_s}"
    settings = Defaults.merge(override)
  end
  
  settings[:exclude] << '/_site' << '/plugins'
  settings[:plugins] = Henshin.sort_plugins( Henshin.load_plugins(settings) )
  settings
end

.load_plugins(opts) ⇒ Array

Loads the plugins, each plugin then calls Henshin.register!, and then we loop through the options and pass the options for the plugin to it.

Parameters:

  • settings (Hash)

    of loaded so far

Returns:

  • (Array)

    list of loaded plugin instances



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/henshin.rb', line 87

def self.load_plugins(opts)  
  plugins = []
  opts[:plugins].each do |l|
    begin
      require 'henshin/plugins/' + l
    rescue LoadError
      require File.join(opts[:root], 'plugins/', l)
    end
  end
  
  @registered_plugins.each do |plugin|
    if plugin[:opts] && opts[plugin[:opts]]
      opts[plugin[:opts]].each do |k, v|
        if k.to_s.include? 'dir'
          opts[plugin[:opts]][k] = File.join(opts[:root], v)
        end
      end
      plugin[:plugin].configure opts[plugin[:opts]]
    end
    plugins << plugin[:plugin]
  end
  
  plugins
end

.register!(plug, opts = nil) ⇒ Array

Each plugin will call this method when loaded from #load_plugins, these plugins then populate @registered_plugins, which is returned from #load_plugins.

Parameters:

  • plugin (Class)

    to load

  • options (Symbol)

    symbol to look up in settings hash

Returns:

  • (Array)

    plugins and options symbol in hashes



118
119
120
121
# File 'lib/henshin.rb', line 118

def self.register!( plug, opts=nil )
  @registered_plugins ||= []
  @registered_plugins << {:plugin => plug.new, :opts => opts}
end

.sort_plugins(plugins) ⇒ Hash

Organises the plugins into generators and layout parses, then turns the generators into a hash with a key for each extension.

Parameters:

  • plugins (Array)

Returns:



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/henshin.rb', line 68

def self.sort_plugins(plugins)
  r = {:generators => {}, :layout_parsers => []}
  plugins.each do |plugin|
    if plugin.is_a? Generator
      plugin.extensions[:input].each do |ext|
        r[:generators][ext] = plugin
      end
    elsif plugin.is_a? LayoutParser
      r[:layout_parsers] << plugin
    end
  end
  r
end

.versionString

Returns current version.

Returns:

  • (String)

    current version



125
126
127
# File 'lib/henshin.rb', line 125

def self.version
  File.read( File.join(File.dirname(__FILE__), '..', 'VERSION') )
end