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, preserve_all: true, preserve_non_vars: false) ⇒ Object

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



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/jekyll-conrefifier.rb', line 148

def apply_vars_to_datafile(contents, matches, path, preserve_all: true, preserve_non_vars: false)
  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|
    match = match.is_a?(Array) ? match.first : match
    safe_match = if preserve_all
                   match.gsub(/\{\{/, '[[\1')
                  elsif preserve_non_vars
                    match.gsub(/\{\{(\s*)(?!\s*(site|page))/, '[[\1')
                  end

    parsed_content = begin
                       parsed = Liquid::Template.parse(safe_match).render(config)
                       parsed.gsub(/\[\[/, '{{\1') if preserve_all || preserve_non_vars
                     rescue StandardError => e
                       puts "Parse error in \n#{matches}: #{e}"
                       match
                     end
    next if parsed_content.nil?
    contents = contents.sub(match, parsed_content)
  end
  contents
end

#in_source_dir(*paths) ⇒ Object



82
83
84
85
86
# File 'lib/jekyll-conrefifier.rb', line 82

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

#old_read_collectionsObject



80
# File 'lib/jekyll-conrefifier.rb', line 80

alias_method :old_read_collections, :read_collections

#read_collectionsObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/jekyll-conrefifier.rb', line 132

def read_collections
  # once we're done reading in the data, 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
  ConrefifierUtils.og_paths.each do |path|
    keys = path.split('/')
    value = keys.inject(data, :fetch)
    yaml_dump = YAML::dump value

    keys[0...-1].inject(data, :fetch)[keys.last] = SafeYAML.load transform_liquid_variables(yaml_dump, path)
  end
  old_read_collections
end

#read_data_to(dir, data) ⇒ Object

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/jekyll-conrefifier.rb', line 89

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

  ConrefifierUtils.og_paths = [] if ConrefifierUtils.og_paths.nil?

  # 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
        src = config['data_source']
        ConrefifierUtils.og_paths << path.slice(dir.index(src) + src.length + 1..-1).sub(/\.[^.]+\z/, '')
        # if we hit upon if/unless conditionals, we'll need to pause and render them
        contents = File.read(path)
        if (matches = contents.scan /(\s*\{% (?:if|unless).+? %\}.*?\{% end(?:if|unless) %\})/m)
          unless ConrefifierUtils.data_file_variables(config, path).nil?
            contents = apply_vars_to_datafile(contents, matches, path, { preserve_all: true } )
          end
        end

        begin
          data[key] = SafeYAML.load(contents)
        rescue StandardError => e
          puts "Load error in \n#{contents}: #{e}"
          raise e
        end
      end
    end
  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



180
181
182
183
184
185
186
# File 'lib/jekyll-conrefifier.rb', line 180

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

  contents
end