#!/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] == '
' content = content.gsub("Mariana Eguaras's blog", "Mariana Eguaras's blog") 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] content.push( '

' + get_date(data[0]) + ' | ' + data[1] + ' | ' + 'EPUB / ' + 'MOBI / ' + 'original' + '

' ) 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*