Skip to content

Commit 613a7a4

Browse files
committed
Merge pull request jekyll#460 from kendagriff/master
RSS Migrator
2 parents 8adfaea + 9523c39 commit 613a7a4

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

lib/jekyll/migrators/rss.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Created by Kendall Buchanan (https://github.com/kendagriff) on 2011-12-22.
2+
# Use at your own risk. The end.
3+
#
4+
# Usage:
5+
# (URL)
6+
# ruby -r '_import/rss.rb' -e "Jekyll::MigrateRSS.process('http://yourdomain.com/your-favorite-feed.xml')"
7+
#
8+
# (Local file)
9+
# ruby -r '_import/rss.rb' -e "Jekyll::MigrateRSS.process('./somefile/on/your/computer.xml')"
10+
11+
require 'rubygems'
12+
require 'rss/1.0'
13+
require 'rss/2.0'
14+
require 'open-uri'
15+
require 'fileutils'
16+
require 'yaml'
17+
18+
module Jekyll
19+
module MigrateRSS
20+
21+
# The `source` argument may be a URL or a local file.
22+
def self.process(source)
23+
content = ""
24+
open(source) { |s| content = s.read }
25+
rss = RSS::Parser.parse(content, false)
26+
27+
raise "There doesn't appear to be any RSS items at the source (#{source}) provided." unless rss
28+
29+
rss.items.each do |item|
30+
formatted_date = item.date.strftime('%Y-%m-%d')
31+
post_name = item.title.split(%r{ |!|/|:|&|-|$|,}).map { |i| i.downcase if i != '' }.compact.join('-')
32+
name = "#{formatted_date}-#{post_name}"
33+
34+
header = {
35+
'layout' => 'post',
36+
'title' => item.title
37+
}
38+
39+
File.open("_posts/#{name}.html", "w") do |f|
40+
f.puts header.to_yaml
41+
f.puts "---\n"
42+
f.puts item.description
43+
end
44+
end
45+
end
46+
end
47+
end

0 commit comments

Comments
 (0)