2
2
3
3
class RSSGenerator < Jekyll ::Generator
4
4
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
6
6
site . pages . each do |page |
7
7
page . data [ 'articles' ] = articles if %w( feed.xml rss.xml ) . include? ( page . name )
8
8
end
9
9
end
10
10
11
11
private
12
12
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 )
18
14
end
19
15
end
20
16
21
17
class Article
22
18
ROW_MATCHER = /^-\s *\[ (?<title>[^\] ]+)\] \( (?<url>[^)]+)\) {:\s *.article(?<data>(\s *data-\w +="[^"]+")+)\s *}\s *$/
23
19
DATA_MATCHER = /data-(?<key>\w +)="(?<value>[^"]+)"/
20
+ CONTEXT_MATCHER = /#articles\s *}\n +(?<rows>(#{ ROW_MATCHER } \n )+)\n *## \[ 동영상\] /m
24
21
InvalidRow = Class . new ( RuntimeError )
22
+ InvalidContent = Class . new ( RuntimeError )
23
+
25
24
attr_reader :info
26
25
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 " )
28
30
rows . map { |row | new ( row ) . to_h }
29
31
end
30
32
@@ -37,21 +39,21 @@ def initialize(row)
37
39
end
38
40
39
41
def to_h
40
- @ info
42
+ info
41
43
end
42
44
43
45
private
44
46
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 ] } "
47
49
else
48
- @ info[ "title" ] = m [ :title ]
50
+ info [ "title" ] = m [ :title ]
49
51
end
50
52
end
51
53
52
54
def set_data ( m )
53
55
m [ :data ] . scan ( DATA_MATCHER ) do |key , value |
54
- @ info[ key ] = value
56
+ info [ key ] = value
55
57
end
56
58
end
57
59
end
0 commit comments