Class: ConrefFS

Inherits:
Nanoc::DataSources::Filesystem
  • Object
show all
Includes:
NanocConrefFS::Ancestry, NanocConrefFS::Variables
Defined in:
lib/nanoc-conref-fs/conref-fs.rb

Constant Summary collapse

DEFAULT_DATA_DIR =
'data'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NanocConrefFS::Ancestry

create_children, create_parents, find_array_children, find_array_parents, find_hash_children, find_hash_parents

Methods included from NanocConrefFS::Variables

data_files, data_files=, fetch_data_file, variables, variables=

Class Method Details

.apply_attributes(config, item, rep) ⇒ Object


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
# File 'lib/nanoc-conref-fs/conref-fs.rb', line 43

def self.apply_attributes(config, item, rep)
  page_vars = NanocConrefFS::Conrefifier.file_variables(config[:page_variables], item[:filename], rep)
  frontmatter_vars = { page: page_vars }.merge(NanocConrefFS::Variables.variables[rep])

  unless page_vars[:data_association].nil?
    association = page_vars[:data_association]
    toc = NanocConrefFS::Variables.fetch_data_file(association, rep)
    item[:parents] = NanocConrefFS::Ancestry::create_parents(toc, item.attributes)
    item[:children] = NanocConrefFS::Ancestry::create_children(toc, item.attributes)
  end

  page_vars.each_pair do |key, value|
    item[key] = value
  end

  item.attributes.each_pair do |key, value|
    if value.is_a?(Array)
      frontmatter = []
      # tried to do this with `map` and it completely erased the attributes
      # array; not sure why
      value.each do |v|
        frontmatter << if v =~ NanocConrefFS::Conrefifier::SINGLE_SUB
                         NanocConrefFS::Conrefifier.apply_liquid(v, frontmatter_vars)
                       else
                         v
                       end
      end
      item[key] = frontmatter
    else
      if value =~ NanocConrefFS::Conrefifier::SINGLE_SUB
        value = NanocConrefFS::Conrefifier.apply_liquid(value, frontmatter_vars)
        item[key] = value
      end
    end
  end
end

.create_ignore_rules(rep, file) ⇒ Object


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/nanoc-conref-fs/conref-fs.rb', line 112

def self.create_ignore_rules(rep, file)
  current_articles = NanocConrefFS::Variables.fetch_data_file(file, rep)
  current_articles = flatten_list(current_articles).flatten
  current_articles = fix_nested_content(current_articles)

  basic_yaml_path = "#{data_dir_name}/#{file.tr!('.', '/')}.yml"
  basic_yaml = NanocConrefFS::Variables.data_files[basic_yaml_path]
  raise "Could not locate #{basic_yaml_path} in the data files." unless basic_yaml
  basic_yaml.gsub!(/\{%.+/, '')
  full_file = YAML.load(basic_yaml)

  full_user_articles = flatten_list(full_file).flatten
  full_user_articles = fix_nested_content(full_user_articles)

  blacklisted_articles = full_user_articles - current_articles
  blacklisted_articles.map do |article|
    "#{article.parameterize}.md"
  end
end

.data_dir_name(config = nil) ⇒ Object

Some of the static class methods below (and elsewhere in the Gem) require access to the `data_dir` value from the config. This need comes about before Nanoc has a chance to properly load up the configuration file, so we're doing a bare-bones load here to gain access to that configuration item.

Parameters:

  • An optional already-loaded config, so that this can be used later in the Nanoc pipeline.

Returns:

  • Custom `data_dir` attribute from the 'conref-fs' data_source

  • Default `data_dir` of 'data' if none is configured in `nanoc.yaml`


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/nanoc-conref-fs/conref-fs.rb', line 93

def self.data_dir_name(config = nil)
  config ||= YAML.load_file('nanoc.yaml')
  config = config.to_h.with_indifferent_access

  # In certain parts of the nanoc pipeline the config is rooted at the
  # data-source already.
  data_dir = config.fetch('data_dir') { nil }
  return data_dir if data_dir

  data_sources = config.fetch('data_sources') { nil }
  return DEFAULT_DATA_DIR unless data_sources

  data_source = data_sources.find { |ds| ds['type'] == 'conref-fs' }

  data_dir = data_source.fetch('data_dir') { nil }
  return DEFAULT_DATA_DIR unless data_dir
  return data_dir
end

.fix_nested_content(articles) ⇒ Object


149
150
151
152
153
154
155
156
157
158
159
# File 'lib/nanoc-conref-fs/conref-fs.rb', line 149

def self.fix_nested_content(articles)
  articles.delete_if do |i|
    if i.is_a? Hash
      articles.concat(i.keys.concat(i.values).flatten)
      true
    else
      false
    end
  end
  articles
end

.flatten_list(arr) ⇒ Object


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/nanoc-conref-fs/conref-fs.rb', line 132

def self.flatten_list(arr)
  result = []
  return result unless arr
  arr.each do |item|
    if item.is_a?(Hash)
      item.each_pair do |key, value|
        result << key
        result.concat(value)
      end
    else
      result << item
    end
  end

  result
end

.load_data_folder(config, rep) ⇒ Object


36
37
38
39
40
41
# File 'lib/nanoc-conref-fs/conref-fs.rb', line 36

def self.load_data_folder(config, rep)
  return unless NanocConrefFS::Variables.variables[rep].nil?
  data_files = NanocConrefFS::Variables.data_files
  data = NanocConrefFS::Datafiles.process(data_files, config, rep)
  NanocConrefFS::Variables.variables[rep] = { 'site' => { 'config' => config, data_dir_name => data } }
end

Instance Method Details

#upObject

Before iterating over the file objects, this method loads the data folder and applies it to an ivar for later usage.


28
29
30
31
32
33
34
# File 'lib/nanoc-conref-fs/conref-fs.rb', line 28

def up
  data_files = NanocConrefFS::Datafiles.collect_data(ConrefFS.data_dir_name(config))
  NanocConrefFS::Variables.data_files = data_files
  NanocConrefFS::Variables.variables = {}
  reps = @config[:reps] || [:default]
  reps.each { |rep| ConrefFS.load_data_folder(@site_config, rep) }
end