Class: Jekyll::Waxify::JekyllConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/waxify/jekyll_config.rb

Overview

JekyllConfig: represents _config.yml in text and yaml

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(framework_dir, jekyll_dir, new_config = {}, config_file = "_config.yml") ⇒ JekyllConfig

Returns a new instance of JekyllConfig.



12
13
14
15
16
17
18
19
# File 'lib/jekyll/waxify/jekyll_config.rb', line 12

def initialize(framework_dir, jekyll_dir, new_config = {}, config_file = "_config.yml")
  @framework_dir = framework_dir
  @jekyll_dir = jekyll_dir
  @config_file = config_file
  @text = File.read("#{@jekyll_dir}/#{@config_file}")
  @yaml = YAML.safe_load(@text)
  @new_config = new_config
end

Instance Attribute Details

#jekyll_dirObject (readonly)

Returns the value of attribute jekyll_dir.



10
11
12
# File 'lib/jekyll/waxify/jekyll_config.rb', line 10

def jekyll_dir
  @jekyll_dir
end

#new_configObject (readonly)

Returns the value of attribute new_config.



10
11
12
# File 'lib/jekyll/waxify/jekyll_config.rb', line 10

def new_config
  @new_config
end

#textObject (readonly)

Returns the value of attribute text.



10
11
12
# File 'lib/jekyll/waxify/jekyll_config.rb', line 10

def text
  @text
end

#yamlObject (readonly)

Returns the value of attribute yaml.



10
11
12
# File 'lib/jekyll/waxify/jekyll_config.rb', line 10

def yaml
  @yaml
end

Instance Method Details

#add(to_add) ⇒ Object



30
31
32
# File 'lib/jekyll/waxify/jekyll_config.rb', line 30

def add(to_add)
  @new_config.deep_merge! to_add
end

#add_collection(coll) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/jekyll/waxify/jekyll_config.rb', line 49

def add_collection(coll)
  return unless coll

  # Make coll images dir and metadata csv file
  FileUtils.mkdir_p "#{@jekyll_dir}/_data/raw_images/#{coll}"
  File.open("#{@jekyll_dir}/_data/#{coll}.csv", "w") { |file| file.write("pid,label\n") }

  @yaml["collections"] ||= {}

  return if @yaml.dig("collections", coll)

  # Add coll to new config stanzas
  add(
    { "collections" => {
      coll =>
        {
          "output" => true,
          "layout" => "wax_item",
          "metadata" => { "source" => "#{coll}.csv" },
          "images" => { "source" => "raw_images/#{coll}" }
        }
    } }
  )
end

#add_corsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/jekyll/waxify/jekyll_config.rb', line 34

def add_cors
  return if @yaml.dig("webrick", "header", "Access-Control-Allow-Origin")

  # Add CORS stanza to _config.yml
  add(
    {
      "webrick" => {
        "headers" => {
          "Access-Control-Allow-Origin" => "*"
        }
      }
    }
  )
end

#deploy_frameworkObject



25
26
27
28
# File 'lib/jekyll/waxify/jekyll_config.rb', line 25

def deploy_framework
  # copy framework documents to jekyll
  FileUtils.cp_r @framework_dir, @jekyll_dir
end

#mergeObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/jekyll/waxify/jekyll_config.rb', line 74

def merge
  @new_config["waxified"] = true

  new_yaml = @new_config.to_yaml.sub("---\n", "")
  
  # collection block is terminated by EOF or a new key, i.e. a line 
  # that begins a character other than whitespace or hash

  groups = @text.match /(^collections:.*\n.+\n)(\Z|[^\s\#].*)/m

  if groups.nil?
    # there are no collections in initial yaml, so append new_yaml
    @text += new_yaml
  else
    # merge collections from @new_config into collections
    collections = YAML.safe_load(groups[1])
    collections.deep_merge! @new_config

    @text = groups.pre_match + collections.to_yaml.sub("---\n","") + groups[2]
  end
end

#saveObject



96
97
98
# File 'lib/jekyll/waxify/jekyll_config.rb', line 96

def save
  File.open(@config_file, "w") { |f| f.puts @text }
end

#waxifiedObject



21
22
23
# File 'lib/jekyll/waxify/jekyll_config.rb', line 21

def waxified
  @yaml["waxified"]
end