Print outline from master tex file

This commit is contained in:
Benjamin Bohard 2020-09-04 10:35:58 +02:00
parent 4b691783be
commit bb699bf2ee
1 changed files with 64 additions and 9 deletions

View File

@ -27,6 +27,10 @@ LATEX_SUBS = [(re.compile('_'), '\\_'),
] ]
DOCUMENTCLASS_RE = re.compile(r'\\documentclass\{(?P<document_class>.+?)\}')
SKBCONFIG_RE = re.compile(r'\\skbconfig\[\n\s*root\s*=\s*(?P<root>.*),\n\s*rep\s*=\s*(?P<rep>.*),\n\s*pub\s*=\s*(?P<pub>.*),\n\s*fig\s*=\s*(?P<fig>.*),\n\s*sli\s*=\s*(?P<sli>.*),\n\s*acr\s*=\s*(?P<acr>.*),\n\s*bib\s*=\s*(?P<bib>.*)\n\s*\]\{skblocal.tex\}', re.M)
SKBINPUT_RE = re.compile(r'[^%]\\skbinput\[from=(?P<rep>.*?)(,.*)?\]\{(?P<tex>.*?)\}', re.M)
def get_unique_name(base): def get_unique_name(base):
now = time.localtime() now = time.localtime()
year = str(now[0]) year = str(now[0])
@ -143,15 +147,11 @@ def main():
""" """
update function update function
""" """
re_class = re.compile(r'\\documentclass\{(?P<document_class>.*)\}')
#skbconfig_re = re.compile(r'\\skbconfig\[\s*root\s*=\s*(?P<root>.*),\s*rep\s*=\s*(?P<rep>.*),\s*pub\s*=\s*(?P<pub>.*),\s*fig\s*=\s*(?P<fig>.*),\s*sli\s*=\s*(?P<sli>.*),\s*acr\s*=\s*(?P<acr>.*),\s*bib\s*=\s*(?P<bib>.*),\s*\]\{skblocal.tex\}', re.M)
skbconfig_re = re.compile(r'\\skbconfig\[\n\s*root\s*=\s*(?P<root>.*),\n\s*rep\s*=\s*(?P<rep>.*),\n\s*pub\s*=\s*(?P<pub>.*),\n\s*fig\s*=\s*(?P<fig>.*),\n\s*sli\s*=\s*(?P<sli>.*),\n\s*acr\s*=\s*(?P<acr>.*),\n\s*bib\s*=\s*(?P<bib>.*)\n\s*\]\{skblocal.tex\}', re.M)
skbinput_re = re.compile(r'[^%]\\skbinput\[from=(?P<rep>.*?)(,.*)?\]\{(?P<tex>.*?)\}', re.M)
with open(args.master, 'r') as master: with open(args.master, 'r') as master:
tex_master = master.read() tex_master = master.read()
tex_class = re_class.search(tex_master) tex_class = DOCUMENTCLASS_RE.search(tex_master)
tex_skbconfig = skbconfig_re.search(tex_master) tex_skbconfig = SKBCONFIG_RE.search(tex_master)
tex_skbinputs = skbinput_re.finditer(tex_master) tex_skbinputs = SKBINPUT_RE.finditer(tex_master)
fragment = TEMPLATES[tex_class.group('document_class')]['fragment'] fragment = TEMPLATES[tex_class.group('document_class')]['fragment']
fragment_pratique = TEMPLATES[tex_class.group('document_class')]['fragment_pratique'] fragment_pratique = TEMPLATES[tex_class.group('document_class')]['fragment_pratique']
@ -177,14 +177,69 @@ def main():
""" """
outline creation outline creation
""" """
part_level = 0
section_level = 1
subsection_level = 2
frametitle_level = 3
framesubtitle_level = 4
def file_path_from_skbinput(skbinput_re, master, skbconfig):
rel_path = path.join(skbconfig.group('root'), skbconfig.group(skbinput_re.group('rep')), skbinput_re.group('tex')) + '.tex'
root_path = path.abspath(path.dirname(master))
return path.normpath(path.join(root_path, rel_path))
def reorder_lists(*args):
reordered_list = []
for l in args:
reordered_list.extend(l)
reordered_list.sort(key=lambda x: x[0])
return reordered_list
def outline_from_include(include, start, document_class):
frametitle_re = re.compile(r'\\frametitle\{(?P<name>.*?)\}')
framesubtitle_re = re.compile(r'\\framesubtitle\{(?P<name>.*?)\}')
skbheading_re = re.compile(r'\\skbheading\{(?P<name>.*?)\}')
with open(include, 'r') as include_fh:
content = include_fh.read()
if document_class == 'beamer':
frametitles = frametitle_re.finditer(content)
framesubtitles = framesubtitle_re.finditer(content)
frametitles_list = [(ft.start(), frametitle_level, ft.group('name')) for ft in frametitles]
framesubtitles_list = [(fs.start(), framesubtitle_level, fs.group('name')) for fs in framesubtitles]
frame_list = reorder_lists(frametitles_list, framesubtitles_list)
if frame_list:
div = int('1{}'.format('0'*len(str(frame_list[-1][0]))))
return [(start + f[0]/div, f[1], f[2]) for f in frame_list]
else:
return []
def outline_format(headers_list):
levels = list(set([hl[1] for hl in headers_list]))
levels.sort()
flattened_levels = {l: levels.index(l) for l in levels}
for header in headers_list:
print('{}{}'.format('\t' * flattened_levels[header[1]], header[2]))
section_re = re.compile(r'\\section\{(?P<name>.*?)\}') section_re = re.compile(r'\\section\{(?P<name>.*?)\}')
part_re = re.compile(r'\\part\{(?P<name>.*?)}') part_re = re.compile(r'\\part\{(?P<name>.*?)}')
subsection_re = re.compile(r'\\subsection\{(?P<name>.*?)\}') subsection_re = re.compile(r'\\subsection\{(?P<name>.*?)\}')
with open(args.master, 'r') as master_tex: with open(args.master, 'r') as master_tex:
master = master_tex.read() master = master_tex.read()
skbconfig = SKBCONFIG_RE.search(master)
document_class = DOCUMENTCLASS_RE.search(master).group('document_class')
parts = part_re.finditer(master) parts = part_re.finditer(master)
section = section_re.finditer(master) sections = section_re.finditer(master)
subsection_re = subsection_re.finditer(master) subsections = subsection_re.finditer(master)
includes = SKBINPUT_RE.finditer(master)
print(document_class)
parts_list = [(part.start(), part_level, part.group('name')) for part in parts]
sections_list = [(section.start(), section_level, section.group('name')) for section in sections]
includes_list = [element for skbinput in includes for element in outline_from_include(file_path_from_skbinput(skbinput, args.master, skbconfig), skbinput.start(), document_class)]
subsections_list = [(subsection.start(), subsection_level, subsection.group('name')) for subsection in subsections]
print(outline_format(reorder_lists(parts_list, sections_list, includes_list, subsections_list)))
jinja_loader = FileSystemLoader('./templates') jinja_loader = FileSystemLoader('./templates')