forked from developer-portal/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss.rb
executable file
·130 lines (113 loc) · 4.18 KB
/
rss.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/ruby
# NOTE: We now use the Python implementation of the script available as rss.py.
#
# This script can update the title page from any RSS feed, but concrete
# adjustments are done specifically to Fedora Planet.
#
# It replaces the content between <!-- BLOG_HEADLINES_START -->
# and <!-- BLOG_HEADLINES_END --> in _site/index.html. The file
# path of index.html can be passed as an argument.
#
# Usage:
#
# ./rss.rb _site/index.html
if ARGV[0]
puts "Setting the index file to: #{ARGV[0]}"
index_file = ARGV[0]
end
require 'rss'
# Templating
require 'liquid'
# For striping HTML tags from post descriptions
require 'action_view'
# Following adjustments are based on Fedora Planet RSS feed.
# If you are changing the feed make sure the changes still apply!
FEED_URL = 'http://fedoraplanet.org/rss20.xml'.freeze
class Article
attr_accessor :author, :title, :description, :date, :url
# This is needed because we use liquid templates
liquid_methods :author, :title, :description, :date, :url
def initialize(author, title, description, date, url)
@author = author
@title = title
@description = description
@date = date
@url = url
end
end
@articles = []
puts "Fetching #{FEED_URL} feed..."
rss = RSS::Parser.parse(FEED_URL, false)
rss.items.take(4).each do |item|
# Extract name from the title
# Original title: NAME: TITLE
# Expected title: TITLE
# Expected name: NAME
author = item.title.gsub(/([^:]*):(.*)/, '\\1').strip
title = item.title.gsub(/([^:]*):(.*)/, '\\2').strip
# Avoid HTML in description
description = ActionView::Base.full_sanitizer.sanitize(item.description)
# Shorter description if necessary
if description && description.length > 140
# Take whole words rather than xy letters
description = description.split[0...25].join(' ')
description += '…' unless ['!', '?', '.'].include? description[-1]
end
# Adjust date, strip time
# Original date: Tue, 08 Sep 2015 10:54:28 +0000
# Expected date: Tue, 08 Sep 2015
regexp = /([a-zA-Z]{3}\, [0-9]{2} [a-zA-Z]{3} [0-9]{4}).*/
date = item.pubDate.to_s.gsub(regexp, '\\1')
@articles << Article.new(author, title, description, date, item.link)
end
template = <<TEMPLATE
<!-- BLOG_HEADLINES_START -->
<div class="container" id="blog-headlines">
<div class="container">
<div class="row">
<div class="col-sm-12">
<h2><span>Fedora Planet</span></h2>
</div>
</div>
<div class="row">
<div class="col-sm-6 blog-headlines">
<article>
<h3><a href="{{ articles[0].url }}">{{ articles[0].title }}</a></h3>
<p>{{ articles[0].description }}</p>
<p><a href="{{ articles[0].url }}">Read more</a></p>
<p class="byline">by <span class="author">{{ articles[0].author }}</span> <span class="date">{{ articles[0].date }}</span></p>
</article>
<article>
<h3><a href="{{ articles[1].url }}">{{ articles[1].title }}</a></h3>
<p>{{ articles[1].description }}</p>
<p><a href="{{ articles[1].url }}">Read more</a></p>
<p class="byline">by <span class="author">{{ articles[1].author }}</span> <span class="date">{{ articles[1].date }}</span></p>
</article>
</div>
<div class="col-sm-6 blog-headlines">
<article>
<h3><a href="{{ articles[1].url }}">{{ articles[2].title }}</a></h3>
<p>{{ articles[2].description }}</p>
<p><a href="{{ articles[2].url }}">Read more</a></p>
<p class="byline">by <span class="author">{{ articles[2].author }}</span> <span class="date">{{ articles[2].date }}</span></p>
</article>
<article>
<h3><a href="{{ articles[3].url }}">{{ articles[3].title }}</a></h3>
<p>{{ articles[3].description }}</p>
<p><a href="{{ articles[3].url }}">Read more</a></p>
<p class="byline">by <span class="author">{{ articles[3].author }}</span> <span class="date">{{ articles[3].date }}</span></p>
</article>
</div>
</div>
</div>
</div>
<!-- BLOG_HEADLINES_END -->
TEMPLATE
blog_posts = Liquid::Template.parse(template).render 'articles' => @articles
index_file ||= File.expand_path('_site/index.html', '.')
contents = File.open(index_file).read.force_encoding('UTF-8')
contents.gsub!(/<!-- BLOG_HEADLINES_START -->.*<!-- BLOG_HEADLINES_END -->/im,
"\\1#{blog_posts}\\3")
File.open(index_file, 'w') do |file|
file.write(contents)
end