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
10
2
11
3
12
4
class Planet
13
5
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'
22
7
23
8
def self . banner
24
- ### todo: add RUBY_PATCHLEVEL or RUBY_PATCH_LEVEL e.g. -p124 - why? why not?
25
9
"planet.rb/#{ VERSION } on Ruby #{ RUBY_VERSION } (#{ RUBY_RELEASE_DATE } ) [#{ RUBY_PLATFORM } ]"
26
10
end
27
11
28
12
29
-
30
-
31
13
def initialize
32
14
puts self . class . banner ## print banner / say hello
33
15
@@ -42,12 +24,7 @@ def initialize
42
24
end
43
25
44
26
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
51
28
Pluto . connect ( @db_config )
52
29
53
30
Pluto ::Model ::Item . latest . limit ( 10 ) . each_with_index do |item , i |
@@ -57,7 +34,7 @@ def run( args )
57
34
end
58
35
end
59
36
60
-
37
+ private
61
38
def generate_blog_post ( item )
62
39
63
40
posts_root = "./_posts"
@@ -69,19 +46,19 @@ def generate_blog_post( item )
69
46
## 2020-12-21- e.g. must include trailing dash (-)
70
47
fn = "#{ posts_root } /#{ item . published . strftime ( '%Y-%m-%d' ) } -#{ title_to_key ( item . title ) } .html"
71
48
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?
81
57
82
58
83
59
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 "
85
62
86
63
if item . content
87
64
f . write item . content
@@ -93,35 +70,23 @@ def generate_blog_post( item )
93
70
end
94
71
end
95
72
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 ( /[ ]+/ , '_' )
100
78
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?
109
81
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
112
83
end
113
-
114
- key
115
- end
116
-
84
+
117
85
end ## class Planet
118
86
119
87
120
88
121
-
122
89
##############################
123
90
# main entry - let's run
124
91
125
- Planet . new . run ( ARGV ) if __FILE__ == $0
126
-
127
-
92
+ Planet . new . run if __FILE__ == $0
0 commit comments