blob: 1133b49518fd851e7ab365116c0223903ed7177a [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001#!/bin/bash
2##
John Koleszarc2140b82010-09-09 08:16:39 -04003## Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar0ea50ce2010-05-18 11:58:33 -04004##
John Koleszar94c52e42010-06-18 12:39:21 -04005## Use of this source code is governed by a BSD-style license
John Koleszar09202d82010-06-04 16:19:40 -04006## 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
John Koleszar94c52e42010-06-18 12:39:21 -04008## in the file PATENTS. All contributing project authors may
John Koleszar09202d82010-06-04 16:19:40 -04009## be found in the AUTHORS file in the root of the source tree.
John Koleszar0ea50ce2010-05-18 11:58:33 -040010##
11
12
13# gen_example_code.sh
14
15self=$0
16
17die_usage() {
18 echo "Usage: $self <example.txt>"
19 exit 1
20}
21
22die() {
23 echo "$@"
24 exit 1
25}
26
27include_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
69txt=$1
70[ -f "$txt" ] || die_usage
71read -r template < "$txt"
72case "$template" in
73 @TEMPLATE*) template=${txt%/*}/${template##@TEMPLATE } ;;
74 *) die "Failed to parse template name from '$template'" ;;
75esac
76
77while 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
85done < "$template"