Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 151 lines (116 sloc) 4.48 KB
#!/usr/bin/env ruby
require 'gli'
require 'planet'
include GLI
TEMPLATES = {
author: '
<div class="author">
<img src="{{ image_url }}" style="width: 96px; height: 96;">
<span style="position: absolute; padding: 32px 15px;">
<i>Original post by <a href="{{ twitter_url }}">{{ author }}</a> - check out <a href="{{ blog_url }}">{{ blog_name }}</a></i>
</span>
</div>
',
header: '---
title: "{{ post_title }}"
kind: article
created_at: {{ post_date }}
author: {{ author }}
layout: post
---
',
planet: '## Planet.rb default config file, modify it and spawn awesomeness!
planet:
posts_directory: source/_posts/
templates_directory: source/_layouts/
blogs:
# Bare minimum to get it working
- author: "Cubox"
feed: "http://blog.cuboxlabs.com/atom.xml"
image: "http://cuboxlabs.com/img/cubox-humans/could-be-you.png"
- author: "Pablo Astigarraga"
feed: "http://blog.poteland.com/atom.xml"
image: "http://poteland.com/images/avatars/red_mage.png"
# Other fields:
twitter: "poteland"
url: "http://blog.poteland.com" # => only required for people that don\'t have a \'url\' field on their RSS/Atom field'
}
program_desc 'Planet.rb is an awesome feed-aggregator gem that consumes RSS/Atom feeds and outputs them in a format suitable to use with Octopress or Jekyll'
desc 'Parses planet.yml file for blogs and generates their posts in Jekyll compliant format under the _posts directory'
command :generate do |c|
c.action do |global_options,options,args|
conf = YAML.load_file('planet.yml')
@planet = Planet.new(
config: conf.fetch('planet', {}),
blogs: conf.fetch('blogs', [])
)
@planet.aggregate
@planet.write_posts
end
end
desc 'Creates basic planet.yml config file'
command :init do |c|
c.action do |global_options,options,args|
abort 'There is already a planet.yml file present' if File.exist? 'planet.yml'
File.open('planet.yml', 'w') { |f| f.write(TEMPLATES[:planet]) }
puts '=> Created default planet.yml'
end
end
desc 'Creates basic templates on the templates_directory specified in planet.yml'
command :create_templates do |c|
c.action do |global_options,options,args|
conf = YAML.load_file('planet.yml')
templates_dir = conf.fetch('planet').fetch('templates_directory', 'source/_layouts/')
FileUtils.mkdir_p(templates_dir)
if !File.exists?(templates_dir + 'author.html')
File.open(templates_dir + 'author.html', 'w') { |f| f.write(TEMPLATES[:author]) }
puts "=> Created default #{ templates_dir }author.html"
else
puts "=> Template #{ templates_dir }author.html already exists, skipping"
end
if !File.exists?(templates_dir + 'header.md')
File.open(templates_dir + 'header.md', 'w') { |f| f.write(TEMPLATES[:header]) }
puts "=> Created default #{ templates_dir }header.md"
else
puts "=> Template #{ templates_dir }header.md already exists, skipping"
end
end
end
desc 'Delete existing templates and recreate them'
command :update_templates do |c|
c.action do |global_options,options,args|
conf = YAML.load_file('planet.yml')
templates_dir = conf.fetch('planet').fetch('templates_directory', 'source/_layouts/')
author = templates_dir + 'author.html'
header = templates_dir + 'header.md'
File.delete(author) if File.exists?(author)
File.delete(header) if File.exists?(header)
response = `#{ $0 } create_templates`
puts response
end
end
pre do |global,command,options,args|
if command.name == :generate
conf = YAML.load_file('planet.yml')
templates_dir = conf.fetch('planet').fetch('templates_directory', 'source/_layouts')
FileUtils.mkdir_p(templates_dir)
files = { author: templates_dir + 'author.html', header: templates_dir + 'header.md' }
files.each do |name, path|
if !File.exists?(path)
puts "=> You are missing some files in your templates directory, planet.rb will create them for you - make sure to review them on #{ templates_dir }!"
response = `#{$0} create_templates`
puts response
elsif TEMPLATES[name] != File.read(path)
puts "!! WARNING: your planet.rb template files are different from the default, if you haven't made specific changes to them yourself then planet.rb default templates might have changed.Run 'planet update_templates' to override your current files with the newest default file available."
end
end
end
true
end
post do |global,command,options,args|
true
end
on_error do |exception|
true
end
exit GLI.run(ARGV)
Something went wrong with that request. Please try again.