#!/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(/