perrotuerto.blog/public/config/build/pos2htmls

137 lines
4.7 KiB
Ruby
Executable File

#!/usr/bin/env ruby
# encoding: UTF-8
# coding: UTF-8
require 'simple_po_parser'
require 'fileutils'
require 'time'
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
po_dir = 'content/po/'
po_path = ''
html_dir = 'content/html/'
html_name = ''
html_tmp = ''
md_name = ''
template_dir = 'config/template/site/'
# Displays help
if ARGV[0] =~ /-h/
puts "pos2htmls generates html files from pos files."
puts "\nUse:"
puts " pos2htmls [po file]"
puts "\nExamples:"
puts " pos2htmls file.po"
puts "\nAll locales are converted to HTML."
puts "\nThe po file has to be in '#{po_dir}*' and the html files are going to be stored in '#{html_dir}', where '*' are the locales folders."
abort
end
# Checks if the user gave the path to the file
check(ARGV[0] == nil, "ERROR: File is required.")
# Cleans path
po_path = po_dir + 'en/' + File.basename(ARGV[0])
html_name = File.basename(ARGV[0], '.*') + '.html'
# Checks if the file exists
check(File.exist?(po_path) == false, "ERROR: File doesn't exist.")
# Creates each HTML file
Dir.glob(po_dir + '*/*.po').each do |po|
if File.basename(po) == File.basename(po_path)
hash = {
'locale' => po.gsub(po_dir, '').split('/')[0],
'file' => html_name,
'title' => '',
'content' => []
}
# Extracts the translations
SimplePoParser.parse(po)[1..-1].each do |e|
translation = e[:msgstr]
if translation.class == Array
translation = translation.join('')
end
hash['content'].push(translation + "\n\n")
end
hash['content'].map!{|l| l.gsub('\\n', "\n")}
# Creates locale folder if it doesn't exists
if !File.directory?(html_dir + hash['locale'])
Dir.mkdir(html_dir + hash['locale'])
end
Dir.chdir(html_dir + hash['locale'])
# Creates a temporary MD file
md_name = File.basename(hash['file'], '.*') + '.md'
file = File.open(md_name, 'w:utf-8')
file.puts hash['content']
file.close
# Creates a temporary HTML file
puts "Creating #{html_dir + hash['locale'] + '/' + html_name}"
system("pc-pandog -i #{md_name} -o #{html_name}")
FileUtils.rm(md_name)
# Gets the title
hash['title'] = File.read(html_name)
.gsub(/\n/, '')
.gsub(/<style>[^<]*?<\/style>/, '')
.split(/<body>\s+<h1[^<]*?>/).last
.split(/<\/h1>/).first.strip
.gsub(/<[^<]*?>/, '')
# Cleans headers and footers and adds new headers and footers
html_tmp = File.read(html_name).split(/\n/)[8..-3]
html_tmp = html_tmp.unshift(File.read('../../../' + template_dir + 'header.html'))
html_tmp = html_tmp.unshift(File.read('../../../' + template_dir + 'head.html'))
if hash['file'] =~ /^\d+/
html_tmp = html_tmp.push(' <script type="text/javascript" src="../../../hashover/comments.php"></script>')
end
html_tmp = html_tmp.push(File.read('../../../' + template_dir + 'footer.html'))
# Some unnecessary cleaning
html_tmp = html_tmp.map{|l| l.gsub(/\s{8}/, ' ')}
html_tmp = html_tmp.join("\n").gsub(/\n\n/, "\n")
# Replaces string according the file, the date and the location
html_tmp = html_tmp.gsub('@locale', hash['locale'])
html_tmp = html_tmp.gsub('@title', hash['title'])
html_tmp = html_tmp.gsub('@file', hash['file'])
html_tmp = html_tmp.gsub('@links', $template_lang[hash['locale']]['links'])
html_tmp = html_tmp.gsub('@about', $template_lang[hash['locale']]['about'])
html_tmp = html_tmp.gsub('@contact', $template_lang[hash['locale']]['contact'])
html_tmp = html_tmp.gsub('@fork', $template_lang[hash['locale']]['fork'])
html_tmp = html_tmp.gsub('@donate', $template_lang[hash['locale']]['donate'])
html_tmp = html_tmp.gsub('@copyfarleft', $template_lang[hash['locale']]['copyfarleft'])
html_tmp = html_tmp.gsub('@copyleft', $template_lang[hash['locale']]['copyleft'])
html_tmp = html_tmp.gsub('@license1', $template_lang[hash['locale']]['license1'])
html_tmp = html_tmp.gsub('@license2', $template_lang[hash['locale']]['license2'])
html_tmp = html_tmp.gsub('@published', $template_lang[hash['locale']]['published'])
html_tmp = html_tmp.gsub('@build', $template_lang[hash['locale']]['build'])
html_tmp = html_tmp.gsub('@date', Time.now.strftime('%Y/%m/%d, %H:%M'))
# Save the data
file = File.open(html_name, 'w:utf-8')
file.puts html_tmp
file.close
Dir.chdir('../../..')
end
end