forked from rails/weblog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_import-this-week-in-rails.rb
executable file
·78 lines (61 loc) · 1.78 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
#!/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 'open-uri'
require 'reverse_markdown'
slug = url.split('/').last
parsed = Nokogiri.parse( open(url + "?body=1").read )
title = parsed.css("title").text
raise "Failed to extract title" if title.empty?
newsletter_html = parsed.at_css("table.body")
raise "Failed to extract newsletter content" if newsletter_html.nil?
tags = %w(h1 h2 h3 h4 p pre)
xpath_query = tags.map { |tag| "//#{tag}" }.join ' | '
simple_newsletter_html = newsletter_html.xpath(xpath_query).to_html
md = ReverseMarkdown.convert(simple_newsletter_html, unknown_tags: :bypass)
raise "Failed to convert newsletter content to markdown" if md.empty?
# remove goodbits ad ("Try Goodbits for free!")
md.gsub!(/^.*Goodbits.*$/, '')
date = Time.now
begin
author = newsletter_html
.xpath("//table[@class='row'][2] //a[contains(@href, 'twitter.com/') or contains(@href, 'github.com/') and not(contains(@href, 'rails'))]")
.first['href']
.split('/')
.last
rescue
author = 'chancancode'
end
meta = %|---
layout: post
title: "#{title}"
categories: news
author: #{author}
published: true
date: #{date.to_s}
---
|
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}"