class ConrefFS

Constants

DEFAULT_DATA_DIR

Public Class Methods

apply_attributes(config, item, rep) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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`

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Public Instance Methods

up() click to toggle source

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

# 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