Wu, Zhenyu | 7005b7e | 2024-04-08 20:53:19 +0200 | [diff] [blame] | 1 | " Vim compiler file |
| 2 | " Compiler: Pandoc |
| 3 | " Maintainer: Konfekt |
| 4 | " |
| 5 | " Expects output file extension, say `:make html` or `:make pdf`. |
| 6 | " Passes additional arguments to pandoc, say `:make html --self-contained`. |
| 7 | |
| 8 | if exists("current_compiler") |
| 9 | finish |
| 10 | endif |
| 11 | |
| 12 | if exists(":CompilerSet") != 2 " older Vim always used :setlocal |
| 13 | command -nargs=* CompilerSet setlocal <args> |
| 14 | endif |
| 15 | |
| 16 | let s:keepcpo = &cpo |
| 17 | set cpo&vim |
| 18 | |
| 19 | let current_compiler = 'pandoc' |
| 20 | |
| 21 | " As of 2024-04-08 pandoc supports the following text input formats with |
| 22 | " an ftplugin on Github: |
| 23 | let s:supported_filetypes = |
| 24 | \ [ 'bibtex', 'markdown', 'creole', 'json', 'csv', 'tsv', 'docbook', |
| 25 | \ 'xml', 'fb2', 'html', 'jira', 'tex', 'mediawiki', 'nroff', 'org', |
| 26 | \ 'rtf', 'rst', 't2t', 'textile', 'twiki', 'typst', 'vimwiki' ] |
| 27 | " .. and out of those the following are included in Vim's runtime: |
| 28 | " 'xml', 'tex', 'html', 'rst', 'json', 'nroff', 'markdown' |
| 29 | |
| 30 | silent! function s:PandocFiletype(filetype) abort |
| 31 | let ft = a:filetype |
| 32 | if ft ==# 'pandoc' |
| 33 | return 'markdown' |
| 34 | elseif ft ==# 'tex' |
| 35 | return 'latex' |
| 36 | elseif ft ==# 'xml' |
| 37 | " Pandoc does not support XML as a generic input format, but it does support |
| 38 | " EndNote XML and Jats XML out of which the latter seems more universal. |
| 39 | return 'jats' |
| 40 | elseif ft ==# 'text' || empty(ft) |
| 41 | return 'markdown' |
| 42 | elseif index(s:supported_filetypes, &ft) >= 0 |
| 43 | return ft |
| 44 | else |
| 45 | echomsg 'Unsupported filetype: ' . a:filetype ', falling back to Markdown as input format!' |
| 46 | return 'markdown' |
| 47 | endif |
| 48 | endfunction |
| 49 | execute 'CompilerSet makeprg=pandoc\ --standalone' . |
| 50 | \ '\ --metadata\ title=%:t:r:S' . |
| 51 | \ '\ --metadata\ lang=' . matchstr(&spelllang, '^\a\a') . |
| 52 | \ '\ --from=' . s:PandocFiletype(&filetype) . |
| 53 | \ '\ --output\ %:r:S.$*\ %:S' |
| 54 | |
| 55 | CompilerSet errorformat="%f",\ line\ %l:\ %m |
| 56 | |
| 57 | let &cpo = s:keepcpo |
| 58 | unlet s:keepcpo |