jekyll-data 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ef504f55a2b8067637d9c39a015d1931bc46cf49
4
+ data.tar.gz: cc1515e3a63bfc0722308ffb08fa9594f2435279
5
+ SHA512:
6
+ metadata.gz: 0eb3122d58976537b4e4a50ce4b51e96b413a148d95d0e3dfa2f5cf210a0f155affe92d44d312615522fbe432e823211de28f69dff740dff1f2a8dea444b98bf
7
+ data.tar.gz: '05840a5d7b27acb2fffb63b93cf8067a3855e8d947f01c2f7b034e03f3fa3eb5d3d63edd5df97627f2dc4399db31cf312389ba9ca060ee2c9fc21b88d4a37883'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Ashwin Maroli
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,124 @@
1
+ # JekyllData
2
+
3
+ [![Gem Version](https://img.shields.io/gem/v/jekyll-data.svg)](https://rubygems.org/gems/jekyll-data)
4
+
5
+ Introducing a plugin that reads data files within **jekyll theme gems** and adds it to the site's internal data hash.
6
+
7
+ ## Installation
8
+
9
+ Simply add the plugin to your site's Gemfile and config file like every other jekyll plugin gems..
10
+ ```ruby
11
+ # Gemfile
12
+
13
+ group :jekyll_plugins do
14
+ gem "jekyll-data"
15
+ end
16
+ ```
17
+ ```yaml
18
+ # _config.yml
19
+
20
+ gems:
21
+ - jekyll-data
22
+
23
+ ```
24
+ ..and run
25
+ ```
26
+ bundle install
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ As long as the gem has been installed properly, and is included in the Gemfile & the config file, data-files supported by Jekyll and present in the `_data` directory at the root of your theme gem will be read. Their contents will be added to the site's internal data hash, provided, an identical data hash doesn't already exist at the site-source.
32
+
33
+ ### Theme Configuration
34
+
35
+ Jekyll themes (built prior to Jekyll 3.2) usually ship with configuration settings defined in the config file, which are then used within the theme's template files directly under the `site` namespace (e.g. `{{ site.myvariable }}`). This is not possible with theme gems as data-files within gems are not natively read (as of Jekyll 3.3), and hence require end-users to inspect a *demo* or *example* directory to source those files.
36
+ This plugin provides a way to have the said data-files read and be used by the site. This plugin expects to find all data-files within the `_data` directory at the root of the theme gem.
37
+
38
+ Theme specific directives *should* be defined in a file named the same as `theme.name`.
39
+ e.g. if *minima* were to ship a YAML file with such directives, then it would've a **_data/minima.yml** and variables within it would be referenced in the template files using a `theme` namespace like so: `{{ theme.myvariable }}` (which is functionally identical to `{{ site.data.minima.myvariable }}`)
40
+
41
+ ### Regular Data-files
42
+
43
+ Regular data files that may be used to supplement theme templates (e.g. demo placeholders) can be named as desired. Either use a sub-directory to house related data-files or declare all of them as a mapped data block within a single file.
44
+ ```yaml
45
+ # <theme-gem>/_data/apparel.yml
46
+
47
+ shirts:
48
+ - color: white
49
+ size: large
50
+ image: s_w_lg.jpg
51
+ - color: black
52
+ size: large
53
+ image: s_b_lg.jpg
54
+
55
+ jeans:
56
+ - color: khaki
57
+ waist: 34
58
+ image: j_kh_34.jpg
59
+ - color: blue
60
+ waist: 32
61
+ image: j_bu_32.jpg
62
+ ```
63
+ is the same as:
64
+ ```yaml
65
+ # <theme-gem>/_data/apparel/shirts.yml
66
+
67
+ - color: white
68
+ size: large
69
+ image: s_w_lg.jpg
70
+ - color: black
71
+ size: large
72
+ image: s_b_lg.jpg
73
+ ```
74
+ ```yaml
75
+ # <theme-gem>/_data/apparel/jeans.yml
76
+
77
+ - color: khaki
78
+ waist: 34
79
+ image: j_kh_34.jpg
80
+ - color: blue
81
+ waist: 32
82
+ image: j_bu_32.jpg
83
+ ```
84
+
85
+ ### User-overrides
86
+
87
+ To override directives shipped with a theme gem, simply have an identical hash at the site-source.
88
+ e.g.
89
+ ```yaml
90
+ # <site_source_dir>/_data/navigation.yml
91
+
92
+ mainmenu:
93
+ - title: Home
94
+ url: /
95
+ - title: Kitchen Diaries
96
+ url: /kitchen-diaries/
97
+ - title: Tips & Tricks
98
+ url: /tips-n-tricks/
99
+ - title: Health Facts
100
+ url: /health/
101
+ - title: About Me
102
+ url: /about/
103
+ ```
104
+ would have overridden the following:
105
+ ```yaml
106
+ # <theme-gem>/_data/navigation/mainmenu.yml
107
+
108
+ - title: Link Item 1
109
+ url: /link-one/
110
+ - title: Link Item 2
111
+ url: /link-two/
112
+ - title: Link Item 3
113
+ url: /link-three/
114
+ ```
115
+
116
+ ## Contributing
117
+
118
+ Bug reports and pull requests are welcome at the [GitHub Repo](https://github.com/ashmaroli/jekyll-data). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
119
+
120
+
121
+ ## License
122
+
123
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
124
+
@@ -0,0 +1,16 @@
1
+ require "jekyll"
2
+ require "jekyll-data/version"
3
+
4
+ # Plugin inclusions
5
+ require_relative "jekyll/theme_reader"
6
+ require_relative "jekyll/readers/theme_data_reader"
7
+ require_relative "jekyll/drops/themed_site_drop"
8
+
9
+ # Monkey-patches
10
+ require_relative "jekyll/theme"
11
+ require_relative "jekyll/drops/unified_payload_drop"
12
+
13
+ # replace Jekyll::Reader with a subclass Jekyll::ThemeReader
14
+ Jekyll::Hooks.register :site, :after_init do |site|
15
+ site.reader = Jekyll::ThemeReader.new(site)
16
+ end
@@ -0,0 +1,3 @@
1
+ module JekyllData
2
+ VERSION = "0.2.0".freeze
3
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: UTF-8
2
+
3
+ module Jekyll
4
+ module Drops
5
+ class ThemedSiteDrop < SiteDrop
6
+ extend Forwardable
7
+
8
+ mutable false
9
+
10
+ def_delegator :@obj, :site_data, :data
11
+ def_delegators :@obj, :theme
12
+
13
+ private
14
+ def_delegator :@obj, :config, :fallback_data
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: UTF-8
2
+
3
+ module Jekyll
4
+ module Drops
5
+ class UnifiedPayloadDrop < Drop
6
+ mutable true
7
+
8
+ attr_accessor :page, :layout, :content, :paginator
9
+ attr_accessor :highlighter_prefix, :highlighter_suffix
10
+
11
+ def jekyll
12
+ JekyllDrop.global
13
+ end
14
+
15
+ def site
16
+ @site_drop ||= ThemedSiteDrop.new(@obj)
17
+ end
18
+
19
+ def theme
20
+ theme_name = site.theme.name
21
+ site.data[theme_name]
22
+ end
23
+
24
+ private
25
+ def fallback_data
26
+ @fallback_data ||= {}
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,37 @@
1
+ module Jekyll
2
+ class ThemeDataReader < DataReader
3
+ attr_reader :site, :content
4
+ def initialize(site)
5
+ @site = site
6
+ @content = {}
7
+ @entry_filter = EntryFilter.new(site)
8
+ end
9
+
10
+ def read(dir)
11
+ return unless site.theme && site.theme.data_path
12
+ base = site.in_theme_dir(dir)
13
+ read_data_to(base, @content)
14
+ @content
15
+ end
16
+
17
+ def read_data_to(dir, data)
18
+ return unless File.directory?(dir) && !@entry_filter.symlink?(dir)
19
+
20
+ entries = Dir.chdir(dir) do
21
+ Dir["*.{yaml,yml,json,csv}"] + Dir["*"].select { |fn| File.directory?(fn) }
22
+ end
23
+
24
+ entries.each do |entry|
25
+ path = @site.in_theme_dir(dir, entry)
26
+ next if @entry_filter.symlink?(path)
27
+
28
+ if File.directory?(path)
29
+ read_data_to(path, data[sanitize_filename(entry)] = {})
30
+ else
31
+ key = sanitize_filename(File.basename(entry, ".*"))
32
+ data[key] = read_data_file(path)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,66 @@
1
+ module Jekyll
2
+ class Theme
3
+ extend Forwardable
4
+ attr_reader :name
5
+ def_delegator :gemspec, :version, :version
6
+
7
+ def initialize(name)
8
+ @name = name.downcase.strip
9
+ configure_sass
10
+ end
11
+
12
+ def root
13
+ # Must use File.realpath to resolve symlinks created by rbenv
14
+ # Otherwise, Jekyll.sanitized path with prepend the unresolved root
15
+ @root ||= File.realpath(gemspec.full_gem_path)
16
+ rescue Errno::ENOENT, Errno::EACCES, Errno::ELOOP
17
+ nil
18
+ end
19
+
20
+ def includes_path
21
+ path_for "_includes".freeze
22
+ end
23
+
24
+ def layouts_path
25
+ path_for "_layouts".freeze
26
+ end
27
+
28
+ def sass_path
29
+ path_for "_sass".freeze
30
+ end
31
+
32
+ def data_path
33
+ path_for "_data".freeze
34
+ end
35
+
36
+ def assets_path
37
+ path_for "assets".freeze
38
+ end
39
+
40
+ def configure_sass
41
+ return unless sass_path
42
+ require "sass"
43
+ Sass.load_paths << sass_path
44
+ end
45
+
46
+ private
47
+
48
+ def path_for(folder)
49
+ path = realpath_for(folder)
50
+ path if path && File.directory?(path)
51
+ end
52
+
53
+ def realpath_for(folder)
54
+ File.realpath(Jekyll.sanitized_path(root, folder.to_s))
55
+ rescue Errno::ENOENT, Errno::EACCES, Errno::ELOOP
56
+ nil
57
+ end
58
+
59
+ def gemspec
60
+ @gemspec ||= Gem::Specification.find_by_name(name)
61
+ rescue Gem::LoadError
62
+ raise Jekyll::Errors::MissingDependencyException,
63
+ "The #{name} theme could not be found."
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,137 @@
1
+ # encoding: UTF-8
2
+ require "csv"
3
+
4
+ module Jekyll
5
+ class ThemeReader < Reader
6
+ def initialize(site)
7
+ @site = site
8
+ @theme_data_files = Dir[File.join(@site.theme.root,
9
+ site.config["data_dir"], "**", "*.{yaml,yml,json,csv}")]
10
+ end
11
+
12
+ # Read Site data from disk and load it into internal data structures.
13
+ #
14
+ # Returns nothing.
15
+ def read
16
+ @site.layouts = LayoutReader.new(site).read
17
+ read_directories
18
+ sort_files!
19
+ @site.data = DataReader.new(site).read(site.config["data_dir"])
20
+ read_theme_data
21
+ CollectionReader.new(site).read
22
+ ThemeAssetsReader.new(site).read
23
+ end
24
+
25
+ # Read data files within a theme gem and add them to internal data
26
+ #
27
+ # Returns a hash appended with new data
28
+ def read_theme_data
29
+ if site.theme && site.theme.data_path
30
+ #
31
+ # show contents of "<theme>/_data/" dir being read
32
+ debug_theme_reader
33
+ theme_data = ThemeDataReader.new(site).read(site.config["data_dir"])
34
+ @site.data = Utils.deep_merge_hashes(theme_data, @site.data)
35
+ #
36
+ # show site.data hash contents
37
+ debug_theme_data_reader
38
+ end
39
+ end
40
+
41
+
42
+ private
43
+
44
+ def debug_theme_reader
45
+ print_clear_line
46
+ print "Reading:", "Theme Data Files..."
47
+ @theme_data_files.each do |file|
48
+ print_value file
49
+ end
50
+ print_clear_line
51
+ print "Merging:", "Theme Data Hash..."
52
+ end
53
+
54
+ def debug_theme_data_reader
55
+ print_clear_line
56
+ print "Site Data:"
57
+ process_hash @site.data
58
+ print_clear_line
59
+ end
60
+
61
+ def process_hash(hash)
62
+ hash.each do |key, value|
63
+ print_key key
64
+ if value.class == Hash
65
+ process_inner_hash value
66
+ else
67
+ print_value "'#{value}'"
68
+ end
69
+ end
70
+ end
71
+
72
+ def process_inner_hash(hash)
73
+ hash.each do |key, value|
74
+ if value.class == Array
75
+ print_label key
76
+ extract_hashes_and_print value
77
+ print_dashes
78
+ elsif value.class == Hash
79
+ print_subkey_and_value key, value
80
+ else
81
+ print_hash key, value
82
+ end
83
+ end
84
+ end
85
+
86
+ def extract_hashes_and_print(array)
87
+ array.each do |h|
88
+ process_inner_hash h
89
+ end
90
+ end
91
+
92
+ def print_hash(key, value)
93
+ key = key.to_s + ":"
94
+ print key, value
95
+ end
96
+
97
+ def print_key(key)
98
+ @dashes = "------------------------"
99
+ print_value @dashes.to_s.cyan
100
+ print "Data Key:", key.to_s.cyan
101
+ print_value @dashes.to_s.cyan
102
+ end
103
+
104
+ def print_subkey_and_value(key, value)
105
+ print_label key
106
+ print_dashes
107
+ value.each do |subkey, val|
108
+ print_hash subkey, val
109
+ end
110
+ print_dashes
111
+ end
112
+
113
+ def print_value(value)
114
+ if value.class == Array
115
+ extract_hashes_and_print value
116
+ else
117
+ print "", value
118
+ end
119
+ end
120
+
121
+ def print_label(key)
122
+ print "#{key.to_s}:"
123
+ end
124
+
125
+ def print_dashes
126
+ print "", @dashes
127
+ end
128
+
129
+ def print_clear_line
130
+ print ""
131
+ end
132
+
133
+ def print(arg1, arg2 = "")
134
+ Jekyll.logger.debug arg1, arg2
135
+ end
136
+ end
137
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-data
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Ashwin Maroli
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ description:
70
+ email:
71
+ - ashmaroli@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - LICENSE.txt
77
+ - README.md
78
+ - lib/jekyll-data.rb
79
+ - lib/jekyll-data/version.rb
80
+ - lib/jekyll/drops/themed_site_drop.rb
81
+ - lib/jekyll/drops/unified_payload_drop.rb
82
+ - lib/jekyll/readers/theme_data_reader.rb
83
+ - lib/jekyll/theme.rb
84
+ - lib/jekyll/theme_reader.rb
85
+ homepage: https://github.com/ashmaroli/jekyll-data
86
+ licenses:
87
+ - MIT
88
+ metadata:
89
+ allowed_push_host: https://rubygems.org
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.6.7
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: A plugin to read data files in Jekyll Theme Gems
110
+ test_files: []