jekyll-conrefifier 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4eb25d8f3d38048ed54156fe184dc31af222fcc5
4
- data.tar.gz: 72174e21d25755198c72de9d19827d57cbd3a4e8
3
+ metadata.gz: 5636a02982502f6cd66e04cf1d53e37d8deb9362
4
+ data.tar.gz: ad4ed60e72f9f9b7ed0a8674c1f4fb0c5ab4ddc1
5
5
  SHA512:
6
- metadata.gz: 940a25e5f7192d994824907a02d3e77740ab4924409834b3034f8449080067dbcb2898751869d3788973e560ae10a53d2a4df85ece34f3a9267adeab4f92079c
7
- data.tar.gz: 59a48157047502a264c83156e95d3febb8ddacfd8290a2f5f599e404398db3e5e806ecbd12d7c05470acf6137920d0cdd1652d41e82084c61e66539cc75d469d
6
+ metadata.gz: 00a8124fb4aec662890b209a2b882ffb7d34c51d495c3673a0e731769c6fb5d9e8d7dd8c05953ff59341bdac52265d1d52735ced44e8bbf7f8a4e761f36f75da
7
+ data.tar.gz: 930fdf8713ef205dda7d1fae12b3c0d712458dc376ca76c603a6d25bc6719e129b137903eeb32fa08470b1e97a4b54e95142d43e200343dc81aa09f4e9011cf9
data/.gitignore CHANGED
@@ -16,3 +16,4 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  spec/fixtures/_site
19
+ .jekyll_metadata
data/.travis.yml CHANGED
@@ -2,5 +2,4 @@ language: ruby
2
2
  rvm:
3
3
  - 2.1
4
4
  - 2.0
5
- - 1.9.3
6
5
  script: "script/cibuild"
@@ -17,7 +17,8 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
20
- spec.add_development_dependency 'jekyll', '~> 2.0'
20
+ spec.add_development_dependency 'jekyll', '>= 2.0'
21
+
21
22
  spec.add_development_dependency 'rake'
22
23
  spec.add_development_dependency 'rspec'
23
24
  end
@@ -1,5 +1,7 @@
1
1
  module Jekyll
2
2
  module ConrefifierUtils
3
+ class << self; attr_accessor :og_paths; end
4
+
3
5
  # fetch the custom scope vars, as defined in _config.yml
4
6
  def self.data_file_variables(config, path)
5
7
  data_vars = {}
@@ -7,35 +9,74 @@ module Jekyll
7
9
  scopes.each do |scope|
8
10
  data_vars = data_vars.merge(scope['values'])
9
11
  end
10
-
11
12
  data_vars
12
13
  end
