blob: 5d90a518c98d364c13f4fcd436ae10a6aa3bba2b [file] [log] [blame]
Wu, Zhenyu7005b7e2024-04-08 20:53:19 +02001" Vim compiler file
2" Compiler: Pandoc
3" Maintainer: Konfekt
Konfekt4927dae2024-11-19 22:41:14 +01004" Last Change: 2024 Nov 19
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`.
Christian Brabandtd30ffdc2024-09-09 20:26:28 +02008" Adjust command-line flags by buffer-local/global variable
9" b/g:pandoc_compiler_args which defaults to empty.
Wu, Zhenyu7005b7e2024-04-08 20:53:19 +020010
11if exists("current_compiler")
12 finish
13endif
14
Wu, Zhenyu7005b7e2024-04-08 20:53:19 +020015let s:keepcpo = &cpo
16set cpo&vim
17
18let current_compiler = 'pandoc'
19
20" As of 2024-04-08 pandoc supports the following text input formats with
21" an ftplugin on Github:
22let s:supported_filetypes =
23 \ [ 'bibtex', 'markdown', 'creole', 'json', 'csv', 'tsv', 'docbook',
24 \ 'xml', 'fb2', 'html', 'jira', 'tex', 'mediawiki', 'nroff', 'org',
25 \ 'rtf', 'rst', 't2t', 'textile', 'twiki', 'typst', 'vimwiki' ]
26" .. and out of those the following are included in Vim's runtime:
27" 'xml', 'tex', 'html', 'rst', 'json', 'nroff', 'markdown'
28
29silent! function s:PandocFiletype(filetype) abort
30 let ft = a:filetype
Konfektd55e6982024-08-20 20:18:28 +020031
32 if ft ==# 'pandoc' | return 'markdown'
33 elseif ft ==# 'tex' | return 'latex'
34 " Pandoc does not support XML as a generic input format, but it does support
35 " EndNote XML and Jats XML out of which the latter seems more universal.
36 elseif ft ==# 'xml' | return 'jats'
37 elseif ft ==# 'text' || empty(ft) | return 'markdown'
38 elseif index(s:supported_filetypes, &ft) >= 0 | return ft
Wu, Zhenyu7005b7e2024-04-08 20:53:19 +020039 else
Konfektd55e6982024-08-20 20:18:28 +020040 echomsg 'Unsupported filetype: '..ft..', falling back to Markdown as input format!'
Wu, Zhenyu7005b7e2024-04-08 20:53:19 +020041 return 'markdown'
42 endif
43endfunction
Wu, Zhenyu7005b7e2024-04-08 20:53:19 +020044
Christian Brabandtd30ffdc2024-09-09 20:26:28 +020045silent! function s:PandocLang()
46 let lang = get(b:, 'pandoc_compiler_lang',
47 \ &spell ? matchstr(&spelllang, '^\a\a') : '')
48 if lang ==# 'en' | let lang = '' | endif
49 return empty(lang) ? '' : '--metadata lang='..lang
50endfunction
Konfektd55e6982024-08-20 20:18:28 +020051
52execute 'CompilerSet makeprg=pandoc'..escape(
Christian Brabandtd30ffdc2024-09-09 20:26:28 +020053 \ ' --standalone'..
54 \ (s:PandocFiletype(&filetype) ==# 'markdown' && (getline(1) =~# '^%\s\+\S\+' || (search('^title:\s+\S+', 'cnw') > 0)) ?
55 \ '' : ' --metadata title=%:t:r:S')..
56 \ ' '..s:PandocLang()..
57 \ ' --from='..s:PandocFiletype(&filetype)..
58 \ ' '..get(b:, 'pandoc_compiler_args', get(g:, 'pandoc_compiler_args', ''))..
Konfekt4927dae2024-11-19 22:41:14 +010059 \ ' --output %:r:S.$* -- %:S', ' \|"')
Konfekt5f5f2832024-08-20 21:57:54 +020060CompilerSet errorformat=\"%f\",\ line\ %l:\ %m
Wu, Zhenyu7005b7e2024-04-08 20:53:19 +020061
62let &cpo = s:keepcpo
63unlet s:keepcpo