Created
May 26, 2023 08:20
-
-
Save msoutopico/c81b082add36cb461c73969e225efe70 to your computer and use it in GitHub Desktop.
pisa25-cdata-wrap.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Usage : Put this script in <ScriptsDir>/project_changed folder. Create a folder if it doesn't exists. | |
* | |
* @authors Yu Tang, Manuel Souto Pico | |
* @version 0.5.6 (dynamic) -- match only XML files for PISA | |
* @date 2023.05.25 | |
*/ | |
import static org.omegat.core.events.IProjectEventListener.PROJECT_CHANGE_TYPE.* | |
// import groovy.xml.XmlSlurper | |
// import org.apache.commons.lang.StringEscapeUtils | |
// import groovy.xml.MarkupBuilder | |
// check that this is a PISA project | |
prop = project.getProjectProperties() | |
proj_name = prop.projectName | |
// The container is the first entity in the project name (before the first underscore) | |
container = (proj_name =~ /^[^_]+/)[0] | |
if (container.contains("pisa")) { | |
console.println("This script runs on PISA25 XML files, let's continue!") | |
} else { | |
console.println("This project is not PISA, let's stop here. Good bye!") | |
return | |
} | |
// prepare | |
String dir | |
def replacePair | |
def skipTraverse(eventType) { | |
if (!eventType.metaClass.hasProperty(eventType, 'skipTraverse')) { | |
eventType.metaClass.skipTraverse = false | |
} | |
eventType.skipTraverse | |
} | |
// tag = "(/?(?:[bui]|br/?|h1|h2|h3|h4|em|strong|ul|ol|li|span|div|p|sup|table|tbody|d|th|tr))" | |
switch (eventType) { | |
case LOAD: | |
// Skip traverse | |
if (skipTraverse(LOAD)) { | |
LOAD.skipTraverse = false // reset the flag | |
return | |
} | |
dir = project.projectProperties.sourceRoot | |
replacePair = [] | |
break | |
case COMPILE: | |
dir = project.projectProperties.targetRoot | |
replacePair = [ | |
[find: /(?s)<text>([\s\S]+?)<\/text>/, replacement: /<text><![CDATA[$1]]><\/text>/] | |
] | |
break | |
default: | |
return null // No output | |
} | |
String ENCODING = 'UTF-8' | |
File rootDir = new File(dir) | |
int modifiedFiles = 0 | |
// options as map | |
def options = [ | |
type : groovy.io.FileType.FILES, | |
nameFilter : ~/.*\.xml/ | |
] | |
// replacer as closure | |
def replacer = {file -> | |
String text = file.getText ENCODING | |
String replaced = text | |
replacePair.each { replaced = replaced.replaceAll(it.find, it.replacement) } | |
if (text != replaced) { | |
file.setText replaced, ENCODING | |
console.println "modified: $file" | |
modifiedFiles++ | |
} | |
} | |
def reloadProjectOnetime = { | |
LOAD.skipTraverse = true // avoid potentially infinity reloading loop | |
javax.swing.SwingUtilities.invokeLater({ | |
org.omegat.gui.main.ProjectUICommands.projectReload() | |
} as Runnable) | |
} | |
// do replace | |
rootDir.traverse options, replacer | |
if (modifiedFiles > 0 && eventType == LOAD) { | |
console.println "$modifiedFiles file(s) modified." | |
reloadProjectOnetime() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment