117 lines
4.6 KiB
Bash
117 lines
4.6 KiB
Bash
#!/bin/bash
|
|
#
|
|
# This script may have been modified to work better with Blueprint.
|
|
# Source code: https://github.com/mrbaseman/parse_yaml
|
|
|
|
function parse_yaml {
|
|
local prefix=$2
|
|
local separator=${3:-_}
|
|
|
|
local indexfix
|
|
# Detect awk flavor
|
|
if awk --version 2>&1 | grep -q "GNU Awk" ; then
|
|
# GNU Awk detected
|
|
indexfix=-1
|
|
elif awk -Wv 2>&1 | grep -q "mawk" ; then
|
|
# mawk detected
|
|
indexfix=0
|
|
fi
|
|
|
|
local s='[[:space:]]*' sm='[ \t]*' w='[a-zA-Z0-9_]*' fs=${fs:-$(echo @|tr @ '\034')} i=${i:- }
|
|
cat $1 | \
|
|
awk -F$fs "{multi=0;
|
|
if(match(\$0,/$sm\|$sm$/)){multi=1; sub(/$sm\|$sm$/,\"\");}
|
|
if(match(\$0,/$sm>$sm$/)){multi=2; sub(/$sm>$sm$/,\"\");}
|
|
while(multi>0){
|
|
str=\$0; gsub(/^$sm/,\"\", str);
|
|
indent=index(\$0,str);
|
|
indentstr=substr(\$0, 0, indent+$indexfix) \"$i\";
|
|
obuf=\$0;
|
|
getline;
|
|
while(index(\$0,indentstr)){
|
|
obuf=obuf substr(\$0, length(indentstr)+1);
|
|
if (multi==1){obuf=obuf \"\\\\n\";}
|
|
if (multi==2){
|
|
if(match(\$0,/^$sm$/))
|
|
obuf=obuf \"\\\\n\";
|
|
else obuf=obuf \" \";
|
|
}
|
|
getline;
|
|
}
|
|
sub(/$sm$/,\"\",obuf);
|
|
print obuf;
|
|
multi=0;
|
|
if(match(\$0,/$sm\|$sm$/)){multi=1; sub(/$sm\|$sm$/,\"\");}
|
|
if(match(\$0,/$sm>$sm$/)){multi=2; sub(/$sm>$sm$/,\"\");}
|
|
}
|
|
print}" | \
|
|
sed -e "s|^\($s\)?|\1-|" \
|
|
-ne "s|^$s#.*||;s|$s#[^\"']*$||;s|^\([^\"'#]*\)#.*|\1|;t1;t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|^\($s\)\($w\)$s:$s[\"']\?\(.*\)$s\$|\1$fs\2$fs\3|" \
|
|
-e "s|^\($s\)[\"']\?\([^&][^$fs]\+\)[\"']$s\$|\1$fs$fs$fs\2|" \
|
|
-e "s|^\($s\)[\"']\?\([^&][^$fs]\+\)$s\$|\1$fs$fs$fs\2|" \
|
|
-e "s|$s\$||p" | \
|
|
awk -F$fs "{
|
|
gsub(/\t/,\" \",\$1);
|
|
if(NF>3){if(value!=\"\"){value = value \" \";}value = value \$4;}
|
|
else {
|
|
if(match(\$1,/^&/)){anchor[substr(\$1,2)]=full_vn;getline};
|
|
indent = length(\$1)/length(\"$i\");
|
|
vname[indent] = \$2;
|
|
value= \$3;
|
|
for (i in vname) {if (i > indent) {delete vname[i]; idx[i]=0}}
|
|
if(length(\$2)== 0){ vname[indent]= ++idx[indent] };
|
|
vn=\"\"; for (i=0; i<indent; i++) { vn=(vn)(vname[i])(\"$separator\")}
|
|
vn=\"$prefix\" vn;
|
|
full_vn=vn vname[indent];
|
|
if(vn==\"$prefix\")vn=\"$prefix$separator\";
|
|
if(vn==\"_\")vn=\"__\";
|
|
}
|
|
assignment[full_vn]=value;
|
|
if(!match(assignment[vn], full_vn))assignment[vn]=assignment[vn] \" \" full_vn;
|
|
if(match(value,/^\*/)){
|
|
ref=anchor[substr(value,2)];
|
|
if(length(ref)==0){
|
|
printf(\"%s=\\\"%s\\\"\n\", full_vn, value);
|
|
} else {
|
|
for(val in assignment){
|
|
if((length(ref)>0)&&index(val, ref)==1){
|
|
tmpval=assignment[val];
|
|
sub(ref,full_vn,val);
|
|
if(match(val,\"$separator\$\")){
|
|
gsub(ref,full_vn,tmpval);
|
|
} else if (length(tmpval) > 0) {
|
|
printf(\"%s=\\\"%s\\\"\n\", val, tmpval);
|
|
}
|
|
assignment[val]=tmpval;
|
|
}
|
|
}
|
|
}
|
|
} else if (length(value) > 0) {
|
|
printf(\"%s=\\\"%s\\\"\n\", full_vn, value);
|
|
}
|
|
}END{
|
|
for(val in assignment){
|
|
if(match(val,\"$separator\$\"))
|
|
printf(\"%s=\\\"%s\\\"\n\", val, assignment[val]);
|
|
}
|
|
}"
|
|
}
|