2 # generates markdown tables from CSV files, after doing lovely sorting
3 # by different columns, blah blah...
4 from copy
import deepcopy
6 def write_mdwn_row(f
, row
):
11 def underlines(header
):
14 row
.append("-" * len(col
))
17 def print_table(title
, header
, areas
, sortby
):
18 fname
= title
.lower().replace(" ", "_")
19 with
open("ls012/%s.mdwn" % fname
, "w") as f
:
20 # write out the page header
21 f
.write("\\newpage{}\n")
23 f
.write("# %s\n" % title
)
25 # sort everything if required
26 if sortby
is not None:
29 # start writing out areas
30 for title
, rows
in areas
.items():
31 # start new page (if not first newpage)
33 f
.write("\\newpage{}\n")
35 if linecount
is None: # skipped first newpage
38 f
.write("## %s\n" % title
)
41 # work out maximum length of items, and adjust header
42 hdr
= deepcopy(header
)
47 for hd
, value
in row
.items():
48 cols
[hd
] = max(cols
[hd
], len(value
))
49 # adjust header (add spaces)
50 for i
, hd
in enumerate(hdr
):
51 n_spaces
= cols
[hd
] - len(hd
)
52 hdr
[i
] = hdr
[i
] + " " * n_spaces
54 write_mdwn_row(f
, hdr
)
55 write_mdwn_row(f
, underlines(hdr
))
57 # adjust row (add same spaces as header width)
59 for (value
, col_len
) in zip(row
.values(), cols
.values()):
60 value
= value
.replace("_", "\_") # latex, duh
61 n_spaces
= col_len
- len(value
)
62 r
.append(value
+ " " * n_spaces
)
67 # approx 6 lines per header
70 # allow 60 rows per page
71 linecount
+= len(rows
)
75 if __name__
== '__main__':
76 with
open("ls012/optable.csv") as f
:
77 l
= map(str.strip
, f
.readlines())
81 if line
.startswith("#"):
82 area
= line
[1:].strip()
85 # split line by commas, whitespace-strip it
86 line
= list(map(str.strip
, line
.split(',')))
91 # create a dictionary by tuple of header+line
92 linedict
= dict(zip(header
, line
))
97 areas
[area
].append(linedict
)
99 # exccellent - now have a dictionary of list of dictionaries:
100 # area - list-of-instructions - dictionary-by-heading
101 print_table("Areas", header
, areas
, None)