Class: Jekyll::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-conrefifier.rb

Instance Method Summary collapse

Instance Method Details

#read(opts = {}) ⇒ Object

allow us to use any variable within Jekyll Frontmatter; for example: title: What are site.data.conrefs.product_name } Pages? renders as “GitHub Pages?” for dotcom, but “GitHub Enterprise Pages?” for Enterprise



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
67
68
69
70
71
72
73
74
75
# File 'lib/jekyll-conrefifier.rb', line 35

def read(opts = {})
  if yaml_file?
    @data = SafeYAML.load_file(path)
  else
    begin
      defaults = @site.frontmatter_defaults.all(url, collection.label.to_sym)
      unless defaults.empty?
        @data = defaults
      end
      @content = File.read(path, merged_file_read_opts(opts))
      if content =~ YAML_FRONT_MATTER_REGEXP
        @content = $POSTMATCH
        prev_match = $1
        prev_match = prev_match.gsub(/\{\{.+?\}\}/) do |match|
          data_vars = ConrefifierUtils.setup_config(@site, opts, path)
          value = ConrefifierUtils.convert(match, data_vars)
          value = Jekyll::Renderer.new(@site, self).convert(value)
          value.sub(/^<p>/, '').sub(/<\/p>$/, '').strip
        end

        data_file = SafeYAML.load(prev_match)
        unless data_file.nil?
          @data = Utils.deep_merge_hashes(defaults, data_file)
        end
      end
    rescue SyntaxError => e
      puts "YAML Exception reading #{path}: #{e.message}"
    rescue Exception => e
      puts "Error reading file #{path}: #{e.message}"
    end
  end

  @data.each_pair do |key, value|
    if value =~ /(\{% (?:if|unless).+? %\}.*?\{% end(?:if|unless) %\})/
      data_vars = ConrefifierUtils.setup_config(@site, opts, path)
      value = ConrefifierUtils.convert(value, data_vars)
      value = Jekyll::Renderer.new(@site, self).convert(value)
      @data[key] = value.sub(/^<p>/, '').sub(/<\/p>$/, '').strip
    end
  end
end