This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Rakefile
96 lines (82 loc) · 2.12 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
88
89
90
91
92
93
94
95
96
require 'open-uri'
require 'date'
# template creator
class Template
attr_accessor :url, :source, :type, :date
def initialize(url)
self.url = url
self.source = open(url, &:read)
self.type = url =~ /weekly-updates/ ? :weekly : :articles
date_str = source.match(%r{>(\d{4}-\d{2}-\d{2})</time>})[1]
self.date = Date.parse(date_str)
end
def content
<<-TEMPLATE.gsub(/^ */, '')
---
layout: post
title: #{translated_title}
author: #{author}
ref: #{title}
refurl: #{url}
translator:
- <a href="GITHUB_URL" target="_blank">번역자 이름</a>
---
#{article}
TEMPLATE
end
def filepath
File.join(type.to_s, '_posts', filename)
end
private
def article
text = source.force_encoding('UTF-8').match(%r{<article>(.+?)</article>}m)[1]
text.gsub! %r{<li>}, ' - '
text.gsub! %r{<a href="(.+?)">(.+?)</a>}, '[\2](\1)'
text.gsub! %r{<h3 id=".+?">(.+?)</h3>}, '### \1'
text.gsub! %r{<div class="blogpost-header">.+?</div>|</?p>|</li>|</?ul>}m, ''
end
def translated_title
if type == :weekly
"Node.js 주간 뉴스 #{date.strftime('%Y년 %-m월 %-d일')}"
else
'번역글의 제목'
end
end
def url_title
if type == :weekly
type
else
url.match(%r{blog/(.+?)/?$})[1].tr('/', '-')
end
end
def filename
"#{date.strftime('%Y-%m-%d')}-#{url_title}.md"
end
def title
source.match(%r{<h1>(.+?)</h1>})[1]
end
def author
matches = source.match(/<span class="blogpost-meta">by (.+?), <time/)
matches ? matches[1] : 'Node'
end
end
namespace :create do
desc 'Create a new article'
task :articles, [:url] do |_, args|
url = args[:url]
create_template(url)
end
private
def create_template(url)
template = Template.new(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