feat: rename watch → toggle, auto-compile on start, built-in opener
Problem: :Preview watch only registered a BufWritePost autocmd without
compiling immediately, required boilerplate to open output files after
first compilation, and was misleadingly named.
Solution: Rename watch → toggle throughout. M.toggle now compiles
immediately on activation. Add an open field to ProviderConfig: true
calls vim.ui.open(), a string[] runs the command with the output path
appended, tracked per-buffer so the file opens only once. All presets
default to { 'xdg-open' }. Health check validates opener binaries.
Guard the async compile callback against invalid buffer ids.
This commit is contained in:
parent
c62c930454
commit
673573044f
12 changed files with 346 additions and 176 deletions
102
README.md
102
README.md
|
|
@ -1,10 +1,9 @@
|
||||||
# preview.nvim
|
# preview.nvim
|
||||||
|
|
||||||
Async document compilation for Neovim.
|
**Async document compilation for Neovim**
|
||||||
|
|
||||||
A framework for compiling documents (LaTeX, Typst, Markdown, etc.)
|
An extensible framework for compiling documents (LaTeX, Typst, Markdown, etc.)
|
||||||
asynchronously with error diagnostics. Ships with zero defaults — you configure
|
asynchronously with error diagnostics.
|
||||||
your own providers.
|
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
|
|
@ -12,59 +11,68 @@ your own providers.
|
||||||
- Compiler errors as native `vim.diagnostic`
|
- Compiler errors as native `vim.diagnostic`
|
||||||
- User events for extensibility (`PreviewCompileStarted`,
|
- User events for extensibility (`PreviewCompileStarted`,
|
||||||
`PreviewCompileSuccess`, `PreviewCompileFailed`)
|
`PreviewCompileSuccess`, `PreviewCompileFailed`)
|
||||||
|
- Built-in presets for Typst, LaTeX, Markdown, and GitHub-flavored Markdown
|
||||||
- `:checkhealth` integration
|
- `:checkhealth` integration
|
||||||
- Zero dependencies beyond Neovim 0.11.0+
|
- Zero dependencies beyond Neovim 0.11.0+
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
- Neovim >= 0.11.0
|
- Neovim 0.11.0+
|
||||||
- A compiler binary for each provider you configure
|
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
```lua
|
Install with your package manager of choice or via
|
||||||
-- lazy.nvim
|
[luarocks](https://luarocks.org/modules/barrettruth/preview.nvim):
|
||||||
{ 'barrettruth/preview.nvim' }
|
|
||||||
```
|
```
|
||||||
|
luarocks install preview.nvim
|
||||||
```vim
|
|
||||||
" luarocks
|
|
||||||
:Rocks install preview.nvim
|
|
||||||
```
|
|
||||||
|
|
||||||
## Configuration
|
|
||||||
|
|
||||||
Use built-in presets for common tools:
|
|
||||||
|
|
||||||
```lua
|
|
||||||
local presets = require('preview.presets')
|
|
||||||
vim.g.preview = {
|
|
||||||
providers = {
|
|
||||||
typst = presets.typst,
|
|
||||||
tex = presets.latex,
|
|
||||||
markdown = presets.markdown,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Or define providers manually:
|
|
||||||
|
|
||||||
```lua
|
|
||||||
vim.g.preview = {
|
|
||||||
providers = {
|
|
||||||
typst = {
|
|
||||||
cmd = { 'typst', 'compile' },
|
|
||||||
args = function(ctx)
|
|
||||||
return { ctx.file }
|
|
||||||
end,
|
|
||||||
output = function(ctx)
|
|
||||||
return ctx.file:gsub('%.typ$', '.pdf')
|
|
||||||
end,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
See `:help preview.nvim` for full documentation.
|
```vim
|
||||||
|
:help preview.nvim
|
||||||
|
```
|
||||||
|
|
||||||
|
## FAQ
|
||||||
|
|
||||||
|
**Q: How do I define a custom provider?**
|
||||||
|
|
||||||
|
```lua
|
||||||
|
require('preview').setup({
|
||||||
|
typst = {
|
||||||
|
cmd = { 'typst', 'compile' },
|
||||||
|
args = function(ctx)
|
||||||
|
return { ctx.file }
|
||||||
|
end,
|
||||||
|
output = function(ctx)
|
||||||
|
return ctx.file:gsub('%.typ$', '.pdf')
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
**Q: How do I override a preset?**
|
||||||
|
|
||||||
|
```lua
|
||||||
|
local presets = require('preview.presets')
|
||||||
|
require('preview').setup({
|
||||||
|
typst = vim.tbl_deep_extend('force', presets.typst, {
|
||||||
|
env = { TYPST_FONT_PATHS = '/usr/share/fonts' },
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
**Q: How do I automatically open the output file?**
|
||||||
|
|
||||||
|
Set `open = true` on your provider (all built-in presets have this enabled) to
|
||||||
|
open the output with `vim.ui.open()` after the first successful compilation.
|
||||||
|
For a specific application, pass a command table:
|
||||||
|
|
||||||
|
```lua
|
||||||
|
typst = vim.tbl_deep_extend('force', presets.typst, {
|
||||||
|
open = { 'sioyek', '--new-instance' },
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
See `:h preview.nvim` for more information.
|
||||||
|
|
|
||||||
|
|
@ -37,18 +37,21 @@ With lazy.nvim:
|
||||||
==============================================================================
|
==============================================================================
|
||||||
CONFIGURATION *preview.nvim-configuration*
|
CONFIGURATION *preview.nvim-configuration*
|
||||||
|
|
||||||
Configure via the `vim.g.preview` global table before the plugin loads.
|
Configure via `require('preview').setup()`.
|
||||||
|
|
||||||
*preview.Config*
|
*preview.setup()*
|
||||||
Fields:~
|
setup({opts?})
|
||||||
|
|
||||||
|
`opts` is a mixed table. Array entries are preset names (see
|
||||||
|
|preview.nvim-presets|). Hash entries with table values are custom
|
||||||
|
provider configs keyed by filetype.
|
||||||
|
|
||||||
|
Fields:~
|
||||||
|
|
||||||
`debug` boolean|string Enable debug logging. A string value
|
`debug` boolean|string Enable debug logging. A string value
|
||||||
is treated as a log file path.
|
is treated as a log file path.
|
||||||
Default: `false`
|
Default: `false`
|
||||||
|
|
||||||
`providers` table Provider configurations keyed by
|
|
||||||
filetype. Default: `{}`
|
|
||||||
|
|
||||||
*preview.ProviderConfig*
|
*preview.ProviderConfig*
|
||||||
Provider fields:~
|
Provider fields:~
|
||||||
|
|
||||||
|
|
@ -74,6 +77,12 @@ Provider fields:~
|
||||||
If a function, receives a
|
If a function, receives a
|
||||||
|preview.Context|.
|
|preview.Context|.
|
||||||
|
|
||||||
|
`open` boolean|string[] Open the output file after the first
|
||||||
|
successful compilation. `true` uses
|
||||||
|
|vim.ui.open()|. A string[] is run as
|
||||||
|
a command with the output path appended.
|
||||||
|
Presets default to `{ 'xdg-open' }`.
|
||||||
|
|
||||||
*preview.Context*
|
*preview.Context*
|
||||||
Context fields:~
|
Context fields:~
|
||||||
|
|
||||||
|
|
@ -82,38 +91,36 @@ Context fields:~
|
||||||
`root` string Project root (git root or file directory).
|
`root` string Project root (git root or file directory).
|
||||||
`ft` string Filetype.
|
`ft` string Filetype.
|
||||||
|
|
||||||
Example:~
|
Example using preset names:~
|
||||||
>lua
|
>lua
|
||||||
vim.g.preview = {
|
require('preview').setup({ 'typst', 'latex', 'markdown' })
|
||||||
providers = {
|
<
|
||||||
typst = {
|
|
||||||
cmd = { 'typst', 'compile' },
|
Example with a custom provider:~
|
||||||
args = function(ctx)
|
>lua
|
||||||
return { ctx.file }
|
require('preview').setup({
|
||||||
end,
|
typst = {
|
||||||
output = function(ctx)
|
cmd = { 'typst', 'compile' },
|
||||||
return ctx.file:gsub('%.typ$', '.pdf')
|
args = function(ctx)
|
||||||
end,
|
return { ctx.file }
|
||||||
error_parser = function(stderr, ctx)
|
end,
|
||||||
local diagnostics = {}
|
output = function(ctx)
|
||||||
for line, col, msg in stderr:gmatch('error:.-(%d+):(%d+):%s*(.-)%\n') do
|
return ctx.file:gsub('%.typ$', '.pdf')
|
||||||
table.insert(diagnostics, {
|
end,
|
||||||
lnum = tonumber(line) - 1,
|
error_parser = function(stderr, ctx)
|
||||||
col = tonumber(col) - 1,
|
local diagnostics = {}
|
||||||
message = msg,
|
for line, col, msg in stderr:gmatch('error:.-(%d+):(%d+):%s*(.-)%\n') do
|
||||||
severity = vim.diagnostic.severity.ERROR,
|
table.insert(diagnostics, {
|
||||||
})
|
lnum = tonumber(line) - 1,
|
||||||
end
|
col = tonumber(col) - 1,
|
||||||
return diagnostics
|
message = msg,
|
||||||
end,
|
severity = vim.diagnostic.severity.ERROR,
|
||||||
},
|
})
|
||||||
tex = {
|
end
|
||||||
cmd = { 'latexmk' },
|
return diagnostics
|
||||||
args = { '-pdf', '-interaction=nonstopmode' },
|
end,
|
||||||
clean = { 'latexmk', '-c' },
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
})
|
||||||
<
|
<
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
|
|
@ -122,32 +129,24 @@ PRESETS *preview.nvim-presets*
|
||||||
preview.nvim ships with pre-built provider configurations for common tools.
|
preview.nvim ships with pre-built provider configurations for common tools.
|
||||||
Import them from `preview.presets`:
|
Import them from `preview.presets`:
|
||||||
|
|
||||||
`presets.typst` typst compile → PDF
|
`presets.typst` typst compile → PDF
|
||||||
`presets.latex` latexmk -pdf → PDF (with clean support)
|
`presets.latex` latexmk -pdf → PDF (with clean support)
|
||||||
`presets.markdown` pandoc → PDF
|
`presets.markdown` pandoc → HTML (standalone, embedded)
|
||||||
|
`presets.github` pandoc → HTML (GitHub-styled)
|
||||||
|
|
||||||
Example:~
|
Pass preset names as array entries to `setup()`:
|
||||||
>lua
|
>lua
|
||||||
local presets = require('preview.presets')
|
require('preview').setup({ 'typst', 'latex', 'markdown' })
|
||||||
vim.g.preview = {
|
|
||||||
providers = {
|
|
||||||
typst = presets.typst,
|
|
||||||
tex = presets.latex,
|
|
||||||
markdown = presets.markdown,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
<
|
<
|
||||||
|
|
||||||
Override individual fields with `vim.tbl_deep_extend`:
|
Override individual fields using `vim.tbl_deep_extend`:
|
||||||
>lua
|
>lua
|
||||||
local presets = require('preview.presets')
|
local presets = require('preview.presets')
|
||||||
vim.g.preview = {
|
require('preview').setup({
|
||||||
providers = {
|
typst = vim.tbl_deep_extend('force', presets.typst, {
|
||||||
typst = vim.tbl_deep_extend('force', presets.typst, {
|
env = { TYPST_FONT_PATHS = '/usr/share/fonts' },
|
||||||
env = { TYPST_FONT_PATHS = '/usr/share/fonts' },
|
}),
|
||||||
}),
|
})
|
||||||
},
|
|
||||||
}
|
|
||||||
<
|
<
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
|
|
@ -160,7 +159,7 @@ COMMANDS *preview.nvim-commands*
|
||||||
`compile` Compile the current buffer (default if omitted).
|
`compile` Compile the current buffer (default if omitted).
|
||||||
`stop` Kill active compilation for the current buffer.
|
`stop` Kill active compilation for the current buffer.
|
||||||
`clean` Run the provider's clean command.
|
`clean` Run the provider's clean command.
|
||||||
`watch` Toggle auto-compile on save for the current buffer.
|
`toggle` Toggle auto-compile on save for the current buffer.
|
||||||
`status` Echo compilation status (idle, compiling, watching).
|
`status` Echo compilation status (idle, compiling, watching).
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
|
|
@ -175,10 +174,10 @@ preview.stop({bufnr?}) *preview.stop()*
|
||||||
preview.clean({bufnr?}) *preview.clean()*
|
preview.clean({bufnr?}) *preview.clean()*
|
||||||
Run the provider's clean command for the buffer.
|
Run the provider's clean command for the buffer.
|
||||||
|
|
||||||
preview.watch({bufnr?}) *preview.watch()*
|
preview.toggle({bufnr?}) *preview.toggle()*
|
||||||
Toggle watch mode for the buffer. When enabled, the buffer is
|
Toggle watch mode for the buffer. When enabled, the buffer is
|
||||||
automatically compiled on each save (`BufWritePost`). Call again
|
immediately compiled and automatically recompiled on each save
|
||||||
to stop watching.
|
(`BufWritePost`). Call again to stop watching.
|
||||||
|
|
||||||
preview.status({bufnr?}) *preview.status()*
|
preview.status({bufnr?}) *preview.status()*
|
||||||
Returns a |preview.Status| table.
|
Returns a |preview.Status| table.
|
||||||
|
|
@ -232,6 +231,7 @@ Run `:checkhealth preview` to verify:
|
||||||
|
|
||||||
- Neovim version >= 0.11.0
|
- Neovim version >= 0.11.0
|
||||||
- Each configured provider's binary is executable
|
- Each configured provider's binary is executable
|
||||||
|
- Each configured provider's opener binary (if any) is executable
|
||||||
- Each configured provider's filetype mapping is valid
|
- Each configured provider's filetype mapping is valid
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
local subcommands = { 'compile', 'stop', 'clean', 'watch', 'status' }
|
local subcommands = { 'compile', 'stop', 'clean', 'toggle', 'status' }
|
||||||
|
|
||||||
---@param args string
|
---@param args string
|
||||||
local function dispatch(args)
|
local function dispatch(args)
|
||||||
|
|
@ -12,8 +12,8 @@ local function dispatch(args)
|
||||||
require('preview').stop()
|
require('preview').stop()
|
||||||
elseif subcmd == 'clean' then
|
elseif subcmd == 'clean' then
|
||||||
require('preview').clean()
|
require('preview').clean()
|
||||||
elseif subcmd == 'watch' then
|
elseif subcmd == 'toggle' then
|
||||||
require('preview').watch()
|
require('preview').toggle()
|
||||||
elseif subcmd == 'status' then
|
elseif subcmd == 'status' then
|
||||||
local s = require('preview').status()
|
local s = require('preview').status()
|
||||||
local parts = {}
|
local parts = {}
|
||||||
|
|
@ -47,7 +47,7 @@ function M.setup()
|
||||||
complete = function(lead)
|
complete = function(lead)
|
||||||
return complete(lead)
|
return complete(lead)
|
||||||
end,
|
end,
|
||||||
desc = 'Compile, stop, clean, watch, or check status of document preview',
|
desc = 'Compile, stop, clean, toggle, or check status of document preview',
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,9 @@ local active = {}
|
||||||
---@type table<integer, integer>
|
---@type table<integer, integer>
|
||||||
local watching = {}
|
local watching = {}
|
||||||
|
|
||||||
|
---@type table<integer, true>
|
||||||
|
local opened = {}
|
||||||
|
|
||||||
---@param val string[]|fun(ctx: preview.Context): string[]
|
---@param val string[]|fun(ctx: preview.Context): string[]
|
||||||
---@param ctx preview.Context
|
---@param ctx preview.Context
|
||||||
---@return string[]
|
---@return string[]
|
||||||
|
|
@ -68,6 +71,9 @@ function M.compile(bufnr, name, provider, ctx)
|
||||||
},
|
},
|
||||||
vim.schedule_wrap(function(result)
|
vim.schedule_wrap(function(result)
|
||||||
active[bufnr] = nil
|
active[bufnr] = nil
|
||||||
|
if not vim.api.nvim_buf_is_valid(bufnr) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
if result.code == 0 then
|
if result.code == 0 then
|
||||||
log.dbg('compilation succeeded for buffer %d', bufnr)
|
log.dbg('compilation succeeded for buffer %d', bufnr)
|
||||||
|
|
@ -76,6 +82,16 @@ function M.compile(bufnr, name, provider, ctx)
|
||||||
pattern = 'PreviewCompileSuccess',
|
pattern = 'PreviewCompileSuccess',
|
||||||
data = { bufnr = bufnr, provider = name, output = output_file },
|
data = { bufnr = bufnr, provider = name, output = output_file },
|
||||||
})
|
})
|
||||||
|
if provider.open and not opened[bufnr] and output_file ~= '' then
|
||||||
|
if provider.open == true then
|
||||||
|
vim.ui.open(output_file)
|
||||||
|
elseif type(provider.open) == 'table' then
|
||||||
|
local open_cmd = vim.list_extend({}, provider.open)
|
||||||
|
table.insert(open_cmd, output_file)
|
||||||
|
vim.system(open_cmd)
|
||||||
|
end
|
||||||
|
opened[bufnr] = true
|
||||||
|
end
|
||||||
else
|
else
|
||||||
log.dbg('compilation failed for buffer %d (exit code %d)', bufnr, result.code)
|
log.dbg('compilation failed for buffer %d (exit code %d)', bufnr, result.code)
|
||||||
if provider.error_parser then
|
if provider.error_parser then
|
||||||
|
|
@ -146,7 +162,7 @@ end
|
||||||
---@param name string
|
---@param name string
|
||||||
---@param provider preview.ProviderConfig
|
---@param provider preview.ProviderConfig
|
||||||
---@param ctx_builder fun(bufnr: integer): preview.Context
|
---@param ctx_builder fun(bufnr: integer): preview.Context
|
||||||
function M.watch(bufnr, name, provider, ctx_builder)
|
function M.toggle(bufnr, name, provider, ctx_builder)
|
||||||
if watching[bufnr] then
|
if watching[bufnr] then
|
||||||
M.unwatch(bufnr)
|
M.unwatch(bufnr)
|
||||||
return
|
return
|
||||||
|
|
@ -168,6 +184,7 @@ function M.watch(bufnr, name, provider, ctx_builder)
|
||||||
once = true,
|
once = true,
|
||||||
callback = function()
|
callback = function()
|
||||||
M.unwatch(bufnr)
|
M.unwatch(bufnr)
|
||||||
|
opened[bufnr] = nil
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -175,6 +192,8 @@ function M.watch(bufnr, name, provider, ctx_builder)
|
||||||
pattern = 'PreviewWatchStarted',
|
pattern = 'PreviewWatchStarted',
|
||||||
data = { bufnr = bufnr, provider = name },
|
data = { bufnr = bufnr, provider = name },
|
||||||
})
|
})
|
||||||
|
|
||||||
|
M.compile(bufnr, name, provider, ctx_builder(bufnr))
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
|
|
@ -244,6 +263,7 @@ end
|
||||||
M._test = {
|
M._test = {
|
||||||
active = active,
|
active = active,
|
||||||
watching = watching,
|
watching = watching,
|
||||||
|
opened = opened,
|
||||||
}
|
}
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,14 @@ function M.check()
|
||||||
else
|
else
|
||||||
vim.health.error('filetype "' .. ft .. '": ' .. bin .. ' not found')
|
vim.health.error('filetype "' .. ft .. '": ' .. bin .. ' not found')
|
||||||
end
|
end
|
||||||
|
if type(provider.open) == 'table' then
|
||||||
|
local opener = provider.open[1]
|
||||||
|
if vim.fn.executable(opener) == 1 then
|
||||||
|
vim.health.ok('filetype "' .. ft .. '": opener ' .. opener .. ' found')
|
||||||
|
else
|
||||||
|
vim.health.error('filetype "' .. ft .. '": opener ' .. opener .. ' not found')
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
---@class preview.ProviderConfig
|
---@class preview.ProviderConfig
|
||||||
|
---@field ft? string
|
||||||
---@field cmd string[]
|
---@field cmd string[]
|
||||||
---@field args? string[]|fun(ctx: preview.Context): string[]
|
---@field args? string[]|fun(ctx: preview.Context): string[]
|
||||||
---@field cwd? string|fun(ctx: preview.Context): string
|
---@field cwd? string|fun(ctx: preview.Context): string
|
||||||
|
|
@ -6,6 +7,7 @@
|
||||||
---@field output? string|fun(ctx: preview.Context): string
|
---@field output? string|fun(ctx: preview.Context): string
|
||||||
---@field error_parser? fun(stderr: string, ctx: preview.Context): preview.Diagnostic[]
|
---@field error_parser? fun(stderr: string, ctx: preview.Context): preview.Diagnostic[]
|
||||||
---@field clean? string[]|fun(ctx: preview.Context): string[]
|
---@field clean? string[]|fun(ctx: preview.Context): string[]
|
||||||
|
---@field open? boolean|string[]
|
||||||
|
|
||||||
---@class preview.Config
|
---@class preview.Config
|
||||||
---@field debug boolean|string
|
---@field debug boolean|string
|
||||||
|
|
@ -32,10 +34,11 @@
|
||||||
---@field output_file string
|
---@field output_file string
|
||||||
|
|
||||||
---@class preview
|
---@class preview
|
||||||
|
---@field setup fun(opts?: table)
|
||||||
---@field compile fun(bufnr?: integer)
|
---@field compile fun(bufnr?: integer)
|
||||||
---@field stop fun(bufnr?: integer)
|
---@field stop fun(bufnr?: integer)
|
||||||
---@field clean fun(bufnr?: integer)
|
---@field clean fun(bufnr?: integer)
|
||||||
---@field watch fun(bufnr?: integer)
|
---@field toggle fun(bufnr?: integer)
|
||||||
---@field status fun(bufnr?: integer): preview.Status
|
---@field status fun(bufnr?: integer): preview.Status
|
||||||
---@field get_config fun(): preview.Config
|
---@field get_config fun(): preview.Config
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
@ -52,39 +55,48 @@ local default_config = {
|
||||||
---@type preview.Config
|
---@type preview.Config
|
||||||
local config = vim.deepcopy(default_config)
|
local config = vim.deepcopy(default_config)
|
||||||
|
|
||||||
local initialized = false
|
---@param opts? table
|
||||||
|
function M.setup(opts)
|
||||||
|
opts = opts or {}
|
||||||
|
vim.validate('preview.setup opts', opts, 'table')
|
||||||
|
|
||||||
local function init()
|
local presets = require('preview.presets')
|
||||||
if initialized then
|
local providers = {}
|
||||||
return
|
local debug = false
|
||||||
end
|
|
||||||
initialized = true
|
|
||||||
|
|
||||||
local opts = vim.g.preview or {}
|
for k, v in pairs(opts) do
|
||||||
|
if k == 'debug' then
|
||||||
vim.validate('preview config', opts, 'table')
|
vim.validate('preview.setup opts.debug', v, { 'boolean', 'string' })
|
||||||
if opts.debug ~= nil then
|
debug = v
|
||||||
vim.validate('preview config.debug', opts.debug, { 'boolean', 'string' })
|
elseif type(k) == 'number' then
|
||||||
end
|
vim.validate('preview.setup preset name', v, 'string')
|
||||||
if opts.providers ~= nil then
|
local preset = presets[v]
|
||||||
vim.validate('preview config.providers', opts.providers, 'table')
|
if preset then
|
||||||
|
providers[preset.ft] = preset
|
||||||
|
end
|
||||||
|
else
|
||||||
|
vim.validate('preview.setup provider config', v, 'table')
|
||||||
|
providers[k] = v
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
config = vim.tbl_deep_extend('force', default_config, opts)
|
config = vim.tbl_deep_extend('force', default_config, {
|
||||||
|
debug = debug,
|
||||||
|
providers = providers,
|
||||||
|
})
|
||||||
|
|
||||||
log.set_enabled(config.debug)
|
log.set_enabled(config.debug)
|
||||||
log.dbg('initialized with %d providers', vim.tbl_count(config.providers))
|
log.dbg('initialized with %d providers', vim.tbl_count(config.providers))
|
||||||
end
|
end
|
||||||
|
|
||||||
---@return preview.Config
|
---@return preview.Config
|
||||||
function M.get_config()
|
function M.get_config()
|
||||||
init()
|
|
||||||
return config
|
return config
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param bufnr? integer
|
---@param bufnr? integer
|
||||||
---@return string?
|
---@return string?
|
||||||
function M.resolve_provider(bufnr)
|
function M.resolve_provider(bufnr)
|
||||||
init()
|
|
||||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||||
local ft = vim.bo[bufnr].filetype
|
local ft = vim.bo[bufnr].filetype
|
||||||
if not config.providers[ft] then
|
if not config.providers[ft] then
|
||||||
|
|
@ -97,7 +109,6 @@ end
|
||||||
---@param bufnr? integer
|
---@param bufnr? integer
|
||||||
---@return preview.Context
|
---@return preview.Context
|
||||||
function M.build_context(bufnr)
|
function M.build_context(bufnr)
|
||||||
init()
|
|
||||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||||
local file = vim.api.nvim_buf_get_name(bufnr)
|
local file = vim.api.nvim_buf_get_name(bufnr)
|
||||||
local root = vim.fs.root(bufnr, { '.git' }) or vim.fn.fnamemodify(file, ':h')
|
local root = vim.fs.root(bufnr, { '.git' }) or vim.fn.fnamemodify(file, ':h')
|
||||||
|
|
@ -111,42 +122,38 @@ end
|
||||||
|
|
||||||
---@param bufnr? integer
|
---@param bufnr? integer
|
||||||
function M.compile(bufnr)
|
function M.compile(bufnr)
|
||||||
init()
|
|
||||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||||
local name = M.resolve_provider(bufnr)
|
local name = M.resolve_provider(bufnr)
|
||||||
if not name then
|
if not name then
|
||||||
vim.notify('[preview.nvim] no provider configured for this filetype', vim.log.levels.WARN)
|
vim.notify('[preview.nvim] no provider configured for this filetype', vim.log.levels.WARN)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local provider = config.providers[name]
|
|
||||||
local ctx = M.build_context(bufnr)
|
local ctx = M.build_context(bufnr)
|
||||||
|
local provider = config.providers[name]
|
||||||
compiler.compile(bufnr, name, provider, ctx)
|
compiler.compile(bufnr, name, provider, ctx)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param bufnr? integer
|
---@param bufnr? integer
|
||||||
function M.stop(bufnr)
|
function M.stop(bufnr)
|
||||||
init()
|
|
||||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||||
compiler.stop(bufnr)
|
compiler.stop(bufnr)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param bufnr? integer
|
---@param bufnr? integer
|
||||||
function M.clean(bufnr)
|
function M.clean(bufnr)
|
||||||
init()
|
|
||||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||||
local name = M.resolve_provider(bufnr)
|
local name = M.resolve_provider(bufnr)
|
||||||
if not name then
|
if not name then
|
||||||
vim.notify('[preview.nvim] no provider configured for this filetype', vim.log.levels.WARN)
|
vim.notify('[preview.nvim] no provider configured for this filetype', vim.log.levels.WARN)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local provider = config.providers[name]
|
|
||||||
local ctx = M.build_context(bufnr)
|
local ctx = M.build_context(bufnr)
|
||||||
|
local provider = config.providers[name]
|
||||||
compiler.clean(bufnr, name, provider, ctx)
|
compiler.clean(bufnr, name, provider, ctx)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param bufnr? integer
|
---@param bufnr? integer
|
||||||
function M.watch(bufnr)
|
function M.toggle(bufnr)
|
||||||
init()
|
|
||||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||||
local name = M.resolve_provider(bufnr)
|
local name = M.resolve_provider(bufnr)
|
||||||
if not name then
|
if not name then
|
||||||
|
|
@ -154,7 +161,7 @@ function M.watch(bufnr)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local provider = config.providers[name]
|
local provider = config.providers[name]
|
||||||
compiler.watch(bufnr, name, provider, M.build_context)
|
compiler.toggle(bufnr, name, provider, M.build_context)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@class preview.Status
|
---@class preview.Status
|
||||||
|
|
@ -166,7 +173,6 @@ end
|
||||||
---@param bufnr? integer
|
---@param bufnr? integer
|
||||||
---@return preview.Status
|
---@return preview.Status
|
||||||
function M.status(bufnr)
|
function M.status(bufnr)
|
||||||
init()
|
|
||||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||||
return compiler.status(bufnr)
|
return compiler.status(bufnr)
|
||||||
end
|
end
|
||||||
|
|
@ -174,7 +180,6 @@ end
|
||||||
M._test = {
|
M._test = {
|
||||||
---@diagnostic disable-next-line: assign-type-mismatch
|
---@diagnostic disable-next-line: assign-type-mismatch
|
||||||
reset = function()
|
reset = function()
|
||||||
initialized = false
|
|
||||||
config = vim.deepcopy(default_config)
|
config = vim.deepcopy(default_config)
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ local M = {}
|
||||||
|
|
||||||
---@type preview.ProviderConfig
|
---@type preview.ProviderConfig
|
||||||
M.typst = {
|
M.typst = {
|
||||||
|
ft = 'typst',
|
||||||
cmd = { 'typst', 'compile' },
|
cmd = { 'typst', 'compile' },
|
||||||
args = function(ctx)
|
args = function(ctx)
|
||||||
return { ctx.file }
|
return { ctx.file }
|
||||||
|
|
@ -9,10 +10,12 @@ M.typst = {
|
||||||
output = function(ctx)
|
output = function(ctx)
|
||||||
return ctx.file:gsub('%.typ$', '.pdf')
|
return ctx.file:gsub('%.typ$', '.pdf')
|
||||||
end,
|
end,
|
||||||
|
open = { 'xdg-open' },
|
||||||
}
|
}
|
||||||
|
|
||||||
---@type preview.ProviderConfig
|
---@type preview.ProviderConfig
|
||||||
M.latex = {
|
M.latex = {
|
||||||
|
ft = 'tex',
|
||||||
cmd = { 'latexmk' },
|
cmd = { 'latexmk' },
|
||||||
args = function(ctx)
|
args = function(ctx)
|
||||||
return { '-pdf', '-interaction=nonstopmode', ctx.file }
|
return { '-pdf', '-interaction=nonstopmode', ctx.file }
|
||||||
|
|
@ -23,18 +26,49 @@ M.latex = {
|
||||||
clean = function(ctx)
|
clean = function(ctx)
|
||||||
return { 'latexmk', '-c', ctx.file }
|
return { 'latexmk', '-c', ctx.file }
|
||||||
end,
|
end,
|
||||||
|
open = { 'xdg-open' },
|
||||||
}
|
}
|
||||||
|
|
||||||
---@type preview.ProviderConfig
|
---@type preview.ProviderConfig
|
||||||
M.markdown = {
|
M.markdown = {
|
||||||
|
ft = 'markdown',
|
||||||
cmd = { 'pandoc' },
|
cmd = { 'pandoc' },
|
||||||
args = function(ctx)
|
args = function(ctx)
|
||||||
local output = ctx.file:gsub('%.md$', '.pdf')
|
local output = ctx.file:gsub('%.md$', '.html')
|
||||||
return { ctx.file, '-o', output }
|
return { ctx.file, '-s', '--embed-resources', '-o', output }
|
||||||
end,
|
end,
|
||||||
output = function(ctx)
|
output = function(ctx)
|
||||||
return ctx.file:gsub('%.md$', '.pdf')
|
return ctx.file:gsub('%.md$', '.html')
|
||||||
end,
|
end,
|
||||||
|
clean = function(ctx)
|
||||||
|
return { 'rm', '-f', (ctx.file:gsub('%.md$', '.html')) }
|
||||||
|
end,
|
||||||
|
open = { 'xdg-open' },
|
||||||
|
}
|
||||||
|
|
||||||
|
---@type preview.ProviderConfig
|
||||||
|
M.github = {
|
||||||
|
ft = 'markdown',
|
||||||
|
cmd = { 'pandoc' },
|
||||||
|
args = function(ctx)
|
||||||
|
local output = ctx.file:gsub('%.md$', '.html')
|
||||||
|
return {
|
||||||
|
ctx.file,
|
||||||
|
'-s',
|
||||||
|
'--embed-resources',
|
||||||
|
'--css',
|
||||||
|
'https://cdn.jsdelivr.net/gh/pixelbrackets/gfm-stylesheet@master/github.css',
|
||||||
|
'-o',
|
||||||
|
output,
|
||||||
|
}
|
||||||
|
end,
|
||||||
|
output = function(ctx)
|
||||||
|
return ctx.file:gsub('%.md$', '.html')
|
||||||
|
end,
|
||||||
|
clean = function(ctx)
|
||||||
|
return { 'rm', '-f', (ctx.file:gsub('%.md$', '.html')) }
|
||||||
|
end,
|
||||||
|
open = { 'xdg-open' },
|
||||||
}
|
}
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
|
|
@ -35,10 +35,10 @@ describe('commands', function()
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('does not error on :Preview watch with no provider', function()
|
it('does not error on :Preview toggle with no provider', function()
|
||||||
require('preview.commands').setup()
|
require('preview.commands').setup()
|
||||||
assert.has_no.errors(function()
|
assert.has_no.errors(function()
|
||||||
vim.cmd('Preview watch')
|
vim.cmd('Preview toggle')
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
|
||||||
|
|
@ -174,7 +174,7 @@ describe('compiler', function()
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('watch', function()
|
describe('toggle', function()
|
||||||
it('registers autocmd and tracks in watching table', function()
|
it('registers autocmd and tracks in watching table', function()
|
||||||
local bufnr = helpers.create_buffer({ 'hello' }, 'text')
|
local bufnr = helpers.create_buffer({ 'hello' }, 'text')
|
||||||
vim.api.nvim_buf_set_name(bufnr, '/tmp/preview_test_watch.txt')
|
vim.api.nvim_buf_set_name(bufnr, '/tmp/preview_test_watch.txt')
|
||||||
|
|
@ -184,7 +184,7 @@ describe('compiler', function()
|
||||||
return { bufnr = b, file = '/tmp/preview_test_watch.txt', root = '/tmp', ft = 'text' }
|
return { bufnr = b, file = '/tmp/preview_test_watch.txt', root = '/tmp', ft = 'text' }
|
||||||
end
|
end
|
||||||
|
|
||||||
compiler.watch(bufnr, 'echo', provider, ctx_builder)
|
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
|
||||||
assert.is_not_nil(compiler._test.watching[bufnr])
|
assert.is_not_nil(compiler._test.watching[bufnr])
|
||||||
|
|
||||||
helpers.delete_buffer(bufnr)
|
helpers.delete_buffer(bufnr)
|
||||||
|
|
@ -208,7 +208,7 @@ describe('compiler', function()
|
||||||
return { bufnr = b, file = '/tmp/preview_test_watch_event.txt', root = '/tmp', ft = 'text' }
|
return { bufnr = b, file = '/tmp/preview_test_watch_event.txt', root = '/tmp', ft = 'text' }
|
||||||
end
|
end
|
||||||
|
|
||||||
compiler.watch(bufnr, 'echo', provider, ctx_builder)
|
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
|
||||||
assert.is_true(fired)
|
assert.is_true(fired)
|
||||||
|
|
||||||
compiler.unwatch(bufnr)
|
compiler.unwatch(bufnr)
|
||||||
|
|
@ -224,10 +224,10 @@ describe('compiler', function()
|
||||||
return { bufnr = b, file = '/tmp/preview_test_watch_toggle.txt', root = '/tmp', ft = 'text' }
|
return { bufnr = b, file = '/tmp/preview_test_watch_toggle.txt', root = '/tmp', ft = 'text' }
|
||||||
end
|
end
|
||||||
|
|
||||||
compiler.watch(bufnr, 'echo', provider, ctx_builder)
|
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
|
||||||
assert.is_not_nil(compiler._test.watching[bufnr])
|
assert.is_not_nil(compiler._test.watching[bufnr])
|
||||||
|
|
||||||
compiler.watch(bufnr, 'echo', provider, ctx_builder)
|
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
|
||||||
assert.is_nil(compiler._test.watching[bufnr])
|
assert.is_nil(compiler._test.watching[bufnr])
|
||||||
|
|
||||||
helpers.delete_buffer(bufnr)
|
helpers.delete_buffer(bufnr)
|
||||||
|
|
@ -251,7 +251,7 @@ describe('compiler', function()
|
||||||
return { bufnr = b, file = '/tmp/preview_test_watch_stop.txt', root = '/tmp', ft = 'text' }
|
return { bufnr = b, file = '/tmp/preview_test_watch_stop.txt', root = '/tmp', ft = 'text' }
|
||||||
end
|
end
|
||||||
|
|
||||||
compiler.watch(bufnr, 'echo', provider, ctx_builder)
|
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
|
||||||
compiler.unwatch(bufnr)
|
compiler.unwatch(bufnr)
|
||||||
assert.is_true(stopped)
|
assert.is_true(stopped)
|
||||||
assert.is_nil(compiler._test.watching[bufnr])
|
assert.is_nil(compiler._test.watching[bufnr])
|
||||||
|
|
@ -273,7 +273,7 @@ describe('compiler', function()
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
compiler.watch(bufnr, 'echo', provider, ctx_builder)
|
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
|
||||||
assert.is_not_nil(compiler._test.watching[bufnr])
|
assert.is_not_nil(compiler._test.watching[bufnr])
|
||||||
|
|
||||||
compiler.stop_all()
|
compiler.stop_all()
|
||||||
|
|
@ -294,7 +294,7 @@ describe('compiler', function()
|
||||||
return { bufnr = b, file = '/tmp/preview_test_watch_status.txt', root = '/tmp', ft = 'text' }
|
return { bufnr = b, file = '/tmp/preview_test_watch_status.txt', root = '/tmp', ft = 'text' }
|
||||||
end
|
end
|
||||||
|
|
||||||
compiler.watch(bufnr, 'echo', provider, ctx_builder)
|
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
|
||||||
s = compiler.status(bufnr)
|
s = compiler.status(bufnr)
|
||||||
assert.is_true(s.watching)
|
assert.is_true(s.watching)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,8 +20,11 @@ function M.delete_buffer(bufnr)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.reset_config(opts)
|
function M.reset_config(opts)
|
||||||
vim.g.preview = opts
|
local preview = require('preview')
|
||||||
require('preview')._test.reset()
|
preview._test.reset()
|
||||||
|
if opts then
|
||||||
|
preview.setup(opts)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
|
|
@ -9,13 +9,7 @@ describe('preview', function()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('config', function()
|
describe('config', function()
|
||||||
it('accepts nil config', function()
|
it('returns defaults before setup is called', function()
|
||||||
assert.has_no.errors(function()
|
|
||||||
preview.get_config()
|
|
||||||
end)
|
|
||||||
end)
|
|
||||||
|
|
||||||
it('applies default values', function()
|
|
||||||
local config = preview.get_config()
|
local config = preview.get_config()
|
||||||
assert.is_false(config.debug)
|
assert.is_false(config.debug)
|
||||||
assert.are.same({}, config.providers)
|
assert.are.same({}, config.providers)
|
||||||
|
|
@ -28,26 +22,44 @@ describe('preview', function()
|
||||||
assert.are.same({}, config.providers)
|
assert.are.same({}, config.providers)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('accepts full provider config', function()
|
it('accepts full provider config via hash entry', function()
|
||||||
helpers.reset_config({
|
helpers.reset_config({
|
||||||
providers = {
|
typst = {
|
||||||
typst = {
|
cmd = { 'typst', 'compile' },
|
||||||
cmd = { 'typst', 'compile' },
|
args = { '%s' },
|
||||||
args = { '%s' },
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
local config = require('preview').get_config()
|
local config = require('preview').get_config()
|
||||||
assert.is_not_nil(config.providers.typst)
|
assert.is_not_nil(config.providers.typst)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('resolves array preset names to provider configs', function()
|
||||||
|
helpers.reset_config({ 'typst', 'markdown' })
|
||||||
|
local config = require('preview').get_config()
|
||||||
|
local presets = require('preview.presets')
|
||||||
|
assert.are.same(presets.typst, config.providers.typst)
|
||||||
|
assert.are.same(presets.markdown, config.providers.markdown)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('resolves latex preset under tex filetype', function()
|
||||||
|
helpers.reset_config({ 'latex' })
|
||||||
|
local config = require('preview').get_config()
|
||||||
|
local presets = require('preview.presets')
|
||||||
|
assert.are.same(presets.latex, config.providers.tex)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('resolves github preset under markdown filetype', function()
|
||||||
|
helpers.reset_config({ 'github' })
|
||||||
|
local config = require('preview').get_config()
|
||||||
|
local presets = require('preview.presets')
|
||||||
|
assert.are.same(presets.github, config.providers.markdown)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('resolve_provider', function()
|
describe('resolve_provider', function()
|
||||||
before_each(function()
|
before_each(function()
|
||||||
helpers.reset_config({
|
helpers.reset_config({
|
||||||
providers = {
|
typst = { cmd = { 'typst', 'compile' } },
|
||||||
typst = { cmd = { 'typst', 'compile' } },
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
preview = require('preview')
|
preview = require('preview')
|
||||||
end)
|
end)
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,10 @@ describe('presets', function()
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('typst', function()
|
describe('typst', function()
|
||||||
|
it('has ft', function()
|
||||||
|
assert.are.equal('typst', presets.typst.ft)
|
||||||
|
end)
|
||||||
|
|
||||||
it('has cmd', function()
|
it('has cmd', function()
|
||||||
assert.are.same({ 'typst', 'compile' }, presets.typst.cmd)
|
assert.are.same({ 'typst', 'compile' }, presets.typst.cmd)
|
||||||
end)
|
end)
|
||||||
|
|
@ -28,6 +32,10 @@ describe('presets', function()
|
||||||
assert.is_string(output)
|
assert.is_string(output)
|
||||||
assert.are.equal('/tmp/document.pdf', output)
|
assert.are.equal('/tmp/document.pdf', output)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('has open enabled', function()
|
||||||
|
assert.are.same({ 'xdg-open' }, presets.typst.open)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('latex', function()
|
describe('latex', function()
|
||||||
|
|
@ -38,6 +46,10 @@ describe('presets', function()
|
||||||
ft = 'tex',
|
ft = 'tex',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
it('has ft', function()
|
||||||
|
assert.are.equal('tex', presets.latex.ft)
|
||||||
|
end)
|
||||||
|
|
||||||
it('has cmd', function()
|
it('has cmd', function()
|
||||||
assert.are.same({ 'latexmk' }, presets.latex.cmd)
|
assert.are.same({ 'latexmk' }, presets.latex.cmd)
|
||||||
end)
|
end)
|
||||||
|
|
@ -59,6 +71,10 @@ describe('presets', function()
|
||||||
assert.is_table(clean)
|
assert.is_table(clean)
|
||||||
assert.are.same({ 'latexmk', '-c', '/tmp/document.tex' }, clean)
|
assert.are.same({ 'latexmk', '-c', '/tmp/document.tex' }, clean)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('has open enabled', function()
|
||||||
|
assert.are.same({ 'xdg-open' }, presets.latex.open)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('markdown', function()
|
describe('markdown', function()
|
||||||
|
|
@ -69,20 +85,84 @@ describe('presets', function()
|
||||||
ft = 'markdown',
|
ft = 'markdown',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
it('has ft', function()
|
||||||
|
assert.are.equal('markdown', presets.markdown.ft)
|
||||||
|
end)
|
||||||
|
|
||||||
it('has cmd', function()
|
it('has cmd', function()
|
||||||
assert.are.same({ 'pandoc' }, presets.markdown.cmd)
|
assert.are.same({ 'pandoc' }, presets.markdown.cmd)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('returns args with file and output flag', function()
|
it('returns args with standalone and embed-resources flags', function()
|
||||||
local args = presets.markdown.args(md_ctx)
|
local args = presets.markdown.args(md_ctx)
|
||||||
assert.is_table(args)
|
assert.is_table(args)
|
||||||
assert.are.same({ '/tmp/document.md', '-o', '/tmp/document.pdf' }, args)
|
assert.are.same(
|
||||||
|
{ '/tmp/document.md', '-s', '--embed-resources', '-o', '/tmp/document.html' },
|
||||||
|
args
|
||||||
|
)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('returns pdf output path', function()
|
it('returns html output path', function()
|
||||||
local output = presets.markdown.output(md_ctx)
|
local output = presets.markdown.output(md_ctx)
|
||||||
assert.is_string(output)
|
assert.is_string(output)
|
||||||
assert.are.equal('/tmp/document.pdf', output)
|
assert.are.equal('/tmp/document.html', output)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('returns clean command', function()
|
||||||
|
local clean = presets.markdown.clean(md_ctx)
|
||||||
|
assert.is_table(clean)
|
||||||
|
assert.are.same({ 'rm', '-f', '/tmp/document.html' }, clean)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('has open enabled', function()
|
||||||
|
assert.are.same({ 'xdg-open' }, presets.markdown.open)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe('github', function()
|
||||||
|
local md_ctx = {
|
||||||
|
bufnr = 1,
|
||||||
|
file = '/tmp/document.md',
|
||||||
|
root = '/tmp',
|
||||||
|
ft = 'markdown',
|
||||||
|
}
|
||||||
|
|
||||||
|
it('has ft', function()
|
||||||
|
assert.are.equal('markdown', presets.github.ft)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('has cmd', function()
|
||||||
|
assert.are.same({ 'pandoc' }, presets.github.cmd)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('returns args with standalone, embed-resources, and css flags', function()
|
||||||
|
local args = presets.github.args(md_ctx)
|
||||||
|
assert.is_table(args)
|
||||||
|
assert.are.same({
|
||||||
|
'/tmp/document.md',
|
||||||
|
'-s',
|
||||||
|
'--embed-resources',
|
||||||
|
'--css',
|
||||||
|
'https://cdn.jsdelivr.net/gh/pixelbrackets/gfm-stylesheet@master/github.css',
|
||||||
|
'-o',
|
||||||
|
'/tmp/document.html',
|
||||||
|
}, args)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('returns html output path', function()
|
||||||
|
local output = presets.github.output(md_ctx)
|
||||||
|
assert.is_string(output)
|
||||||
|
assert.are.equal('/tmp/document.html', output)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('returns clean command', function()
|
||||||
|
local clean = presets.github.clean(md_ctx)
|
||||||
|
assert.is_table(clean)
|
||||||
|
assert.are.same({ 'rm', '-f', '/tmp/document.html' }, clean)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('has open enabled', function()
|
||||||
|
assert.are.same({ 'xdg-open' }, presets.github.open)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue