This repository has been archived by the owner on Dec 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
_import-this-week-in-rails.rb
executable file
·101 lines (81 loc) · 2.04 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
#!/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 https://world.hey.com/this.week.in.rails/halloween-edition-zeitwerk-migration-guide-selenium-webdriver-and-some-ruby-3-1-snacks-66c67b91
#
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://world.hey.com/this.week.in.rails/halloween-edition-zeitwerk-migration-guide-selenium-webdriver-and-some-ruby-3-1-snacks-66c67b91
"
exit -1
end
require 'uri'
require 'open-uri'
require 'json'
require 'nokogiri'
require 'reverse_markdown'
require 'date'
post_date = ARGV[1] || Date.today.to_s
class HeyWorldEmail
attr_accessor :email
attr_accessor :post_date
def initialize(post_date, raw_email)
@post_date = post_date
@email = Nokogiri::HTML(raw_email)
end
def date
Date.parse(@post_date)
end
def title
email.css("title").text
end
# Author is mentioned in the first text block of the email
def author
intro_html = content
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 content
email.css(".trix-content")
end
def markdown_render
ReverseMarkdown.convert content
end
end
uri = URI.parse(url)
path_parts = uri.path.split("/")
slug = path_parts.last
hey_world_email = HeyWorldEmail.new(post_date, uri.open.read)
meta = %|---
layout: post
title: "#{hey_world_email.title}"
categories: news
author: #{hey_world_email.author}
published: true
date: #{hey_world_email.date}
---
|
md = hey_world_email.markdown_render
post_content = meta + md
post_path = "_posts/#{hey_world_email.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}"