diff options
| author | Sergii Tykhomyrov <sergii@tykhomyrov.net> | 2025-02-05 12:16:47 +0100 |
|---|---|---|
| committer | Sergii Tykhomyrov <sergii@tykhomyrov.net> | 2025-02-05 12:16:47 +0100 |
| commit | d73d2d3dd249cf73fca41583d7a167355a006ffc (patch) | |
| tree | f8a53853486f0a030ae2b35722f943bd377a3c59 /nvim | |
| parent | 09ccb63b74894a39b041ac159452b4f970083c39 (diff) | |
| download | dotfiles-d73d2d3dd249cf73fca41583d7a167355a006ffc.tar.gz dotfiles-d73d2d3dd249cf73fca41583d7a167355a006ffc.zip | |
Add AsciiDoc preview functionality
Diffstat (limited to 'nvim')
| -rw-r--r-- | nvim/dot-config/nvim/lua/asciidoc-preview/init.lua | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/nvim/dot-config/nvim/lua/asciidoc-preview/init.lua b/nvim/dot-config/nvim/lua/asciidoc-preview/init.lua new file mode 100644 index 0000000..9061888 --- /dev/null +++ b/nvim/dot-config/nvim/lua/asciidoc-preview/init.lua @@ -0,0 +1,84 @@ +local api = vim.api +local fn = vim.fn + +local augroup = api.nvim_create_augroup('AsciiDocPreview', { clear = true }) + +local config = { + convert_cmd = 'asciidoctor -o %html %adoc', + preview_cmd = 'xdg-open %html', + refresh_interval = 2000, +} + +local function get_html_path() + return fn.expand('%:p'):gsub('%.adoc$', '.html') +end + +local function convert_to_html() + local adoc_path = fn.expand('%:p') + local html_path = get_html_path() + + local cmd = config.convert_cmd + :gsub('%%adoc', adoc_path) + :gsub('%%html', html_path) + + fn.system(cmd) + + return html_path +end + +local function open_preview() + local html_path = get_html_path() + local cmd = string.format('%s %s', config.preview_cmd, html_path) + + fn.system(cmd) +end + +local refresh_timer = nil + +local function start_auto_refresh() + if refresh_timer then + refresh_timer:stop() + end + + refresh_timer = vim.loop.new_timer() + refresh_timer:start(0, config.refresh_interval, vim.schedule_wrap(function() + if api.nvim_buf_get_option(0, 'modified') then + api.nvim_command('write') + convert_to_html() + end + end)) +end + +local function stop_auto_refresh() + if refresh_timer then + refresh_timer:stop() + refresh_timer = nil + end +end + +api.nvim_create_user_command('AsciiDocPreviewStart', function() + convert_to_html() + open_preview() + start_auto_refresh() +end, {}) + +api.nvim_create_user_command('AsciiDocPreviewStop', function() + stop_auto_refresh() +end, {}) + +api.nvim_create_autocmd('VimLeave', { + group = augroup, + callback = function() + stop_auto_refresh() + end, +}) + +return { + start_preview = function() + convert_to_html() + open_preview() + start_auto_refresh() + end, + stop_preview = stop_auto_refresh, + config = config +} |
