gen_helptags.lua (2217B)
1 -- Does the same as `nvim -c "helptags ++t doc" -c quit` 2 -- without needing to run a "nvim" binary, which is needed for cross-compiling. 3 local out = arg[1] 4 local dir = arg[2] 5 6 local dirfd = vim.uv.fs_opendir(dir, nil, 1) 7 local files = {} 8 while true do 9 local file = dirfd:readdir() 10 if file == nil then 11 break 12 end 13 if file[1].type == 'file' and vim.endswith(file[1].name, '.txt') then 14 table.insert(files, file[1].name) 15 end 16 end 17 18 local tags = {} 19 for _, fn in ipairs(files) do 20 local in_example = false 21 for line in io.lines(dir .. '/' .. fn) do 22 if in_example then 23 local first = string.sub(line, 1, 1) 24 if first ~= ' ' and first ~= '\t' and first ~= '' then 25 in_example = false 26 end 27 end 28 local chunks = vim.split(line, '*', { plain = true }) 29 local next_valid = false 30 local n_chunks = #chunks 31 for i, chunk in ipairs(chunks) do 32 if next_valid and not in_example then 33 if #chunk > 0 and string.find(chunk, '[ \t|]') == nil then 34 local next = string.sub(chunks[i + 1], 1, 1) 35 if next == ' ' or next == '\t' or (i == n_chunks - 1 and next == '') then 36 table.insert(tags, { chunk, fn }) 37 end 38 end 39 end 40 41 if i == n_chunks - 1 then 42 break 43 end 44 next_valid = false 45 local lastend = string.sub(chunk, -1) -- "" for empty string 46 if lastend == ' ' or lastend == '\t' or (i == 1 and lastend == '') then 47 next_valid = true 48 end 49 end 50 51 if line:find('^>[a-z0-9]*$') or line:find(' >[a-z0-9]*$') then 52 in_example = true 53 end 54 end 55 end 56 57 table.insert(tags, { 'help-tags', 'tags' }) 58 table.sort(tags, function(a, b) 59 return a[1] < b[1] 60 end) 61 62 local f = io.open(out, 'w') 63 local lasttagname, lastfn = nil 64 for _, tag in ipairs(tags) do 65 local tagname, fn = unpack(tag) 66 if tagname == lasttagname then 67 error('duplicate tags in ' .. fn .. (lastfn ~= fn and (' and ' .. lastfn) or '')) 68 end 69 lasttagname, lastfn = tagname, fn 70 71 if tagname == 'help-tags' then 72 f:write(tagname .. '\t' .. fn .. '\t1\n') 73 else 74 local escaped = string.gsub(tagname, '[\\/]', '\\%0') 75 f:write(tagname .. '\t' .. fn .. '\t/*' .. escaped .. '*\n') 76 end 77 end 78 f:close()