pandoc.vim 2.3 KB

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