Class: ConrefFS

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

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



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

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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/nanoc-conref-fs/conref-fs.rb', line 105

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 = NanocConrefFS::Variables.data_files["data/#{file.tr!('.', '/')}.yml"]
  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

.fix_nested_content(articles) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/nanoc-conref-fs/conref-fs.rb', line 140

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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/nanoc-conref-fs/conref-fs.rb', line 123

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



24
25
26
27
28
29
# File 'lib/nanoc-conref-fs/conref-fs.rb', line 24

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' => data } }
end

Instance Method Details

#data_dir_nameObject



20
21
22
# File 'lib/nanoc-conref-fs/conref-fs.rb', line 20

def data_dir_name
  config.fetch(:data_dir) { |_| 'data' }
end

#parse_with_frontmatter(content_filename) ⇒ Object

There are a lot of problems when trying to parse liquid out of the frontmatter—all of them dealing with collision between the { character in Liquid and its signfigance in YAML. We’ll overload the parse method here to resolve those issues ahead of time.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/nanoc-conref-fs/conref-fs.rb', line 72

def parse_with_frontmatter(content_filename)
  # Read data
  data = read(content_filename)

  # Check presence of metadata section
  if data !~ /\A-{3,5}\s*$/
    return ParseResult.new(content: data, attributes: {}, attributes_data: '')
  end

  # Split data
  pieces = data.split(/^(-{5}|-{3})[ \t]*\r?\n?/, 3)
  if pieces.size < 4
    raise RuntimeError.new(
      "The file '#{content_filename}' appears to start with a metadata section (three or five dashes at the top) but it does not seem to be in the correct format.",
    )
  end

  # N.B. the only change to the original function
  pieces[2].gsub!(/^([^:]+): (\{\{.+)/, '\1: \'\2\'')

  # Parse
  begin
    meta = YAML.load(pieces[2]) || {}
  rescue Exception => e
    raise "Could not parse YAML for #{content_filename}: #{e.message}"
  end
  verify_meta(meta, content_filename)
  content = pieces[4]

  # Done
  ParseResult.new(content: content, attributes: meta, attributes_data: pieces[2])
end

#upObject

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



12
13
14
15
16
17
18
# File 'lib/nanoc-conref-fs/conref-fs.rb', line 12

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