Skip to content

Instantly share code, notes, and snippets.

@maltehol
Forked from enpassant/vimwiki2html.md
Last active February 22, 2019 21:52
Show Gist options
  • Save maltehol/5babfa4384a77ed27b634a110190bf85 to your computer and use it in GitHub Desktop.
Save maltehol/5babfa4384a77ed27b634a110190bf85 to your computer and use it in GitHub Desktop.
Convert VimWiki to HTML (markdown, mediawiki)

With this wiki2html.sh bash script and pandoc program, you can convert markdown to html.

Usage: In the vim list section of the .vimrcfile, include options:

let g:vimwiki_list = [{'path': ‘your_wiki_place',
  \ 'path_html': ‘wiki_html_location’,
  \ 'syntax': 'markdown',
  \ 'ext': '.md',
  \ 'custom_wiki2html': ‘link to the custom converter, i.e. wiki2html.sh'}]

If you want to use this converter for temporary wikis then add these to .vimrc:

  autocmd FileType vimwiki call SetMarkdownOptions()

  function! SetMarkdownOptions()
    call VimwikiSet('syntax', 'markdown')
    call VimwikiSet('custom_wiki2html', 'wiki2html.sh')
  endfunction

:VimwikiAll2HTML and <leader>wh work well.

#!/bin/bash
FORCE="$1"
SYNTAX="$2"
EXTENSION="$3"
OUTPUTDIR="$4"
INPUT="$5"
CSSFILE="$6"
FILE=$(basename "$INPUT")
FILENAME=$(basename "$INPUT" .$EXTENSION)
FILEPATH=${INPUT%$FILE}
OUTDIR=${OUTPUTDIR%$FILEPATH*}
OUTPUT="$OUTDIR"/$FILENAME
CSSFILENAME=$(basename "$6")
HAS_MATH=$(grep -o "\$.\+\$" "$INPUT")
if [ ! -z "$HAS_MATH" ]; then
MATH="--mathjax=./MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
else
MATH=""
fi
# >&2 echo "MATH: $MATH"
sed -r 's/(\[.+\])\(([^#][^)#]+)([^)]*)\)/\1(\2.html\3)/g' <"$INPUT" | pandoc $MATH -s -f $SYNTAX -t html -c $CSSFILENAME | sed -r 's/<li>(.*)\[ \]/<li class="todo done0">\1/g; s/<li>(.*)\[\.\]/<li class="todo done1">\1/g; s/<li>(.*)\[o\]/<li class="todo done2">\1/g; s/<li>(.*)\[O\]/<li class="todo done3">\1/g; s/<li>(.*)\[X\]/<li class="todo done4">\1/g' >"$OUTPUT.html"
@maltehol
Copy link
Author

Added local link and partial todo support.

Also recognizes math, if inline math is used and uses a local copy of MathJax

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment