Browse code

Development of mdtopot and pottopos

Nika Zhenya authored on 28/01/2019 19:39:10
Showing 2 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,93 @@
1
+#!/usr/bin/env ruby
2
+# encoding: UTF-8
3
+# coding: UTF-8
4
+
5
+require 'simple_po_parser'
6
+require 'fileutils'
7
+require 'time'
8
+require 'json'
9
+
10
+# Displays message when something goes wrong
11
+def check condition, err_msg
12
+  if condition then puts err_msg + ' For help use -h or --help.'; abort end
13
+end
14
+
15
+# To the root directory
16
+Dir.chdir(File.dirname(__FILE__) + '/../..')
17
+
18
+# Variables
19
+name      = 'Nika Zhenya <nika.zhenya@cliteratu.re>'
20
+pot_dir   = 'content/pot/'
21
+md_dir    = 'content/md/'
22
+md        = ''
23
+md_clean  = []
24
+md_path   = ''
25
+js        = ''
26
+js_path   = ''
27
+pot       = ''
28
+pot_clean = ''
29
+pot_path  = ''
30
+
31
+# Displays help
32
+if ARGV[0] =~ /-h/
33
+  puts "mdtopot generates a pot file from a md file."
34
+  puts "\nUse:"
35
+  puts "  mdtopot [md file] [option]"
36
+  puts "\nOption:"
37
+  puts "  -j  Joins to an existing pot file."
38
+  puts "\nExamples:"
39
+  puts "  mdtopot file.md"
40
+  puts "  mdtopot file.md -j"
41
+  puts "\nThe md file has to be in '#{md_dir}' and the pot file is going to be stored in '#{pot_dir}'."
42
+  abort
43
+end
44
+
45
+# Checks if the user gave the path to the file
46
+check(ARGV[0] == nil, "ERROR: File is required.")
47
+
48
+# Cleans path
49
+md_path = md_dir + File.basename(ARGV[0])
50
+
51
+# Check if the file exists
52
+check(File.exist?(md_path) == false, "ERROR: File doesn't exist.")
53
+
54
+# Gets the content and create an array where each element is a md text block
55
+md = File.read(md_path).split("\n\n").reject{|c| c.empty?}
56
+
57
+# Cleans md with special attention to lists
58
+md.each do |block|
59
+  if block.strip =~ /^[\*\-\+]\s+/ || block.strip =~ /^\d+\.\s+/
60
+    block.gsub!("\n", '\n' + " \\\n")
61
+  else
62
+    block.gsub!("\n", " \\\n")
63
+  end
64
+
65
+  md_clean.push('_("' + block.gsub('"', '\"')  + '")')
66
+end
67
+
68
+# Creates a temporary js file
69
+js_path = md_dir + File.basename(md_path, '.*') + '.js'
70
+js = File.open(js_path, 'w:UTF-8')
71
+js.puts md_clean
72
+js.close
73
+
74
+# Create pot file based in js file
75
+pot_path = pot_dir + File.basename(md_path, '.*') + '.pot'
76
+system("xgettext #{js_path} #{ARGV[1] != nil ? '-j' : ''} --language=JavaScript --from-code=UTF-8 -o #{pot_path}")
77
+
78
+# Cleans pot file
79
+pot_clean = File.read(pot_path)
80
+pot_clean.gsub!(/\n"\s/, "\n\"")
81
+         .gsub!(/#[\s,].*?\n/, '')
82
+         .gsub!(/"Language.*?\n/, '')
83
+         .gsub!(/"PO-Revision-Date:.*?\n/, '')
84
+         .gsub!('"Report-Msgid-Bugs-To: ', '"Report-Msgid-Bugs-To: ' + name)
85
+         .gsub!('PACKAGE VERSION', File.basename(md_path, '.*') + ' 1.0')
86
+         .gsub!('FULL NAME <EMAIL@ADDRESS>', name)
87
+         .gsub!('charset=CHARSET', 'charset=UTF-8')
88
+pot = File.open(pot_path, 'w:UTF-8')
89
+pot.puts pot_clean
90
+pot.close
91
+
92
+# Removes temporary js file
93
+FileUtils.rm(js_path)
0 94
new file mode 100755
... ...
@@ -0,0 +1,65 @@
1
+#!/usr/bin/env ruby
2
+# encoding: UTF-8
3
+# coding: UTF-8
4
+
5
+require 'simple_po_parser'
6
+require 'fileutils'
7
+require 'time'
8
+require 'json'
9
+
10
+# Displays message when something goes wrong
11
+def check condition, err_msg
12
+  if condition then puts err_msg + ' For help use -h or --help.'; abort end
13
+end
14
+
15
+# To the root directory
16
+Dir.chdir(File.dirname(__FILE__) + '/../..')
17
+
18
+# Variables
19
+pot_dir  = 'content/pot/'
20
+pot_path = ''
21
+po_dir   = 'content/po/'
22
+po_name  = ''
23
+locales  = ['en', 'es']
24
+
25
+# Displays help
26
+if ARGV[0] =~ /-h/
27
+  puts "pottopos generates po files from a pot file."
28
+  puts "\nUse:"
29
+  puts "  pottopos [pot file] [option]"
30
+  puts "\nOption:"
31
+  puts "  -m  Merges to an existing po file."
32
+  puts "\nExamples:"
33
+  puts "  pottopos file.pot"
34
+  puts "  pottopos file.pot -m"
35
+  puts "\nCurrent supported locales:"
36
+  puts "  " + locales.join("\n  ")
37
+  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."
38
+  abort
39
+end
40
+
41
+# Checks if the user gave the path to the file
42
+check(ARGV[0] == nil, "ERROR: File is required.")
43
+
44
+# Cleans path
45
+pot_path = pot_dir + File.basename(ARGV[0])
46
+po_name  = File.basename(ARGV[0], '.*') + '.po'
47
+
48
+# Check if the file exists
49
+check(File.exist?(pot_path) == false, "ERROR: File doesn't exist.")
50
+
51
+# Creates locale directory if need it
52
+locales.each do |l|
53
+  if File.exist?(po_dir + l) == false
54
+    Dir.mkdir(po_dir + l)
55
+  end
56
+end
57
+
58
+# Creates or merges po files
59
+locales.each do |l|
60
+  if ARGV[1] == nil
61
+    system("msginit --input=#{pot_path} --locale=#{l} --no-translator --output=#{po_dir + l}/#{po_name}")
62
+  else
63
+    system("msgmerge --update #{po_dir + l}/#{po_name} #{pot_path}")
64
+  end
65
+end