Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 149 lines (123 sloc) 4.766 kB
55e4eee @pledbrook Add a useful left_to_do.groovy script for translators so that they ca…
pledbrook authored
1 #! /usr/bin/env groovy
2
3 /**
4 * Script that recursively compares everything within {hidden} macros in a
5 * user guide translation with another gdoc source directory. By default,
6 * the translation is compared against the reference 'en' docs.
7 */
fd5f1cb @fordguo merge the DiffLang to left_to_do
fordguo authored
8 if (!args || args.size() > 3) {
55e4eee @pledbrook Add a useful left_to_do.groovy script for translators so that they ca…
pledbrook authored
9 println """\
10 USAGE
11
fd5f1cb @fordguo merge the DiffLang to left_to_do
fordguo authored
12 left_to_do LANG [LANG] [-nc]
55e4eee @pledbrook Add a useful left_to_do.groovy script for translators so that they ca…
pledbrook authored
13
14 where
15
16 LANG = a language code, e.g. 'en' or 'pt_BR'
fd5f1cb @fordguo merge the DiffLang to left_to_do
fordguo authored
17 -nc means: copy the new file if the target LANG file does not exist
55e4eee @pledbrook Add a useful left_to_do.groovy script for translators so that they ca…
pledbrook authored
18
19 Compares the contents of the {hidden} blocks in the user guide for the first language
20 against the entire contents of the second language. By default, the second language
21 is 'en', i.e. the reference user guide (which has no {hidden} blocks).\
22 """.stripIndent()
23 System.exit 1
24 }
25
26
27 // Create a temporary directory to dump the extracted {hidden} contents to.
28 def tmpDocDir = new File("tmp")
29 if (tmpDocDir.exists()) {
30 println "Removing the old temporary output directory"
31 tmpDocDir.deleteDir()
32 }
33
34 tmpDocDir.mkdirs()
35
36 // Recursively copy the {hidden} blocks from the source language to the temporary
37 // directory, ensuring the resulting files have the same relative paths and names
38 // as the originals.
39 def srcDir = new File("src", args[0])
40 if (!srcDir.exists()) {
41 println "No source directory for language '${args[0]}'"
42 System.exit 2
43 }
fd5f1cb @fordguo merge the DiffLang to left_to_do
fordguo authored
44 def refDir = new File("src", args.size() >= 2 ? args[1] : 'en')
45 def isNc = (args.size() >= 3 && args[2]=='-nc') ? true :false
46 def ant = new AntBuilder()
55e4eee @pledbrook Add a useful left_to_do.groovy script for translators so that they ca…
pledbrook authored
47
48 srcDir.eachFileRecurse { f ->
49 if (f.directory) {
50 // Recreate this directory in the temporary output folder.
51 new File(tmpDocDir, relativePath(srcDir, f)).mkdir()
52 }
53 else {
54 // Read the source file, create a target file with the same
55 // name and relative location, and write out the contents of
56 // {hidden} blocks to that target file. Note the target file
57 // will include the {hidden} macros.
fd5f1cb @fordguo merge the DiffLang to left_to_do
fordguo authored
58 def relPathStr = relativePath(srcDir, f)
59 def targetFile = new File(refDir, relPathStr)
60 if (isNc && !targetFile.exists()) {
61 if (!targetFile.parentFile.exists()) {
62 targetFile.parentFile.mkdirs()
63 }
64 ant.copy(file:f,tofile:targetFile)
65 }
66 targetFile = new File(tmpDocDir, relPathStr)
55e4eee @pledbrook Add a useful left_to_do.groovy script for translators so that they ca…
pledbrook authored
67 writeHiddenToFile f.text, targetFile
68 }
69 }
70
71 // Now recursively diff the generated files against the reference directory.
72 generateDiff refDir, tmpDocDir
73
74 /// End of script execution ///
75
76
77 def relativePath(File baseDir, File file) {
78 def relativePath = file.absolutePath - baseDir.absolutePath
79 if (relativePath[0] == File.separator) relativePath = relativePath[1..-1]
80 return relativePath
81 }
82
83 def writeHiddenToFile(String text, File target) {
84 target.withWriter { w ->
85 def marker = 0
86
87 while (marker != -1) {
88 def startHidden = findNextMacro(text, marker, "hidden")
89 def startCode = findNextMacro(text, marker, "code")
90
91 def start
92 def macro
93 def writeMacro = false
94 if (startHidden != -1 && (startHidden < startCode || startCode == -1)) {
95 macro = "hidden"
96 start = startHidden
97 }
98 else {
99 macro = "code"
100 start = startCode
101 writeMacro = true
102 }
103 def end = start == -1 ? -1 : findNextMacro(text, start + 1, macro)
104
105 if (end != -1) {
106 if (!writeMacro) {
107 start = start + macroLength(macro)
108 }
109 else {
110 end = end + macroLength(macro)
111 }
112
113 w << text.substring(start, end)
114
115 if (writeMacro) w << '\n'
116
117 marker = end
118 if (!writeMacro) marker++
119 }
120 else {
121 marker = -1
122 }
123 }
124 }
125 }
126
127 def generateDiff(File refDir, File targetDir) {
128 def pb = new ProcessBuilder(["diff", "--unified", "--recursive", refDir.absolutePath, targetDir.absolutePath])
129 pb.redirectErrorStream true
130
131 def process = pb.start()
132 process.consumeProcessOutputStream System.out
133 process.waitFor()
134 }
135
136 def findNextMacro(String text, start, macro) {
137 // This matches {macro} and {macro:...} unless the opening
138 // brace is escaped with a backslash. The first parentheses
139 // correspond to a negative lookbehind to exclude '\{'.
140 def m = text =~ /(?<!\\)\{${macro}(?::[^\}]+)?\}/
141 m.region start, text.size()
142 return m.find() ? m.start() : -1
143 }
144
145 def macroLength(macro) {
146 // Length of macro name + 2 for the '{' and '}'
147 return macro.size() + 2
148 }
Something went wrong with that request. Please try again.