-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
149 lines (116 loc) · 4.32 KB
/
main.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import argparse
import csv
import sys
from pathlib import Path
from pick import pick
CSV_FILE = 'input.csv'
TEMPLATE_FILE = 'template.tex'
MIN_FIELDS = 3
PLACEHOLDER_CMD = '%%%%%custom-command%%%%%'
PLACEHOLDER_CCOMMENT = '%%%%%ccomments%%%%%'
COLOR_REV_DEFAULT = 'colorRevDefault'
def custom_tex_command(fieldnames: list[str]) -> str:
amount = len(fieldnames)
colors = '\\colorlet{' + COLOR_REV_DEFAULT + '}{black!15!white}\n\n'
cmd = (
colors
+ f'''\\newcommand{{\\ccomment}}[{amount + 1}]{{
\\begin{{tcolorbox}}[title=#2, breakable, colback=white, coltitle=black, colbacktitle=#1]
\\textbf{{{fieldnames[1]}:}} #3 \\tcblower
\\textbf{{{fieldnames[2]}:}} #4
'''
)
for i in range(3, amount):
cmd += f' \\\\ \\textbf{{{fieldnames[i]}:}} #{i+2} \n'
cmd += '\\end{tcolorbox}\n}\n'
return cmd
def color_command(rev_ids: list[str]) -> str:
cmd = ''
for rev_id in rev_ids:
cmd += f'\\colorlet{{color{rev_id}}}{{black!15!white}}\n'
return cmd
def to_latex(s: str) -> str:
replacements = {
'&': '\\&',
'%': '\\%',
'$': '\\$',
'#': '\\#',
'_': '\\_',
'{': '\\{',
'}': '\\}',
'~': '\\textasciitilde',
'^': '\\textasciicircum',
'\\': '\\textbackslash',
'|': '\\textbar',
'<': '\\textless',
'>': '\\textgreater',
'[': '{[}',
']': '{]}',
}
tex = "".join(replacements.get(c, c) for c in s)
tex = tex.replace('\n-', '\n\\\\-')
tex = tex.replace('\n -', '\n\\\\ -')
return tex
def extract_rev_id(full_id: str) -> str:
return full_id.split(".")[0].split("-")[0].split(":")[0]
def create_box(row, fieldnames: list[str], reviewer_ids: list[str]) -> str:
box_cmd = '\n\n\\ccomment'
rev_id = extract_rev_id(row[fieldnames[0]])
if rev_id in reviewer_ids:
box_cmd += f'{{\ncolor{rev_id}\n}}'
else:
box_cmd += f'{{\n{COLOR_REV_DEFAULT}\n}}'
for field in fieldnames:
tex = to_latex(row[field])
tex = tex.rstrip()
box_cmd += f'{{ % {field}\n{tex}\n}}'
return box_cmd
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='desc')
parser.add_argument('comment_file', type=Path, help='The comment csv file')
parser.add_argument('tex_file', type=Path, help='The TeX output file')
args = parser.parse_args()
box_cmd = ''
cmd = ''
color_cmd = ''
with open(args.comment_file, 'r', encoding='utf8') as csv_file:
dialect = csv.Sniffer().sniff(csv_file.readline(), delimiters=',;')
csv_file.seek(0)
reader = csv.DictReader(csv_file, delimiter=dialect.delimiter)
if reader.fieldnames is None:
print('Unable to detect columns in CSV file')
sys.exit()
if len(reader.fieldnames) < MIN_FIELDS:
print(
f'CSV file must have at least {MIN_FIELDS} columns but has {len(reader.fieldnames)}'
+ '.\nUnable to proceed.'
)
sys.exit()
selected = pick(
reader.fieldnames,
'Select at least three fields (ID, reviewer comment, author response)',
multiselect=True,
min_selection_count=MIN_FIELDS,
)
selected_fields: list[str] = [s[0] for s in selected if type(s) is tuple]
cmd = custom_tex_command(selected_fields)
all_reviewer_ids: list[str] = []
for row in reader:
is_empty_row = not any(row.values())
if is_empty_row:
continue
rev_id = extract_rev_id(row[selected_fields[0]])
if rev_id not in all_reviewer_ids:
all_reviewer_ids.append(rev_id)
box_cmd += create_box(row, selected_fields, all_reviewer_ids)
color_cmd = color_command(all_reviewer_ids)
final = ''
with open(TEMPLATE_FILE, 'r', encoding='utf8') as template:
for line in template:
replaced = line.replace(PLACEHOLDER_CMD, color_cmd + cmd)
replaced = replaced.replace(PLACEHOLDER_CCOMMENT, box_cmd)
final += replaced
args.tex_file.parent.mkdir(parents=True, exist_ok=True)
with open(args.tex_file, 'w', encoding='utf8') as target:
target.write(final)
print(f'Done! Created LaTeX document: file://{Path.cwd()}/{args.tex_file}')