-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplanet.rb
96 lines (65 loc) · 2.02 KB
/
planet.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
###
# to run use
# ruby ./planet.rb
require 'pluto/models' ## see https://rubygems.org/gems/pluto-models
class Planet
VERSION = '1.0.0'
def self.banner
"planet.rb/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
end
def initialize
puts self.class.banner ## print banner / say hello
puts "db settings:"
@db_config = {
adapter: 'sqlite3',
database: './planet.db'
}
pp @db_config
end
def run
Pluto.connect( @db_config )
Pluto::Model::Item.latest.limit(10).each_with_index do |item,i|
puts "[#{i+1}] #{item.title}"
generate_blog_post( item )
end
end
private
def generate_blog_post( item )
posts_root = "./_posts"
FileUtils.mkdir_p( posts_root ) ## make sure path exists
## note:
## jekyll pattern for blogs must follow
## 2020-12-21- e.g. must include trailing dash (-)
fn = "#{posts_root}/#{item.published.strftime('%Y-%m-%d')}-#{title_to_key(item.title)}.html"
frontmatter = {
'title' => item.title,
'created_at' => item.published,
'author' => item.feed.title,
'layout' => 'post'
}
frontmatter['original_link'] = item.url unless item.url.empty?
File.open( fn, 'w:utf-8' ) do |f|
f.write frontmatter.to_yaml # note: to_vaml starts yaml "document" with ---
f.write "---\n\n"
if item.content
f.write item.content
elsif item.summary
f.write item.summary
else
## warn: no content found for feed
end
end
end
def title_to_key( title )
key = title.downcase
key = key.gsub( /[^a-z0-9 -]/, '' ) ## for now remove all chars except a-z, 0-9, space and dash (-)
key = key.strip
key = key.gsub( /[ ]+/, '_' )
## note: might result in null string (use hash)
key = "post#{Digest::MD5.hexdigest( title )}" if key.empty?
key
end
end ## class Planet
##############################
# main entry - let's run
Planet.new.run if __FILE__ == $0