Last active
March 9, 2017 01:44
-
-
Save sskjames/de70c9b0dbbe62093b32e1e5cf016de8 to your computer and use it in GitHub Desktop.
Wraps all Tamil lines in a file with OpenLP formatting tags
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
/** | |
* Wraps all Tamil lines in a file with openlp formatting tags. | |
*/ | |
import groovy.transform.Field | |
@Field static final String PULLI = "\u0bcd"; | |
if(args.length > 0) { | |
def file = new File(args[0]) | |
if(file.exists()) { | |
File formattedFile = formatFile(file, file.getParentFile()) | |
if(formattedFile.exists()) { | |
formattedFile.renameTo(file) | |
} | |
} else { | |
println "$file doesn't exist" | |
} | |
} else { | |
println "Usage: groovy <script-name> <file>" | |
} | |
static File formatFile(File file, File targetDir) { | |
def target = new File(targetDir, "converted-" + file.getName()) | |
StringBuilder builder = new StringBuilder(); | |
file.withReader("UTF-8") { | |
def line | |
while ((line = it.readLine()) != null) { | |
if (line != null && line != "" ) { | |
if(isTamilText(line)) { | |
println("Formatting the line ${line}") | |
builder.append(wrapWithFormattingTag(line)).append("\r\n") | |
} else { | |
builder.append(line).append("\r\n") | |
} | |
} else { | |
builder.append("\r\n") | |
} | |
} | |
} | |
target.withWriter("UTF-8") { | |
it.write(builder.toString()) | |
} | |
return target; | |
} | |
static boolean isTamilText(String line) { | |
return line.contains(PULLI); | |
} | |
static String wrapWithFormattingTag(String line) { | |
return "{y}"+ line + "{/y}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment