NBash

Форк
0
120 строк · 4.6 Кб
1
#!/usr/bin/env bash
2

3
# GPL-3.0 license
4
# source: https://github.com/mrbaseman/parse_yaml.git
5

6
function parse_yaml
7
{
8
   local prefix=$2
9
   local separator=${3:-_}
10

11
   local indexfix
12
   # Detect awk flavor
13
   if awk --version 2>&1 | grep -q "GNU Awk" ; then
14
      # GNU Awk detected
15
      indexfix=-1
16
   elif awk -Wv 2>&1 | grep -q "mawk" ; then
17
      # mawk detected
18
      indexfix=0
19
   fi
20

21
   local s='[[:space:]]*' sm='[ \t]*' w='[a-zA-Z0-9_]*' fs=${fs:-$(echo @|tr @ '\034')} i=${i:-  }
22
   cat $1 | \
23
   awk -F$fs "{multi=0; 
24
       if(match(\$0,/$sm\|$sm$/)){multi=1; sub(/$sm\|$sm$/,\"\");}
25
       if(match(\$0,/$sm>$sm$/)){multi=2; sub(/$sm>$sm$/,\"\");}
26
       while(multi>0){
27
           str=\$0; gsub(/^$sm/,\"\", str);
28
           indent=index(\$0,str);
29
           indentstr=substr(\$0, 0, indent+$indexfix) \"$i\";
30
           obuf=\$0;
31
           getline;
32
           while(index(\$0,indentstr)){
33
               obuf=obuf substr(\$0, length(indentstr)+1);
34
               if (multi==1){obuf=obuf \"\\\\n\";}
35
               if (multi==2){
36
                   if(match(\$0,/^$sm$/))
37
                       obuf=obuf \"\\\\n\";
38
                       else obuf=obuf \" \";
39
               }
40
               getline;
41
           }
42
           sub(/$sm$/,\"\",obuf);
43
           print obuf;
44
           multi=0;
45
           if(match(\$0,/$sm\|$sm$/)){multi=1; sub(/$sm\|$sm$/,\"\");}
46
           if(match(\$0,/$sm>$sm$/)){multi=2; sub(/$sm>$sm$/,\"\");}
47
       }
48
   print}" | \
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|" \
72
        -e "s|$s\$||p" | \
73
   awk -F$fs "{
74
      gsub(/\t/,\"        \",\$1);
75
      if(NF>3){if(value!=\"\"){value = value \" \";}value = value  \$4;}
76
      else {
77
        if(match(\$1,/^&/)){anchor[substr(\$1,2)]=full_vn;getline};
78
        indent = length(\$1)/length(\"$i\");
79
        vname[indent] = \$2;
80
        value= \$3;
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\")}
84
        vn=\"$prefix\" vn;
85
        full_vn=vn vname[indent];
86
        if(vn==\"$prefix\")vn=\"$prefix$separator\";
87
        if(vn==\"_\")vn=\"__\";
88
      }
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)];
93
         if(length(ref)==0){
94
           printf(\"%s=\\\"%s\\\"\n\", full_vn, value);
95
         } else {
96
           for(val in assignment){
97
              if((length(ref)>0)&&index(val, ref)==1){
98
                 tmpval=assignment[val];
99
                 sub(ref,full_vn,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);
104
                 }
105
                 assignment[val]=tmpval;
106
              }
107
           }
108
         }
109
      } else if (length(value) > 0) {
110
         printf(\"%s=\\\"%s\\\"\n\", full_vn, value);
111
      }
112
   }END{
113
      for(val in assignment){
114
         if(match(val,\"$separator\$\"))
115
            printf(\"%s=\\\"%s\\\"\n\", val, assignment[val]);
116
      }
117
   }"
118
}
119

120
parse_yaml "$@"
121

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.