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