forked from nodejs/nodejs-ko
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRakefile
87 lines (74 loc) · 1.77 KB
/
Rakefile
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
require 'open-uri'
require 'date'
class Template
attr_reader :type, :url, :source
def initialize(type, url)
@type, @url = type, url
end
def content
<<-TEMPLATE.gsub(/^ */, '')
---
layout: post
title: 번역글의 제목
author: #{author}
ref: #{title}
refurl: #{url}
translator:
- <a href="GITHUB_URL" target="_blank">번역자 이름</a>
---
Content.
TEMPLATE
end
def filepath
File.join(type.to_s, '_posts', filename)
end
private
def source
@source ||= open(url, &:read)
end
def url_title
if type == :weekly
type
else
url.scan(/([^\/]+)-\w+$/).first.first
end
end
def date
date_str = source.scan(/<meta property="article:published_time" content="(.+?)">/).first.first
Date.parse(date_str).strftime("%Y-%m-%d")
end
def filename
"#{date}-#{url_title}.md"
end
def title
source.scan(/<title>(.+?) — Medium<\/title>/).first.first
end
def author
source.scan(/<link rel="author" href="https:\/\/medium\.com\/@(.+?)">/).first.first
end
end
namespace :create do
desc "Create a new article"
task :articles, [:url] do |_, args|
url = args[:url]
create_template(:articles, url)
end
desc "Create a new weekly news"
task :weekly, [:url] do |_, args|
url = args[:url]
create_template(:weekly, url)
end
private
def create_template(type, url)
template = Template.new(type, url)
$stderr.print "Creating template `#{template.filepath}'... "
if File.exist?(template.filepath)
warn "Could not create template, `#{template.filepath}' already exists."
else
File.open(template.filepath, 'w') {|f| f.write template.content }
warn 'done.'
end
rescue => e
warn e.message
end
end