perrotuerto.blog/old/config/build/generate-indexes-feeds

127 lines
4.6 KiB
Ruby
Executable File

#!/usr/bin/env ruby
# encoding: UTF-8
# coding: UTF-8
require 'date'
require 'fileutils'
require File.realdirpath(__FILE__).gsub(/build.*$/, '') + "template/site/main_lang.rb"
# Displays message when something goes wrong
def check condition, err_msg
if condition then puts err_msg + ' For help use -h or --help.'; abort end
end
# To the root directory
Dir.chdir(File.dirname(__FILE__) + '/../..')
# Variables
locale = ''
content = []
template_dir = 'config/template/site/'
# Displays help
if ARGV[0] =~ /-h/
puts "create-indexes-feeds generates the index and feed file for each language."
puts "\nUse:"
puts " create-indexes-feeds"
abort
end
# Formats date in RFC 2822
def format_date date
raw_date = date.gsub(/[\/:]/, ',').gsub(/[\s+|[A-Za-zÁÉÍÓÚÜáéíóúü]+]/, '').split(',')
raw_date = raw_date.reject{|e| e.empty?}
par_date = DateTime.new(raw_date[0].to_i, raw_date[1].to_i, raw_date[2].to_i,
raw_date[3].to_i, raw_date[4].to_i, raw_date[5].to_i,
DateTime.now.to_s[-6..-6] + DateTime.now.to_s[-4..-4])
return par_date.rfc2822
end
# Itinerates each locale
Dir.glob('content/html/*').each do |local|
locale = local.split('/').last
content = []
rss = []
# Gets the metadata of each post
Dir.glob(local + '/*').each do |f|
if File.basename(f) =~ /^\d+/
file = f.split('/').last
title = File.read(f)
.gsub(/\n/, '')
.split(/<section>\s+<h1[^<]*?>/).last
.split(/<\/h1>/).first
.strip
date = File.read(f)
.gsub(/\n/, '')
.gsub(/^.*?#{$template_lang[locale]['published']}\s+([^<]+?)<.*$/, '\1')
.gsub(/\s*\|\s*/, '')
.strip
if title != '' && title !~ /DOCTYPE/
content.push(' <div class="post">' + "\n" +
' <p><a href="' + file + '">' + file.gsub(/_.*$/, '').to_i.to_s + '. ' + title + '</a></p>' + "\n" +
' <p>[' + $template_lang[locale]['published'] + ' ' + date + ']</p>' + "\n" +
' </div>')
end
end
end
# Inverse posts order from the newest to the oldest
content.sort!.reverse!
# Creates the items for RSS feed
content.each do |c|
title = c.split('">').last.split('</a>').first
link = 'https://perrotuerto.blog/content/html/' + locale + '/' + c.split('href="').last.split('"').first
date = format_date(c.split('[').last.split(']').first)
rss.push(' <item>' + "\n" +
' <title>' + title + '</title>' + "\n" +
' <link>' + link + '</link>' + "\n" +
' <pubDate>' + date + '</pubDate>' + "\n" +
' <guid isPermaLink="false">guid' + title.split('.')[0] + '</guid>' + "\n" +
' </item>')
end
# Cleans and adds headers and footer for index
content.unshift(File.read(template_dir + 'header.html'))
content.unshift(File.read(template_dir + 'head.html'))
content.push(File.read(template_dir + 'footer.html'))
content = content.join("\n").gsub(/\n\n/, "\n")
# Replaces some strings for index
content = content.gsub('@file', 'index.html')
content = content.gsub('@locale', locale)
content = content.gsub('@title', $template_lang[locale]['main'])
content = content.gsub('@links', $template_lang[locale]['links'])
content = content.gsub('@about', $template_lang[locale]['about'])
content = content.gsub('@contact', $template_lang[locale]['contact'])
content = content.gsub('@fork', $template_lang[locale]['fork'])
content = content.gsub('@donate', $template_lang[locale]['donate'])
content = content.gsub('@copyfarleft', $template_lang[locale]['copyfarleft'])
content = content.gsub('@copyleft', $template_lang[locale]['copyleft'])
content = content.gsub('@license1', $template_lang[locale]['license1'])
content = content.gsub('@license2', $template_lang[locale]['license2'])
content = content.gsub('@build', $template_lang[locale]['build'])
content = content.gsub('@date', Time.now.strftime('%Y/%m/%d, %H:%M'))
# Creates index
file = File.open(local + '/index.html', 'w:utf-8')
file.puts content
file.close
# Cleans and add header and footer for RSS
rss.unshift(File.read(template_dir + 'head.xml'))
rss.push(File.read(template_dir + 'foot.xml'))
rss = rss.join("\n").gsub(/\n\n/, "\n")
# Replaces some strings for RSS
rss = rss.gsub('@locale', locale)
rss = rss.gsub('@date', format_date(Time.now.strftime('%Y/%m/%d, %H:%M')))
# Creates RSS
file = File.open(Dir.pwd + '/feed/' + locale + '/rss.xml', 'w:utf-8')
file.puts rss
file.close
end