-
Notifications
You must be signed in to change notification settings - Fork 1
/
latexme.py
119 lines (97 loc) · 3.59 KB
/
latexme.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
# -*- coding:utf-8 -*-
import sys
import os
from collections import namedtuple, defaultdict
CODE_DIR = 'code'
Sections = defaultdict(lambda : 'Other',
{
'graphs' : 'Graph',
'binary-search' : 'Binary Search',
'disjoint-set' : 'Disjoint Set',
'c++11' : 'C++11 Features',
'fenwick' : 'Fenwick',
'geometry' : 'Geometry',
'math' : 'Math',
'string' : 'String',
})
FileTypes = {
'cpp' : 'C++',
'md' : 'Markdown',
'java' : 'Java',
'py' : 'Python',
}
def is_code(code):
return code.filetype in 'C++', 'Java', 'Python'
def latexify(text):
return text.replace('_', r'\_').replace('[', r'\lbrack ').replace(
']', r'\rbrack ')
Entry = namedtuple('Entry', ['filename', 'text', 'filetype',
'section', 'path'])
def append(collection, item):
collection.append(item)
def recursive_collect(collection, directory):
for fname in os.listdir(directory):
path = os.path.join(directory, fname)
if os.path.isdir(path):
if not path.endswith('test'):
recursive_collect(collection, path)
elif os.path.isfile(path):
with open(path, 'r') as f:
content = [latexify(line) for line in f.readlines()]
def first_match(dictionary, string):
for key in dictionary:
if string.endswith(key):
return dictionary[key]
return None
filetype = first_match(FileTypes, fname)
section = first_match(Sections, directory)
if filetype and section:
append(collection, Entry(
latexify(fname), content, latexify(filetype),
latexify(section), path))
header = r"""\title{A Competitive Programming Cheat Sheet}
\documentclass[11pt,twocolumn,landscape]{article}
\usepackage{listings}
\usepackage[landscape,margin=0.5in]{geometry}
\usepackage[usenames,dvipsnames]{color}
\usepackage[utf8]{inputenc}
\author{23.15\% Mer Kräm}
\definecolor{comment-color}{rgb}{0.0,0.3,0.0} % Comment color
\definecolor{highlight}{RGB}{255,251,204} % Code highlight color
\definecolor{light-gray}{gray}{0.85}
\definecolor{background-color}{gray}{0.95}
\definecolor{string-color}{rgb}{0.05,0.6,0.0}
\definecolor{keyword-color}{RGB}{255,41,41}
\lstset{
language=C++,
basicstyle=\footnotesize,
backgroundcolor=\color{background-color},
commentstyle=\color{comment-color},
stringstyle=\color{string-color},
keywordstyle=\color{keyword-color},
}
\begin{document}
\maketitle
\clearpage
"""
if __name__ == '__main__':
if len(sys.argv) != 2:
print("'python3 latexme.py output_filename' needs to be run")
exit(1)
entries = []
recursive_collect(entries, CODE_DIR)
entries.sort(key=lambda x: x.section or "ZZZ")
last_section = "TheCoolestPlaceholderAlive"
sections = ""
for entry in entries:
if entry.section != last_section:
#sections += r"\clearpage" + "\n"
sections += r"\section{" + entry.section + "}\n\n"
if is_code(entry):
sections += r'\subsection{' + entry.filename + '}\n'
sections += r'\lstinputlisting{"' + entry.path + '"}\n\n'
last_section = entry.section
sections += r"\end{document}" + "\n"
full_text = header + sections
with open(sys.argv[1], 'w') as f:
print(full_text, file=f)