Module: NanocConrefFS::Conrefifier

Defined in:
lib/nanoc-conref-fs/conrefifier.rb

Constant Summary collapse

SINGLE_SUB =
/(\{\{[^\}]+\}\})/m
BLOCK_SUB =
/\{% (?:if|unless).+? %\}.*?\{% end(?:if|unless) %\}/m

Class Method Summary collapse

Class Method Details

.apply_liquid(content, data_vars) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/nanoc-conref-fs/conrefifier.rb', line 63

def self.apply_liquid(content, data_vars)
  data_vars['page'] = data_vars[:page].stringify_keys
  result = Liquid::Template.parse(content, error_mode: :warn).render(data_vars)
  # This second pass renders any previously inserted
  # data conditionals within the body. If a Liquid parse
  # returns a blank string, we'll return the original
  if result =~ NanocConrefFS::Conrefifier::SINGLE_SUB
    result = result.gsub(NanocConrefFS::Conrefifier::SINGLE_SUB) do |match|
      liquified = NanocConrefFS::Conrefifier.apply_liquid(match, data_vars)
      liquified.empty? ? match : liquified
    end
  end
  result
end

.file_variables(variables, path, rep) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/nanoc-conref-fs/conrefifier.rb', line 8

def self.file_variables(variables, path, rep)
  return {} if variables.nil?

  data_vars = {}

  scopes = variables.select do |v|
    scope_block = v[:scope]
    scoped_path = scope_block[:path].empty? || Regexp.new(scope_block[:path]) =~ path
    scoped_rep  = scope_block[:reps].nil? || scope_block[:reps].include?(rep)
    scoped_path && scoped_rep
  end

  # I benchmarked that assignment is much
  # faster than merging an empty hash
  if scopes.length == 1
    data_vars = scopes.first[:values]
  else
    scopes.each do |scope|
      data_vars = data_vars.merge(scope[:values])
    end
  end

  data_vars
end

.liquify(config, path:, content:, rep:) ⇒ Object



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

def self.liquify(config, path:, content:, rep:)
  page_vars = NanocConrefFS::Conrefifier.file_variables(config[:page_variables], path, rep)
  page_vars = { page: page_vars }.merge(NanocConrefFS::Variables.variables[rep])

  # we must obfuscate essential ExtendedMarkdownFilter content
  content = content.gsub(/\{\{\s*#(\S+)\s*\}\}/, '[[#\1]]')
  content = content.gsub(/\{\{\s*\/(\S+)\s*\}\}/, '[[/\1]]')
  content = content.gsub(/\{\{\s*(octicon-\S+\s*[^\}]+)\s*\}\}/, '[[\1]]')

  begin
    result = content

    # This pass replaces any matched conditionals
    if result =~ NanocConrefFS::Conrefifier::BLOCK_SUB || result =~ NanocConrefFS::Conrefifier::SINGLE_SUB
      result = NanocConrefFS::Conrefifier.apply_liquid(result, page_vars)
    end
  rescue Liquid::SyntaxError => e
    # unrecognized Liquid, so just return the content
    STDERR.puts "Could not convert #{result}: #{e.message}"
  rescue => e
    raise "#{e.message}: #{e.inspect}"
  end

  result = result.gsub(/\[\[\s*#(\S+)\s*\]\]/, '{{#\1}}')
  result = result.gsub(/\[\[\s*\/(\S+)\s*\]\]/, '{{/\1}}')
  result = result.gsub(/\[\[\s*(octicon-\S+\s*[^\]]+)\s*\]\]/, '{{\1}}')

  result
end