254 lines
6.7 KiB
Ruby
254 lines
6.7 KiB
Ruby
#!/usr/bin/env ruby
|
|
# encoding: UTF-8
|
|
# coding: UTF-8
|
|
|
|
require 'fileutils'
|
|
require 'time'
|
|
require 'json'
|
|
|
|
# Also requires Pecas
|
|
|
|
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]
|
|
$head = `cat template/head.html`
|
|
$header = `cat template/header.html`
|
|
$footer = `cat template/footer.html`
|
|
$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
|
|
|
|
# Replaces strings
|
|
def replace_content content, title = 'Main'
|
|
elements = [
|
|
['language', $language],
|
|
['site_name', $site_name],
|
|
['site_description', $site_description],
|
|
['site_keywords', $site_keywords],
|
|
['site_link', $site_link],
|
|
['site_img', $site_img],
|
|
['author_name', $author_name],
|
|
['author_email', $author_email],
|
|
['date', get_date($date)],
|
|
['title', title]
|
|
]
|
|
|
|
elements.each do |e|
|
|
content = content.gsub("$#{e[0]}$", e[1])
|
|
end
|
|
|
|
if content.split("\n")[0] == '<header>'
|
|
content = content.gsub("Mariana Eguaras's blog", "<a target=\"_blank\" href=\"https://marianaeguaras.com/blog/\">Mariana Eguaras's blog</a>")
|
|
end
|
|
|
|
if title == 'Main'
|
|
content.gsub!('href="../', 'href="')
|
|
end
|
|
|
|
return content
|
|
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 => ''
|
|
}
|
|
|
|
# Gets properties from MD
|
|
def item_split s
|
|
return s.gsub(/@\S+\['(.*?)'\]/, '\1').strip.split('\',\'')
|
|
end
|
|
|
|
puts "Building '" + File.basename(md) + "'…"
|
|
|
|
# Analizes MD
|
|
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)
|
|
elsif l =~ /^@current\[.*?\]\s*?$/
|
|
data = item_split(l)
|
|
item[:pubDate] = get_date(data[0], true)
|
|
item[:category] = data[1]
|
|
item[:description] = data[2] + ' <a href="' + item[:link] + '">Read more</a>.'
|
|
content.push(
|
|
'<p class="meta">' +
|
|
get_date(data[0]) + ' | ' +
|
|
data[1] + ' | ' +
|
|
'<span class="smallcap"><a target="_blank" href="' + $site_link + '/epub/' + File.basename(md, ".*") + '.epub">EPUB</a></span> / ' +
|
|
'<span class="smallcap"><a target="_blank" href="' + $site_link + '/mobi/' + File.basename(md, ".*") + '.mobi">MOBI</a></span> / ' +
|
|
'<a target="_blank" href="' + data[3] + '">original</a>' +
|
|
'</p>'
|
|
)
|
|
else
|
|
content.push(l)
|
|
end
|
|
end
|
|
|
|
# Creates an MD with some changes
|
|
html_name = 'html/' + File.basename(md, '.*') + '.html'
|
|
new_md_name = 'html/' + File.basename(md)
|
|
new_md = File.new(new_md_name, 'w:UTF-8')
|
|
new_md.puts content
|
|
new_md.close
|
|
|
|
# Converts MD to HTML
|
|
system("pc-pandog -i #{new_md_name} -o #{html_name}")
|
|
FileUtils.rm(new_md_name)
|
|
|
|
# EPUB and MOBI goes here
|
|
|
|
# Changes some things for the final HTML
|
|
write = false
|
|
new_html = []
|
|
html = File.open(html_name, 'r:UTF-8')
|
|
html.each do |l|
|
|
if write && l !~ /^\s*<style/
|
|
new_html.push(l.gsub(/\s+<\/body>/, ''))
|
|
end
|
|
|
|
if l =~ /^\s*?<body/ || l =~ /^\s*?<\/body/
|
|
write = !write
|
|
end
|
|
end
|
|
html.close
|
|
html = File.open(html_name, 'w:UTF-8')
|
|
html.puts replace_content($head, item[:title]), replace_content($header)
|
|
html.puts new_html
|
|
html.puts replace_content($footer)
|
|
html.close
|
|
|
|
# Adds content to RSS
|
|
$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
|
|
|
|
# Generates core CSS
|
|
Dir.chdir('css')
|
|
system("pc-add --add css")
|
|
Dir.chdir('..')
|
|
|
|
# Gets MDs to convert in other formats
|
|
Dir.glob('md/*.{md}').each_with_index do |md, i|
|
|
convert_md(File.absolute_path(md))
|
|
end
|
|
|
|
# Last info added to RSS
|
|
$rss[:channel][:lastBuildDate] = get_date($date, true)
|
|
$rss[:channel][:items].sort_by!{|h| h[:link]}.reverse!
|
|
|
|
# Builds the index.html
|
|
puts "Building 'index.html'…"
|
|
html_content = []
|
|
$rss[:channel][:items].each do |item|
|
|
inner_html = '<div id="' + item[:guid].split('/').last + '"><p>' +
|
|
'<a href="' + item[:link] + '">' + item[:title] + '</a>' +
|
|
'</p><p class="meta">' + get_date(Time.parse(item[:pubDate]).to_s.split(/\s+/)[0]) +
|
|
' | ' + item[:category] + '</p><p>' + item[:description] + '</p></div>'
|
|
html_content.push(inner_html.gsub!($site_link, '.'))
|
|
end
|
|
html = File.new('index.html', 'w:UTF-8')
|
|
html.puts replace_content($head), replace_content($header)
|
|
html.puts html_content
|
|
html.puts replace_content($footer)
|
|
html.close
|
|
|
|
# Builds the RSS
|
|
puts "Building 'rss.xml'…"
|
|
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
|