File: split_man.py

package info (click to toggle)
re2c 4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,512 kB
  • sloc: cpp: 34,160; ml: 8,494; sh: 5,311; makefile: 1,014; haskell: 611; python: 431; ansic: 234; javascript: 113
file content (82 lines) | stat: -rw-r--r-- 2,013 bytes parent folder | download
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
#!/usr/bin/env python3

"""
Generates language-specific manpages by replacing placeholder tokens in the
input manpage.
"""

import os
import re
import sys

if len(sys.argv) != 3:
    print('usage:', sys.argv[0], '<input> <output>')
    exit(1)

input = sys.argv[1]
output = sys.argv[2]

# Extract language name from the output filename.
lang = re.search('re2([a-z]+)', os.path.basename(output)).group(1).encode('utf-8')
prog = b're2' + lang
hdr_ext = None
disclaimer = b''

if lang == b'c':
    src_ext = b'c'
    hdr_ext = b'h'
    lang_name = b'C/C++'
    disclaimer = b'Note: some examples are in C++ (but can be adapted to C).'
elif lang == b'd':
    src_ext = b'd'
    lang_name = b'D'
elif lang == b'go':
    src_ext = b'go'
    lang_name = b'Go'
elif lang == b'hs':
    lang = b'haskell'
    src_ext = b'hs'
    lang_name = b'Haskell'
elif lang == b'java':
    src_ext = b'java'
    lang_name = b'Java'
elif lang == b'js':
    src_ext = b'js'
    lang_name = b'JavaScript'
elif lang == b'ocaml':
    src_ext = b'ml'
    lang_name = b'OCaml'
elif lang == b'py':
    lang = b'python'
    src_ext = b'py'
    lang_name = b'Python'
elif lang == b'rust':
    src_ext = b'rs'
    lang_name = b'Rust'
elif lang == b'swift':
    src_ext = b'swift'
    lang_name = b'Swift'
elif lang == b'v':
    src_ext = b'v'
    lang_name = b'V'
elif lang == b'zig':
    src_ext = b'zig'
    lang_name = b'Zig'
else:
    print('***', sys.argv[0], ': unknown lang:', lang)
    exit(1)
if hdr_ext == None:
    hdr_ext = src_ext

with open(input, 'rb') as input_file:
    file_bytes = input_file.read()

file_bytes = file_bytes.replace(b'RE2C_LANG_NAME', lang_name)
file_bytes = file_bytes.replace(b'RE2C_LANG', lang)
file_bytes = file_bytes.replace(b'RE2C_PROG', prog)
file_bytes = file_bytes.replace(b'RE2C_SOURCE_EXT', src_ext)
file_bytes = file_bytes.replace(b'RE2C_HEADER_EXT', hdr_ext)
file_bytes = file_bytes.replace(b'RE2C_DISCLAIMER', disclaimer)

with open(output, 'wb') as output_file:
    output_file.write(file_bytes)