diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2020-10-05 12:23:49 +0200 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2020-10-05 12:23:49 +0200 |
commit | c6f9c682ab2e94c26da15a7cd341aca08e1064e2 (patch) | |
tree | 93a321bb7b21793a1ad4919a770bbd389c3c3471 | |
parent | Now takes any heading. (diff) | |
download | vimwiki-scripts-c6f9c682ab2e94c26da15a7cd341aca08e1064e2.tar.gz vimwiki-scripts-c6f9c682ab2e94c26da15a7cd341aca08e1064e2.tar.xz |
Huge rewrite.
-rwxr-xr-x | do.py | 172 |
1 files changed, 120 insertions, 52 deletions
@@ -6,6 +6,82 @@ import sys import pwd import subprocess from subprocess import PIPE +from base64 import b64encode +import os.path as path + +path_base = '/' + +# ------------------------------------------------------------ + +def resolve_mail(addr, frag, comment): + + outstr = '' + + msg_id = addr + mailfile = subprocess.run(f"mu find -u 'i:{msg_id}' --fields 'l'", shell=True, stdout=PIPE).stdout.decode('UTF-8').strip() + mail = subprocess.run(f"mu view {mailfile}", shell=True, stdout=PIPE).stdout.decode('UTF-8') + outstr += '\\begin{verbatim}\n' + if frag == 'short': + last_arrow = False + out = [] + for line in mail.split('\n'): + if not line: + out += [''] + continue + if line[0] == '>': + arrow = True + if arrow and not last_arrow: + out += ['> [...]'] + last_arrow = True + continue + out += [line] + outstr += '\n'.join(out) + else: + outstr += mail + '\n' + outstr += '\\end{verbatim}\n' + return outstr + + +def resolve_help(addr, frag, comment): + pass + return '' + + +def resolve_file(addr, frag, comment): + extension = addr.split('.')[-1] + + full_file = path.join(path_base, addr) + + if extension == 'pdf': + return f'\\includepdf[pages=-,pagecommand={{}},width=\\textwidth]{{{full_file}}}\n' + # TODO + # elif extension == 'tex': + # return f'\\subfile{{{full_file}}}\n' + else: + with open(full_file, 'r') as f: + return f.read() + '\n' + + +def resolve_default(addr, frag, comment): + if addr == '': + page = get_heading(frag, data) + outstr += '\\begin{verbatim}\n' + outstr += page + '\n' + outstr += '\\end{verbatim}\n' + else: + print(addr) + print("Vimwiki intra-page links not yet supported") + outstr = '' + return outstr + +# Should all take (url without protocol, fragment, comment) +resolvers = { + 'mail': resolve_mail, + 'file': resolve_file, + 'help': resolve_help, +} + +# ------------------------------------------------------------ def get_heading(name, data): start_match = re.search(f'(?m)^(=+) {name} =+$', data) @@ -22,12 +98,16 @@ def get_heading(name, data): else: return tail +# ------------------------------------------------------------ [_, infile, heading, *rest] = sys.argv file = open(infile, 'r') data = file.read() +path_base = path.dirname(infile) + + output = open('doc.tex', 'w') name = pwd.getpwuid(os.getuid()).pw_gecos @@ -40,6 +120,8 @@ output.write(f''' \\usepackage[swedish]{{babel}} \\usepackage{{verbatim}} \\usepackage{{fullpage}} +\\usepackage{{pdfpages}} +\\usepackage{{subfiles}} \\title{{{heading}}} \\date\\today @@ -53,69 +135,55 @@ output.write(f''' page1 = get_heading(heading, data) # bilagor = get_heading('Bilagor', page1).strip().split('\n') r = "\\[\\[([^|#\\]]*)(?:#([^|\\]]*))?(?:[|]([^\\]]*))?\\]\\]" -bilagor = re.findall(r, page1) +# bilagor = re.findall(r, page1) +tag_iter = re.finditer(r, page1) # bilagor = [m[0] for m in re.findall(r, data)] -output.write('\\begin{verbatim}\n') -output.write(page1 + '\n') -output.write('\\end{verbatim}\n\\appendix\n') +bilagor = [] -print (bilagor) +outstr = "" +pos = 0 -for (address, frag, comment) in bilagor: - print(address,frag,comment) - # m = re.search('^[0-9]+', item) - # if m: - # print(f'Bilaga {m.group(0)}') - # output.write(f'\\section*{{Bilaga {m.group(0)}}}\n') +bilaga_nr = 1 +for match in tag_iter: + url = match.group(1) or '' + frag = match.group(2) or '' + comment = match.group(3) or '' - if comment: - output.write(f"\\section{{{comment}}}\n") - else: - output.write(f"\\section{{{address}}}\n") + outstr += page1[pos:match.start()] + pos = match.end() + + uid = b64encode((url + frag).encode('UTF-8')) + + title = comment or url or fragment + + # outstr += title + f"(bilaga~\\ref{{{uid}}})" + # TODO + # outstr += title + f' (bilaga {chr(bilaga_nr + 64)})' + outstr += title + " (se bilaga)" + bilaga_nr += 1 try: - proto, addr = address.split(":", 1) + proto, addr = url.split(":", 1) except ValueError: proto = '' - addr = address + addr = url print(proto, addr) - if proto == 'mail': - msg_id = addr - mailfile = subprocess.run(f"mu find -u 'i:{msg_id}' --fields 'l'", shell=True, stdout=PIPE).stdout.decode('UTF-8').strip() - mail = subprocess.run(f"mu view {mailfile}", shell=True, stdout=PIPE).stdout.decode('UTF-8') - output.write('\\begin{verbatim}\n') - if frag == 'short': - last_arrow = False - out = [] - for line in mail.split('\n'): - if not line: - out += [''] - continue - if line[0] == '>': - arrow = True - if arrow and not last_arrow: - out += ['> [...]'] - last_arrow = True - continue - out += [line] - output.write('\n'.join(out)) - else: - output.write(mail + '\n') - output.write('\\end{verbatim}\n') - - elif proto == 'help': - pass - else: - if addr == '': - page = get_heading(frag, data) - output.write('\\begin{verbatim}\n') - output.write(page + '\n') - output.write('\\end{verbatim}\n') - else: - print(addr) - print("Vimwiki intra-page links not yet supported") + # disabled for full PDF:s + # attach_str = f"\\section{{{title}}}\n\\label{{{uid}}}\n" + attach_str = '' + + resolver = resolvers.get(proto, resolve_default) + attach_str += resolver(addr, frag, comment) + bilagor += [attach_str] + + +output.write('\\begin{verbatim}\n') +output.write(outstr + '\n') +output.write('\\end{verbatim}\n\\appendix\n') + +output.write('\n'.join(bilagor)) output.write('\\end{document}') |