-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedium_generator.rb
46 lines (36 loc) · 1.13 KB
/
medium_generator.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
require 'feedjira'
require 'httparty'
require 'sanitize'
# lol
class String
def truncate(truncate_at, options = {})
return dup unless length > truncate_at
options[:omission] ||= '...'
length_with_room_for_omission = truncate_at - options[:omission].length
stop = if options[:separator]
rindex(options[:separator], length_with_room_for_omission) ||
length_with_room_for_omission
else
length_with_room_for_omission
end
"#{self[0...stop]}#{options[:omission]}"
end
end
Feedjira.parse(HTTParty.get("https://medium.com/feed/@robdel12").body).entries.each do |e|
puts "--------------"
puts "Generating posts from medium feed..."
p "Title: #{e.title}, published on Medium #{e.url} #{e}"
title = e[:title]
slug = e[:title].downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
content = e[:content]
front_matter = <<-HEREDOC
---
layout: post
title: "#{title}"
date: #{e[:published].to_s}
excerpt: "#{Sanitize.fragment(content).truncate(225)}"
---
HEREDOC
path = "#{__dir__}/_posts/#{e[:published].to_s.split(' ').first}-#{slug}.md"
File.write(path, "#{front_matter} #{content}")
end