blob: ecc935a836fc402e8990d1bf54aaadc1b1aebc9a [file] [log] [blame]
Wu, Zhenyu7005b7e2024-04-08 20:53:19 +02001" Vim compiler file
2" Compiler: Pandoc
3" Maintainer: Konfekt
Konfektd55e6982024-08-20 20:18:28 +02004" Last Change: 2024 Aug 20
Wu, Zhenyu7005b7e2024-04-08 20:53:19 +02005"
6" Expects output file extension, say `:make html` or `:make pdf`.
7" Passes additional arguments to pandoc, say `:make html --self-contained`.
8
9if exists("current_compiler")
10 finish
11endif
12
Wu, Zhenyu7005b7e2024-04-08 20:53:19 +020013let s:keepcpo = &cpo
14set cpo&vim
15
16let current_compiler = 'pandoc'
17
18" As of 2024-04-08 pandoc supports the following text input formats with
19" an ftplugin on Github:
20let 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
27silent! function s:PandocFiletype(filetype) abort
28 let ft = a:filetype
Konfektd55e6982024-08-20 20:18:28 +020029
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, Zhenyu7005b7e2024-04-08 20:53:19 +020037 else
Konfektd55e6982024-08-20 20:18:28 +020038 echomsg 'Unsupported filetype: '..ft..', falling back to Markdown as input format!'
Wu, Zhenyu7005b7e2024-04-08 20:53:19 +020039 return 'markdown'
40 endif
41endfunction
Wu, Zhenyu7005b7e2024-04-08 20:53:19 +020042
Konfektd55e6982024-08-20 20:18:28 +020043let b:pandoc_compiler_from = get(b:, 'pandoc_compiler_from', s:PandocFiletype(&filetype))
44let b:pandoc_compiler_lang = get(b:, 'pandoc_compiler_lang', &spell ? matchstr(&spelllang, '^\a\a') : '')
45
46execute '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', ' ')
Konfekt5f5f2832024-08-20 21:57:54 +020055CompilerSet errorformat=\"%f\",\ line\ %l:\ %m
Wu, Zhenyu7005b7e2024-04-08 20:53:19 +020056
57let &cpo = s:keepcpo
58unlet s:keepcpo