blob: 40d2d026339c17d49bb9043fae93d48eb4408bec [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
Alexander Abrosimov1aa68df2025-05-15 20:06:29 +02005" 2025 May 15 Update the title regex for CompilerSet #17321
Wu, Zhenyu7005b7e2024-04-08 20:53:19 +02006"
7" Expects output file extension, say `:make html` or `:make pdf`.
8" Passes additional arguments to pandoc, say `:make html --self-contained`.
Christian Brabandtd30ffdc2024-09-09 20:26:28 +02009" Adjust command-line flags by buffer-local/global variable
10" b/g:pandoc_compiler_args which defaults to empty.
Wu, Zhenyu7005b7e2024-04-08 20:53:19 +020011
12if exists("current_compiler")
13 finish
14endif
15
Wu, Zhenyu7005b7e2024-04-08 20:53:19 +020016let s:keepcpo = &cpo
17set cpo&vim
18
19let current_compiler = 'pandoc'
20
21" As of 2024-04-08 pandoc supports the following text input formats with
22" an ftplugin on Github:
23let 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
30silent! function s:PandocFiletype(filetype) abort
31 let ft = a:filetype
Konfektd55e6982024-08-20 20:18:28 +020032
33 if ft ==# 'pandoc' | return 'markdown'
34 elseif ft ==# 'tex' | return 'latex'
35 " Pandoc does not support XML as a generic input format, but it does support
36 " EndNote XML and Jats XML out of which the latter seems more universal.
37 elseif ft ==# 'xml' | return 'jats'
38 elseif ft ==# 'text' || empty(ft) | return 'markdown'
39 elseif index(s:supported_filetypes, &ft) >= 0 | return ft
Wu, Zhenyu7005b7e2024-04-08 20:53:19 +020040 else
Konfektd55e6982024-08-20 20:18:28 +020041 echomsg 'Unsupported filetype: '..ft..', falling back to Markdown as input format!'
Wu, Zhenyu7005b7e2024-04-08 20:53:19 +020042 return 'markdown'
43 endif
44endfunction
Wu, Zhenyu7005b7e2024-04-08 20:53:19 +020045
Christian Brabandtd30ffdc2024-09-09 20:26:28 +020046silent! function s:PandocLang()
47 let lang = get(b:, 'pandoc_compiler_lang',
48 \ &spell ? matchstr(&spelllang, '^\a\a') : '')
49 if lang ==# 'en' | let lang = '' | endif
50 return empty(lang) ? '' : '--metadata lang='..lang
51endfunction
Konfektd55e6982024-08-20 20:18:28 +020052
53execute 'CompilerSet makeprg=pandoc'..escape(
Christian Brabandtd30ffdc2024-09-09 20:26:28 +020054 \ ' --standalone'..
Alexander Abrosimov1aa68df2025-05-15 20:06:29 +020055 \ (s:PandocFiletype(&filetype) ==# 'markdown' && (getline(1) =~# '^%\s\+\S\+' || (search('^title:\s\+\S\+', 'cnw') > 0)) ?
Christian Brabandtd30ffdc2024-09-09 20:26:28 +020056 \ '' : ' --metadata title=%:t:r:S')..
57 \ ' '..s:PandocLang()..
58 \ ' --from='..s:PandocFiletype(&filetype)..
59 \ ' '..get(b:, 'pandoc_compiler_args', get(g:, 'pandoc_compiler_args', ''))..
Konfekt4927dae2024-11-19 22:41:14 +010060 \ ' --output %:r:S.$* -- %:S', ' \|"')
Konfekt5f5f2832024-08-20 21:57:54 +020061CompilerSet errorformat=\"%f\",\ line\ %l:\ %m
Wu, Zhenyu7005b7e2024-04-08 20:53:19 +020062
63let &cpo = s:keepcpo
64unlet s:keepcpo