feat(presets): add asciidoctor preset
Adds an asciidoctor preset for AsciiDoc → HTML compilation with SSE live-reload. Includes a parse_asciidoctor error parser handling the "asciidoctor: SEVERITY: file: line N: message" format for both ERROR and WARNING diagnostics.
This commit is contained in:
parent
3a3a0783e8
commit
2a9110865b
3 changed files with 115 additions and 0 deletions
|
|
@ -161,6 +161,7 @@ Import them from `preview.presets`:
|
||||||
`presets.tectonic` tectonic → PDF (Rust-based LaTeX engine)
|
`presets.tectonic` tectonic → PDF (Rust-based LaTeX engine)
|
||||||
`presets.markdown` pandoc → HTML (standalone, embedded)
|
`presets.markdown` pandoc → HTML (standalone, embedded)
|
||||||
`presets.github` pandoc → HTML (GitHub-styled, `-f gfm` input)
|
`presets.github` pandoc → HTML (GitHub-styled, `-f gfm` input)
|
||||||
|
`presets.asciidoctor` asciidoctor → HTML (AsciiDoc with SSE reload)
|
||||||
|
|
||||||
Enable presets with `preset_name = true`:
|
Enable presets with `preset_name = true`:
|
||||||
>lua
|
>lua
|
||||||
|
|
|
||||||
|
|
@ -93,6 +93,29 @@ local function parse_pandoc(output)
|
||||||
return diagnostics
|
return diagnostics
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param output string
|
||||||
|
---@return preview.Diagnostic[]
|
||||||
|
local function parse_asciidoctor(output)
|
||||||
|
local diagnostics = {}
|
||||||
|
for line in output:gmatch('[^\r\n]+') do
|
||||||
|
local severity, _, lnum, msg =
|
||||||
|
line:match('^asciidoctor: (%u+): (.+): line (%d+): (.+)$')
|
||||||
|
if lnum then
|
||||||
|
local sev = vim.diagnostic.severity.ERROR
|
||||||
|
if severity == 'WARNING' then
|
||||||
|
sev = vim.diagnostic.severity.WARN
|
||||||
|
end
|
||||||
|
table.insert(diagnostics, {
|
||||||
|
lnum = tonumber(lnum) - 1,
|
||||||
|
col = 0,
|
||||||
|
message = msg,
|
||||||
|
severity = sev,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return diagnostics
|
||||||
|
end
|
||||||
|
|
||||||
---@type preview.ProviderConfig
|
---@type preview.ProviderConfig
|
||||||
M.typst = {
|
M.typst = {
|
||||||
ft = 'typst',
|
ft = 'typst',
|
||||||
|
|
@ -219,4 +242,24 @@ M.github = {
|
||||||
reload = true,
|
reload = true,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
---@type preview.ProviderConfig
|
||||||
|
M.asciidoctor = {
|
||||||
|
ft = 'asciidoc',
|
||||||
|
cmd = { 'asciidoctor' },
|
||||||
|
args = function(ctx)
|
||||||
|
return { ctx.file, '-o', ctx.output }
|
||||||
|
end,
|
||||||
|
output = function(ctx)
|
||||||
|
return (ctx.file:gsub('%.adoc$', '.html'))
|
||||||
|
end,
|
||||||
|
error_parser = function(output)
|
||||||
|
return parse_asciidoctor(output)
|
||||||
|
end,
|
||||||
|
clean = function(ctx)
|
||||||
|
return { 'rm', '-f', (ctx.file:gsub('%.adoc$', '.html')) }
|
||||||
|
end,
|
||||||
|
open = true,
|
||||||
|
reload = true,
|
||||||
|
}
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
|
|
@ -447,4 +447,75 @@ describe('presets', function()
|
||||||
assert.are.same({}, diagnostics)
|
assert.are.same({}, diagnostics)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe('asciidoctor', function()
|
||||||
|
local adoc_ctx = {
|
||||||
|
bufnr = 1,
|
||||||
|
file = '/tmp/document.adoc',
|
||||||
|
root = '/tmp',
|
||||||
|
ft = 'asciidoc',
|
||||||
|
output = '/tmp/document.html',
|
||||||
|
}
|
||||||
|
|
||||||
|
it('has ft', function()
|
||||||
|
assert.are.equal('asciidoc', presets.asciidoctor.ft)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('has cmd', function()
|
||||||
|
assert.are.same({ 'asciidoctor' }, presets.asciidoctor.cmd)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('returns args with file and output', function()
|
||||||
|
assert.are.same(
|
||||||
|
{ '/tmp/document.adoc', '-o', '/tmp/document.html' },
|
||||||
|
presets.asciidoctor.args(adoc_ctx)
|
||||||
|
)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('returns html output path', function()
|
||||||
|
assert.are.equal('/tmp/document.html', presets.asciidoctor.output(adoc_ctx))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('returns clean command', function()
|
||||||
|
assert.are.same(
|
||||||
|
{ 'rm', '-f', '/tmp/document.html' },
|
||||||
|
presets.asciidoctor.clean(adoc_ctx)
|
||||||
|
)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('has open enabled', function()
|
||||||
|
assert.is_true(presets.asciidoctor.open)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('has reload enabled for SSE', function()
|
||||||
|
assert.is_true(presets.asciidoctor.reload)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('parses error messages', function()
|
||||||
|
local output =
|
||||||
|
'asciidoctor: ERROR: document.adoc: line 8: invalid part, must have at least one section'
|
||||||
|
local diagnostics = presets.asciidoctor.error_parser(output, adoc_ctx)
|
||||||
|
assert.are.equal(1, #diagnostics)
|
||||||
|
assert.are.equal(7, diagnostics[1].lnum)
|
||||||
|
assert.are.equal(0, diagnostics[1].col)
|
||||||
|
assert.are.equal(
|
||||||
|
'invalid part, must have at least one section',
|
||||||
|
diagnostics[1].message
|
||||||
|
)
|
||||||
|
assert.are.equal(vim.diagnostic.severity.ERROR, diagnostics[1].severity)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('parses warning messages', function()
|
||||||
|
local output =
|
||||||
|
'asciidoctor: WARNING: document.adoc: line 52: section title out of sequence'
|
||||||
|
local diagnostics = presets.asciidoctor.error_parser(output, adoc_ctx)
|
||||||
|
assert.are.equal(1, #diagnostics)
|
||||||
|
assert.are.equal(51, diagnostics[1].lnum)
|
||||||
|
assert.are.equal(vim.diagnostic.severity.WARN, diagnostics[1].severity)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('returns empty table for clean output', function()
|
||||||
|
assert.are.same({}, presets.asciidoctor.error_parser('', adoc_ctx))
|
||||||
|
end)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue