Class: Jekyll::Document

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

Constant Summary collapse

FRONT_REGEXP =

remove when on a moderner Jekyll

/\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m

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



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
76
# File 'lib/jekyll-conrefifier.rb', line 36

def read(opts = {})
  if yaml_file?
    @data = SafeYAML.load_file(path)
  else
    begin
      defaults = @site.frontmatter_defaults.all(url, collection.label.to_sym)
      @data = defaults unless defaults.empty?
      @content = File.read(path, merged_file_read_opts(opts))
      if content =~ FRONT_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 = value.gsub(/:/, ':')
          value = value.gsub(/\\"/, '"')
          value.sub(/^<p>/, '').sub(%r{</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|
    next unless 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(%r{</p>$}, '').strip
  end
end