summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2021-08-20 17:27:57 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2021-08-20 17:27:57 +0200
commit6774296ba0d4e35db5309856bec16c0331fd2055 (patch)
tree5db3b596f0dd87190f818a373dcbf25eb91f4cf3
parentAdd options["mode"] == "tex". (diff)
downloadvimwiki-scripts-6774296ba0d4e35db5309856bec16c0331fd2055.tar.gz
vimwiki-scripts-6774296ba0d4e35db5309856bec16c0331fd2055.tar.xz
Move stuff into main, makeing code library loadable.
-rwxr-xr-xdo.py332
1 files changed, 170 insertions, 162 deletions
diff --git a/do.py b/do.py
index 897f028..89d56fa 100755
--- a/do.py
+++ b/do.py
@@ -15,21 +15,9 @@ import argparse
path_base = '/'
-parser = argparse.ArgumentParser(
- description='parse vimwiki files',
- formatter_class=argparse.ArgumentDefaultsHelpFormatter)
-parser.add_argument('--mode', choices=["tex", "txt"],
- default='txt', help="Format of output")
-parser.add_argument('infile', help="Wiki-file to use for input")
-parser.add_argument('heading', help="Which heading to output")
-args = parser.parse_args()
-
-
-mode = args.mode
-
# ------------------------------------------------------------
-def includepdf(addr, full_file):
+def includepdf(mode, addr, full_file):
if mode == 'tex':
arg = 'frame'
arg += ',pages={-}'
@@ -70,7 +58,7 @@ def texify_filename(str):
return out
-def resolve_mail(addr, frag, comment):
+def resolve_mail(mode, addr, frag, comment):
outstr = ''
@@ -109,7 +97,7 @@ def resolve_mail(addr, frag, comment):
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)
+ outstr += includepdf(mode, safe_filename, '/tmp/' + safe_filename)
else:
if mode == 'tex':
outstr += f'\\subsection{{{texify_filename(part.get_filename())}}}\n'
@@ -149,12 +137,12 @@ def resolve_mail(addr, frag, comment):
return outstr
-def resolve_help(addr, frag, comment):
+def resolve_help(mode, addr, frag, comment):
pass
return ''
-def resolve_file(addr, frag, comment):
+def resolve_file(mode, addr, frag, comment):
extension = addr.split('.')[-1]
full_file = path.join(path_base, addr)
@@ -164,7 +152,7 @@ def resolve_file(addr, frag, comment):
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)
+ s += includepdf(mode, addr, full_file)
return s
# TODO
# elif extension == 'tex':
@@ -174,7 +162,7 @@ def resolve_file(addr, frag, comment):
return f.read() + '\n'
-def resolve_default(addr, frag, comment):
+def resolve_default(mode, addr, frag, comment):
outstr = ''
if addr == '':
page = get_heading(frag, data)
@@ -189,7 +177,9 @@ def resolve_default(addr, frag, comment):
outstr = ''
return outstr
-# Should all take (url without protocol, fragment, comment)
+# Should all take (mode string, url without protocol, fragment, comment)
+# and return the complete text of the attachement, formatted according
+# to mode
resolvers = {
'mail': resolve_mail,
'file': resolve_file,
@@ -216,177 +206,195 @@ def get_heading(name, data):
# ------------------------------------------------------------
-infile = args.infile
-heading = args.heading
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser(
+ description='parse vimwiki files',
+ formatter_class=argparse.ArgumentDefaultsHelpFormatter)
+ parser.add_argument('--mode', choices=["tex", "txt"],
+ default='txt', help="Format of output")
+ parser.add_argument('infile', help="Wiki-file to use for input")
+ parser.add_argument('heading', help="Which heading to output")
+ args = parser.parse_args()
-file = open(infile, 'r')
-data = file.read()
+ mode = args.mode
-path_base = path.dirname(infile)
+ infile = args.infile
+ heading = args.heading
-output = open(f'doc.{mode}', 'w')
+ file = open(infile, 'r')
+ data = file.read()
-name = pwd.getpwuid(os.getuid()).pw_gecos
+ path_base = path.dirname(infile)
-if mode == 'tex':
- output.write(f'''
- \\documentclass[a4paper]{{article}}
+ output = open(f'doc.{mode}', 'w')
- \\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}}
+ name = pwd.getpwuid(os.getuid()).pw_gecos
- ''')
-elif mode == 'txt':
- sectioncounter = 0
- subsectioncounter = 0
+ 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}}
+
+ ''')
-# '\\verbatiminput
+ elif mode == 'txt':
+ sectioncounter = 0
+ subsectioncounter = 0
-page1 = get_heading(heading, data)
-# bilagor = get_heading('Bilagor', page1).strip().split('\n')
-r = r"\[\[([^|#\]]*)(?:#([^|\]]*))?(?:[|]([^\]]*))?\]\]"
-# bilagor = re.findall(r, page1)
-tag_iter = re.finditer(r, page1)
+ # '\\verbatiminput
-# bilagor = [m[0] for m in re.findall(r, data)]
+ page1 = get_heading(heading, data)
+ # bilagor = get_heading('Bilagor', page1).strip().split('\n')
-bilagor = []
+ # Find all links
+ r = r"\[\[([^|#\]]*)(?:#([^|\]]*))?(?:[|]([^\]]*))?\]\]"
+ # bilagor = re.findall(r, page1)
+ tag_iter = re.finditer(r, page1)
-outstr = ""
-pos = 0
+ # bilagor = [m[0] for m in re.findall(r, data)]
-bilaga_nr = 1
-for match in tag_iter:
- url = match.group(1) or ''
- frag = match.group(2) or ''
- comment = match.group(3) or ''
+ bilagor = []
- outstr += page1[pos:match.start()]
- pos = match.end()
+ outstr = ""
+ pos = 0
- uid = hashlib.md5((url + frag).encode('UTF-8')).hexdigest()
+ # Resolve all links
+ bilaga_nr = 1
+ for match in tag_iter:
+ url = match.group(1) or ''
+ frag = match.group(2) or ''
+ comment = match.group(3) or ''
- title = comment or url or frag
+ outstr += page1[pos:match.start()]
+ pos = match.end()
- # 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 = url.split(":", 1)
- except ValueError:
- proto = ''
- addr = url
- print(proto, addr)
-
- # disabled for full PDF:s
- attach_str = ''
- 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'
+ uid = hashlib.md5((url + frag).encode('UTF-8')).hexdigest()
- resolver = resolvers.get(proto, resolve_default)
- attach_str += resolver(addr, frag, comment)
- bilagor += [attach_str]
+ title = comment or url or frag
+ # outstr += title + f"(bilaga~\\ref{{{uid}}})"
+ # TODO
+ outstr += title + f' (bilaga {chr(bilaga_nr + 64)})'
+ # outstr += title + " (se bilaga)"
+ bilaga_nr += 1
-outstr += page1[pos:]
+ try:
+ proto, addr = url.split(":", 1)
+ except ValueError:
+ proto = ''
+ addr = url
+ print(proto, addr)
-options = {
- 'date': '\\today' if mode == 'tex' else f'{datetime.now():%Y-%m-%d}',
- 'mode': 'vimwiki',
- }
+ # disabled for full PDF:s
+ attach_str = ''
+ 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]
+
+
+ outstr += page1[pos:]
+
+ options = {
+ 'date': '\\today' if mode == 'tex' else f'{datetime.now():%Y-%m-%d}',
+ 'mode': 'vimwiki',
+ }
+
+ # split the text into paragraph blocks
+ lines = outstr.split('\n')
+ paragraphs = []
+ paragraph = []
+ for line in lines:
+ if line == '':
+ if paragraph == []:
+ continue
+ else:
+ paragraphs.append(paragraph)
+ paragraph = []
+ else:
+ paragraph.append(line)
+
+ # check the first paragraph for a deffinition list, and parse that
+ # into variables
+ for i in range(len(paragraphs[0])):
+ line = paragraphs[0][i]
+ if m := re.match('^(\w+)\s*::\s*(.*)', line):
+ if m[1] == 'date':
+ dt = dtparser.parse(m[2])
+ if mode == 'tex':
+ options['date'] = f'{dt:%Y--%m--%d}'
+ else:
+ options['date'] = f'{dt:%Y-%m-%d}'
-# split the text into paragraph blocks
-lines = outstr.split('\n')
-paragraphs = []
-paragraph = []
-for line in lines:
- if line == '':
- if paragraph == []:
- continue
+ else:
+ options[m[1]] = m[2]
else:
- paragraphs.append(paragraph)
- paragraph = []
+ # remove handled lines, keep remaining lines in paragraphs list
+ paragraphs[0] = paragraphs[0][i:]
+ break
else:
- paragraph.append(line)
-
-# check the first paragraph for a deffinition list, and parse that
-# into variables
-for i in range(len(paragraphs[0])):
- line = paragraphs[0][i]
- if m := re.match('^(\w+)\s*::\s*(.*)', line):
- if m[1] == 'date':
- dt = dtparser.parse(m[2])
- if mode == 'tex':
- options['date'] = f'{dt:%Y--%m--%d}'
- else:
- options['date'] = f'{dt:%Y-%m-%d}'
+ # remove first paragraph from paragraphs list
+ paragraphs = paragraphs[1:]
+
+
+
+ if mode == 'tex':
+ output.write(f"""
+ \\title{{{heading}}}
+ \\date{{{options['date']}}}
+ \\author{{{name}}}
+ \\begin{{document}}
+ \\maketitle
+ % \\tableofcontents
+ """)
+ if options['mode'] == 'tex':
+ pass
else:
- options[m[1]] = m[2]
- else:
- # remove handled lines, keep remaining lines in paragraphs list
- paragraphs[0] = paragraphs[0][i:]
- break
-else:
- # remove first paragraph from paragraphs list
- paragraphs = paragraphs[1:]
-
-
-if mode == 'tex':
- output.write(f"""
- \\title{{{heading}}}
- \\date{{{options['date']}}}
- \\author{{{name}}}
- \\begin{{document}}
- \\maketitle
- % \\tableofcontents
- """)
- if options['mode'] == 'tex':
- pass
- else:
- output.write('\\begin{verbatim}\n')
-elif mode == 'txt':
- d = options['date']
- output.write(f'\n{heading.center(40)}\n{d.center(40)}\n')
-
-for paragraph in paragraphs:
- output.write('\n'.join(paragraph))
- output.write('\n\n')
-# output.write(outstr + '\n')
-
-if mode == 'tex':
- if options['mode'] == 'tex':
- pass
- else:
- output.write('\\end{verbatim}\n')
- output.write('\\appendix\n')
+ output.write('\\begin{verbatim}\n')
+ elif mode == 'txt':
+ d = options['date']
+ output.write(f'\n{heading.center(40)}\n{d.center(40)}\n')
+
+ for paragraph in paragraphs:
+ output.write('\n'.join(paragraph))
+ output.write('\n\n')
+ # output.write(outstr + '\n')
-output.write('\n'.join(bilagor))
+ if mode == 'tex':
+ if options['mode'] == 'tex':
+ pass
+ else:
+ output.write('\\end{verbatim}\n')
+ output.write('\\appendix\n')
-if mode == 'tex':
- output.write('\\end{document}')
+ output.write('\n'.join(bilagor))
+
+ if mode == 'tex':
+ output.write('\\end{document}')