forked from rails/weblog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_import-this-week-in-rails.rb
executable file
·117 lines (96 loc) · 2.61 KB
/
_import-this-week-in-rails.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env ruby
#
# Import "This Week in Rails" issue to a blog post
#
# Usage:
#
# _import-this-week-in-rails.rb PUBLIC_PAGE_URL
#
# Example:
#
# _import-this-week-in-rails.rb http://us3.campaign-archive2.com/?u=2721e27ce456363785acc5405&id=5aed0d5741
#
url = ARGV[0]
if url.nil?
puts "Usage:
_import-this-week-in-rails.rb PUBLIC_PAGE_URL
Example:
_import-this-week-in-rails.rb https://rails-weekly.ongoodbits.com/2015/01/30/relation-or-file-fixtures-kwargs-and-more
"
exit -1
end
require 'uri'
require 'open-uri'
require 'json'
require 'nokogiri'
require 'reverse_markdown'
class GoodbitsEmail
attr_accessor :email
def initialize(raw_email)
@email = JSON.parse(raw_email, symbolize_names: true)[:newsletter_email]
end
def title
email[:subject].strip
end
# Author is mentioned in the first text block of the email
def author
intro_html = Nokogiri::HTML(content_blocks_for("Text")[0][:variables][:html_text])
begin
intro_html
.xpath("//a[contains(@href, 'twitter.com/') or contains(@href, 'github.com/') and not(contains(@href, 'rails'))]")
.first['href']
.split('/')
.last
rescue
'chancancode'
end
end
def markdown_render
md =[]
email[:content_blocks].each do |cb|
next if ["Header", "Footer"].include?(cb[:component_name])
case cb[:component_name]
when "Text"
md << html_to_md(cb[:variables][:html_text])
when "Subheading"
md << "## #{cb[:variables][:subheading]}\n"
when "Image"
md << "\n" if cb[:image_url] != nil
when "Article"
md << "### [#{cb[:variables][:title]}](#{cb[:variables][:link_to]})\n"
md << html_to_md(cb[:variables][:html_description])
end
end
md.join("\n")
end
private
def html_to_md(html)
ReverseMarkdown.convert(html, unknown_tags: :bypass)
end
def content_blocks_for(component_name)
email[:content_blocks].select {|cb|
cb[:component_name] == component_name
}
end
end
uri = URI.parse(url + ".json")
path_parts = uri.path.split("/")
slug = path_parts.last.gsub(".json", "")
date = Date.new(*path_parts[1..3].map(&:to_i))
goodbits_email = GoodbitsEmail.new(uri.open.read)
meta = %|---
layout: post
title: "#{goodbits_email.title}"
categories: news
author: #{goodbits_email.author}
published: true
date: #{date.to_s}
---
|
md = goodbits_email.markdown_render
post_content = meta + md
post_path = "_posts/#{date.strftime('%Y-%m-%d')}-this-week-in-rails-#{slug}.markdown"
File.open(post_path, 'w') do |f|
f.write post_content
end
system "#{ENV['EDITOR'] || 'open'} #{post_path}"