summaryrefslogtreecommitdiff
path: root/examples/gen_example_text.sh
blob: fcc73d8c1f0657960d7d1b0e51500ef40b8c20c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/bash
##
##  Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
##
##  Use of this source code is governed by a BSD-style license 
##  that can be found in the LICENSE file in the root of the source
##  tree. An additional intellectual property rights grant can be found
##  in the file PATENTS.  All contributing project authors may 
##  be found in the AUTHORS file in the root of the source tree.
##


# gen_example_text.sh

self=$0

die_usage() {
    echo "Usage: $self <example.txt>"
    exit 1
}

die() {
    echo "$@"
    exit 1
}

include_block() {
    local on_block
    while IFS=$'\n' read -r t_line; do
        case "$t_line" in
            \~*\ ${block_name})
                if [ "x$on_block" == "xyes" ]; then
                    return 0;
                else
                    on_block=yes
                fi
                ;;
            *)
                if [ "x$on_block" == "xyes" ]; then
                    echo "$t_line"
                fi
                ;;
        esac
    done
    echo "WARNING: failed to find text for block $block_name" >&2
    return 1
}

txt=$1
[ -f "$txt" ] || die_usage
read -r template < "$txt"
case "$template" in
    @TEMPLATE*) template=${txt%/*}/${template##@TEMPLATE } ;;
    *) die "Failed to parse template name from '$template'" ;;
esac

fence="~~~~~~~~~"
fence="${fence}${fence}"
fence="${fence}${fence}"
fence="${fence}${fence}"
while IFS=$'\n' read -r line; do
    case "$line" in
        @TEMPLATE*)
            template=${template##@TEMPLATE }
            template=${template%.c}.txt
            ;;
        @DEFAULT)
            include_block < "$template"
            ;;
        ~~~*)
            block_name=${line##~* }
            [ "$block_name" == "INTRODUCTION" ] || echo "$fence"
            ;;
        *)  echo "$line"
            ;;
    esac
done < "$txt"

echo
echo "Putting It All Together"
echo "======================="
echo "${fence}"
${self%/*}/gen_example_code.sh "${txt}"
echo "${fence}"