Skip to content

Commit

Permalink
footer and header are created from templates now.
Browse files Browse the repository at this point in the history
This allows more flexibility of output without forcing
users to dig into the code of the gem.
  • Loading branch information
PoTe committed Apr 19, 2012
1 parent a28aa49 commit e5e8539
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,4 +1,5 @@
_posts/
_layouts/
planet.yml
*.gem
*.gempsec
sample.txt
62 changes: 57 additions & 5 deletions bin/planet
Expand Up @@ -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

Expand All @@ -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'
Expand All @@ -42,7 +53,7 @@ command :init do |c|
planet:
posts_directory: _posts/
templates_directory: _templates/
templates_directory: _layouts/
blogs:
- author: "Pablo Astigarraga"
Expand All @@ -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
Expand Down
41 changes: 23 additions & 18 deletions lib/planet/planet.rb
@@ -1,5 +1,6 @@
require 'simple-rss'
require 'open-uri'
require 'mustache'

class Planet

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions planet.gemspec
Expand Up @@ -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.