Skip to content

Commit 4780e6d

Browse files
committed
Move content regexp to article
1 parent 5f3b0cd commit 4780e6d

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

_plugins/generators.rb

+14-12
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,31 @@
22

33
class RSSGenerator < Jekyll::Generator
44
def generate(site)
5-
@index_page = site.pages.find { |page| page.name == "index.md" }
5+
@content = site.pages.find { |page| page.name == "index.md" }.content
66
site.pages.each do |page|
77
page.data['articles'] = articles if %w(feed.xml rss.xml).include?(page.name)
88
end
99
end
1010

1111
private
1212
def articles
13-
@articles ||= Article.all(rows)
14-
end
15-
16-
def rows
17-
@index_page.content.split(/#articles\s*}\n\n/).last.split(/## \[동영상\]/).first.split(/\n/)
13+
@articles ||= Article.all(@content)
1814
end
1915
end
2016

2117
class Article
2218
ROW_MATCHER = /^-\s*\[(?<title>[^\]]+)\]\((?<url>[^)]+)\){:\s*.article(?<data>(\s*data-\w+="[^"]+")+)\s*}\s*$/
2319
DATA_MATCHER = /data-(?<key>\w+)="(?<value>[^"]+)"/
20+
CONTEXT_MATCHER = /#articles\s*}\n+(?<rows>(#{ROW_MATCHER}\n)+)\n*## \[동영상\]/m
2421
InvalidRow = Class.new(RuntimeError)
22+
InvalidContent = Class.new(RuntimeError)
23+
2524
attr_reader :info
2625

27-
def self.all(rows)
26+
def self.all(content)
27+
m = content.match(CONTEXT_MATCHER)
28+
raise InvalidContent, "invalid content: #{content}" unless m
29+
rows = m[:rows].split("\n")
2830
rows.map { |row| new(row).to_h }
2931
end
3032

@@ -37,21 +39,21 @@ def initialize(row)
3739
end
3840

3941
def to_h
40-
@info
42+
info
4143
end
4244

4345
private
4446
def set_title(m)
45-
if @info["tags"].include?("translated")
46-
@info["title"] = "[번역] #{m[:title]}"
47+
if info["tags"].include?("translated")
48+
info["title"] = "[번역] #{m[:title]}"
4749
else
48-
@info["title"] = m[:title]
50+
info["title"] = m[:title]
4951
end
5052
end
5153

5254
def set_data(m)
5355
m[:data].scan(DATA_MATCHER) do |key, value|
54-
@info[key] = value
56+
info[key] = value
5557
end
5658
end
5759
end

spec/generators_spec.rb

+12-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@
66

77
describe Article do
88
describe ".all" do
9-
subject(:articles) { Article.all([
10-
'- [title](https://link.to.article){: .article data-date="2015.01.12" data-tags="translated ruby" data-author="author" }',
11-
'- [title](https://link.to.article){: .article data-date="2015.01.12" data-tags="ruby" data-author="author" }',
12-
]) }
13-
it { expect(articles.size).to be(2) }
9+
context 'when invalid row' do
10+
let(:content) { "" }
11+
subject(:articles) { Article.all(content) }
12+
it { expect { articles }.to raise_error Article::InvalidContent }
13+
end
14+
15+
context 'when index.md' do
16+
let(:content) { `cat index.md` }
17+
subject(:articles) { Article.all(content) }
18+
it { expect(articles.class).to be(Array)}
19+
it { expect(articles.size).to be > 1 }
20+
end
1421
end
1522

1623
describe "#new" do

0 commit comments

Comments
 (0)