-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgenerators.rb
59 lines (50 loc) · 1.39 KB
/
generators.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
require 'jekyll'
class RSSGenerator < Jekyll::Generator
def generate(site)
@content = site.pages.find { |page| page.name == "index.md" }.content
site.pages.each do |page|
page.data['articles'] = articles if %w(feed.xml rss.xml).include?(page.name)
end
end
private
def articles
@articles ||= Article.all(@content)
end
end
class Article
ROW_MATCHER = /^-\s*\[(?<title>[^\]]+)\]\((?<url>[^)]+)\){:\s*.article(?<data>(\s*data-\w+="[^"]+")+)\s*}\s*$/
DATA_MATCHER = /data-(?<key>\w+)="(?<value>[^"]+)"/
CONTEXT_MATCHER = /#articles\s*}\n+(?<rows>(#{ROW_MATCHER}\n)+)\n*## \[동영상\]/m
InvalidRow = Class.new(RuntimeError)
InvalidContent = Class.new(RuntimeError)
attr_reader :info
def self.all(content)
m = content.match(CONTEXT_MATCHER)
raise InvalidContent, "invalid content: #{content}" unless m
rows = m[:rows].split("\n")
rows.map { |row| new(row).to_h }
end
def initialize(row)
m = row.match(ROW_MATCHER)
raise InvalidRow, "invalid row: #{row}" unless m
@info = { "url" => m[:url] }
set_data(m)
set_title(m)
end
def to_h
info
end
private
def set_title(m)
if info["tags"].include?("translated")
info["title"] = "[번역] #{m[:title]}"
else
info["title"] = m[:title]
end
end
def set_data(m)
m[:data].scan(DATA_MATCHER) do |key, value|
info[key] = value
end
end
end