summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2022-05-21 00:17:24 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2022-05-21 00:17:24 +0200
commitb5013fcc52119a5f17648ae5c4c17928fceb9a87 (patch)
tree9f09f3a076dfa6a2f375907ee956f2fa18a54713
parentwork (diff)
downloadvimwiki-scripts-b5013fcc52119a5f17648ae5c4c17928fceb9a87.tar.gz
vimwiki-scripts-b5013fcc52119a5f17648ae5c4c17928fceb9a87.tar.xz
Included wiki attachements now correct level.
-rwxr-xr-xdo.py126
1 files changed, 74 insertions, 52 deletions
diff --git a/do.py b/do.py
index b143818..72720c1 100755
--- a/do.py
+++ b/do.py
@@ -14,6 +14,7 @@ from datetime import datetime
from dateutil import parser as dtparser
import argparse
import sys
+import shutil
path_base = '/'
@@ -333,6 +334,53 @@ def get_heading(name, data):
# ------------------------------------------------------------
+def renumber_wiki_headings(text):
+ # Re-level all headings
+ longest = 0
+ shortest = 1000
+ for m in re.finditer(r'(?m)^ *(=+).*=+ *$', text):
+ longest = max(longest, len(m[1]))
+ shortest = min(shortest, len(m[1]))
+ for length in range(longest, shortest + 1):
+ text = re.sub('=' * length, '=' * (length - shortest + 1), text)
+ return text
+
+def format_wikitext(mode, paragraphs):
+ if mode == 'txt':
+ for paragraph in paragraphs:
+ output.write('\n'.join(paragraph))
+ output.write('\n\n')
+ elif mode == 'tex':
+ if shutil.which('pandoc'):
+ wiki_file = '/tmp/file.vimwiki'
+ tex_file = '/tmp/file.tex'
+
+ text = '\n\n'.join('\n'.join(par) for par in paragraphs) + '\n\n'
+ full_text = renumber_wiki_headings(text)
+
+ with open(wiki_file, 'w') as f:
+ f.write(full_text)
+ cmd = subprocess.run(['pandoc', '-f', 'vimwiki', '-t', 'latex', '-o', tex_file, wiki_file])
+ with open(tex_file) as f:
+ for line in f:
+ output.write(line)
+
+ else: # no pandoc
+ if options['mode'] == 'tex':
+ pass
+ else:
+ output.write('\\begin{verbatim}\n')
+
+ for paragraph in paragraphs:
+ output.write('\n'.join(paragraph))
+ output.write('\n\n')
+
+ if options['mode'] == 'tex':
+ pass
+ else:
+ output.write('\\end{verbatim}\n')
+
+
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='parse vimwiki files',
@@ -380,31 +428,29 @@ if __name__ == '__main__':
if mode == 'tex':
- output.write(f'''
-\\PassOptionsToPackage{{unicode}}{{hyperref}}
-\\PassOptionsToPackage{{hyphens}}{{url}}
-
-\\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}}
-\\usepackage{{amsmath,amssymb}}
-\\usepackage{{lmodern}}
-\\usepackage{{iftex}}
-\\usepackage{{xcolor}}
-\\providecommand{{\\tightlist}}{{%
- \\setlength{{\\itemsep}}{{0pt}}\\setlength{{\\parskip}}{{0pt}}}}
-% \\setcounter{{secnumdepth}}{{-\\maxdimen}} % remove section numbering
+ output.write(r'''
+\PassOptionsToPackage{unicode}{hyperref}
+\PassOptionsToPackage{hyphens}{url}
+
+\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}
+\usepackage{amsmath,amssymb}
+\usepackage{lmodern}
+\usepackage{iftex}
+\usepackage{xcolor}
+\providecommand{\tightlist}{\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
''')
elif mode == 'txt':
@@ -514,6 +560,7 @@ if __name__ == '__main__':
+ # Preamble
if mode == 'tex':
output.write(f"""
\\title{{{heading}}}
@@ -523,39 +570,14 @@ if __name__ == '__main__':
\\maketitle
% \\tableofcontents
""")
- if options['mode'] == 'tex':
- pass
- else:
- # output.write('\\begin{verbatim}\n')
- pass
elif mode == 'txt':
d = options['date']
output.write(f'\n{heading.center(40)}\n{d.center(40)}\n')
- if mode == 'txt':
- for paragraph in paragraphs:
- output.write('\n'.join(paragraph))
- output.write('\n\n')
- else:
- with open('/tmp/file.vimwiki', 'w') as f:
- for paragraph in paragraphs:
- f.write('\n'.join(paragraph))
- f.write('\n\n')
- cmd = subprocess.run(['pandoc', '-f', 'vimwiki', '-t', 'latex', '-o', '/tmp/file.tex', '/tmp/file.vimwiki'])
- cmd = subprocess.run(['sed', '-i', 's/subsection/section/g', '/tmp/file.tex'])
- with open('/tmp/file.tex') as f:
- for line in f:
- print(f)
- output.write(line)
-
- # output.write(outstr + '\n')
+ format_wikitext(mode, paragraphs)
+ # Postamble
if mode == 'tex':
- if options['mode'] == 'tex':
- pass
- else:
- pass
- # output.write('\\end{verbatim}\n')
output.write('\\appendix\n')
output.write('\n'.join(bilagor))