Merge #30105 fix(tohtml): quote font-family names

This commit is contained in:
Justin M. Keyes
2024-09-08 03:32:33 -07:00
committed by GitHub
2 changed files with 70 additions and 35 deletions

View File

@@ -1293,9 +1293,25 @@ local function opt_to_global_state(opt, title)
local fonts = {} local fonts = {}
if opt.font then if opt.font then
fonts = type(opt.font) == 'string' and { opt.font } or opt.font --[[@as (string[])]] fonts = type(opt.font) == 'string' and { opt.font } or opt.font --[[@as (string[])]]
elseif vim.o.guifont:match('^[^:]+') then for i, v in pairs(fonts) do
table.insert(fonts, vim.o.guifont:match('^[^:]+')) fonts[i] = ('"%s"'):format(v)
end end
elseif vim.o.guifont:match('^[^:]+') then
-- Example:
-- Input: "Font,Escape\,comma, Ignore space after comma"
-- Output: { "Font","Escape,comma","Ignore space after comma" }
local prev = ''
for name in vim.gsplit(vim.o.guifont:match('^[^:]+'), ',', { trimempty = true }) do
if vim.endswith(name, '\\') then
prev = prev .. vim.trim(name:sub(1, -2) .. ',')
elseif vim.trim(name) ~= '' then
table.insert(fonts, ('"%s%s"'):format(prev, vim.trim(name)))
prev = ''
end
end
end
-- Generic family names (monospace here) must not be quoted
-- because the browser recognizes them as font families.
table.insert(fonts, 'monospace') table.insert(fonts, 'monospace')
--- @type vim.tohtml.state.global --- @type vim.tohtml.state.global
local state = { local state = {

View File

@@ -136,27 +136,27 @@ local function run_tohtml_and_assert(screen, func)
screen:expect({ grid = expected.grid, attr_ids = expected.attr_ids }) screen:expect({ grid = expected.grid, attr_ids = expected.attr_ids })
end end
describe(':TOhtml', function() ---@param guifont boolean
--- @type test.functional.ui.screen local function test_generates_html(guifont, expect_font)
local screen
before_each(function()
clear({ args = { '--clean' } })
screen = Screen.new(80, 80)
screen:attach({ term_name = 'xterm' })
exec('colorscheme default')
end)
it('expected internal html generated', function()
insert([[line]]) insert([[line]])
exec('set termguicolors') exec('set termguicolors')
local bg = fn.synIDattr(fn.hlID('Normal'), 'bg#', 'gui') local bg = fn.synIDattr(fn.hlID('Normal'), 'bg#', 'gui')
local fg = fn.synIDattr(fn.hlID('Normal'), 'fg#', 'gui') local fg = fn.synIDattr(fn.hlID('Normal'), 'fg#', 'gui')
exec_lua [[ local tmpfile = t.tmpname()
local outfile = vim.fn.tempname() .. '.html'
local html = require('tohtml').tohtml(0,{title="title",font="dumyfont"}) exec_lua(
[[
local guifont, outfile = ...
local html = (guifont
and require('tohtml').tohtml(0,{title="title"})
or require('tohtml').tohtml(0,{title="title",font={ "dumyfont","anotherfont" }}))
vim.fn.writefile(html, outfile) vim.fn.writefile(html, outfile)
vim.cmd.split(outfile) vim.cmd.split(outfile)
]] ]],
guifont,
tmpfile
)
local out_file = api.nvim_buf_get_name(api.nvim_get_current_buf()) local out_file = api.nvim_buf_get_name(api.nvim_get_current_buf())
eq({ eq({
'<!DOCTYPE html>', '<!DOCTYPE html>',
@@ -166,7 +166,7 @@ describe(':TOhtml', function()
'<title>title</title>', '<title>title</title>',
('<meta name="colorscheme" content="%s"></meta>'):format(api.nvim_get_var('colors_name')), ('<meta name="colorscheme" content="%s"></meta>'):format(api.nvim_get_var('colors_name')),
'<style>', '<style>',
'* {font-family: dumyfont,monospace}', ('* {font-family: %s,monospace}'):format(expect_font),
('body {background-color: %s; color: %s}'):format(bg, fg), ('body {background-color: %s; color: %s}'):format(bg, fg),
'</style>', '</style>',
'</head>', '</head>',
@@ -178,9 +178,28 @@ describe(':TOhtml', function()
'</body>', '</body>',
'</html>', '</html>',
}, fn.readfile(out_file)) }, fn.readfile(out_file))
end
describe(':TOhtml', function()
--- @type test.functional.ui.screen
local screen
before_each(function()
clear({ args = { '--clean' } })
screen = Screen.new(80, 80)
screen:attach({ term_name = 'xterm' })
exec('colorscheme default')
end) end)
it('expected internal html generated from range', function() it('generates html with given font', function()
test_generates_html(false, '"dumyfont","anotherfont"')
end)
it("generates html, respects 'guifont'", function()
exec_lua [[vim.o.guifont='Font,Escape\\,comma, Ignore space after comma']]
test_generates_html(true, '"Font","Escape,comma","Ignore space after comma"')
end)
it('generates html from range', function()
insert([[ insert([[
line1 line1
line2 line2
@@ -218,7 +237,7 @@ describe(':TOhtml', function()
}, fn.readfile(out_file)) }, fn.readfile(out_file))
end) end)
it('highlight attributes generated', function() it('generates highlight attributes', function()
--Make sure to uncomment the attribute in `html_syntax_match()` --Make sure to uncomment the attribute in `html_syntax_match()`
exec('hi LINE guisp=#00ff00 gui=' .. table.concat({ exec('hi LINE guisp=#00ff00 gui=' .. table.concat({
'bold', 'bold',