-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfmt
executable file
·82 lines (68 loc) · 2.36 KB
/
rfmt
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
#!/usr/bin/python
# Developed by Joshua Gutow. Copyright 2016 under the MIT License.
desc = '''This program takes manually word-wrapped files and un-word-wraps them.
This program un-word-wraps using the following rules:
1. If there are multiple newlines in a row, only the first will be removed.
2. It will only merge lines if they have the same indent level.
3. It will try to preserve lists and simple bullet points.
You can give a file name, pipe text to it, or run rfmt without any parameters,
and press CTRL-D (EOF) when you are done. If given both a file and STDIN, rfmt
will reformat the file.'''
import argparse
import sys
import re
# Sets up the command line arguments.
parser = argparse.ArgumentParser(description=desc,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("File", type=argparse.FileType('r'), metavar='F',
help="File to be read from.",
nargs='?')
args = parser.parse_args()
# Reads either from STDIN or the specified file.
if args.File != None:
file = args.File
else:
file = sys.stdin
# Sets up the lines to manipulated on by the program.
# An empty string is derived from an empty line.
text = []
for line in file:
text.append(line.rstrip())
# Merges two lines together.
def merge(one, two):
if one == "" or one[-1] == "\n":
return one + two.lstrip()
return one + " " + two.lstrip()
# Inserts a newline char between two lines.
def append(one, two):
return one + "\n" + two
# Figures out the indent level of a function.
def indent_level(string):
lvl = 0
for char in string:
if char == "\t":
lvl += 1
else:
break
return lvl
# Figures out if the line contains the start of a list.
def start_list(string):
list_patterns = ["[0-9]?[.][ ]", "[*][ ]", "[A-Z][.][ ]"]
string = string.lstrip()
for regex in list_patterns:
if re.match(regex, string):
return True
return False
ret = ""
# Does the logic to figure out what lines to merge and what to append.
for num in range(0, len(text)):
line = text[num]
if line == "":
ret += "\n"
elif num != 0 and indent_level(line) != indent_level(text[num-1]):
ret = append(ret, line)
elif start_list(line):
ret = append(ret, line)
else:
ret = merge(ret, line)
print(ret)