From 605f9704870943a37dfe503aac2fa1c4e7c79741 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Mon, 9 Nov 2020 21:19:27 +0100 Subject: Add mode switch. --- do.py | 120 ++++++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 77 insertions(+), 43 deletions(-) diff --git a/do.py b/do.py index 1f4e6a9..c7e763d 100755 --- a/do.py +++ b/do.py @@ -10,19 +10,26 @@ from base64 import b64encode import os.path as path import email from email.policy import default +from datetime import datetime path_base = '/' +# mode = 'tex' +mode = 'txt' + # ------------------------------------------------------------ def includepdf(addr, full_file): - arg = 'frame' - arg += ',pages={-}' - pagecommand = '\\thispagestyle{fancy}' - pagecommand += f'\\lhead{{Bilaga \\Alph{{section}}.\\arabic{{subsection}} {texify_filename(addr)} }}' - arg += f',pagecommand={{{pagecommand}}}' - arg += ',width=\\textwidth' - return f'\\phantomsection\\stepcounter{{subsection}}\\includepdf[{arg}]{{{full_file}}}\n' + if mode == 'tex': + arg = 'frame' + arg += ',pages={-}' + pagecommand = '\\thispagestyle{fancy}' + pagecommand += f'\\lhead{{Bilaga \\Alph{{section}}.\\arabic{{subsection}} {texify_filename(addr)} }}' + arg += f',pagecommand={{{pagecommand}}}' + arg += ',width=\\textwidth' + return f'\\phantomsection\\stepcounter{{subsection}}\\includepdf[{arg}]{{{full_file}}}\n' + elif mode == 'txt': + return f"Se pdf {addr}\n" def shorten_mail(str): @@ -64,7 +71,8 @@ def resolve_mail(addr, frag, comment): with open(mailfile, 'rb') as f: msg = email.message_from_binary_file(f, policy=default) - outstr += '\\begin{verbatim}\n' + if mode == 'tex': + outstr += '\\begin{verbatim}\n' # mail += msg.get_content_type() + '\n' @@ -77,7 +85,8 @@ def resolve_mail(addr, frag, comment): outstr += shorten_mail(body.get_content()) else: outstr += body.get_content() - outstr += '\\end{verbatim}\n' + if mode == 'tex': + outstr += '\\end{verbatim}\n' # TODO merge this logic with the resolve file logic below for part in msg.iter_attachments(): @@ -92,15 +101,21 @@ def resolve_mail(addr, frag, comment): # outstr += part.get_filename() + '\n' outstr += includepdf(safe_filename, '/tmp/' + safe_filename) else: - outstr += f'\\subsection{{{texify_filename(part.get_filename())}}}\n' + if mode == 'tex': + outstr += f'\\subsection{{{texify_filename(part.get_filename())}}}\n' + elif mode == 'txt': + subsectioncounter += 1 + outstr += f' {sectioncounter}.{subsectioncounter}. {part.get_filename()}\n' content = part.get_content() if max(len(s) for s in content.split('\n')) > 100: # outstr += '\\setlength\\parindent{24pt}' outstr += part.get_content() + '\n' else: - outstr += '\n\\begin{verbatim}' + if mode == 'tex': + outstr += '\n\\begin{verbatim}' outstr += part.get_content() + '\n' - outstr += '\\end{verbatim}' + if mode == 'tex': + outstr += '\\end{verbatim}' # mail += f'{part.get_content_type()} - {part.get_filename()}\n' # for part in msg.walk(): @@ -150,9 +165,11 @@ def resolve_default(addr, frag, comment): outstr = '' if addr == '': page = get_heading(frag, data) - outstr += '\\begin{verbatim}\n' + if mode == 'tex': + outstr += '\\begin{verbatim}\n' outstr += page + '\n' - outstr += '\\end{verbatim}\n' + if mode == 'tex': + outstr += '\\end{verbatim}\n' else: print(addr) print("Vimwiki intra-page links not yet supported") @@ -187,37 +204,44 @@ def get_heading(name, data): [_, infile, heading, *rest] = sys.argv + file = open(infile, 'r') data = file.read() path_base = path.dirname(infile) -output = open('doc.tex', 'w') +output = open(f'doc.{mode}', 'w') name = pwd.getpwuid(os.getuid()).pw_gecos -output.write(f''' -\\documentclass[a4paper]{{article}} - -\\usepackage[T1]{{fontenc}} -\\usepackage[utf8]{{inputenc}} -\\usepackage[swedish]{{babel}} -\\usepackage{{verbatim}} -\\usepackage{{fullpage}} -\\usepackage{{pdfpages}} -\\usepackage{{subfiles}} -\\usepackage{{parskip}} -\\usepackage{{fancyhdr}} -\\usepackage{{hyperref}} - -\\title{{{heading}}} -\\date\\today -\\author{{{name}}} -\\begin{{document}} -\\maketitle -% \\tableofcontents -''') +if mode == 'tex': + output.write(f''' + \\documentclass[a4paper]{{article}} + + \\usepackage[T1]{{fontenc}} + \\usepackage[utf8]{{inputenc}} + \\usepackage[swedish]{{babel}} + \\usepackage{{verbatim}} + \\usepackage{{fullpage}} + \\usepackage{{pdfpages}} + \\usepackage{{subfiles}} + \\usepackage{{parskip}} + \\usepackage{{fancyhdr}} + \\usepackage{{hyperref}} + + \\title{{{heading}}} + \\date\\today + \\author{{{name}}} + \\begin{{document}} + \\maketitle + % \\tableofcontents + ''') +elif mode == 'txt': + d = f'{datetime.now():%Y-%m-%d}' + output.write(f'\n{heading.center(40)}\n{d.center(40)}\n') + sectioncounter = 0 + subsectioncounter = 0 # '\\verbatiminput @@ -262,20 +286,30 @@ for match in tag_iter: # disabled for full PDF:s attach_str = '' - if proto != 'file': - attach_str += f"\\section{{{title}}}\n\\label{{{uid}}}\n" - else: - attach_str += f"\\phantomsection\n\\label{{{uid}}}\n" + if mode == 'tex': + if proto != 'file': + attach_str += f"\\section{{{title}}}\n\\label{{{uid}}}\n" + else: + attach_str += f"\\phantomsection\n\\label{{{uid}}}\n" + elif mode == 'txt': + sectioncounter += 1 + subsectioncounter = 0 + if proto != 'file': + st = f"{chr(sectioncounter + 64)}. {title}" + attach_str += f'\n{st}\n{"="*len(st)}\n' resolver = resolvers.get(proto, resolve_default) attach_str += resolver(addr, frag, comment) bilagor += [attach_str] -output.write('\\begin{verbatim}\n') +if mode == 'tex': + output.write('\\begin{verbatim}\n') output.write(outstr + '\n') -output.write('\\end{verbatim}\n\\appendix\n') +if mode == 'tex': + output.write('\\end{verbatim}\n\\appendix\n') output.write('\n'.join(bilagor)) -output.write('\\end{document}') +if mode == 'tex': + output.write('\\end{document}') -- cgit v1.2.3