Added custom parse for wiki to use vimwiki to html commands

This commit is contained in:
2024-02-24 13:55:00 +01:00
parent ecb66c5fb4
commit 9c8664d68f
2 changed files with 76 additions and 0 deletions

View File

@@ -93,8 +93,10 @@ return require('packer').startup(function(use)
vim.g.vimwiki_list = { vim.g.vimwiki_list = {
{ {
path = '~/Documents/cybersecurity/wiki/', path = '~/Documents/cybersecurity/wiki/',
path_html = '~/Documents/cybersecurity/wiki/generated_html',
syntax = 'markdown', syntax = 'markdown',
ext = '.md', ext = '.md',
custom_wiki2html = '~/.config/vimwiki/parse_wiki.py',
}, },
} }
end, end,

74
.config/vimwiki/parse_wiki.py Executable file
View File

@@ -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:])