Created
April 24, 2023 15:25
-
-
Save msoutopico/7b10a45d33bd42242d9f99b3b12b59d5 to your computer and use it in GitHub Desktop.
ysc-resx-html.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.5 (dynamic) -- match only ResX files for YSC | |
* @date 2023.04.19 | |
*/ | |
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 YSC 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("ysc")) { | |
console.println("This script runs on YSC ResX files, let's continue!") | |
} else { | |
console.println("This project is not YSC, 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 = [ | |
[find: /<(${tag})>/, replacement: /<$1>/] | |
] | |
break | |
case COMPILE: | |
dir = project.projectProperties.targetRoot | |
replacePair = [ | |
[find: /<(${tag})>/, replacement: /<$1>/] | |
] | |
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 : ~/.*\.resx/ | |
] | |
// 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