forked from bitcoin-dot-org/Bitcoin.org
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalerts.rb
80 lines (71 loc) · 2.81 KB
/
alerts.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
# This file is licensed under the MIT License (MIT) available on
# http://opensource.org/licenses/MIT.
#alerts.rb generates alert pages using files in _alerts
#and assign them the 'alert' category.
#This is later used to loop through site.pages in order
#to display the alert's list in chronological order, both
#on the "Alerts" page and RSS file.
#If "banner" variable is set in one alert file, site.ALERT
#variable is set, allowing a clickable alert banner to be
#displayed in _layouts/base.html .
#If "alias" variable is set in one alert file, a short alias
#file for the alert (like /android.html) is generated for
#Bitcoin Core non-clickable alerts.
require 'yaml'
module Jekyll
class AlertPage < Page
def initialize(site, base, lang, srcdir, src, dstdir, dst, date)
@site = site
@base = base
@dir = '/'+dstdir
@name = dst
self.process(dst)
self.read_yaml(File.join(base, srcdir), src)
self.data['lang'] = lang
self.data['date'] = date
self.data['layout'] = 'alert'
if dstdir == ''
self.data['canonical'] = '/en/alert/' + src.split('.')[0]
else
self.data['category'] = 'alert'
if self.data.has_key?('banner') and !self.data['banner'].nil? and self.data['banner'].length>0
site.config['ALERT']='<a href="/'+dstdir+'/'+dst.gsub('.html','')+'">'+self.data['banner']+'</a>'
end
if self.data.has_key?('active') and !self.data['active'].nil? and self.data['active'] == true
site.config['STATUS']=1
end
if self.data.has_key?('alias')
site.pages << AlertPage.new(site, base, lang, srcdir, src, '', self.data['alias']+'.html', date)
site.pages << AlertPage.new(site, base, lang, srcdir, src, '', self.data['alias']+'/index.html', date)
end
end
end
end
class AlertPageGenerator < Generator
def generate(site)
#Generate each alert based on templates
site.config['STATUS'] = 0
#Do nothing if plugin is disabled
if !ENV['ENABLED_PLUGINS'].nil? and ENV['ENABLED_PLUGINS'].index('alerts').nil?
print 'Alerts disabled' + "\n"
return
end
Dir.foreach('_alerts') do |file|
next if file == '.' or file == '..'
lang = 'en'
src = file
dst = file
srcdir = '_alerts'
dstdir = lang + '/alert'
date = dst.split('-')
next if date.length < 4
date = date[0] + '-' + date[1] + '-' + date[2]
next if !/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/.match(date)
site.pages << AlertPage.new(site, site.source, lang, '_alerts', src, dstdir, dst, date)
end
#TODO alerts are only generated for english language,
#but they could also be translated at some point. They would however
#need to fallback to english when no translation is available.
end
end
end