14
+
15
+ def self.setup_config(site, opts, path)
16
+ data_vars = path.nil? ? {} : ConrefifierUtils.data_file_variables(site.config, opts[:actual_path] || path)
17
+ config = { 'page' => data_vars }
18
+ config = { 'site' => { 'data' => site.data, 'config' => site.config } }.merge(config)
19
+ end
20
+
21
+ def self.convert(content, data_vars)
22
+ value = Liquid::Template.parse(content).render(data_vars)
23
+ # protects against situations where [page.version] prevented a conversion
24
+ if value =~ /\{\{/
25
+ value = Liquid::Template.parse(value).render(data_vars)
26
+ end
27
+ value = value.gsub('"', '\"')
28
+ end
13
29
  end
14
30
 
15
31
  class Document
16
- alias_method :old_read, :read
17
-
18
32
  # allow us to use any variable within Jekyll Frontmatter; for example:
19
33
  # title: What are {{ site.data.conrefs.product_name[site.audience] }} Pages?
20
34
  # renders as "GitHub Pages?" for dotcom, but "GitHub Enterprise Pages?" for Enterprise
21
35
  def read(opts = {})
22
- old_read(opts)
36
+ if yaml_file?
37
+ @data = SafeYAML.load_file(path)
38
+ else
39
+ begin
40
+ defaults = @site.frontmatter_defaults.all(url, collection.label.to_sym)
41
+ unless defaults.empty?
42
+ @data = defaults
43
+ end
44
+ @content = File.read(path, merged_file_read_opts(opts))
45
+ if content =~ YAML_FRONT_MATTER_REGEXP
46
+ @content = $POSTMATCH
47
+ prev_match = $1
48
+ prev_match = prev_match.gsub(/\{\{.+?\}\}/) do |match|
49
+ data_vars = ConrefifierUtils.setup_config(@site, opts, path)
50
+ value = ConrefifierUtils.convert(match, data_vars)
51
+ value = Jekyll::Renderer.new(@site, self).convert(value)
52
+ value.sub(/^<p>/, '').sub(/<\/p>$/, '').strip
53
+ end
54
+
55
+ data_file = SafeYAML.load(prev_match)
56
+ unless data_file.nil?
57
+ @data = Utils.deep_merge_hashes(defaults, data_file)
58
+ end
59
+ end
60
+ rescue SyntaxError => e
61
+ puts "YAML Exception reading #{path}: #{e.message}"
62
+ rescue Exception => e
63
+ puts "Error reading file #{path}: #{e.message}"
64
+ end
65
+ end
66
+
23
67
  @data.each_pair do |key, value|
24
- if value =~ /\{\{.+?\}\}/ || value =~ /(\{% (?:if|unless).+? %\}.*?\{% end(?:if|unless) %\})/
25
- data_vars = path.nil? ? {} : ConrefifierUtils.data_file_variables(@site.config, opts[:actual_path] || path)
26
- config = { 'page' => data_vars }
27
- config = { 'site' => { 'data' => @site.data, 'config' => @site.config } }.merge(config)
28
-
29
- value = Liquid::Template.parse(value).render(config)
30
- @data[key] = Jekyll::Renderer.new(@site, self).convert(value)
31
- @data[key] = @data[key].sub(/^<p>/, '').sub(/<\/p>$/, '').strip
68
+ if value =~ /(\{% (?:if|unless).+? %\}.*?\{% end(?:if|unless) %\})/
69
+ data_vars = ConrefifierUtils.setup_config(@site, opts, path)
70
+ value = ConrefifierUtils.convert(value, data_vars)
71
+ value = Jekyll::Renderer.new(@site, self).convert(value)
72
+ @data[key] = value.sub(/^<p>/, '').sub(/<\/p>$/, '').strip
32
73
  end
33
74
  end
34
75
  end
35
76
  end
36
77
 
37
78
  class Site
38
- alias_method :old_read, :read
79
+ alias_method :old_read_collections, :read_collections
39
80
 
40
81
  def in_source_dir(*paths)
41
82
  paths.reduce(source) do |base, path|
@@ -51,6 +92,8 @@ module Jekyll
51
92
  Dir['*.{yaml,yml,json,csv}'] + Dir['*'].select { |fn| File.directory?(fn) }
52
93
  end
53
94
 
95
+ ConrefifierUtils.og_paths = [] if ConrefifierUtils.og_paths.nil?
96
+
54
97
  # all of this is copied from the Jekyll source, except...
55
98
  entries.each do |entry|
56
99
  path = self.in_source_dir(dir, entry)
@@ -64,6 +107,8 @@ module Jekyll
64
107
  when '.csv'
65
108
  data[key] = CSV.read(path, :headers => true).map(&:to_hash)
66
109
  else
110
+ src = config['data_source']
111
+ ConrefifierUtils.og_paths << path.slice(dir.index(src) + src.length + 1..-1).sub(/\.[^.]+\z/, '')
67
112
  # if we hit upon if/unless conditionals, we'll need to pause and render them
68
113
  contents = File.read(path)
69
114
  if (matches = contents.scan /(\{% (?:if|unless).+? %\}.*?\{% end(?:if|unless) %\})/m)
@@ -73,22 +118,28 @@ module Jekyll
73
118
  contents = contents.gsub(/\[\[/, '{{')
74
119
  end
75
120
  end
121
+
76
122
  data[key] = SafeYAML.load(contents)
77
123
  end
78
124
  end
79
125
  end
126
+ end
80
127
 
81
- # once we're all done, we need to iterate once more to parse out `{{ }}` blocks.
128
+ def read_collections
129
+ # once we're done reading in the data, we need to iterate once more to parse out `{{ }}` blocks.
82
130
  # two reasons for this: one, we need to collect every data file before attempting to
83
131
  # parse these vars; two, the Liquid parse above obliterates these tags, so we
84
132
  # first need to convert them into `[[ }}`, and *then* continue with the parse
85
- data.each_pair do |datafile, value|
133
+ ConrefifierUtils.og_paths.each do |path|
134
+ keys = path.split('/')
135
+ value = keys.inject(data, :fetch)
86
136
  yaml_dump = YAML::dump value
87
- data[datafile] = SafeYAML.load transform_liquid_variables(yaml_dump, datafile)
137
+ keys[0...-1].inject(data, :fetch)[keys.last] = SafeYAML.load transform_liquid_variables(yaml_dump, path)
88
138
  end
139
+ old_read_collections
89
140
  end
90
141
 
91
- # apply the custom scoope plus the rest of the `site.data` information
142
+ # apply the custom scope plus the rest of the `site.data` information
92
143
  def apply_vars_to_datafile(contents, matches, path)
93
144
  return contents if matches.empty?
94
145
 
@@ -98,23 +149,25 @@ module Jekyll
98
149
  config = { 'site' => { 'data' => self.data, 'config' => self.config } }.merge(config)
99
150
 
100
151
  matches.each do |match|
152
+ match = match.is_a?(Array) ? match.first : match
153
+
101
154
  parsed_content = begin
102
- Liquid::Template.parse(match.first).render(config)
155
+ Liquid::Template.parse(match).render(config)
103
156
  rescue
104
- match.first
157
+ match
105
158
  end
106
- unless match.first =~ /\{\{/ && parsed_content.empty?
107
- contents = contents.sub(match.first, parsed_content)
159
+
160
+ unless match =~ /\{\{/ && parsed_content.empty?
161
+ contents = contents.sub(match, parsed_content)
108
162
  end
109
163
  end
110
-
111
164
  contents
112
165
  end
113
166
 
114
167
  # allow us to use any variable within Jekyll data files; for example:
115
168
  # - '{{ site.data.conrefs.product_name[site.audience] }} Glossary'
116
169
  # renders as "GitHub Glossary" for dotcom, but "GitHub Enterprise Glossary" for Enterprise
117
- def transform_liquid_variables(contents, path=nil)
170
+ def transform_liquid_variables(contents, path = nil)
118
171
  if (matches = contents.scan /(\{\{.+?\}\})/)
119
172
  contents = apply_vars_to_datafile(contents, matches, path)
120
173
  end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module JekyllConrefifier
2
- VERSION = '0.4.1'
2
+ VERSION = '0.4.2'
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require "jekyll"
2
2
  require File.expand_path("lib/jekyll-conrefifier.rb")
3
3
 
4
+ Jekyll::Document::YAML_FRONT_MATTER_REGEXP = /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m
5
+
4
6
  RSpec.configure do |config|
5
7
  config.run_all_when_everything_filtered = true
6
8
  config.filter_run :focus
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-conrefifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garen J. Torikian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-10 00:00:00.000000000 Z
11
+ date: 2015-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '2.0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.0'
27
27
  - !ruby/object:Gem::Dependency