Permalink
Browse files

footer and header are created from templates now.

This allows more flexibility of output without forcing
users to dig into the code of the gem.
  • Loading branch information...
1 parent a28aa49 commit e5e85393396a77d420c31a6bde26f6c8c1bb9504 @pote committed Apr 19, 2012
Showing with 83 additions and 24 deletions.
  1. +2 −1 .gitignore
  2. +57 −5 bin/planet
  3. +23 −18 lib/planet/planet.rb
  4. +1 −0 planet.gemspec
View
3 .gitignore
@@ -1,4 +1,5 @@
_posts/
+_layouts/
+planet.yml
*.gem
*.gempsec
-sample.txt
View
62 bin/planet
@@ -7,23 +7,34 @@ require 'planet/planet'
include GLI
-program_desc 'Planet.rb is an awesome feed-aggregator gem, that basically consumes RSS/Atom feeds and outputs them in a format suitable to use with Octopress or Jekyll'
+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.open('planet.yml', 'r').read
+ templates_dir = conf.fetch('planet').fetch('templates_directory', '_layout')
+
+ FILES = [templates_dir, templates_dir + 'author.html', templates_dir + 'header.md']
+
+ FILES.each do |file|
+ unless File.exists?(file)
+ puts '=> Your are missing some files in your templates directory, run "planet create_templates" to solve this'
+ raise "Missing Templates"
+ end
+ end
+
@planet = Planet.new(conf.fetch('planet', {}))
- #@planet = Planet.new()
conf['blogs'].each do |blog|
@planet.blogs << Planet::Blog.new(
blog['feed'],
blog['author'],
blog['image'],
- []
+ [],
+ @planet
)
end
@@ -33,7 +44,7 @@ command :generate do |c|
end
end
-desc 'Creates basic planet.yml files'
+desc 'Creates basic planet.yml config file'
command :init do |c|
c.action do |global_options,options,args|
raise Exception.new('There is already a planet.yml file present') if File.exist? 'planet.yml'
@@ -42,7 +53,7 @@ command :init do |c|
planet:
posts_directory: _posts/
- templates_directory: _templates/
+ templates_directory: _layouts/
blogs:
- author: "Pablo Astigarraga"
@@ -54,10 +65,51 @@ blogs:
image: "http://cuboxlabs.com/img/cubox-humans/could-be-you.png"'
File.open('planet.yml', 'w') { |f| f.write(default) }
+
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.open('planet.yml', 'r').read
+
+ templates_dir = conf.fetch('planet').fetch('templates_directory', '_layouts/')
+
+ Dir.mkdir(templates_dir) unless File.exists?(templates_dir)
+
+ author = '<div class="author">
+<img src="{{ image }}" style="width: 48px; height: 48px;">
+<span style="position: absolute; padding: 12px;">
+ <i>Original post by {{ author }} - <a href="{{ link }}">read it from the source</a></i>
+</span>
+</div>
+'
+ if !File.exists?(templates_dir + 'author.html')
+ File.open(templates_dir + 'author.html', 'w') { |f| f.write(author) }
+ puts "=> Created default #{ templates_dir }author.html"
+ else
+ puts "=> Template #{ templates_dir }author.html already exists, skipping"
+ end
+
+ header ='---
+title: "{{ title }}"
+kind: article
+created_at: {{ date }}
+author: {{ author }}
+layout: post
+---
+'
+ if !File.exists?(templates_dir + 'header.md')
+ File.open(templates_dir + 'header.md', 'w') { |f| f.write(header) }
+ puts "=> Created default #{ templates_dir }header.md"
+ else
+ puts "=> Template #{ templates_dir }header.md already exists, skipping"
+ end
+ end
+end
+
pre do |global,command,options,args|
true
end
View
41 lib/planet/planet.rb
@@ -1,5 +1,6 @@
require 'simple-rss'
require 'open-uri'
+require 'mustache'
class Planet
@@ -70,7 +71,7 @@ def write_posts
puts "=> Writing #{ posts.size } posts to the #{ posts_dir } directory"
posts(filter: {date: true, order: :date}).each do |post|
- file_name = posts_dir.concat post.file_name
+ file_name = posts_dir + post.file_name
File.open(file_name + '.markdown', "w+") { |f| f.write(post.to_s) }
end
@@ -90,14 +91,16 @@ def to_hash
def header
## TODO: We need categories/tags
- "---
-title: \"%{title}\"
-kind: article
-created_at: %{date}
-author: %{author}
-layout: post
----
- " % self.to_hash
+ file = self.blog.planet.config.fetch('templates_directory', '_layouts/') + 'header.md'
+ file_contents = File.open(file, 'r').read
+
+ data = {
+ title: self.title,
+ date: self.date,
+ author: self.blog.author
+ }
+
+ Mustache.render(file_contents, data)
end
def file_name
@@ -108,21 +111,23 @@ def file_name
end
def footer
- "
-<div class=\"author\">
- <img src=\"#{ self.blog.image }\"/ style=\"width: 48px; height: 48px;\">
- <span style=\"position: absolute; padding: 12px;\">
- <i>Original post by #{ self.blog.author } - <a href=\"#{ self.link }\">read it from the source</a></i>
- </span>
-</div>
-"
+ file = self.blog.planet.config.fetch('templates_directory', '_layouts/') + 'author.html'
+ file_contents = File.open(file, 'r').read
+
+ data = {
+ image: self.blog.image,
+ author: self.blog.author,
+ link: self.link
+ }
+
+ Mustache.render(file_contents, data)
end
def to_s
self.header + self.content + self.footer
end
end
- class Blog < Struct.new(:feed, :author, :image, :posts)
+ class Blog < Struct.new(:feed, :author, :image, :posts, :planet)
end
end
View
1 planet.gemspec
@@ -20,4 +20,5 @@ lib/planet/planet.rb
s.add_development_dependency('rake')
s.add_runtime_dependency('gli')
s.add_runtime_dependency('simple-rss')
+ s.add_runtime_dependency('mustache')
end

0 comments on commit e5e8539

Please sign in to comment.