Skip to content
forked from neovim/neovim

Commit

Permalink
fix(gen_help_html): handle delimiter, heading
Browse files Browse the repository at this point in the history
Problem:
vimdoc grammar added new forms that are not handled in our HTML
generator. neovim/tree-sitter-vimdoc#134

Solution:
Update `gen_help_html.lua`.

Fixes neovim#29277
  • Loading branch information
justinmk committed Jun 19, 2024
1 parent fe5d127 commit c63e7d9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
38 changes: 19 additions & 19 deletions runtime/doc/usr_21.txt
Original file line number Diff line number Diff line change
Expand Up @@ -303,21 +303,21 @@ use, and save this in a session. Then you can go back to this layout whenever
you want.
For example, this is a nice layout to use:
>
+----------------------------------------+
| VIM - main help file |
| |
|Move around: Use the cursor keys, or "h|
|help.txt================================|
|explorer | |
|dir |~ |
|dir |~ |
|file |~ |
|file |~ |
|file |~ |
|file |~ |
|~/=========|[No File]===================|
| |
+----------------------------------------+
+----------------------------------------+
| VIM - main help file |
| |
|Move around: Use the cursor keys, or "h|
|help.txt================================|
|explorer | |
|dir |~ |
|dir |~ |
|file |~ |
|file |~ |
|file |~ |
|file |~ |
|~/=========|[No File]===================|
| |
+----------------------------------------+
<
This has a help window at the top, so that you can read this text. The narrow
vertical window on the left contains a file explorer. This is a Vim plugin
Expand Down Expand Up @@ -448,9 +448,9 @@ trust the files you are editing: >
:set nomodeline
Use this format for the modeline:
Use this format for the modeline: >
any-text vim:set {option}={value} ... : any-text ~
any-text vim:set {option}={value} ... : any-text
The "any-text" indicates that you can put any text before and after the part
that Vim will use. This allows making it look like a comment, like what was
Expand All @@ -462,9 +462,9 @@ using something like "gvim:" will not work.
typing the ":set" command, except that you need to insert a backslash before a
colon (otherwise it would be seen as the end of the modeline).

Another example:
Another example: >
// vim:set textwidth=72 dir=c\:\tmp: use c:\tmp here ~
// vim:set textwidth=72 dir=c\:\tmp: use c:\tmp here
There is an extra backslash before the first colon, so that it's included in
the ":set" command. The text after the second colon is ignored, thus a remark
Expand Down
21 changes: 14 additions & 7 deletions scripts/gen_help_html.lua
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,6 @@ local function visit_node(root, level, lang_tree, headings, opt, stats)
or nil
-- Parent kind (string).
local parent = root:parent() and root:parent():type() or nil
local text = ''
-- Gets leading whitespace of `node`.
local function ws(node)
node = node or root
Expand All @@ -508,6 +507,7 @@ local function visit_node(root, level, lang_tree, headings, opt, stats)
return string.format('%s%s', ws_, vim.treesitter.get_node_text(node, opt.buf))
end

local text = ''
local trimmed ---@type string
if root:named_child_count() == 0 or node_name == 'ERROR' then
text = node_text()
Expand Down Expand Up @@ -537,12 +537,17 @@ local function visit_node(root, level, lang_tree, headings, opt, stats)
if is_noise(text, stats.noise_lines) then
return '' -- Discard common "noise" lines.
end
-- Remove "===" and tags from ToC text.
local hname = (node_text():gsub('%-%-%-%-+', ''):gsub('%=%=%=%=+', ''):gsub('%*.*%*', ''))
-- Remove tags from ToC text.
local heading_node = first(root, 'heading')
local hname = trim(node_text(heading_node):gsub('%*.*%*', ''))
if not heading_node or hname == '' then
return '' -- Spurious "===" or "---" in the help doc.
end

-- Use the first *tag* node as the heading anchor, if any.
local tagnode = first(root, 'tag')
local tagnode = first(heading_node, 'tag')
-- Use the *tag* as the heading anchor id, if possible.
local tagname = tagnode and url_encode(node_text(tagnode:child(1), false))
local tagname = tagnode and url_encode(trim(node_text(tagnode:child(1), false)))
or to_heading_tag(hname)
if node_name == 'h1' or #headings == 0 then
---@type nvim.gen_help_html.heading
Expand All @@ -555,7 +560,9 @@ local function visit_node(root, level, lang_tree, headings, opt, stats)
)
end
local el = node_name == 'h1' and 'h2' or 'h3'
return ('<%s id="%s" class="help-heading">%s</%s>\n'):format(el, tagname, text, el)
return ('<%s id="%s" class="help-heading">%s</%s>\n'):format(el, tagname, trimmed, el)
elseif node_name == 'heading' then
return trimmed
elseif node_name == 'column_heading' or node_name == 'column_name' then
if root:has_error() then
return text
Expand Down Expand Up @@ -695,7 +702,7 @@ local function visit_node(root, level, lang_tree, headings, opt, stats)
return string.format('%s</span>', s)
end
return s
elseif node_name == 'modeline' then
elseif node_name == 'delimiter' or node_name == 'modeline' then
return ''
elseif node_name == 'ERROR' then
if ignore_parse_error(opt.fname, trimmed) then
Expand Down

0 comments on commit c63e7d9

Please sign in to comment.