Skip to content

Commit

Permalink
Merge pull request #10 from mnyrop/force-option
Browse files Browse the repository at this point in the history
Force option
  • Loading branch information
mnyrop committed Aug 2, 2018
2 parents df64f73 + 9898e4d commit abc9ee8
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 43 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ script:
- bundle exec rspec
- bundle exec rubocop
after_script:
- tree build
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
68 changes: 37 additions & 31 deletions lib/pagemaster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,70 +11,76 @@ def init_with_program(prog)
prog.command(:pagemaster) do |c|
c.syntax 'pagemaster [options] [args]'
c.description 'Generate md pages from collection data.'
c.option 'no-perma', '--no-permalink', 'Skips adding hard-coded permalink'
c.option :no_perma, '--no-permalink', 'Skips adding hard-coded permalink.'
c.option :force, '--force', 'Erases pre-existing collection before regenerating.'
c.action { |args, options| execute(args, options) }
end
end

def execute(args, options)
def execute(args, opts = {})
config = YAML.load_file('_config.yml')
abort 'Cannot find collections in config' unless config.key?('collections')
perma = false
perma = config['permalink'] == 'pretty' ? '/' : '.html' unless options.key? 'no-perma'
args.each do |name|
abort "Cannot find #{name} in collection config" unless config['collections'].key? name
meta = {
'id_key' => config['collections'][name].fetch('id_key'),
'layout' => config['collections'][name].fetch('layout'),
'source' => config['collections'][name].fetch('source')
id_key: config['collections'][name].fetch('id_key'),
layout: config['collections'][name].fetch('layout'),
source: config['collections'][name].fetch('source'),
ext: config.fetch('permalink', '') == 'pretty' ? '/' : '.html'
}
data = ingest(meta)
generate_pages(name, meta, data, perma)
generate_pages(name, meta, data, opts)
end
end

def ingest(meta)
src = '_data/' + meta['source']
src = "_data/#{meta[:source]}"
puts "Processing #{src}...."
case File.extname(src)
when '.csv' then data = CSV.read(src, headers: true, encoding: 'utf-8').map(&:to_hash)
when '.json' then data = JSON.parse(File.read(src).encode('UTF-8'))
when '.yml' then data = YAML.load_file(src)
else abort 'Collection source must have a valid extension (.csv, .yml, or .json)'
end
data = case File.extname(src)
when '.csv'
CSV.read(src, headers: true).map(&:to_hash)
when '.json'
JSON.parse(File.read(src).encode('UTF-8'))
when '.yml'
YAML.load_file(src)
else
raise 'Collection source must have a valid extension (.csv, .yml, or .json)'
end
detect_duplicates(meta, data)
data
rescue StandardError
abort "Cannot load #{src}. check for typos and rebuild."
raise "Cannot load #{src}. check for typos and rebuild."
end

def detect_duplicates(meta, data)
ids = []
data.each { |d| ids << d[meta['id_key']] }
ids = data.map { |d| d[meta[:data]] }
duplicates = ids.detect { |i| ids.count(i) > 1 } || []
abort "Your collection duplicate ids: \n#{duplicates}" unless duplicates.empty?
raise "Your collection duplicate ids: \n#{duplicates}" unless duplicates.empty?
end

def generate_pages(name, meta, data, perma)
completed = 0
skipped = 0
dir = '_' + name
def generate_pages(name, meta, data, opts)
dir = "_#{name}"
perma = opts.fetch(:no_perma, meta[:ext])

if opts.fetch(:force, false)
FileUtils.rm_rf(dir)
puts "Overwriting #{dir} directory with --force."
end

