Skip to content

Commit 269468a

Browse files
committed
Extract Article class
1 parent b6b363b commit 269468a

2 files changed

Lines changed: 57 additions & 28 deletions

File tree

‎_plugins/generators.rb‎

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,57 @@
11
require 'jekyll'
22

33
class RSSGenerator < Jekyll::Generator
4-
ROW_MATCHER = /^-\s*\[(?<title>[^\]]+)\]\((?<url>[^)]+)\){:\s*.article(?<data>(\s*data-\w+="[^"]+")+)\s*}\s*$/
5-
DATA_MATCHER = /data-(?<key>\w+)="(?<value>[^"]+)"/
64
def generate(site)
75
@index_page = site.pages.find { |page| page.name == "index.md" }
86
site.pages.each do |page|
97
page.data['articles'] = articles if %w(feed.xml rss.xml).include?(page.name)
108
end
119
end
1210

11+
private
1312
def articles
14-
unless @articles
15-
@articles = []
16-
rows.each do |row|
17-
article = {}
18-
m = row.match(ROW_MATCHER)
19-
m[:data].scan(DATA_MATCHER) do |key, value|
20-
article[key] = value
21-
end
22-
set_title(article, m)
23-
article["url"] = m[:url]
24-
@articles << article
25-
end
26-
end
27-
@articles
13+
@articles ||= Article.all(rows)
2814
end
2915

30-
private
3116
def rows
3217
@index_page.content.split(/#articles\s*}\n\n/).last.split(/## \[동영상\]/).first.split(/\n/)
3318
end
19+
end
3420

35-
def set_title(article, m)
36-
if article["tags"].include?("translated")
37-
article["title"] = "[번역] #{m[:title]}"
21+
class Article
22+
ROW_MATCHER = /^-\s*\[(?<title>[^\]]+)\]\((?<url>[^)]+)\){:\s*.article(?<data>(\s*data-\w+="[^"]+")+)\s*}\s*$/
23+
DATA_MATCHER = /data-(?<key>\w+)="(?<value>[^"]+)"/
24+
InvalidRow = Class.new(RuntimeError)
25+
attr_reader :info
26+
27+
def self.all(rows)
28+
rows.map { |row| new(row).to_h }
29+
end
30+
31+
def initialize(row)
32+
m = row.match(ROW_MATCHER)
33+
raise InvalidRow, "invalid row: #{row}" unless m
34+
@info = { "url" => m[:url] }
35+
set_data(m)
36+
set_title(m)
37+
end
38+
39+
def to_h
40+
@info
41+
end
42+
43+
private
44+
def set_title(m)
45+
if @info["tags"].include?("translated")
46+
@info["title"] = "[번역] #{m[:title]}"
3847
else
39-
article["title"] = m[:title]
48+
@info["title"] = m[:title]
49+
end
50+
end
51+
52+
def set_data(m)
53+
m[:data].scan(DATA_MATCHER) do |key, value|
54+
@info[key] = value
4055
end
4156
end
4257
end

‎spec/generators_spec.rb‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,32 @@
22
describe RSSGenerator do
33
subject(:generator) { RSSGenerator.new }
44
it { is_expected.not_to be_nil }
5+
end
56

6-
describe "#articles" do
7-
before do
8-
allow(subject).to receive(:rows).and_return([
7+
describe Article do
8+
describe ".all" do
9+
subject(:articles) { Article.all([
910
'- [title](https://link.to.article){: .article data-date="2015.01.12" data-tags="translated ruby" data-author="author" }',
1011
'- [title](https://link.to.article){: .article data-date="2015.01.12" data-tags="ruby" data-author="author" }',
11-
])
12+
]) }
13+
it { expect(articles.size).to be(2) }
14+
end
15+
16+
describe "#new" do
17+
subject(:article) { Article.new(row) }
18+
context 'when translated row' do
19+
let(:row) { '- [title](https://link.to.article){: .article data-date="2015.01.12" data-tags="translated ruby" data-author="author" }' }
20+
it { expect(subject.to_h["title"]).to eq("[번역] title") }
1221
end
1322

14-
it { expect(subject.articles.size).to be(2) }
15-
it { expect(subject.articles.first["title"]).to eq("[번역] title") }
16-
it { expect(subject.articles.last["title"]).to eq("title") }
23+
context 'when normal row' do
24+
let(:row) { '- [title](https://link.to.article){: .article data-date="2015.01.12" data-tags="ruby" data-author="author" }' }
25+
it { expect(subject.to_h["title"]).to eq("title") }
26+
end
1727

28+
context 'when invalid row' do
29+
let(:row) { '' }
30+
it { expect { article }.to raise_error Article::InvalidRow }
31+
end
1832
end
1933
end

0 commit comments

Comments
 (0)