Class: Moft::Configuration

Inherits:
Hash
  • Object
show all
Defined in:
lib/moft/configuration.rb

Constant Summary collapse

DEFAULTS =

Default options. Overriden by values in _config.yml. Strings rather than symbols are used for compatability with YAML.

{
  'source'        => Dir.pwd,
  'destination'   => File.join(Dir.pwd, '_site'),
  'plugins'       => '_plugins',
  'layouts'       => '_layouts',
  'keep_files'    => ['.git','.svn'],

  'timezone'      => nil,           # use the local timezone

  'safe'          => false,
  'show_drafts'   => nil,
  'limit_posts'   => 0,
  'lsi'           => false,
  'future'        => true,           # remove and make true just default
  'pygments'      => true,           # remove and make true just default

  'markdown'      => 'maruku',
  'permalink'     => 'date',
  'baseurl'       => '/',
  'include'       => ['.htaccess'],
  'static_includes' => [],
  'exclude'       => [],
  'paginate_path' => 'page:num',

  'markdown_ext'  => 'markdown,mkd,mkdn,md',
  'textile_ext'   => 'textile',

  'port'          => '4000',
  'host'          => '0.0.0.0',

  'excerpt_separator' => "\n\n",

  'maruku' => {
    'use_tex'    => false,
    'use_divs'   => false,
    'png_engine' => 'blahtex',
    'png_dir'    => 'images/latex',
    'png_url'    => '/images/latex'
  },

  'rdiscount' => {
    'extensions' => []
  },

  'redcarpet' => {
    'extensions' => []
  },

  'kramdown' => {
    'auto_ids'      => true,
    'footnote_nr'   => 1,
    'entity_output' => 'as_char',
    'toc_levels'    => '1..6',
    'smart_quotes'  => 'lsquo,rsquo,ldquo,rdquo',
    'use_coderay'   => false,

    'coderay' => {
      'coderay_wrap'              => 'div',
      'coderay_line_numbers'      => 'inline',
      'coderay_line_number_start' => 1,
      'coderay_tab_width'         => 4,
      'coderay_bold_every'        => 10,
      'coderay_css'               => 'style'
    }
  },

  'redcloth' => {
    'hard_breaks' => true
  }
}

Instance Method Summary collapse

Methods inherited from Hash

#deep_merge, #pluralized_array

Instance Method Details

#backwards_compatibilizeObject

Public: Ensure the proper options are set in the configuration to allow for backwards-compatibility with Moft pre-1.0

Returns the backwards-compatible configuration



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/moft/configuration.rb', line 150

def backwards_compatibilize
  config = clone
  # Provide backwards-compatibility
  if config.has_key?('auto') || config.has_key?('watch')
    Moft::Logger.warn "Deprecation:", "Auto-regeneration can no longer" +
                        " be set from your configuration file(s). Use the"+
                        " --watch/-w command-line option instead."
    config.delete('auto')
    config.delete('watch')
  end

  if config.has_key? 'server'
    Moft::Logger.warn "Deprecation:", "The 'server' configuration option" +
                        " is no longer accepted. Use the 'moft serve'" +
                        " subcommand to serve your site with WEBrick."
    config.delete('server')
  end

  config
end

#config_files(override) ⇒ Object

Public: Generate list of configuration files from the override

override - the command-line options hash

Returns an Array of config files



100
101
102
103
104
105
106
# File 'lib/moft/configuration.rb', line 100

def config_files(override)
  # Get configuration from <source>/_config.yml or <source>/<config_file>
  config_files = override.delete('config')
  config_files = File.join(source(override), "_config.yml") if config_files.to_s.empty?
  config_files = [config_files] unless config_files.is_a? Array
  config_files
end

#read_config_file(file) ⇒ Object

Public: Read configuration and return merged Hash

file - the path to the YAML file to be read in

Returns this configuration, overridden by the values in the file



113
114
115
116
117
118
# File 'lib/moft/configuration.rb', line 113

def read_config_file(file)
  next_config = YAML.safe_load_file(file)
  raise "Configuration file: (INVALID) #{file}".yellow if !next_config.is_a?(Hash)
  Moft::Logger.info "Configuration file:", file
  next_config
end

#read_config_files(files) ⇒ Object

Public: Read in a list of configuration files and merge with this hash

files - the list of configuration file paths

Returns the full configuration, with the defaults overridden by the values in the configuration files



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/moft/configuration.rb', line 126

def read_config_files(files)
  configuration = clone

  begin
    files.each do |config_file|
      new_config = read_config_file(config_file)
      configuration = configuration.deep_merge(new_config)
    end
  rescue SystemCallError
    # Errno:ENOENT = file not found
    Moft::Logger.warn "Configuration file:", "none"
  rescue => err
    Moft::Logger.warn "WARNING:", "Error reading configuration. " +
                 "Using defaults (and options)."
    $stderr.puts "#{err}"
  end

  configuration.backwards_compatibilize
end

#source(override) ⇒ Object

Public: Directory of the Moft source folder

override - the command-line options hash

Returns the path to the Moft source directory



91
92
93
# File 'lib/moft/configuration.rb', line 91

def source(override)
  override['source'] || self['source'] || DEFAULTS['source']
end

#stringify_keysObject

Public: Turn all keys into string

Return a copy of the hash where all its keys are strings



82
83
84
# File 'lib/moft/configuration.rb', line 82

def stringify_keys
  reduce({}) { |hsh,(k,v)| hsh.merge(k.to_s => v) }
end