File size: 1,562 Bytes
908351f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import re


def main():
    args = parse_command_line()
    data = read(args.input)
    data = convert(data)
    write(args.output, data)


def parse_command_line():
    parser = argparse.ArgumentParser(
        description='Replace \\def with \\newcommand where possible.',
    )
    parser.add_argument(
        'input',
        help='TeX input file with \\def',
    )
    parser.add_argument(
        '--output',
        '-o',
        required=True,
        help='TeX output file with \\newcommand',
    )

    return parser.parse_args()

def read(path):
    with open(path, mode='rb') as handle:
        return handle.read()


def convert(data):
    return re.sub(
        rb'((?:\\(?:expandafter|global|long|outer|protected)'
        rb'(?: +|\r?\n *)?)*)?'
        rb'\\def *(\\[a-zA-Z]+) *(?:#+([0-9]))*\{',
        replace,
        data,
    )


def replace(match):
    prefix = match.group(1)
    if (
            prefix is not None and
            (
                b'expandafter' in prefix or
                b'global' in prefix or
                b'outer' in prefix or
                b'protected' in prefix
            )
    ):
        pass #return match.group(0)

    result = rb'\newcommand'

    result += b'{' + match.group(2) + b'}'
    if match.lastindex == 3:
        result += b'[' + match.group(3) + b']'

    result += b'{'
    return result


def write(path, data):
    with open(path, mode='wb') as handle:
        handle.write(data)

    print('=> File written: {0}'.format(path))


if __name__ == '__main__':
    main()