Class: Jekyll::Site
- Inherits:
-
Object
- Object
- Jekyll::Site
- Defined in:
- lib/jekyll-conrefifier.rb
Instance Method Summary collapse
-
#apply_vars_to_datafile(contents, matches, path) ⇒ Object
apply the custom scope plus the rest of the ‘site.data` information.
- #in_source_dir(*paths) ⇒ Object
- #old_read_collections ⇒ Object
- #read_collections ⇒ Object
-
#read_data_to(dir, data) ⇒ Object
allows us to filter data file contents via conditionals, eg.
-
#transform_liquid_variables(contents, path = nil) ⇒ Object
allow us to use any variable within Jekyll data files; for example: - ‘site.data.conrefs.product_name } Glossary’ renders as “GitHub Glossary” for dotcom, but “GitHub Enterprise Glossary” for Enterprise.
Instance Method Details
#apply_vars_to_datafile(contents, matches, path) ⇒ Object
apply the custom scope plus the rest of the ‘site.data` information
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/jekyll-conrefifier.rb', line 143 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| match = match.is_a?(Array) ? match.first : match parsed_content = begin Liquid::Template.parse(match).render(config) rescue match end unless match =~ /\{\{/ && parsed_content.empty? contents = contents.sub(match, parsed_content) end end contents end |
#in_source_dir(*paths) ⇒ Object
81 82 83 84 85 |
# File 'lib/jekyll-conrefifier.rb', line 81 def in_source_dir(*paths) paths.reduce(source) do |base, path| Jekyll.sanitized_path(base, path) end end |
#old_read_collections ⇒ Object
79 |
# File 'lib/jekyll-conrefifier.rb', line 79 alias_method :old_read_collections, :read_collections |
#read_collections ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/jekyll-conrefifier.rb', line 128 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 == … %`
88 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 |
# File 'lib/jekyll-conrefifier.rb', line 88 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 /(\{% (?: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 end |
#transform_liquid_variables(contents, path = nil) ⇒ Object
allow us to use any variable within Jekyll data files; for example:
-
‘site.data.conrefs.product_name } Glossary’
renders as “GitHub Glossary” for dotcom, but “GitHub Enterprise Glossary” for Enterprise
170 171 172 173 174 175 176 |
# File 'lib/jekyll-conrefifier.rb', line 170 def transform_liquid_variables(contents, path = nil) if (matches = contents.scan /(\{\{.+?\}\})/) contents = apply_vars_to_datafile(contents, matches, path) end contents end |