Advertisement
KosIvantsov

insert_custom_strings.groovy

Oct 15th, 2013
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 4.70 KB | None | 0 0
  1. /*
  2.  * Purpose: Insert custom string specified in external file, one by one
  3.  * #Files:  Requires 'custom_strings.ini' in  '.ini' subfolder
  4.  *  in the current project's root
  5.  * #File format:    Plain text, where *each* line is:
  6.  *  [Sting in the source] [Tab] [String to insert];
  7.  *  only the last line *must* be empty.
  8.  * #Details:    http://wp.me/p3fHEs-86
  9.  *
  10.  * @author  Kos Ivantsov
  11.  * @date    2013-10-15
  12.  * @version 0.3
  13.  */
  14. import org.omegat.util.StaticUtils
  15. import static javax.swing.JOptionPane.*
  16. import static org.omegat.util.Platform.*
  17.  
  18. use_seg_subs = 'no'
  19.  
  20. def prop = project.projectProperties
  21. if (!prop) {
  22.     final def title = 'Insert custom strings'
  23.     final def msg   = 'Please try again after you open a project.'
  24.     console.println msg
  25.     showMessageDialog null, msg, title, INFORMATION_MESSAGE
  26.     return
  27. }
  28.  
  29. def srcfile = editor.currentFile
  30. def ste = editor.currentEntry
  31. def cur_text = ste.getSrcText()
  32. def cur_seg = ste.entryNum()
  33. def configdir = StaticUtils.getConfigDir()
  34.  
  35. def folder = prop.projectRoot
  36. def fileloc = folder+'.ini/custom_strings.ini'
  37. def stringfile = new File(fileloc)
  38. def infofile
  39. infofile = configdir+'script/custom_strings.txt/'
  40. infofile = new File(infofile)
  41. srcexport = new File(configdir+'script/source.txt/')
  42. srcmod = srcexport.lastModified().toString()
  43. subst_file = new File(folder+'.ini/segment_substitution.ini')
  44.  
  45. if (! stringfile.exists()) {
  46.     final def title = 'No file'
  47.     final def msg   = """\
  48. File $stringfile
  49. that defines strings to be inserted doesn't exist\
  50. """
  51.     showMessageDialog null, msg, title, INFORMATION_MESSAGE
  52.     console.println msg
  53.     return
  54.     }
  55.  
  56. if (! subst_file.exists()) {
  57.     use_seg_subs = 'no'
  58.     }
  59.  
  60. def length = stringfile.readLines().size()
  61. def search_array = []
  62. def replace_array = []
  63. def count = 0
  64.  
  65. while ( count < length ) {
  66.     ln = stringfile.readLines().get(count).tokenize('\t')
  67.     sr = ln[0]
  68.     rp = ln[1]
  69.     search_array.add(sr)
  70.     replace_array.add(rp)
  71.     count++
  72.     }
  73.  
  74. def tag_index_array = []
  75. def cur_txt_array = []
  76.  
  77. def range = 0..<search_array.size()
  78.  
  79. for (i in range) {
  80.     cur_text.findAll(search_array[i]) {
  81.         if (it =~ "\\["){
  82.         it = it[0]}
  83.         tag_index_array.add("${cur_text.indexOf(it)}:$i")
  84.         cur_txt_array.add("${cur_text.indexOf(it)}<rem>$it")
  85.         }
  86. }
  87.  
  88. tag_index_array.sort{it.findAll("\\d+:")}
  89. cur_txt_array.sort{it.findAll("\\d+<")}
  90. for (i in 0..<tag_index_array.size()){
  91.     newitem = tag_index_array[i].replaceAll("\\d+:", "")
  92.     tag_index_array.putAt(i, newitem)
  93.     newword = cur_txt_array[i].replaceAll("\\d+<rem>", "")
  94.     cur_txt_array.putAt(i, newword)
  95.     }
  96. def seg_tag_count = tag_index_array.size()
  97.  
  98. if (seg_tag_count == 0){
  99.     console.println "no insert_strings in current segment"
  100.     return
  101.     }
  102.  
  103. def curtag_number
  104. def writeinfo(file, src, seg, num, mod) {
  105.     file.write(src+"\n",'UTF-8')
  106.     file.append(seg+"\n", 'UTF-8')
  107.     file.append(mod+"\n", 'UTF-8')
  108.     file.append(num+"\n", 'UTF-8')
  109. }
  110.  
  111. if (!infofile.exists() || infofile.readLines().get(0) != "$srcfile" || infofile.readLines().get(1) != "$cur_seg" || infofile.readLines().get(2) != "$srcmod") {
  112.     writeinfo(infofile, srcfile, cur_seg, "0", srcmod)
  113.     curtag_number = infofile.readLines().get(3) as int
  114.     if (seg_tag_count > 1) {
  115.         writeinfo(infofile, srcfile, cur_seg, "1", srcmod)
  116.         }
  117.     } else {
  118.     curtag_number = infofile.readLines().get(3) as int
  119.     if (curtag_number < seg_tag_count-1){
  120.     writeinfo(infofile, srcfile, cur_seg, "${curtag_number+1}", srcmod)
  121.         } else
  122.         writeinfo(infofile, srcfile, cur_seg, "0", srcmod)
  123. }
  124.  
  125. num = tag_index_array[curtag_number] as int
  126. text = cur_txt_array[curtag_number]
  127. ser = search_array[num]
  128. rep = (replace_array[num] =~ /\$(\d+)/ ).replaceAll( '\\${(it[$1] as String) }' )
  129. shell = new GroovyShell()
  130. eval = {statement, arg -> shell.setVariable 'it', arg; shell.evaluate '"' + statement + '"' }
  131. text = text.replaceAll(/null/, 'repl0')
  132. text = text.replaceAll(ser) { eval rep, it }
  133. text = text.replaceAll(/null/, '')
  134. text = text.replaceAll(/repl0/, 'null')
  135.  
  136. if (use_seg_subs == 'yes'){
  137.     length = subst_file.readLines().size()
  138.     search_array = []
  139.     replace_array = []
  140.     count = 0
  141.    
  142.     while ( count < length ) {
  143.         ln = subst_file.readLines().get(count).tokenize('\t')
  144.         sr = ln[0]
  145.         rp = ln[1]
  146.         search_array.add(sr)
  147.         replace_array.add(rp)
  148.         count++
  149.         }
  150.        
  151.         range = 0..<search_array.size()
  152.        
  153.     for ( i in range) {
  154.         ser = search_array[i]
  155.         rep = (replace_array[i] =~ /\$(\d+)/ ).replaceAll( '\\${(it[$1] as String) }' )
  156.         shell = new GroovyShell()
  157.         eval = {statement, arg -> shell.setVariable 'it', arg; shell.evaluate '"' + statement + '"' }
  158.         text = text.replaceAll(/null/, 'repl0')
  159.         text = text.replaceAll(ser) { eval rep, it }
  160.         text = text.replaceAll(/null/, '')
  161.         text = text.replaceAll(/repl0/, 'null')
  162.         }
  163.     }
  164.  
  165. editor.insertText(text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement