Skip to content

Instantly share code, notes, and snippets.

@andrewshadura
Created April 9, 2016 13:08
Show Gist options
  • Save andrewshadura/cff37d668903bb1938c31ad90a3053ab to your computer and use it in GitHub Desktop.
Save andrewshadura/cff37d668903bb1938c31ad90a3053ab to your computer and use it in GitHub Desktop.
Convert an unstructured translations YAML-like file into the structured "almost YAML"
#!/usr/bin/gawk -f
function flush_out(x) {
# debug:
if (x != "") printf x ": "
if (out["key"] == "") {
return
}
if (index(out["value"], "\r\\") == 0) {
if (match(out["value"], /^"([^ "]*)"$/, inside)) {
out["value"] = inside[1]
} else if (match(out["value"], /^"(.*["].*)"$/, inside)) {
out["value"] = "'" inside[1] "'"
}
if (list) {
print out["indent"] " - " out["value"]
} else {
print out["indent"] out["key"] ": " out["value"]
}
} else {
if (match(out["value"], /^"(.*)"$/, inside)) {
out["value"] = inside[1]
}
if (list) {
print out["indent"] " - |-"
} else {
print out["indent"] out["key"] ": |-"
}
split(out["value"], lines, /\r\\/)
for (ll in lines) {
print out["indent"] " " lines[ll]
}
}
out["key"] = ""
}
BEGIN {
level = ""
indent = ""
lastindex = -1
list = 0
}
/\r\\$/ {
getline nextline
$0 = $0 nextline
}
{
while (match($0, /\r\\$/)) {
getline nextline
$0 = $0 nextline
}
if (match($0, /^([a-z][a-z])\.([a-z_0-9\[\].]+): (.*)/, a)) {
lang = a[1]
lasthier = nhier
hier = a[1] "." a[2]
nhier = hier
sub(/\[.*\]/, "", nhier)
# print "@@" hier
split(hier, b, ".")
match(hier, /\[([0-9]+)\]/, indices)
currentindex = indices[1]
indenthere = indent
for (x in c) {
y = b[x]
sub(/\[.*\]/, "", y)
# print "%%1 " x " -> " b[x] " -> " y " -> " c[x]
if (y == c[x]) {
if (index(b[x], "[") == 0) {
indenthere = indenthere " "
delete b[x]
}
} else {
break
}
}
for (x in b) {
# print "%%2 " x " -> " b[x]
if (index(b[x], "[") == 0) {
flush_out()
print indenthere b[x] ":"
} else {
sub(/\[.*\]/, "", b[x])
if (out["key"] == b[x]) {
if (!list) {
print indenthere b[x] ":"
}
inhenthere = indenthere " "
out["indent"] = indenthere
list = 1
flush_out()
} else {
flush_out()
list = 0
}
out["indent"] = indenthere
out["key"] = b[x]
out["value"] = a[3]
}
c[x] = b[x]
indenthere = indenthere " "
}
if (length(c) > x) {
l = length(c)
for (i = x + 1; i <= l; i++) {
# print "** " i " -> " c[i]
delete c[i]
}
}
delete b
# print a[2] a[3] >> "i18n/" lang ".yml"
} else {
# print $0 >> "i18n/" lang ".yml"
}
}
END {
flush_out()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment