Advertisement
KosIvantsov

replace_with_template.groovy

Jul 12th, 2013
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 2.46 KB | None | 0 0
  1. /*
  2.  * Purpose: Modify current target sequentially replacing [Search patterns]
  3.  *  with [Replace patterns], specified in external file
  4.  * #Files:  Requires 'segment_substitution.ini' in '.ini' subfolder
  5.  *      of the current project's root
  6.  * #File format:    Plain text, where *each* line is:
  7.  *  [Search Pattern] [Tab] [Replace Pattern];
  8.  *  only the last line *must* be empty.
  9.  * #Details:    http://wp.me/p3fHEs-5e
  10.  *
  11.  * @author  Kos Ivantsov (with major contributions by Yu Tang)
  12.  * @date    2013-07-12
  13.  * @version 0.3
  14.  */
  15.  
  16. import static javax.swing.JOptionPane.*
  17. import static org.omegat.util.Platform.*
  18.  
  19. def prop = project.projectProperties
  20. if (!prop) {
  21.   final def title = 'Replace using substitution file'
  22.   final def msg   = 'Please try again after you open a project.'
  23.   showMessageDialog null, msg, title, INFORMATION_MESSAGE
  24.   return
  25. }
  26.  
  27. def folder = prop.projectRoot
  28. def fileloc = folder+'.ini/segment_substitution.ini'
  29. subst_file = new File(fileloc)
  30.  
  31. if (! subst_file.exists()) {
  32.     final def title = 'No file'
  33.     final def msg   = 'Substitution file ' + subst_file + ' doesn\'t exist.'
  34.     showMessageDialog null, msg, title, INFORMATION_MESSAGE
  35.     return
  36.     }
  37.  
  38. length = subst_file.readLines().size()
  39. search_array = []
  40. replace_array = []
  41. def count = 0
  42.  
  43. while ( count < length ) {
  44.     ln = subst_file.readLines().get(count).tokenize('\t')
  45.     sr = ln[0]
  46.     rp = ln[1]
  47.     search_array.add(sr)
  48.     replace_array.add(rp)
  49.     count++
  50.     }
  51.  
  52. def range = 0..<search_array.size()
  53.  
  54. if (editor.selectedText){
  55.     target = editor.selectedText
  56.     }else
  57.     /*
  58.      * The script can either use source text, replacing the target after all  
  59.      * substitutions, or it can use the text that is already placed as translation,
  60.      * replacing it with itself after substituting
  61.      * To enable desired behavior, use one of the two following lines (be
  62.      * sure to have the other commented out)
  63.      */
  64.     //target = editor.currentEntry.getSrcText();
  65.     target = editor.getCurrentTranslation()
  66.    
  67. for ( i in range) {
  68.     ser = search_array[i]
  69.     rep = (replace_array[i] =~ /\$(\d+)/ ).replaceAll( '\\${(it[$1] as String) }' )
  70.     shell = new GroovyShell()
  71.     eval = {statement, arg -> shell.setVariable 'it', arg; shell.evaluate '"' + statement + '"' }
  72.     target = target.replaceAll(/null/, 'repl0')
  73.     target = target.replaceAll(ser) { eval rep, it }
  74.     target = target.replaceAll(/null/, '')
  75.     target = target.replaceAll(/repl0/, 'null')
  76.     }
  77.  
  78. if (editor.selectedText){
  79.     editor.insertText(target)
  80.     }else
  81. editor.replaceEditText(target)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement