aboutsummaryrefslogtreecommitdiffstats
path: root/src/gpp.py
blob: 7771398438af8f5656ea63ee72dcf427b49ca1a8 (plain) (blame)
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
#!/usr/bin/env python3

import sys

symbol = '@'
encoding = 'utf-8'
input_file = '/dev/stdin'
output_file = '/dev/stdout'

data = None
with open(input_file, 'rb') as file:
    data = file.read().decode(encoding, 'error').split('\n')

entered = False
bashed = []

def pp(line):
    rc = ''
    symb = False
    for c in line:
        if symb:
            symb = False
            if c == symbol:
                rc += c
        elif c == symbol:
            symb = True
        else:
            rc += c
    return rc

for lineno in range(len(data)):
    line = data[lineno]
    if line.startswith(symbol + '<'):
        bashed.append(line[2:])
        entered = True
    elif line.startswith(symbol + '>'):
        bashed.append(line[2:])
        entered = False
    elif entered:
        bashed.append(line)
    else:
        line = '\'%s\'' % line.replace('\'', '\'\\\'\'')
        bashed.append('echo %i %s' % (lineno, pp(line)))