“Filtered” Note Export in #OmegaT

This script is variation of the one published before that exports all notes in the current project. The only difference is that this one allows you to select which notes will get exported based on the first line of the note. The resultant HTML table will consist of four columns: Source, Target, Filtered Notes (adjustable heading name), and Reply.
Say, you want to be able to export only the notes that start with <query>, as you’ve been using this word (<query>) to mark your questions to the client. In order to do so, go to line 14 and specify which mark-word was used. Note: The mark-word used to filter notes should be found in the very beginning of the very first line of the note, otherwise it’ll be ignored. In line 15 you can specify the column heading.

All project notes

All project notes

Only filtered notes

Only filtered notes

The left screenshot shows all notes exported. The right one contains only the filtered ones (there’s only one in my example).
Here’s the listing of the script, the heading is its download link

  • write_queries.groovy
     * #Purpose:	Export certain project's notes into a HTML table
     * #Files:	Writes 'NAME_query.html' in the 'script_output' subfolder
     *		of current project's root.
     * #Details:	http://wp.me/p3fHEs-9Z
     * @author:	Kos Ivantsov
     * @date:	2014-03-09
     * @version:	0.2
    def search	= '<query>'
    def noteth	= 'Query'
    def tagbg	= '#E5E4E2'	//tag background in context
    def tagfg	= 'green'	//tag foreground in context
    def contxtfg	= 'darkgray'	//font foreground in context
    def notebg	= 'yellow'	//note background
    def curtagbg	= '#BCC6DD'	//tag background in segment's text
    def curtagfg	= 'green'	//tag foreground in segment's text
    def curtxtbg	= '#BCC6CC'	//font background in segment's text
    def filenmbg	= 'lightgray'	//cell background for filename
    def thbg	= 'gray'	//table heading cells background
    import static javax.swing.JOptionPane.*
    import static org.omegat.util.Platform.*
    import org.omegat.util.StaticUtils
    def prop = project.projectProperties
    if (!prop) {
    	final def title = 'Export project notes'
    	final def msg   = 'Please try again after you open a project.'
    	showMessageDialog null, msg, title, INFORMATION_MESSAGE
    def folder = prop.projectRoot+'script_output/'
    projname = new File(prop.getProjectRoot()).getName()
    table_file = new File(folder + projname + '_queries.html')
    // create folder if it doesn't exist
    if (! (new File (folder)).exists()) {
    	(new File(folder)).mkdir()
    count = 0
    def painttag = {x, tbg, tfg -> x.replaceAll(/(\&lt\;\/?\s?\w+\s?\/?\d+?\s?\/?\s?\/?\&gt\;)/, /\<sup\>\<font size=\"1\"  style=\"background-color:$tbg\; color:$tfg" \>$1\<\/font\>\<\/sup\>/)}
    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
    <body>\n""", 'UTF-8')
    files = project.projectFiles
    	for (i in 0 ..< files.size())
    		fi = files[i]
    		filename = new File(fi.filePath).name
      <table border=\"1px\" style=\"margin-bottom:50px\" width=\"100%\">
      <tr align=\"center\"><th style=\"border:1px solid black\" colspan=\"4\" width=\"100%\" bgcolor=$filenmbg><strong>$filename</strong></th></tr>
    	<th bgcolor=$thbg>Source</th>
    	<th bgcolor=$thbg>Target</th>
    	<th bgcolor=$thbg>$noteth</th>
    	<th bgcolor=$thbg>Reply</th>
      </tr>\n""", 'UTF-8')
    		for (j in 0 ..< fi.entries.size())
    			ste = fi.entries[j]
    			info = project.getTranslationInfo(ste)
    			source = ste.getSrcText()
    			target = info ? info.translation : null
    			if (info.hasNote()) {
    				if (info.note =~ /^$search/) {
    					unitnote = "\n        ${StaticUtils.makeValidXML(info.note.replaceAll(/^$search/, ''))}"
    					if (j != 0) {
    						prevsrc = StaticUtils.makeValidXML(fi.entries[j-1].getSrcText())
    						prevsrc = painttag(prevsrc, tagbg, tagfg)
    						prevtrg = project.getTranslationInfo(fi.entries[j-1]) ? project.getTranslationInfo(fi.entries[j-1]).translation : null
    						if (prevtrg == null || prevtrg.size() == 0) {
    							prevtrg = '**N/A**'
    						prevtrg = painttag(StaticUtils.makeValidXML(prevtrg), tagbg, tagfg)
    							prevsrc = '**N/A**'
    							prevtrg = '**N/A**'
    					if (j < fi.entries.size()-1) {
    						nextsrc = StaticUtils.makeValidXML(fi.entries[j+1].getSrcText())
    						nextsrc = painttag(nextsrc, tagbg, tagfg)
    						nexttrg = project.getTranslationInfo(fi.entries[j+1]) ? project.getTranslationInfo(fi.entries[j+1]).translation : null
    						if (nexttrg == null || nexttrg.size() == 0) {
    							nexttrg = '**N/A**'
    						nexttrg = painttag(StaticUtils.makeValidXML(nexttrg), tagbg, tagfg)
    							nextsrc = '**N/A**'
    							nexttrg = '**N/A**'
    					if (target == null) {
    						target = "zzznullzzz"
    					if (target.size() == 0 ) {
    						target = "<EMPTY>"
    					source = StaticUtils.makeValidXML(source)
    					source = painttag(source, curtagbg, tagfg)
    					target = StaticUtils.makeValidXML(target).replaceAll(/zzznullzzz/, /⁠/)
    					target = painttag(target, curtagbg, tagfg)
        <td style=\"border:1px solid black\" width=\"25%\"><small><font style=\"color:$contxtfg\">$prevsrc </font></small><font style=\"background-color:$curtxtbg\">$source</font><small><font style=\"color:$contxtfg\"> $nextsrc </font></small></td>
        <td style=\"border:1px solid black\" width=\"25%\"><small><font style=\"color:$contxtfg\">$prevtrg </font></small><font style=\"background-color:$curtxtbg\">$target</font><small><font style=\"color:$contxtfg\"> $nexttrg </font></small></td>
        <td style=\"border:1px solid black ; background-color:$notebg\" width=\"25%\">$unitnote</td>
        <td style=\"border:1px solid black\" width=\"25%\" align=\"center\">⁠</td>    
      </tr>""", 'UTF-8')
    		table_file.append("\n  </table>\n", 'UTF-8')
    table_file.append("</body>\n</html>", 'UTF-8')
    String contents = table_file.getText('UTF-8') 
    contents = contents.replaceAll(/\<table border=\"1px\" style=\"margin-bottom:50px\" width=\"100\%\">\s+\<tr align=\"center\">\<th style=\"border:1px solid black\" colspan=\"4\" width=\"100\%\" bgcolor=$filenmbg\>\<strong\>.*\<\/strong\>\<\/th>\<\/tr\>\n\s+\<tr\>\n\t\<th bgcolor=$thbg\>Source\<\/th\>\n\t\<th bgcolor=$thbg\>Target\<\/th\>\n\t\<th bgcolor=$thbg\>$noteth\<\/th\>\n\t\<th bgcolor=$thbg\>Reply\<\/th\>\n\s+\<\/tr\>\n\s+\<\/table\>/, '')
    table_file.write(contents, 'UTF-8')
    console.println "$count segments written to $table_file"

Comments and ideas are always welcome.

But as of now,


3 thoughts on ““Filtered” Note Export in #OmegaT

  1. Pingback: (CAT) - “Filtered” Note Export in #...
  2. Pingback: (CAT) – “Filtered” Note Export in #OmegaT | Translator’s Recipes | Glossarissimo!
  3. Pingback: “Filtered” Note Export in #OmegaT |...

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s