summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2022-05-19 16:01:59 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2022-05-19 16:01:59 +0200
commit43c765e9a525eea7c3e30aee80bbf6bbe8eab7d2 (patch)
tree388b486ff2269e6abf73bb539d809008f2e14b60
parentInterwiki links? (diff)
downloadvimwiki-scripts-43c765e9a525eea7c3e30aee80bbf6bbe8eab7d2.tar.gz
vimwiki-scripts-43c765e9a525eea7c3e30aee80bbf6bbe8eab7d2.tar.xz
Bettre inclusion of attachements, +local url type.
-rwxr-xr-xdo.py69
1 files changed, 42 insertions, 27 deletions
diff --git a/do.py b/do.py
index 0b93d7d..7ee4986 100755
--- a/do.py
+++ b/do.py
@@ -23,13 +23,22 @@ def includepdf(mode, 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'
+ before = f'\\rhead{{Bilaga \\Alph{{section}}.\\arabic{{subsection}} {texify_filename(addr)} }}\n'
+ return before + f'\\phantomsection\\stepcounter{{subsection}}\n\\includepdf[{arg}]{{{full_file}}}\n'
elif mode == 'txt':
return f"Se pdf {addr}\n"
+def includeimg(mode, addr, full_file):
+ if mode == 'tex':
+ pagecommand = '\\thispagestyle{fancy}'
+ pagecommand += f'\\lhead{{Bilaga \\Alph{{section}}.\\arabic{{subsection}} {texify_filename(addr)} }}'
+ arg = 'width=\\textwidth'
+ return f'\\phantomsection\\stepcounter{{subsection}}\n\\includegraphics[{arg}]{{{full_file}}}\n'
+ elif mode == 'txt':
+ return f"Se bild {addr}\n"
+
def shorten_mail(str):
last_arrow = False
@@ -112,7 +121,7 @@ def resolve_mail(mode, source_file_name, addr, frag, comment):
# mail += msg.get_content_type() + '\n'
- body = msg.get_body(preferencelist=('related', 'plain', 'html'))
+ body = msg.get_body(preferencelist=('plain', 'html'))
for field in ['Date', 'From', 'To' 'Cc', 'Subject']:
h = msg.get(field)
if h: outstr += f'{field}: {h}\n'
@@ -126,7 +135,7 @@ def resolve_mail(mode, source_file_name, addr, frag, comment):
# TODO merge this logic with the resolve file logic below
for part in msg.iter_attachments():
- if part.get_content_type() == 'application/pdf':
+ if part.get_content_type() == 'application/pdf' or part.get_content_type().split('/')[0] == 'image':
safe_filename = part.get_filename() \
.replace('/', '_') \
.replace('å', 'a') \
@@ -135,7 +144,10 @@ def resolve_mail(mode, source_file_name, addr, frag, comment):
with open('/tmp/' + safe_filename, 'wb') as f:
f.write(part.get_content())
# outstr += part.get_filename() + '\n'
- outstr += includepdf(mode, safe_filename, '/tmp/' + safe_filename)
+ if part.get_content_type() == 'application/pdf':
+ outstr += includepdf(mode, safe_filename, '/tmp/' + safe_filename)
+ else:
+ outstr += includeimg(mode, safe_filename, '/tmp/' + safe_filename)
else:
if mode == 'tex':
outstr += f'\\subsection{{{texify_filename(part.get_filename())}}}\n'
@@ -192,6 +204,8 @@ def resolve_file(mode, source_file_name, addr, frag, comment):
# s = f'\\includepdf[pages={{1}},width=\\textwidth]{{{full_file}}}\n'
s += includepdf(mode, addr, full_file)
return s
+ elif extension.lower() in ['jpg', 'jpeg', 'png', 'gif']:
+ return includeimg(mode, addr, full_file)
# TODO
# elif extension == 'tex':
# return f'\\subfile{{{full_file}}}\n'
@@ -246,6 +260,7 @@ def resolve_default(mode, source_file_name, addr, frag, comment):
resolvers = {
'mail': resolve_mail,
'file': resolve_file,
+ 'local': resolve_file,
'help': resolve_help,
}
@@ -301,20 +316,20 @@ if __name__ == '__main__':
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}}
- \\usepackage{{soul}}
-
+\\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}}
+\\usepackage{{soul}}
+\\usepackage{{graphics}}
''')
elif mode == 'txt':
@@ -368,14 +383,14 @@ if __name__ == '__main__':
# disabled for full PDF:s
attach_str = ''
if mode == 'tex':
- if proto != 'file':
+ if proto not in ['file', 'local']:
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':
+ if proto not in ['file', 'local']:
st = f"{chr(sectioncounter + 64)}. {title}"
attach_str += f'\n{st}\n{"="*len(st)}\n'
@@ -412,12 +427,12 @@ if __name__ == '__main__':
if mode == 'tex':
output.write(f"""
- \\title{{{heading}}}
- \\date{{{options['date']}}}
- \\author{{{name}}}
- \\begin{{document}}
- \\maketitle
- % \\tableofcontents
+\\title{{{heading}}}
+\\date{{{options['date']}}}
+\\author{{{name}}}
+\\begin{{document}}
+\\maketitle
+% \\tableofcontents
""")
if options['mode'] == 'tex':
pass