22
33class 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
1915end
2016
2117class 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
5759end
0 commit comments