Class: Jekyll::Site

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

Instance Method Summary collapse

Instance Method Details

#apply_vars_to_datafile(contents, matches, path) ⇒ Object

apply the custom scoope plus the rest of the ‘site.data` information



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/jekyll-conrefifier.rb', line 92

def apply_vars_to_datafile(contents, matches, path)
  return contents if matches.empty?

  data_vars = path.nil? ? {} : ConrefifierUtils.data_file_variables(config, path)

  config = { 'page' => data_vars }
  config = { 'site' => { 'data' => self.data, 'config' => self.config } }.merge(config)

  matches.each do |match|
    parsed_content = begin
                      Liquid::Template.parse(match.first).render(config)
                     rescue
                      match.first
                     end
    unless match.first =~ /\{\{/ && parsed_content.empty?
      contents = contents.sub(match.first, parsed_content)
    end
  end

  contents
end

#in_source_dir(*paths) ⇒ Object



40
41
42
43
44
# File 'lib/jekyll-conrefifier.rb', line 40

def in_source_dir(*paths)
  paths.reduce(source) do |base, path|
    Jekyll.sanitized_path(base, path)
  end
end

#read_data_to(dir, data) ⇒ Object

allows us to filter data file contents via conditionals, eg. ‘if page.version == … %`



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
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/jekyll-conrefifier.rb', line 47

def read_data_to(dir, data)
  return unless File.directory?(dir) && (!safe || !File.symlink?(dir))

  entries = Dir.chdir(dir) do
    Dir['*.{yaml,yml,json,csv}'] + Dir['*'].select { |fn| File.directory?(fn) }
  end

  # all of this is copied from the Jekyll source, except...
  entries.each do |entry|
    path = self.in_source_dir(dir, entry)
    next if File.symlink?(path) && safe

    key = sanitize_filename(File.basename(entry, '.*'))
    if File.directory?(path)
      read_data_to(path, data[key] = {})
    else
      case File.extname(path).downcase
      when '.csv'
        data[key] = CSV.read(path, :headers => true).map(&:to_hash)
      else
        # if we hit upon if/unless conditionals, we'll need to pause and render them
        contents = File.read(path)
        if (matches = contents.scan /(\{% (?:if|unless).+? %\}.*?\{% end(?:if|unless) %\})/m)
          unless ConrefifierUtils.data_file_variables(config, path).nil?
            contents = contents.gsub(/\{\{/, '[[')
            contents = apply_vars_to_datafile(contents, matches, path)
            contents = contents.gsub(/\[\[/, '{{')
          end
        end
        data[key] = SafeYAML.load(contents)
      end
    end
  end

  # once we're all done, we need to iterate once more to parse out `{{ }}` blocks.
  # two reasons for this: one, we need to collect every data file before attempting to
  # parse these vars; two, the Liquid parse above obliterates these tags, so we
  # first need to convert them into `[[ }}`, and *then* continue with the parse
  data.each_pair do |datafile, value|
    yaml_dump = YAML::dump value
    data[datafile] = SafeYAML.load transform_liquid_variables(yaml_dump, datafile)
  end
end

#transform_liquid_variables(contents, path = nil) ⇒ Object

allow us to use any variable within Jekyll data files; for example:

renders as “GitHub Glossary” for dotcom, but “GitHub Enterprise Glossary” for Enterprise



117
118
119
120
121
122
123
# File 'lib/jekyll-conrefifier.rb', line 117

def transform_liquid_variables(contents, path=nil)
  if (matches = contents.scan /(\{\{.+?\}\})/)
    contents = apply_vars_to_datafile(contents, matches, path)
  end

  contents
end