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