Skip to content

Commit cdeecd9

Browse files
authored
Update planet.rb
1 parent f13eae5 commit cdeecd9

File tree

1 file changed

+24
-59
lines changed

1 file changed

+24
-59
lines changed

planet.rb

+24-59
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,15 @@
1-
# encoding: utf-8
2-
3-
4-
$RUBYLIBS_DEBUG = true ## turn on debugging messages for pluto & friends libs
5-
6-
7-
# 3rd party ruby gems/libs
8-
require 'pluto/models'
9-
1+
require 'newscast' ## see https://rubygems.org/gems/newscast
102

113

124
class Planet
135

14-
MAJOR = 1
15-
MINOR = 0
16-
PATCH = 0
17-
VERSION = [MAJOR,MINOR,PATCH].join('.')
18-
19-
def self.version
20-
VERSION
21-
end
6+
VERSION = '1.0.0'
227

238
def self.banner
24-
### todo: add RUBY_PATCHLEVEL or RUBY_PATCH_LEVEL e.g. -p124 - why? why not?
259
"planet.rb/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
2610
end
2711

2812

29-
30-
3113
def initialize
3214
puts self.class.banner ## print banner / say hello
3315

@@ -42,12 +24,7 @@ def initialize
4224
end
4325

4426

45-
def run( args )
46-
unless File.exists?( @db_config[:database])
47-
puts "** error: database #{@db_config[:database]} missing; please check pluto documention for importing feeds etc."
48-
exit 1
49-
end
50-
27+
def run
5128
Pluto.connect( @db_config )
5229

5330
Pluto::Model::Item.latest.limit(10).each_with_index do |item,i|
@@ -57,7 +34,7 @@ def run( args )
5734
end
5835
end
5936

60-
37+
private
6138
def generate_blog_post( item )
6239

6340
posts_root = "./_posts"
@@ -69,19 +46,19 @@ def generate_blog_post( item )
6946
## 2020-12-21- e.g. must include trailing dash (-)
7047
fn = "#{posts_root}/#{item.published.strftime('%Y-%m-%d')}-#{title_to_key(item.title)}.html"
7148

72-
frontmatter =<<TXT
73-
---
74-
title: "#{item.title.gsub("\"","\\\"")}"
75-
created_at: #{item.published}
76-
author: #{item.feed.title}
77-
layout: post
78-
original_link: "#{item.url unless item.url.empty?}"
79-
---
80-
TXT
49+
frontmatter = {
50+
'title' => item.title,
51+
'created_at' => item.published,
52+
'author' => item.feed.title,
53+
'layout' => 'post'
54+
}
55+
56+
frontmatter['original_link'] = item.url unless item.url.empty?
8157

8258

8359
File.open( fn, 'w:utf-8' ) do |f|
84-
f.write frontmatter
60+
f.write frontmatter.to_yaml # note: to_vaml starts yaml "document" with ---
61+
f.write "---\n\n"
8562

8663
if item.content
8764
f.write item.content
@@ -93,35 +70,23 @@ def generate_blog_post( item )
9370
end
9471
end
9572

96-
97-
private
98-
99-
def title_to_key( title )
73+
def title_to_key( title )
74+
key = title.downcase
75+
key = key.gsub( /[^a-z0-9\- ]/, '' ) ## for now remove all chars except a-z and 0-9
76+
key = key.strip
77+
key = key.gsub( /[ ]+/, '_' )
10078

101-
### fix: use textutils.title_to_key ??
102-
key = title.downcase
103-
key = key.gsub( 'ü', 'ue' )
104-
key = key.gsub( 'é', 'e' )
105-
106-
key = key.gsub( /[^a-z0-9\- ]/, '' ) ## for now remove all chars except a-z and 0-9
107-
key = key.strip
108-
key = key.gsub( /[ ]+/, '_' )
79+
## note: might result in null string (use hash)
80+
key = "post#{Digest::MD5.hexdigest( title )}" if key.empty?
10981

110-
if key.blank? ## note: might result in null string (use timestamp)
111-
key = "post#{Time.now.strftime('%Y%m%d%H%M%S%L')}"
82+
key
11283
end
113-
114-
key
115-
end
116-
84+
11785
end ## class Planet
11886

11987

12088

121-
12289
##############################
12390
# main entry - let's run
12491

125-
Planet.new.run( ARGV ) if __FILE__ == $0
126-
127-
92+
Planet.new.run if __FILE__ == $0

0 commit comments

Comments
 (0)