Advertisement
KosIvantsov

search_replace_batch.groovy

Jul 12th, 2013
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 6.22 KB | None | 0 0
  1. /*
  2.  * #Purpose:     Batch search and replace in the whole project
  3.  * #Files:       Requires 'search_replace.ini' in the current project's root
  4.  * #File format: Plain text, where *each* line is:
  5.  *    [Search Pattern] [Tab] [Replace Pattern];
  6.  *    only the last line *must* be empty.
  7.  * #Details:     http: // <a class="linkification-ext" href="http://wp.me/p3fHEs-5e" title="Linkification: http://wp.me/p3fHEs-5e">wp.me/p3fHEs-5e</a>
  8.  *  
  9.  *
  10.  * @author  Kos Ivantsov
  11.  * @based on Didier Briel's "search and replace script"
  12.  * @date    2013-07-22
  13.  * @version 0.1
  14.  */
  15.  
  16. import static javax.swing.JOptionPane.*
  17. import static org.omegat.util.Platform.*
  18. import groovy.swing.SwingBuilder
  19. import java.awt.Component
  20. import javax.swing.JButton
  21. import javax.swing.JTable
  22. import javax.swing.table.*
  23. import javax.swing.event.*
  24. import java.awt.event.*
  25. import java.awt.BorderLayout as BL
  26.  
  27. /*
  28.  * set 'GUI' to anything but 'true' (with quotes) in the next line
  29.  * if you don't need a GUI listing of all changed segments
  30.  */
  31. def GUI = 'true'
  32.  
  33. def prop = project.projectProperties
  34. if (!prop) {
  35.   final def title = 'Batch search and replace'
  36.   final def msg   = 'Please try again after you open a project.'
  37.   showMessageDialog null, msg, title, INFORMATION_MESSAGE
  38.   return
  39. }
  40.  
  41. //def folder = prop.projectRoot
  42. //def fileloc = folder+'/subst_template.txt'
  43. subst_file = new File(prop.projectRoot+'/search_replace.ini')
  44.  
  45. if (! subst_file.exists()) {
  46.     final def title = 'No file'
  47.     final def msg   = 'Template file for batch search and replace'+'\n' + subst_file +'\n'+'doesn\'t exist.'
  48.     showMessageDialog null, msg, title, INFORMATION_MESSAGE
  49.     return
  50.     }
  51.  
  52. length = subst_file.readLines().size()
  53. search_array = []
  54. replace_array = []
  55. data = []
  56. def count = 0
  57.  
  58. while ( count < length ) {
  59.     ln = subst_file.readLines().get(count).tokenize('\t')
  60.     sr = ln[0]
  61.     rp = ln[1]
  62.     search_array.add(sr)
  63.     replace_array.add(rp)
  64.     count++ ;
  65.     }
  66.  
  67. def range = 0..(search_array.size() - 1)
  68.  
  69. def segment_count = 0
  70.  
  71. console.println "Do a search and replace.\n${'-'*10}\n"
  72.  
  73. project.allEntries.each { ste ->
  74.     source = ste.getSrcText();
  75.     target = project.getTranslationInfo(ste) ? project.getTranslationInfo(ste).translation : null;
  76.     initial_target = target
  77.  
  78.     // Skip untranslated segments
  79.     if (target == null) return
  80.  
  81.  
  82.     for ( i in range) {
  83.         ser = search_array[i]
  84.         rep = (replace_array[i] =~ /\$(\d+)/ ).replaceAll( '\\${(it[$1] as String) }' )
  85.         shell = new GroovyShell()
  86.         eval = {statement, arg -> shell.setVariable 'it', arg; shell.evaluate '"' + statement + '"' }
  87.         target = target.replaceAll(/null/, 'repl0')
  88.         target = target.replaceAll(ser) { eval rep, it }
  89.         target = target.replaceAll(/null/, '')
  90.         target = target.replaceAll(/repl0/, 'null')
  91.         }
  92.  
  93.  
  94.     if (initial_target != target) {
  95.         data.add([ seg: ste.entryNum(), source: source, in_target: initial_target, target: target ])
  96.         segment_count++
  97.         editor.gotoEntry(ste.entryNum())
  98.         console.println(ste.entryNum() + "\t" + ste.srcText + "\t" + target )
  99.         editor.replaceEditText(target)
  100.     }
  101. }
  102.  
  103. swing = new SwingBuilder()
  104. frame = swing.frame(title:'Changed segments', preferredSize: [1024, 500]) {
  105.     scrollPane {
  106.         table() {
  107.             tableModel(list:data) {
  108.                 propertyColumn(editable: true, header:'Segment', propertyName:'seg', minWidth: 80, maxWidth: 80, preferredWidth: 80,
  109.                         cellEditor: new TableCellEditor()
  110.                         {
  111.                             public void cancelCellEditing() {}
  112.                             public boolean stopCellEditing()    {   return false;   }
  113.                             public Object getCellEditorValue()  {   return value;   }
  114.                             public boolean isCellEditable(EventObject anEvent)  {   return true;    }
  115.                             public boolean shouldSelectCell(EventObject anEvent)    {return true; }
  116.                             public void addCellEditorListener(CellEditorListener l) {}
  117.                             public void removeCellEditorListener(CellEditorListener l)  {}
  118.                             public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
  119.                             {
  120.                                 println("value: " + value);
  121.                                 org.omegat.core.Core.getEditor().gotoEntry(value);
  122.                             }
  123.                              
  124.                         },
  125.                         cellRenderer: new TableCellRenderer()
  126.                         {
  127.                             public Component getTableCellRendererComponent(JTable table,
  128.                             Object value,
  129.                             boolean isSelected,
  130.                             boolean hasFocus,
  131.                             int row,
  132.                             int column)
  133.                             {
  134.                                 def btn = new JButton()
  135.                                 btn.setText(value.toString())
  136.                                 return btn
  137.                                  
  138.                             }
  139.                         }
  140.                         )
  141.                 propertyColumn(editable: false, header:'Source',propertyName:'source', minWidth: 150, preferredWidth: 350)
  142.                 propertyColumn(editable: false, header:'Initial Target',propertyName:'in_target', minWidth: 150, preferredWidth: 350)
  143.                 propertyColumn(editable: false, header:'Target',propertyName:'target', minWidth: 150, preferredWidth: 350)
  144.             }
  145.         }
  146.          
  147.     }
  148.      panel(constraints: BL.SOUTH){
  149.             button('Quit', actionPerformed:{
  150.                 frame.visible = false
  151.             })
  152.         }
  153. }
  154.  
  155. console.println "\n${'-'*10}"+"\n"+"Segments modified: " + segment_count
  156. if (segment_count == 0){
  157.     final def title = 'Search and Replace'
  158.     final def msg   = 'No segments can be changed.'
  159.     showMessageDialog null, msg, title, INFORMATION_MESSAGE
  160.     return
  161. }
  162.  
  163. if (GUI == 'true') {
  164.     if (segment_count != 0){
  165.     frame.pack()
  166.     frame.show()
  167.         }
  168.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement