Advertisement
KosIvantsov

write_table.groovy

Oct 9th, 2013
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 2.33 KB | None | 0 0
  1. /* THIS SCRIPT HAS BEEN SPONSORED BY TRANSLATION AGENCY VELIOR [ http://velior.ru ]
  2.  *
  3.  * #Purpose:    Export the project into a HTML table
  4.  * #Files:  Writes 'project_table.html' in the 'script_output' subfolder
  5.  *  of current project's root. Each source file is exported to a separate
  6.  *  table that has the name of the file as its heading, all source segments
  7.  *  in the left column and corresponding target segments in the right.
  8.  * #Details:    http://wp.me/p3fHEs-7L
  9.  *
  10.  * @author: Kos Ivantsov
  11.  * @date:   2013-10-10
  12.  * @version:    0.1
  13.  */
  14.  
  15. import static javax.swing.JOptionPane.*
  16. import static org.omegat.util.Platform.*
  17. import org.omegat.util.StaticUtils
  18.  
  19. def prop = project.projectProperties
  20. if (!prop) {
  21.     final def title = 'Export project to table'
  22.     final def msg   = 'Please try again after you open a project.'
  23.     showMessageDialog null, msg, title, INFORMATION_MESSAGE
  24.     return
  25. }
  26. def folder = prop.projectRoot+'script_output/'
  27. table_file = new File(folder+'project_table.html')
  28. // create folder if it doesn't exist
  29. if (! (new File (folder)).exists()) {
  30.     (new File(folder)).mkdir()
  31.     }
  32. count = 0
  33. table_file.write("""\
  34. <html>\n<head>
  35. <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
  36. <body>""", 'UTF-8')
  37.  
  38. files = project.projectFiles
  39.     for (i in 0 ..< files.size())
  40.     {
  41.         fi = files[i]
  42.         table_file.append("""\
  43.  <table border=\"1px\" style=\"margin-bottom:50px\" width=\"100%\">
  44.  <tr align=\"center\"><th style=\"border:1px solid black\" colspan=\"2\" width=\"100%\" bgcolor=lightgray><strong>$fi.filePath</strong></th></tr>\n""", 'UTF-8')
  45.         for (j in 0 ..< fi.entries.size())
  46.         {
  47.             ste = fi.entries[j]
  48.             source = ste.getSrcText()
  49.             target = project.getTranslationInfo(ste) ? project.getTranslationInfo(ste).translation : null;
  50.             if (target == null)
  51.             target = "zzznullzzz"
  52.             if (target.size() == 0 )
  53.             target = "<EMPTY>"
  54.             source = StaticUtils.makeValidXML(source)
  55.             target = StaticUtils.makeValidXML(target).replaceAll(/zzznullzzz/, /&#8288;/)
  56.             table_file.append("""\
  57.  <tr><td style=\"border:1px solid black\" width=\"50%\">$source</td>
  58.  <td style=\"border:1px solid black\" width=\"50%\">$target</td></tr>""", 'UTF-8')
  59.             count++
  60.         }
  61.         table_file.append("  </table>\n", 'UTF-8')
  62.     }
  63. table_file.append("</body>\n</html>", 'UTF-8')
  64. console.println "$count segments written to $table_file"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement