diff --git a/.config/nvim/lua/configs/packer.lua b/.config/nvim/lua/configs/packer.lua index 771bcc1..66463e8 100644 --- a/.config/nvim/lua/configs/packer.lua +++ b/.config/nvim/lua/configs/packer.lua @@ -93,8 +93,10 @@ return require('packer').startup(function(use) vim.g.vimwiki_list = { { path = '~/Documents/cybersecurity/wiki/', + path_html = '~/Documents/cybersecurity/wiki/generated_html', syntax = 'markdown', ext = '.md', + custom_wiki2html = '~/.config/vimwiki/parse_wiki.py', }, } end, diff --git a/.config/vimwiki/parse_wiki.py b/.config/vimwiki/parse_wiki.py new file mode 100755 index 0000000..62f26ec --- /dev/null +++ b/.config/vimwiki/parse_wiki.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 + +import os +from os import path +import re +import shutil +import subprocess +import sys + + +def convert( + force, + syntax, + extension, + output_dir, + input_file, + css_file, + template_path, + template_default, + template_ext, + root_path, + custom_args, +): + if shutil.which("pandoc") is None: + print("Error: pandoc not found", file=sys.stderr) + sys.exit(1) + + if syntax != "markdown": + print("Error: Unsupported syntax", file=sys.stderr) + sys.exit(1) + + input_file_name = path.splitext(path.basename(input_file))[0] + output_file = path.join(output_dir, input_file_name) + path.extsep + "html" + + with open(input_file, "r", encoding="utf8") as f: + lines = f.read() + + # Look for title in metadata + match = re.search( + "^(?:---|\.\.\.)$\n.*title: ([^\n]+)$\n.*^(?:---|\.\.\.)$", + lines, + re.MULTILINE | re.DOTALL, + ) + title = match.group(1) if match else input_file_name.title() + + template = path.join(template_path, template_default + path.extsep + template_ext) + command = [ + "pandoc", + "--filter={}/.config/vimwiki/linkParser.hs".format(os.environ['HOME']), + "--section-divs", + "--template={}".format(template) if path.isfile(template) else "", + "-s", + "--highlight-style=pygments", + "--metadata", + "pagetitle={}".format(title), + custom_args if custom_args != "-" else "", + "-f", + "markdown", + "-t", + "html", + "-o", + output_file, + "-", + ] + + # Prune empty elements from command list + command = list(filter(None, command)) + + # Run command + subprocess.run(command, check=True, encoding="utf8", input=lines) + + +if __name__ == "__main__": + convert(*sys.argv[1:])