Development of mdtopot and pottopos

This commit is contained in:
Nika Zhenya 2019-01-28 21:39:10 -06:00
parent cc33be7852
commit c8af01b56c
2 changed files with 158 additions and 0 deletions

93
config/build/mdtopot Executable file
View File

@ -0,0 +1,93 @@
#!/usr/bin/env ruby
# encoding: UTF-8
# coding: UTF-8
require 'simple_po_parser'
require 'fileutils'
require 'time'
require 'json'
# 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
name = 'Nika Zhenya <nika.zhenya@cliteratu.re>'
pot_dir = 'content/pot/'
md_dir = 'content/md/'
md = ''
md_clean = []
md_path = ''
js = ''
js_path = ''
pot = ''
pot_clean = ''
pot_path = ''
# Displays help
if ARGV[0] =~ /-h/
puts "mdtopot generates a pot file from a md file."
puts "\nUse:"
puts " mdtopot [md file] [option]"
puts "\nOption:"
puts " -j Joins to an existing pot file."
puts "\nExamples:"
puts " mdtopot file.md"
puts " mdtopot file.md -j"
puts "\nThe md file has to be in '#{md_dir}' and the pot file is going to be stored in '#{pot_dir}'."
abort
end
# Checks if the user gave the path to the file
check(ARGV[0] == nil, "ERROR: File is required.")
# Cleans path
md_path = md_dir + File.basename(ARGV[0])
# Check if the file exists
check(File.exist?(md_path) == false, "ERROR: File doesn't exist.")
# Gets the content and create an array where each element is a md text block
md = File.read(md_path).split("\n\n").reject{|c| c.empty?}
# Cleans md with special attention to lists
md.each do |block|
if block.strip =~ /^[\*\-\+]\s+/ || block.strip =~ /^\d+\.\s+/
block.gsub!("\n", '\n' + " \\\n")
else
block.gsub!("\n", " \\\n")
end
md_clean.push('_("' + block.gsub('"', '\"') + '")')
end
# Creates a temporary js file
js_path = md_dir + File.basename(md_path, '.*') + '.js'
js = File.open(js_path, 'w:UTF-8')
js.puts md_clean
js.close
# Create pot file based in js file
pot_path = pot_dir + File.basename(md_path, '.*') + '.pot'
system("xgettext #{js_path} #{ARGV[1] != nil ? '-j' : ''} --language=JavaScript --from-code=UTF-8 -o #{pot_path}")
# Cleans pot file
pot_clean = File.read(pot_path)
pot_clean.gsub!(/\n"\s/, "\n\"")
.gsub!(/#[\s,].*?\n/, '')
.gsub!(/"Language.*?\n/, '')
.gsub!(/"PO-Revision-Date:.*?\n/, '')
.gsub!('"Report-Msgid-Bugs-To: ', '"Report-Msgid-Bugs-To: ' + name)
.gsub!('PACKAGE VERSION', File.basename(md_path, '.*') + ' 1.0')
.gsub!('FULL NAME <EMAIL@ADDRESS>', name)
.gsub!('charset=CHARSET', 'charset=UTF-8')
pot = File.open(pot_path, 'w:UTF-8')
pot.puts pot_clean
pot.close
# Removes temporary js file
FileUtils.rm(js_path)

65
config/build/pottopos Executable file
View File

@ -0,0 +1,65 @@
#!/usr/bin/env ruby
# encoding: UTF-8
# coding: UTF-8
require 'simple_po_parser'
require 'fileutils'
require 'time'
require 'json'
# 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
pot_dir = 'content/pot/'
pot_path = ''
po_dir = 'content/po/'
po_name = ''
locales = ['en', 'es']
# Displays help
if ARGV[0] =~ /-h/
puts "pottopos generates po files from a pot file."
puts "\nUse:"
puts " pottopos [pot file] [option]"
puts "\nOption:"
puts " -m Merges to an existing po file."
puts "\nExamples:"
puts " pottopos file.pot"
puts " pottopos file.pot -m"
puts "\nCurrent supported locales:"
puts " " + locales.join("\n ")
puts "\nThe pot file has to be in '#{pot_dir}' and the po files are going to be stored in '#{po_dir}/*', where '*' is 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
pot_path = pot_dir + File.basename(ARGV[0])
po_name = File.basename(ARGV[0], '.*') + '.po'
# Check if the file exists
check(File.exist?(pot_path) == false, "ERROR: File doesn't exist.")
# Creates locale directory if need it
locales.each do |l|
if File.exist?(po_dir + l) == false
Dir.mkdir(po_dir + l)
end
end
# Creates or merges po files
locales.each do |l|
if ARGV[1] == nil
system("msginit --input=#{pot_path} --locale=#{l} --no-translator --output=#{po_dir + l}/#{po_name}")
else
system("msgmerge --update #{po_dir + l}/#{po_name} #{pot_path}")
end
end