planet 0.3.1 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/bin/planet +49 -47
  2. data/lib/planet/version.rb +1 -1
  3. data/lib/planet.rb +29 -15
  4. metadata +17 -1
data/bin/planet CHANGED
@@ -1,11 +1,11 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- $: << File.expand_path(File.dirname(File.realpath(__FILE__)) + '/../lib')
4
- require 'rubygems'
5
3
  require 'gli'
4
+ require 'box'
6
5
  require 'planet'
7
6
 
8
7
  include GLI
8
+ include Box
9
9
 
10
10
  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'
11
11
 
@@ -13,7 +13,7 @@ desc 'Parses planet.yml file for blogs and generates their posts in Jekyll compl
13
13
  command :generate do |c|
14
14
 
15
15
  c.action do |global_options,options,args|
16
- conf = YAML.load File.open('planet.yml', 'r').read
16
+ conf = YAML.load_file('planet.yml')
17
17
 
18
18
  @planet = Planet.new(config: conf.fetch('planet', {}))
19
19
 
@@ -38,29 +38,9 @@ end
38
38
  desc 'Creates basic planet.yml config file'
39
39
  command :init do |c|
40
40
  c.action do |global_options,options,args|
41
- raise Exception.new('There is already a planet.yml file present') if File.exist? 'planet.yml'
41
+ abort 'There is already a planet.yml file present' if File.exist? 'planet.yml'
42
42
 
43
- default = '## Planet.rb default config file, modify it and spawn awesomeness!
44
-
45
- planet:
46
- posts_directory: source/_posts/
47
- templates_directory: source/_layouts/
48
-
49
- blogs:
50
- # Bare minimum to get it working
51
- - author: "Cubox"
52
- feed: "http://blog.cuboxlabs.com/atom.xml"
53
- image: "http://cuboxlabs.com/img/cubox-humans/could-be-you.png"
54
-
55
- - author: "Pablo Astigarraga"
56
- feed: "http://blog.poteland.com/atom.xml"
57
- image: "http://poteland.com/images/avatars/red_mage.png"
58
- # Other fields:
59
- twitter: "poteland"
60
- url: "http://blog.poteland.com" # => only required for people that don\'t have a \'url\' field on their RSS/Atom field'
61
-
62
-
63
- File.open('planet.yml', 'w') { |f| f.write(default) }
43
+ File.open('planet.yml', 'w') { |f| f.write(Box::FILES['planet.yml']) }
64
44
 
65
45
  puts '=> Created default planet.yml'
66
46
  end
@@ -71,35 +51,19 @@ command :create_templates do |c|
71
51
  c.action do |global_options,options,args|
72
52
  conf = YAML.load File.open('planet.yml', 'r') { |f| f.read }
73
53
 
74
- templates_dir = conf.fetch('planet').fetch('templates_directory', '_layouts/')
54
+ templates_dir = conf.fetch('planet').fetch('templates_directory', 'source/_layouts/')
75
55
 
76
56
  FileUtils.mkdir_p(templates_dir)
77
57
 
78
- author = '
79
- <div class="author">
80
- <img src="{{ image_url }}" style="width: 96px; height: 96;">
81
- <span style="position: absolute; padding: 32px 15px;">
82
- <i>Original post by <a href="{{ twitter_url }}">{{ author }}</a> - check out <a href="{{ blog_url }}">{{ blog_name }}</a></i>
83
- </span>
84
- </div>
85
- '
86
58
  if !File.exists?(templates_dir + 'author.html')
87
- File.open(templates_dir + 'author.html', 'w') { |f| f.write(author) }
59
+ File.open(templates_dir + 'author.html', 'w') { |f| f.write(Box::FILES['author.html']) }
88
60
  puts "=> Created default #{ templates_dir }author.html"
89
61
  else
90
62
  puts "=> Template #{ templates_dir }author.html already exists, skipping"
91
63
  end
92
64
 
93
- header ='---
94
- title: "{{ post_title }}"
95
- kind: article
96
- created_at: {{ post_date }}
97
- author: {{ author }}
98
- layout: post
99
- ---
100
- '
101
65
  if !File.exists?(templates_dir + 'header.md')
102
- File.open(templates_dir + 'header.md', 'w') { |f| f.write(header) }
66
+ File.open(templates_dir + 'header.md', 'w') { |f| f.write(Box::FILES['header.md']) }
103
67
  puts "=> Created default #{ templates_dir }header.md"
104
68
  else
105
69
  puts "=> Template #{ templates_dir }header.md already exists, skipping"
@@ -111,12 +75,12 @@ pre do |global,command,options,args|
111
75
  if command.name == :generate
112
76
  conf = YAML.load File.open('planet.yml', 'r') { |f| f.read }
113
77
 
114
- templates_dir = conf.fetch('planet').fetch('templates_directory', 'source/_layouts')
78
+ templates_dir = conf.fetch('planet').fetch('templates_directory', 'source/_layouts/')
115
79
  FileUtils.mkdir_p(templates_dir)
116
80
 
117
- FILES = [templates_dir, templates_dir + 'author.html', templates_dir + 'header.md']
81
+ files = [templates_dir, templates_dir + 'author.html', templates_dir + 'header.md']
118
82
 
119
- FILES.each do |file|
83
+ files.each do |file|
120
84
  unless File.exists?(file)
121
85
  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 }!"
122
86
  response = `planet create_templates`
@@ -137,3 +101,41 @@ on_error do |exception|
137
101
  end
138
102
 
139
103
  exit GLI.run(ARGV)
