Home | History | Annotate | Download | only in examples
      1 #!/bin/bash
      2 ##
      3 ##  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
      4 ##
      5 ##  Use of this source code is governed by a BSD-style license
      6 ##  that can be found in the LICENSE file in the root of the source
      7 ##  tree. An additional intellectual property rights grant can be found
      8 ##  in the file PATENTS.  All contributing project authors may
      9 ##  be found in the AUTHORS file in the root of the source tree.
     10 ##
     11 
     12 
     13 # gen_example_code.sh
     14 
     15 self=$0
     16 
     17 die_usage() {
     18     echo "Usage: $self <example.txt>"
     19     exit 1
     20 }
     21 
     22 die() {
     23     echo "$@"
     24     exit 1
     25 }
     26 
     27 include_block() {
     28     show_bar=$1
     29     block_name=${line##*@}
     30     indent=${line%%${block_name}}
     31     indent=${#indent}
     32     [ $indent -eq 1 ] && indent=0
     33     local on_block
     34     while IFS=$'\n' read -r t_line; do
     35         case "$t_line" in
     36             \~*\ ${block_name})
     37                 if [ "x$on_block" == "xyes" ]; then
     38                     return 0;
     39                 else
     40                     on_block=yes
     41                 fi
     42                 ;;
     43             @DEFAULT)
     44                 if [ "x$on_block" == "xyes" ]; then
     45                     include_block $show_bar < "${template%.c}.txt"
     46                     return 0
     47                 fi
     48                 ;;
     49             *)
     50                 if [ "x$on_block" == "xyes" ]; then
     51                     local rem
     52                     (( rem = 78 - indent ))
     53                     case "$block_name" in
     54                       \**) printf "%${indent}s * %s\n" "" "$t_line" ;;
     55                         *)
     56                             if [ "$show_bar" == "yes" ]; then
     57                                 printf "%${indent}s%-${rem}s//\n" "" "$t_line"
     58                             else
     59                                 printf "%${indent}s%s\n" "" "$t_line"
     60                             fi
     61                             ;;
     62                     esac
     63                 fi
     64         esac
     65     done
     66     return 1
     67 }
     68 
     69 txt=$1
     70 [ -f "$txt" ] || die_usage
     71 read -r template < "$txt"
     72 case "$template" in
     73     @TEMPLATE*) template=${txt%/*}/${template##@TEMPLATE } ;;
     74     *) die "Failed to parse template name from '$template'" ;;
     75 esac
     76 
     77 while IFS=$'\n' read -r line; do
     78     case "$line" in
     79         @*) include_block yes < "$txt" \
     80             || include_block < "${template%.c}.txt" \
     81             #|| echo "WARNING: failed to find text for block $block_name" >&2
     82             ;;
     83         *)  echo "$line" ;;
     84     esac
     85 done < "$template"
     86