summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2020-10-05 14:14:29 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2020-10-05 14:14:29 +0200
commit9cff43d8849c6c0abdf5cc06b95f07592ba94163 (patch)
tree23eb2015c31c78a7ef3a746e89d080d84949e664
parentFix vimwiki links. (diff)
downloadvimwiki-scripts-9cff43d8849c6c0abdf5cc06b95f07592ba94163.tar.gz
vimwiki-scripts-9cff43d8849c6c0abdf5cc06b95f07592ba94163.tar.xz
Buncha work.
-rwxr-xr-xdo.py131
1 files changed, 111 insertions, 20 deletions
diff --git a/do.py b/do.py
index 15f44b6..217d231 100755
--- a/do.py
+++ b/do.py
@@ -8,37 +8,116 @@ import subprocess
from subprocess import PIPE
from base64 import b64encode
import os.path as path
+import email
+from email.policy import default
path_base = '/'
# ------------------------------------------------------------
+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'
+
+
+def shorten_mail(str):
+ last_arrow = False
+ out = []
+ for line in str.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)
+
+
+def texify_filename(str):
+ out = ''
+ for c in str:
+ if c == '_':
+ out += '\\_'
+ else:
+ out += c
+
+ return out
+
+
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')
+ # mail = subprocess.run(f"mu view {mailfile}", shell=True, stdout=PIPE).stdout.decode('UTF-8')
+
+ with open(mailfile, 'rb') as f:
+ msg = email.message_from_binary_file(f, policy=default)
+
outstr += '\\begin{verbatim}\n'
+
+ # mail += msg.get_content_type() + '\n'
+
+ body = msg.get_body(preferencelist=('related', 'plain', 'html'))
+ for field in ['Date', 'From', 'To' 'Cc', 'Subject']:
+ if h := msg.get(field):
+ outstr += f'{field}: {h}\n'
+ outstr += '\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)
+ outstr += shorten_mail(body.get_content())
else:
- outstr += mail + '\n'
+ outstr += body.get_content()
outstr += '\\end{verbatim}\n'
+
+ # TODO merge this logic with the resolve file logic below
+ for part in msg.iter_attachments():
+ if part.get_content_type() == 'application/pdf':
+ safe_filename = part.get_filename() \
+ .replace('/', '_') \
+ .replace('å', 'a') \
+ .replace('ä', 'a') \
+ .replace('ö', 'o')
+ with open('/tmp/' + safe_filename, 'wb') as f:
+ f.write(part.get_content())
+ # outstr += part.get_filename() + '\n'
+ outstr += includepdf(safe_filename, '/tmp/' + safe_filename)
+ else:
+ outstr += f'\\subsection{{{texify_filename(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}'
+ outstr += part.get_content() + '\n'
+ outstr += '\\end{verbatim}'
+ # mail += f'{part.get_content_type()} - {part.get_filename()}\n'
+
+ # for part in msg.walk():
+ # mail += f'{part.get_content_type()} - {part.get_filename()}\n'
+
+ # for part in msg.walk():
+ #for part in msg.walk():
+ # if part.get_content_maintype() == 'multipart':
+ # continue
+ # mail += f'== {part.get_content_type()} ==\n\n'
+ # if part.get_content_type() == 'application/pdf':
+ # mail += part.get_filename() + '\n\n'
+ # else:
+ # mail += part.as_string(policy=default)
+
+ # mail = msg.as_string()
+
return outstr
@@ -53,7 +132,12 @@ def resolve_file(addr, frag, comment):
full_file = path.join(path_base, addr)
if extension == 'pdf':
- return f'\\includepdf[pages=-,pagecommand={{}},width=\\textwidth]{{{full_file}}}\n'
+ s = '\\stepcounter{section}\n'
+ s += '\\phantomsection\n'
+ # s += f'\\addcontentsline{{toc}}{{chapter}}{{{texify_filename(addr)}}}\n'
+ # s = f'\\includepdf[pages={{1}},width=\\textwidth]{{{full_file}}}\n'
+ s += includepdf(addr, full_file)
+ return s
# TODO
# elif extension == 'tex':
# return f'\\subfile{{{full_file}}}\n'
@@ -123,12 +207,16 @@ output.write(f'''
\\usepackage{{fullpage}}
\\usepackage{{pdfpages}}
\\usepackage{{subfiles}}
+\\usepackage{{parskip}}
+\\usepackage{{fancyhdr}}
+\\usepackage{{hyperref}}
\\title{{{heading}}}
\\date\\today
\\author{{{name}}}
\\begin{{document}}
\\maketitle
+% \\tableofcontents
''')
# '\\verbatiminput
@@ -161,8 +249,8 @@ for match in tag_iter:
# outstr += title + f"(bilaga~\\ref{{{uid}}})"
# TODO
- # outstr += title + f' (bilaga {chr(bilaga_nr + 64)})'
- outstr += title + " (se bilaga)"
+ outstr += title + f' (bilaga {chr(bilaga_nr + 64)})'
+ # outstr += title + " (se bilaga)"
bilaga_nr += 1
try:
@@ -174,7 +262,10 @@ for match in tag_iter:
# disabled for full PDF:s
attach_str = ''
- attach_str += f"\\section{{{title}}}\n\\label{{{uid}}}\n"
+ if proto != 'file':
+ attach_str += f"\\section{{{title}}}\n\\label{{{uid}}}\n"
+ else:
+ attach_str += f"\\phantomsection\n\\label{{{uid}}}\n"
resolver = resolvers.get(proto, resolve_default)
attach_str += resolver(addr, frag, comment)