104
+
105
+ __END__
106
+ @@ header.md
107
+ ---
108
+ title: "{{ post_title }}"
109
+ kind: article
110
+ created_at: {{ post_date }}
111
+ author: {{ author }}
112
+ layout: post
113
+ ---
114
+
115
+ @@ author.html
116
+ <div class="author">
117
+ <img src="{{ image_url }}" style="width: 96px; height: 96;">
118
+ <span style="position: absolute; padding: 32px 15px;">
119
+ <i>Original post by <a href="{{ twitter_url }}">{{ author }}</a> - check out <a href="{{ blog_url }}">{{ blog_name }}</a></i>
120
+ </span>
121
+ </div>
122
+
123
+ @@ planet.yml
124
+ ## planet.rb default config file, modify it and spawn awesomeness!
125
+
126
+ planet:
127
+ posts_directory: source/_posts/
128
+ templates_directory: source/_layouts/
129
+
130
+ blogs:
131
+ # Bare minimum to get it working
132
+ - author: "Cubox"
133
+ feed: "http://blog.cuboxlabs.com/atom.xml"
134
+ image: "http://cuboxlabs.com/img/cubox-humans/could-be-you.png"
135
+
136
+ - author: "Pablo Astigarraga"
137
+ feed: "http://blog.poteland.com/atom.xml"
138
+ image: "http://poteland.com/images/avatars/red_mage.png"
139
+ # Other fields:
140
+ twitter: "poteland"
141
+ url: "http://blog.poteland.com" # => only required for people that don\'t have a \'url\' field on their RSS/Atom field'
@@ -2,7 +2,7 @@ module Planet
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 3
5
- PATCH = 1
5
+ PATCH = 3
6
6
 
7
7
  def self.to_s
8
8
  [MAJOR, MINOR, PATCH].join('.')
data/lib/planet.rb CHANGED
@@ -20,16 +20,15 @@ class Planet < Struct.new(:config, :blogs)
20
20
  blog.name ||= feed.title || 'the source'
21
21
  blog.url ||= feed.url
22
22
 
23
- ## This is slightly gay.
24
23
  if blog.url.nil?
25
- raise "#{ blog.author }'s blog does not have a url field on it's feed, you will need to specify it on planet.yml"
24
+ abort "#{ blog.author }'s blog does not have a url field on it's feed, you will need to specify it on planet.yml"
26
25
  end
27
26
 
28
27
  feed.entries.each do |entry|
29
28
  blog.posts << @post = Post.new(
30
29
  title: entry.title.sanitize,
31
- content: entry.content.strip.gsub('<img src="', "<img src=\"#{ blog.url }"), ## => I don't like this that much, move it away
32
- date: entry.published, ## and check that it's needed post by post.
30
+ content: blog.sanitize_images(entry.content.strip),
31
+ date: entry.published,
33
32
  url: blog.url + entry.url,
34
33
  blog: blog
35
34
  )
@@ -61,6 +60,10 @@ class Planet < Struct.new(:config, :blogs)
61
60
  self.blog = attributes.fetch(:blog, nil)
62
61
  end
63
62
 
63
+ def to_s
64
+ "#{ header }#{ content }#{ footer }"
65
+ end
66
+
64
67
  def to_hash
65
68
  {
66
69
  post_content: self.content,
@@ -79,28 +82,25 @@ class Planet < Struct.new(:config, :blogs)
79
82
  def header
80
83
  ## TODO: We need categories/tags
81
84
  file = self.blog.planet.config.fetch('templates_directory', '_layouts/') + 'header.md'
82
- file_contents = File.open(file, 'r') { |f| f.read }
85
+ file_contents = File.read(file)
83
86
 
84
87
  Mustache.render(file_contents, self.to_hash)
85
88
  end
86
89
 
87
- def file_name
88
- name_date = date ? date.strftime('%Y-%m-%d') : nil
89
- name_title = title.downcase.scan(/\w+/).join('-')
90
-
91
- [name_date, name_title].join('-')
92
- end
93
-
94
90
  def footer
95
91
  file = self.blog.planet.config.fetch('templates_directory', '_layouts/') + 'author.html'
96
- file_contents = File.open(file, 'r') { |f| f.read }
92
+ file_contents = File.read(file)
97
93
 
98
94
  Mustache.render(file_contents, self.to_hash)
99
95
  end
100
96
 
101
- def to_s
102
- "#{ header }#{ content }#{ footer }"
97
+ def file_name
98
+ name_date = date ? date.strftime('%Y-%m-%d') : nil
99
+ name_title = title.downcase.scan(/\w+/).join('-')
100
+
101
+ [name_date, name_title].join('-')
103
102
  end
103
+
104
104
  end
105
105
 
106
106
  class Blog < Struct.new(:url, :feed, :name, :author, :image, :twitter, :posts, :planet)
@@ -115,5 +115,19 @@ class Planet < Struct.new(:config, :blogs)
115
115
  self.posts = attributes.fetch('posts', [])
116
116
  self.planet = attributes[:planet]
117
117
  end
118
+
119
+ def sanitize_images(html)
120
+ ## We take all images with src not matching http refs and append
121
+ ## the original blog to them.
122
+ html.scan(/<img src="([^h"]+)"/).flatten.each do |img|
123
+ if img[0] == '/'
124
+ html.gsub!(img, "#{ self.url }#{ img }")
125
+ else
126
+ html.gsub!(img, "#{ self.url }/#{ img }")
127
+ end
128
+ end
129
+
130
+ html
131
+ end
118
132
  end
119
133
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: planet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -75,6 +75,22 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: box
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
78
94
  description:
79
95
  email: pote@tardis.com.uy
80
96
  executables: