159 lines
3.9 KiB
Ruby
159 lines
3.9 KiB
Ruby
#!/usr/bin/env ruby
|
|
# encoding: UTF-8
|
|
# coding: UTF-8
|
|
|
|
require 'fileutils'
|
|
require 'time'
|
|
require 'json'
|
|
|
|
Encoding.default_internal = Encoding::UTF_8
|
|
Dir.chdir(File.dirname(__FILE__))
|
|
|
|
# Variables
|
|
$language = 'en-US'
|
|
$site_name = 'Publishing is Coding: Change My Mind'
|
|
$site_description = 'A broken english version of some entries published in Mariana Eguaras\'s blog.'
|
|
$site_keywords = 'publishing, blog, book, ebook, methodology, foss, libre-software, format, markdown, html, epub, pdf, mobi, latex, tex'
|
|
$site_link = 'https://blog.cliteratu.re'
|
|
$site_img = 'icon.png'
|
|
$author_name = 'Nika Zhenya'
|
|
$author_email = 'nika.zhenya@cliteratu.re'
|
|
$date = Time.now.to_s.split(' ')[0]
|
|
$rss = {
|
|
:channel => {
|
|
:title => $site_name,
|
|
:link => $site_link,
|
|
:description => $site_description,
|
|
:language => $language,
|
|
:managingEditor => $author_email + ' (' + $author_name + ')',
|
|
:lastBuildDate => '',
|
|
:image => {
|
|
:title => $site_name,
|
|
:url => $site_link + '/' + $site_img,
|
|
:link => $site_link
|
|
},
|
|
:items => []
|
|
}
|
|
}
|
|
|
|
# Definitions
|
|
|
|
# Gets date in a proper format
|
|
def get_date d, rfc = false
|
|
d = d.split('-')
|
|
|
|
def month m
|
|
months = [
|
|
'January', 'February', 'March', 'April',
|
|
'May', 'June', 'July', 'August',
|
|
'September', 'October', 'November', 'December'
|
|
]
|
|
|
|
return months[m.to_i - 1]
|
|
end
|
|
|
|
if rfc
|
|
return Time.new(d[0], d[1], d[2]).rfc2822
|
|
else
|
|
return month(d[1]) + ' ' +
|
|
d[2].to_i.to_s + ', ' +
|
|
d[0]
|
|
end
|
|
end
|
|
|
|
# Converts MD in other formats
|
|
def convert_md md
|
|
content = []
|
|
original = {:link => '', :pubDate => ''}
|
|
item = {
|
|
:guid => $site_link + '/html/' + File.basename(md, '.*'),
|
|
:title => '',
|
|
:link => $site_link + '/html/' + File.basename(md, '.*') + '.html',
|
|
:description => '',
|
|
:author => $author_email + ' (' + $author_name + ')',
|
|
:category => '',
|
|
:pubDate => ''
|
|
}
|
|
|
|
def item_split s
|
|
return s.gsub(/@\S+\['(.*?)'\]/, '\1').strip.split('\',\'')
|
|
end
|
|
|
|
file = File.open(md, 'r:UTF-8')
|
|
file.each_with_index do |l, i|
|
|
if i == 0
|
|
item[:title] = l.gsub(/^#/, '').strip
|
|
end
|
|
|
|
if l =~ /^@original\[.*?\]\s*?$/
|
|
data = item_split(l)
|
|
content.push(
|
|
'<p class="original">Spanish: ' +
|
|
get_date(data[0]) + ' | ' +
|
|
'<a target="_blank" href="' + data[1] + '">' +
|
|
'source</a></p>'
|
|
)
|
|
elsif l =~ /^@current\[.*?\]\s*?$/
|
|
data = item_split(l)
|
|
item[:pubDate] = get_date(data[0], true)
|
|
item[:category] = data[1]
|
|
item[:description] = data[2]
|
|
content.push(
|
|
'<p class="current">English: ' +
|
|
get_date(data[0]) + ' | ' +
|
|
data[1] + '</p>'
|
|
)
|
|
else
|
|
content.push(l)
|
|
end
|
|
end
|
|
|
|
$rss[:channel][:items].push(item)
|
|
end
|
|
|
|
# Converts the RSS to a XML syntax
|
|
def convert_xml file, hash, space = ''
|
|
hash.each do |k, v|
|
|
if k.to_s != 'items'
|
|
file.puts space + '<' + k.to_s + '>'
|
|
|
|
if k.to_s == 'channel'
|
|
file.puts space + ' <atom:link href="' + $site_link + '/feed/rss.xml" rel="self" type="application/rss+xml" />'
|
|
end
|
|
end
|
|
|
|
if v.class == Hash
|
|
convert_xml(file, v, space + ' ')
|
|
elsif v.class == Array
|
|
v.each do |e|
|
|
file.puts space + '<item>'
|
|
convert_xml(file, e, space + ' ')
|
|
file.puts space + '</item>'
|
|
end
|
|
else
|
|
file.puts space + ' ' + v
|
|
end
|
|
|
|
if k.to_s != 'items'
|
|
file.puts space + '</' + k.to_s + '>'
|
|
end
|
|
end
|
|
end
|
|
|
|
# Deployment
|
|
|
|
# Gets MDs to convert in other formats
|
|
Dir.glob('md/*.{md}').each_with_index do |md, i|
|
|
convert_md(File.absolute_path(md))
|
|
end
|
|
|
|
# Builds the RSS
|
|
$rss[:channel][:lastBuildDate] = get_date($date, true)
|
|
$rss[:channel][:items].sort_by!{|h| h[:link]}.reverse!
|
|
xml = File.new('feed/rss.xml', 'w:UTF-8')
|
|
xml.puts '<?xml version="1.0" ?>'
|
|
xml.puts '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">'
|
|
convert_xml(xml, $rss)
|
|
xml.puts '</rss>'
|
|
xml.close
|