fix(docs-html): misaligned tabs after conceal #20690

Problem:
`gen_help_html.lua` does not properly handle tab characters after
"concealed" text (tags, taglinks, codespans). This causes misaligned
layout in "old" (preformatted) docs.

For text like `*tag*`, |tag_link|, and `code_span`, Vim hides the "*",
"|", "`" characters, but Vim still counts those characters for "virtual
column" when a tab character follows it. So if you have a tag of say
6 characters long, those two concealed character would lead to the tab
character after it start at column 8. gen_help_html.lua doesn't account
for that which leads to formatting flaws in the generated output.

Solution:
Add two spaces after concealed nodes that are followed by a tab char.
This commit is contained in:
Yee Cheng Chin 2022-10-20 03:22:46 -07:00 committed by GitHub
parent 3b0777cfa5
commit 10ab7489eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -390,6 +390,18 @@ local function visit_validate(root, level, lang_tree, opt, stats)
end
end
-- Fix tab alignment issues caused by concealed characters like |, `, * in tags
-- and code blocks.
local function fix_tab_after_conceal(text, next_node_text)
-- Vim tabs take into account the two concealed characters even though they
-- are invisible, so we need to add back in the two spaces if this is
-- followed by a tab to make the tab alignment to match Vim's behavior.
if string.sub(next_node_text,1,1) == '\t' then
text = text .. ' '
end
return text
end
-- Generates HTML from node `root` recursively.
local function visit_node(root, level, lang_tree, headings, opt, stats)
level = level or 0
@ -506,12 +518,20 @@ local function visit_node(root, level, lang_tree, headings, opt, stats)
if ignored then
return text
end
return ('%s<a href="%s#%s">%s</a>'):format(ws(), helppage, url_encode(tagname), html_esc(tagname))
local s = ('%s<a href="%s#%s">%s</a>'):format(ws(), helppage, url_encode(tagname), html_esc(tagname))
if node_name == 'taglink' and opt.old then
s = fix_tab_after_conceal(s, node_text(root:next_sibling()))
end
return s
elseif vim.tbl_contains({'codespan', 'keycode'}, node_name) then
if root:has_error() then
return text
end
return ('%s<code>%s</code>'):format(ws(), trimmed)
local s = ('%s<code>%s</code>'):format(ws(), trimmed)
if node_name == 'codespan' and opt.old then
s = fix_tab_after_conceal(s, node_text(root:next_sibling()))
end
return s
elseif node_name == 'argument' then
return ('%s<code>{%s}</code>'):format(ws(), text)
elseif node_name == 'codeblock' then
@ -533,6 +553,10 @@ local function visit_node(root, level, lang_tree, headings, opt, stats)
end
local el = in_heading and 'span' or 'code'
local s = ('%s<a name="%s"></a><%s class="%s">%s</%s>'):format(ws(), url_encode(tagname), el, cssclass, trimmed, el)
if opt.old then
s = fix_tab_after_conceal(s, node_text(root:next_sibling()))
end
if in_heading and prev ~= 'tag' then
-- Start the <span> container for tags in a heading.
-- This makes "justify-content:space-between" right-align the tags.