mkdir_p(dir)
data.each do |item|
pagename = slug(item.fetch(meta['id_key']))
pagepath = dir + '/' + pagename + '.md'
item['permalink'] = '/' + name + '/' + pagename + perma if perma
item['layout'] = meta['layout']
pagename = slug(item.fetch(meta[:id_key]))
pagepath = "#{dir}/#{pagename}.md"
item['permalink'] = "/#{name}/#{pagename}#{perma}" if perma
item['layout'] = meta[:layout]
if File.exist?(pagepath)
puts "#{pagename}.md already exits. Skipping."
skipped += 1
else
File.open(pagepath, 'w') { |file| file.write(item.to_yaml.to_s + '---') }
completed += 1
File.open(pagepath, 'w') { |f| f.write("#{item.to_yaml}---") }
end
end
rescue StandardError
abort 'Pagemaster exited for some reason, most likely a missing or invalid id_key.'
raise 'Pagemaster exited for some reason, most likely a missing or invalid id_key.'
end

def slug(str)
Expand Down
2 changes: 1 addition & 1 deletion pagemaster.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Gem::Specification.new do |s|
s.date = '2018-03-27'
s.summary = 'jekyll pagemaster plugin'
s.description = 'jekyll plugin for generating md pages from csv/json/yml'
s.authors = ['Marii Nyröp']
s.authors = ['Marii Nyrop']
s.files = ['lib/pagemaster.rb']
s.require_path = 'lib'
s.homepage = 'https://github.com/mnyrop/pagemaster'
Expand Down
22 changes: 22 additions & 0 deletions spec/fake/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,25 @@ def add_collections_to_config(args, collection_data)
output = YAML.dump config
File.write('_config.yml', output)
end

def quiet_stdout
if QUIET
begin
orig_stderr = $stderr.clone
orig_stdout = $stdout.clone
$stderr.reopen File.new('/dev/null', 'w')
$stdout.reopen File.new('/dev/null', 'w')
retval = yield
rescue StandardError => e
$stdout.reopen orig_stdout
$stderr.reopen orig_stderr
raise e
ensure
$stdout.reopen orig_stdout
$stderr.reopen orig_stderr
end
retval
else
yield
end
end
34 changes: 24 additions & 10 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,56 @@
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
require 'pagemaster'

include FileUtils
QUIET = !ENV['DEBUG']

describe 'pagemaster' do
Fake.site
collection_data = Fake.data
args = collection_data.map { |c| c[0] }
add_collections_to_config(args, collection_data)
dirs = args.map { |a| '_' + a }

let(:dirs) { args.map { |a| "_#{a}" } }

context 'with --no-permalink' do
options = { 'no-perma' => true }
it 'throws no errors' do
Pagemaster.execute(args, options)
expect { quiet_stdout { Pagemaster.execute(args, no_perma: true) } }.not_to raise_error
end
it 'makes the correct dirs' do
dirs.each { |dir| expect(exist(dir)) }
end
it 'generates md pages' do
dirs.each { |dir| expect(Dir.glob(dir + '/*.md')) }
dirs.each { |dir| expect(Dir.glob("#{dir}/*.md")) }
end
it 'skips writing permalinks' do
Dir.glob(dirs.first + '/*.md').each do |p|
Dir.glob("#{dirs.first}/*.md").each do |p|
page = YAML.load_file(p)
expect(!page.key?('permalink'))
end
end
end

context 'with --force' do
it 'throws no errors' do
expect { quiet_stdout { Pagemaster.execute(args, force: true) } }.not_to raise_error
end
it 'deletes the dir' do
expect { Pagemaster.execute(args, force: true) }.to output(/.*Overwriting.*/).to_stdout
expect { Pagemaster.execute(args, force: true) }.not_to output(/.*Skipping.*/).to_stdout
end
it 'regenerates md pages' do
dirs.each { |dir| expect(Dir.glob("#{dir}/*.md")) }
end
end

context 'with default options' do
options = {}
it 'throws no errors' do
rm_rf(dirs)
Pagemaster.execute(args, options)
expect { quiet_stdout { Pagemaster.execute(args) } }.not_to raise_error
end
it 'skips existing pages' do
expect { Pagemaster.execute([args.first]) }.to output(/.*Skipping.*/).to_stdout
end
it 'writes permalinks' do
Dir.glob(dirs.first + '/*.md').each do |p|
Dir.glob("#{dirs.first}/*.md").each do |p|
page = YAML.load_file(p)
expect(page.key?('permalink'))
end
Expand Down

0 comments on commit abc9ee8

Please sign in to comment.