Module: Mail2FrontMatter

Defined in:
lib/mail2frontmatter/parser.rb,
lib/mail2frontmatter/writer.rb,
lib/mail2frontmatter/version.rb,
lib/mail2frontmatter/watcher.rb,
lib/mail2frontmatter/committer.rb,
lib/mail2frontmatter/preprocessor.rb,
lib/mail2frontmatter/configuration.rb

Overview

Holds configuration and nothing more

Defined Under Namespace

Classes: Committer, Parser, PreProcessor, Watcher, Writer

Constant Summary collapse

VERSION =
'0.1.2'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/mail2frontmatter/configuration.rb', line 8

def config
  @config
end

.loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/mail2frontmatter/configuration.rb', line 6

def logger
  @logger
end

Class Method Details

.set_config(passed_config = nil, &_block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mail2frontmatter/configuration.rb', line 10

def set_config(passed_config = nil, &_block)
  # load config from file
  if passed_config.is_a?(String)
    @config = YAML.load_file(passed_config).deep_symbolize_keys!

  # load config from file at default data/mail2frontmatter.yml relative from run directory
  elsif passed_config.is_a?(NilClass)

    default_config = File.join(Dir.pwd, 'data', 'mail2frontmatter.yml')

    if File.exist?(default_config)
      @config = YAML.load_file(default_config).deep_symbolize_keys!
    else
      fail LoadError, 'no configuration given or found at ./data/mail2frontmatter.yml'
    end

  elsif passed_config.is_a?(Hash)
    @config = passed_config

  else
    fail ArgumentError, 'not a valid configuration type'
  end

  # setup logger, use provided location
  if @config[:log_file]
    @logger = Logger.new(@config[:log_file])

  # or a sensible default if non was provided
  elsif Dir.exist?(File.join(Dir.pwd, 'log'))
    @logger = Logger.new(File.join(Dir.pwd, 'log', 'mail2frontmatter.log'))

  # finally fallback onto STDOUT
  else
    @logger = Logger.new(STDOUT)
  end

  @config[:git] ||= {
    path:  Dir.pwd
  }

  # set default for data directory unless already specified
  # the data directory is where the posts will be written to
  unless @config[:data_directory]
    # TODO
    # if middleman?
    @config[:data_directory] = File.join(Dir.pwd, 'source', 'blog')
    # elsif jekyll?
    # else
    # end
  end

  # set default for media directory unless already specified
  # the media directory is where the attachments will be saved to
  unless @config[:media_directory]
    # TODO
    # if middleman?
    @config[:media_directory] = File.join(Dir.pwd, 'source')
    # elsif jekyll?
    # else
    # end
  end

  # now we yield the config object in case the user would like to overwrite any defaults
  yield(@config) if block_given?

  # finally, if pre-processors were specified, we can try requiring them
  preprocessors = @config[:preprocessors] || []

  preprocessors.each do |processor|
    begin
      require "mail2frontmatter/#{processor[:key]}"
    rescue LoadError => e
      puts "could not require specified preprocessor '#{processor[:key]}.rb', no such file in load path. Check your configuration and try again \n\n"
      raise e
    end

    "Mail2FrontMatter::#{processor[:key].underscore.camelize}".constantize.register(processor[:options] || {})
  end
end