#!/usr/bin/env ruby # encoding: UTF-8 # coding: UTF-8 require 'fileutils' # 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 ' 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 "md2pot generates a pot file from a md file." puts "\nUse:" puts " md2pot [md file] [option]" puts "\nOption:" puts " -j Joins to an existing pot file." puts "\nExamples:" puts " md2pot file.md" puts " md2pot 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]) # Checks 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 # Creates 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).split("\n")[5..-1].join("\n") pot_clean.gsub!(/\n"\s/, "\n" + '"') pot_clean.gsub!(/"Language.*?\n/, '') pot_clean.gsub!(/"PO-Revision-Date:.*?\n/, '') pot_clean.gsub!('"Report-Msgid-Bugs-To: ', '"Report-Msgid-Bugs-To: ' + name) pot_clean.gsub!('PACKAGE VERSION', File.basename(md_path, '.*') + ' 1.0') pot_clean.gsub!('FULL NAME ', name) pot_clean.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)