Skip to content
Snippets Groups Projects
Commit f1339082 authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei
Browse files

Fix a lot of stuff

parent 4b213008
No related branches found
No related tags found
No related merge requests found
#!/bin/bash #!/bin/bash
#tex_file=$(mktemp) if [ "$1" = "DEBUG" ]; then
python3 notebook/gen_latex.py # --header=notebook/header.tex --output=$tex_file tex_file=result.tex
python3 notebook/gen_latex.py --header=notebook/header.tex --output=$tex_file
else
tex_file=$(mktemp)
python3 notebook/gen_latex.py --header=notebook/header.tex --output=$tex_file
#pdflatex $tex_file -output-directory . && pdflatex $tex_file -output-directory . &&
#pdflatex $tex_file -output-directory . pdflatex $tex_file -output-directory .
#mv tmp.pdf caderno.pdf mv tmp.pdf caderno.pdf
#rm tmp* rm tmp*
fi
/// Template
#include <bits/stdc++.h> #include <bits/stdc++.h>
#define EPS 1e-6 #define EPS 1e-6
......
import argparse
from pathlib import Path from pathlib import Path
class Tree: class Tree:
# Constructor.
# @param dirs list of directories to build Latex over
def __init__(self, dirs): def __init__(self, dirs):
self.dirs = dirs; self.dirs = dirs;
self.tree = {} self.tree = {}
self.build() self.build()
# Sorts the lists on tree leaves.
def sort(self, sub):
if type(sub) == list:
return sorted(sub)
else:
for i in sub:
sub[i] = self.sort(sub[i])
return sub
# Builds a tree represented as dict, with structure of files # Builds a tree represented as dict, with structure of files
# and directories. # and directories.
def build(self): def build(self):
for di in self.dirs: for di in self.dirs:
path_list = Path(di).glob('**/*.cpp') path_list = sorted(list(Path(di).glob('**/*.cpp')))
for path in path_list: for path in path_list:
branch = str(path).split('/') branch = str(path).split('/')
curr = self.tree node = self.tree
for i in branch[:-2]: for i in branch[:-2]:
if not i in curr: if i not in node:
curr[i] = {} node[i] = {}
curr = curr[i] node = node[i]
if not branch[-2] in curr: if branch[-2] not in node:
curr[branch[-2]] = [] node[branch[-2]] = []
curr[branch[-2]].append(str(branch[-1])) node[branch[-2]].append(str(branch[-1]))
self.tree = self.sort(self.tree)
# Allows access to tree dict from instance of class.
def __getitem__(self, arg): def __getitem__(self, arg):
return self.tree[arg] return self.tree[arg]
# Allows iteration on tree dict from instance of class.
def __iter__(self): def __iter__(self):
return iter(self.tree) return iter(self.tree)
class LatexGenerator: class LatexGenerator:
def __init__(self, tree): def __init__(self, tree, output, header):
# TODO: Create output file, add header and finish code parsing and latex self.output = open(output, 'w')
self.header = open(header, 'r')
self.tree = tree self.tree = tree
self.hierarchy = ['chapter'] + [i*'sub' + 'section' for i in range(3)] self.files = {}
self.hierarchy = ['part', 'chapter'] + [i*'sub' + 'section' for i in range(3)]
self._add_header()
self.gen_latex(tree) self.gen_latex(tree)
self.output.write('\end{document}')
# Adds Latex header to the output.
def _add_header(self):
lines = self.header.readlines()
for i in lines:
self.output.write(i)
self.output.write('\\newpage\n')
self.output.write('\n')
# Prints section title in Latex format. # Prints section title in Latex format.
# TODO: Instead of parsing format, get name of section through header of
# the algorithm/problem file. Capitalization should be used only for
# directories.
def gen_title_latex(self, content, depth): def gen_title_latex(self, content, depth):
text = ' '.join(list(map(lambda x : x.capitalize(), self.output.write('\\' + self.hierarchy[depth] + '{' + content + '}' + '\n')
content.split('.')[0].split('_'))))
print('\\' + self.hierarchy[depth] + '{' + text + '}')
# Prints code in Latex format. # Prints code in Latex format.
def gen_code_latex(self, path): def gen_code_latex(self, path):
# TODO: Add code parsing funcion and code to latex generation # TODO: Add code parsing funcion to extract text
pass self.output.write('\\begin{lstlisting}[style=customcpp]\n')
with open(path) as f:
lines = f.readlines()
for i in lines:
self.output.write(i)
self.output.write('\\end{lstlisting}\n')
self.output.write('\n')
# Generates Latex for entire tree recursively # Generates Latex for entire tree recursively
def gen_latex(self, sub, path = '', depth = 0): def gen_latex(self, sub, path = '', depth = 0):
if type(sub) == list: if type(sub) == list:
for i in sub: for i in sub:
self.gen_title_latex(i, depth) self.gen_title_latex(self._parse_file_name(path + i), depth)
self.gen_code_latex(path + i) self.gen_code_latex(path + i)
else: else:
for i in sub: for i in sub:
self.gen_title_latex(i, depth) self.gen_title_latex(self._parse_dir_name(i), depth)
self.gen_latex(sub[i], path + i + '/', depth + 1) self.gen_latex(sub[i], path + i + '/', depth + 1)
#self.output.write('\\newpage\n')
self.output.write('\n')
# Parses name of the section (capitalize)
def _parse_dir_name(self, name):
return ' '.join(list(map(lambda x : x.capitalize(), name.split('_'))))
# Parses name of file by getting the first line of the source code.
def _parse_file_name(self, name):
with open(name) as f:
result = f.readline()
return result[4:-1]
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('--header',
action='store',
type=str,
help='Header of Latex file')
parser.add_argument('--output',
action='store',
type=str,
help='Output Latex file')
return parser.parse_args()
def main(): def main():
dirs = [ 'algorithms', 'misc' ] args = get_args()
tree = Tree(dirs)
tex = LatexGenerator(tree) tree = Tree(['algorithms', 'misc'])
tex = LatexGenerator(tree, args.output, args.header)
if __name__ == "__main__": if __name__ == "__main__":
main() main()
\documentclass{article} \documentclass[oneside]{book}
\usepackage{listings} \usepackage{listings}
\usepackage{titlesec} \usepackage{titlesec}
\usepackage[a4paper, total={6in, 8in}]{geometry} \usepackage[a4paper, total={6in, 8in}]{geometry}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment