pandoc.vim 2.3 KB

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