4
# source: https://github.com/mrbaseman/parse_yaml.git
9
local separator=${3:-_}
13
if awk --version 2>&1 | grep -q "GNU Awk" ; then
16
elif awk -Wv 2>&1 | grep -q "mawk" ; then
21
local s='[[:space:]]*' sm='[ \t]*' w='[a-zA-Z0-9_]*' fs=${fs:-$(echo @|tr @ '\034')} i=${i:- }
24
if(match(\$0,/$sm\|$sm$/)){multi=1; sub(/$sm\|$sm$/,\"\");}
25
if(match(\$0,/$sm>$sm$/)){multi=2; sub(/$sm>$sm$/,\"\");}
27
str=\$0; gsub(/^$sm/,\"\", str);
28
indent=index(\$0,str);
29
indentstr=substr(\$0, 0, indent+$indexfix) \"$i\";
32
while(index(\$0,indentstr)){
33
obuf=obuf substr(\$0, length(indentstr)+1);
34
if (multi==1){obuf=obuf \"\\\\n\";}
36
if(match(\$0,/^$sm$/))
42
sub(/$sm$/,\"\",obuf);
45
if(match(\$0,/$sm\|$sm$/)){multi=1; sub(/$sm\|$sm$/,\"\");}
46
if(match(\$0,/$sm>$sm$/)){multi=2; sub(/$sm>$sm$/,\"\");}
49
sed -e "s|^\($s\)?|\1-|" \
50
-ne "s|^$s#.*||;s|$s#[^\"']*$||;s|^\([^\"'#]*\)#.*|\1|;t1;t;:1;s|^$s\$||;t2;p;:2;d" | \
51
sed -ne "s|,$s\]$s\$|]|" \
52
-e ":1;s|^\($s\)\($w\)$s:$s\(&$w\)\?$s\[$s\(.*\)$s,$s\(.*\)$s\]|\1\2: \3[\4]\n\1$i- \5|;t1" \
53
-e "s|^\($s\)\($w\)$s:$s\(&$w\)\?$s\[$s\(.*\)$s\]|\1\2: \3\n\1$i- \4|;" \
54
-e ":2;s|^\($s\)-$s\[$s\(.*\)$s,$s\(.*\)$s\]|\1- [\2]\n\1$i- \3|;t2" \
55
-e "s|^\($s\)-$s\[$s\(.*\)$s\]|\1-\n\1$i- \2|;p" | \
56
sed -ne "s|,$s}$s\$|}|" \
57
-e ":1;s|^\($s\)-$s{$s\(.*\)$s,$s\($w\)$s:$s\(.*\)$s}|\1- {\2}\n\1$i\3: \4|;t1" \
58
-e "s|^\($s\)-$s{$s\(.*\)$s}|\1-\n\1$i\2|;" \
59
-e ":2;s|^\($s\)\($w\)$s:$s\(&$w\)\?$s{$s\(.*\)$s,$s\($w\)$s:$s\(.*\)$s}|\1\2: \3 {\4}\n\1$i\5: \6|;t2" \
60
-e "s|^\($s\)\($w\)$s:$s\(&$w\)\?$s{$s\(.*\)$s}|\1\2: \3\n\1$i\4|;p" | \
61
sed -e "s|^\($s\)\($w\)$s:$s\(&$w\)\(.*\)|\1\2:\4\n\3|" \
62
-e "s|^\($s\)-$s\(&$w\)\(.*\)|\1- \3\n\2|" | \
63
sed -ne "s|^\($s\):|\1|" \
64
-e "s|^\($s\)\(---\)\($s\)||" \
65
-e "s|^\($s\)\(\.\.\.\)\($s\)||" \
66
-e "s|^\($s\)-$s[\"']\(.*\)[\"']$s\$|\1$fs$fs\2|p;t" \
67
-e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p;t" \
68
-e "s|^\($s\)-$s\(.*\)$s\$|\1$fs$fs\2|" \
69
-e "s|^\($s\)\($w\)$s:$s[\"']\?\(.*\)$s\$|\1$fs\2$fs\3|" \
70
-e "s|^\($s\)[\"']\?\([^&][^$fs]\+\)[\"']$s\$|\1$fs$fs$fs\2|" \
71
-e "s|^\($s\)[\"']\?\([^&][^$fs]\+\)$s\$|\1$fs$fs$fs\2|" \
75
if(NF>3){if(value!=\"\"){value = value \" \";}value = value \$4;}
77
if(match(\$1,/^&/)){anchor[substr(\$1,2)]=full_vn;getline};
78
indent = length(\$1)/length(\"$i\");
81
for (i in vname) {if (i > indent) {delete vname[i]; idx[i]=0}}
82
if(length(\$2)== 0){ vname[indent]= ++idx[indent] };
83
vn=\"\"; for (i=0; i<indent; i++) { vn=(vn)(vname[i])(\"$separator\")}
85
full_vn=vn vname[indent];
86
if(vn==\"$prefix\")vn=\"$prefix$separator\";
87
if(vn==\"_\")vn=\"__\";
89
assignment[full_vn]=value;
90
if(!match(assignment[vn], full_vn))assignment[vn]=assignment[vn] \" \" full_vn;
91
if(match(value,/^\*/)){
92
ref=anchor[substr(value,2)];
94
printf(\"%s=\\\"%s\\\"\n\", full_vn, value);
96
for(val in assignment){
97
if((length(ref)>0)&&index(val, ref)==1){
98
tmpval=assignment[val];
100
if(match(val,\"$separator\$\")){
101
gsub(ref,full_vn,tmpval);
102
} else if (length(tmpval) > 0) {
103
printf(\"%s=\\\"%s\\\"\n\", val, tmpval);
105
assignment[val]=tmpval;
109
} else if (length(value) > 0) {
110
printf(\"%s=\\\"%s\\\"\n\", full_vn, value);
113
for(val in assignment){
114
if(match(val,\"$separator\$\"))
115
printf(\"%s=\\\"%s\\\"\n\", val, assignment[val]);