fakestache-ssg

[ARCHIVED] A static site generator in Bash 4.
Log | Files | Refs | Submodules | README

parse-yaml (701B)


      1 #!/bin/bash
      2 
      3 # Source: https://gist.github.com/pkuczynski/8665367#file-parse_yaml-sh
      4 
      5 parse_yaml() {
      6     local prefix=$2
      7     local s
      8     local w
      9     local fs
     10     s='[[:space:]]*'
     11     w='[a-zA-Z0-9_]*'
     12     fs="$(echo @|tr @ '\034')"
     13     sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
     14         -e "s|^\($s\)\($w\)$s[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" "$1" |
     15         awk -F"$fs" '{
     16     indent = length($1)/2;
     17     vname[indent] = $2;
     18     for (i in vname) {if (i > indent) {delete vname[i]}}
     19         if (length($3) > 0) {
     20             vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
     21             printf("%s%s%s=(\"%s\")\n", "'"$prefix"'",vn, $2, $3);
     22         }
     23     }' | sed 's/_=/+=/g'
     24 }