Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ashmaroli committed Oct 16, 2016
0 parents commit 97a8d51
Show file tree
Hide file tree
Showing 12 changed files with 356 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.gem
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gemspec
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Ashwin Maroli

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# JekyllData


## Installation


## Usage


## Development


## Contributing

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.


## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).

36 changes: 36 additions & 0 deletions jekyll-data.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'jekyll-data/version'

Gem::Specification.new do |spec|
spec.name = "jekyll-data"
spec.version = JekyllData::VERSION
spec.authors = ["Ashwin Maroli"]
spec.email = ["[email protected]"]

spec.summary = %q{A plugin to read data files in Jekyll Theme Gems}
spec.homepage = "https://github.com/ashmaroli/jekyll-data"
spec.license = "MIT"

# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
# to allow pushing to a single host or delete this section to allow pushing to any host.
if spec.respond_to?(:metadata)
spec.metadata['allowed_push_host'] = "https://rubygems.org"
else
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
end

spec.files = `git ls-files -z`.split("\x0").select do |f|
f.match(%r{^(lib/|(LICENSE|README)((\.(txt|md|markdown)|$)))}i)
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "jekyll", "~> 3.3"

spec.add_development_dependency "bundler", "~> 1.12"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "minitest", "~> 5.0"
end
16 changes: 16 additions & 0 deletions lib/jekyll-data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require "jekyll"
require "jekyll-data/version"

# Plugin inclusions
require_relative "jekyll/theme_reader"
require_relative "jekyll/readers/theme_data_reader"
require_relative "jekyll/drops/themed_site_drop"

# Monkey-patches
require_relative "jekyll/theme"
require_relative "jekyll/drops/unified_payload_drop"

# replace Jekyll::Reader with a subclass Jekyll::ThemeReader
Jekyll::Hooks.register :site, :after_init do |site|
site.reader = Jekyll::ThemeReader.new(site)
end
3 changes: 3 additions & 0 deletions lib/jekyll-data/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module JekyllData
VERSION = "0.1.0".freeze
end
17 changes: 17 additions & 0 deletions lib/jekyll/drops/themed_site_drop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# encoding: UTF-8

module Jekyll
module Drops
class ThemedSiteDrop < SiteDrop
extend Forwardable

mutable false

def_delegator :@obj, :site_data, :data
def_delegators :@obj, :theme

private
def_delegator :@obj, :config, :fallback_data
end
end
end
30 changes: 30 additions & 0 deletions lib/jekyll/drops/unified_payload_drop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# encoding: UTF-8

module Jekyll
module Drops
class UnifiedPayloadDrop < Drop
mutable true

attr_accessor :page, :layout, :content, :paginator
attr_accessor :highlighter_prefix, :highlighter_suffix

def jekyll
JekyllDrop.global
end

def site
@site_drop ||= ThemedSiteDrop.new(@obj)
end

def theme
theme_name = site.theme.name
site.data[theme_name]
end

private
def fallback_data
@fallback_data ||= {}
end
end
end
end
37 changes: 37 additions & 0 deletions lib/jekyll/readers/theme_data_reader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Jekyll
class ThemeDataReader < DataReader
attr_reader :site, :content
def initialize(site)
@site = site
@content = {}
@entry_filter = EntryFilter.new(site)
end

def read(dir)
return unless site.theme && site.theme.data_path
base = site.in_theme_dir(dir)
read_data_to(base, @content)
@content
end

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

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

entries.each do |entry|
path = @site.in_theme_dir(dir, entry)
next if @entry_filter.symlink?(path)

if File.directory?(path)
read_data_to(path, data[sanitize_filename(entry)] = {})
else
key = sanitize_filename(File.basename(entry, ".*"))
data[key] = read_data_file(path)
end
end
end
end
end
66 changes: 66 additions & 0 deletions lib/jekyll/theme.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
module Jekyll
class Theme
extend Forwardable
attr_reader :name
def_delegator :gemspec, :version, :version

def initialize(name)
@name = name.downcase.strip
configure_sass
end

def root
# Must use File.realpath to resolve symlinks created by rbenv
# Otherwise, Jekyll.sanitized path with prepend the unresolved root
@root ||= File.realpath(gemspec.full_gem_path)
rescue Errno::ENOENT, Errno::EACCES, Errno::ELOOP
nil
end

def includes_path
path_for "_includes".freeze
end

def layouts_path
path_for "_layouts".freeze
end

def sass_path
path_for "_sass".freeze
end

def data_path
path_for "_data".freeze
end

def assets_path
path_for "assets".freeze
end

def configure_sass
return unless sass_path
require "sass"
Sass.load_paths << sass_path
end

private

def path_for(folder)
path = realpath_for(folder)
path if path && File.directory?(path)
end

def realpath_for(folder)
File.realpath(Jekyll.sanitized_path(root, folder.to_s))
rescue Errno::ENOENT, Errno::EACCES, Errno::ELOOP
nil
end

def gemspec
@gemspec ||= Gem::Specification.find_by_name(name)
rescue Gem::LoadError
raise Jekyll::Errors::MissingDependencyException,
"The #{name} theme could not be found."
end
end
end
96 changes: 96 additions & 0 deletions lib/jekyll/theme_reader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# encoding: UTF-8
require "csv"

module Jekyll
class ThemeReader < Reader
def initialize(site)
@site = site
@theme_data_files = Dir[File.join(@site.theme.root, site.config["data_dir"], "/*")]
end

# Read Site data from disk and load it into internal data structures.
#
# Returns nothing.
def read
@site.layouts = LayoutReader.new(site).read
read_directories
sort_files!
@site.data = DataReader.new(site).read(site.config["data_dir"])
read_theme_data
CollectionReader.new(site).read
ThemeAssetsReader.new(site).read
end

# Read data files within a theme gem and add them to internal data
#
# Returns a hash appended with new data
def read_theme_data
if site.theme && site.theme.data_path
#
# show contents of "<theme>/_data/" dir being read
debug_theme_reader
theme_data = ThemeDataReader.new(site).read(site.config["data_dir"])
@site.data = Utils.deep_merge_hashes(theme_data, @site.data)
#
# show site.data hash contents
debug_theme_data_reader
end
end


private

def debug_theme_reader
Jekyll.logger.debug ""
Jekyll.logger.debug "Reading:", "Theme Data Files..."

@theme_data_files.each do | file |
Jekyll.logger.debug "", file
end

Jekyll.logger.debug ""
Jekyll.logger.debug "Merging:", "Theme Data Hash..."
end

def debug_theme_data_reader
Jekyll.logger.debug ""
Jekyll.logger.debug "Site Data:"
theme = site.config["theme"]
@site.data.each do | key, value |
unless key == theme
print_key key
print_value value
end
end
Jekyll.logger.debug ""
end

def print_key(key)
dashes = "------------------------"
key = key.to_s

Jekyll.logger.debug "", dashes
Jekyll.logger.debug "", key
Jekyll.logger.debug "", dashes
end

def print_value(value)
if value.class == Array
value.each do | entry |
entry.each do | k, v |
print_hash k, v
end
end
elsif value.class == Hash
value.each do | k, v |
print_hash k, v
end
end
end

def print_hash(key, value)
key = key.to_s + ":"
Jekyll.logger.debug key, value
end
end
end

0 comments on commit 97a8d51

Please sign in to comment.