2022-12-15 13:07:37 +00:00
|
|
|
local Config = require("lazy.core.config")
|
|
|
|
local Util = require("lazy.util")
|
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
|
|
|
function M.index(plugin)
|
|
|
|
if Config.options.readme.skip_if_doc_exists and vim.loop.fs_stat(plugin.dir .. "/doc") then
|
|
|
|
return {}
|
|
|
|
end
|
2023-01-01 19:23:41 +00:00
|
|
|
local files = vim.tbl_flatten(vim.tbl_map(function(file)
|
|
|
|
return vim.fn.expand(plugin.dir .. "/" .. file, false, true)
|
|
|
|
end, Config.options.readme.files))
|
2022-12-21 17:23:27 +00:00
|
|
|
---@type table<string,{file:string, tag:string, line:string}>
|
2022-12-15 13:07:37 +00:00
|
|
|
local tags = {}
|
2023-01-01 19:23:41 +00:00
|
|
|
for _, file in ipairs(files) do
|
|
|
|
file = Util.norm(file)
|
2022-12-15 13:07:37 +00:00
|
|
|
if vim.loop.fs_stat(file) then
|
2023-01-01 19:23:41 +00:00
|
|
|
local rel_file = file:sub(#plugin.dir + 1)
|
|
|
|
local tag_filename = string.gsub(plugin.name .. vim.fn.fnamemodify(rel_file, ":h:gs?/?-?"), "-$", "")
|
2022-12-15 13:07:37 +00:00
|
|
|
local lines = vim.split(Util.read_file(file), "\n")
|
|
|
|
for _, line in ipairs(lines) do
|
|
|
|
local title = line:match("^#+%s*(.*)")
|
|
|
|
if title then
|
2023-01-01 19:23:41 +00:00
|
|
|
local tag = tag_filename .. "-" .. title:lower():gsub("%W+", "-")
|
2022-12-15 13:07:37 +00:00
|
|
|
tag = tag:gsub("%-+", "-"):gsub("%-$", "")
|
2023-01-01 09:56:18 +00:00
|
|
|
line = line:gsub("([%[%]/])", "\\%1")
|
2023-01-01 19:23:41 +00:00
|
|
|
tags[tag] = { tag = tag, line = line, file = tag_filename .. ".md" }
|
2022-12-15 13:07:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
table.insert(lines, [[<!-- vim: set ft=markdown: -->]])
|
2023-01-01 19:23:41 +00:00
|
|
|
Util.write_file(Config.options.readme.root .. "/doc/" .. tag_filename .. ".md", table.concat(lines, "\n"))
|
2022-12-15 13:07:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return tags
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.update()
|
2022-12-23 12:13:28 +00:00
|
|
|
vim.cmd.helptags(Config.plugins["lazy.nvim"].dir .. "/doc")
|
2022-12-15 13:07:37 +00:00
|
|
|
local docs = Config.options.readme.root .. "/doc"
|
|
|
|
vim.fn.mkdir(docs, "p")
|
|
|
|
|
|
|
|
Util.ls(docs, function(path, name, type)
|
|
|
|
if type == "file" and name:sub(-2) == "md" then
|
|
|
|
vim.loop.fs_unlink(path)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
---@type {file:string, tag:string, line:string}[]
|
|
|
|
local tags = {}
|
|
|
|
for _, plugin in pairs(Config.plugins) do
|
2022-12-21 17:23:27 +00:00
|
|
|
for key, tag in pairs(M.index(plugin)) do
|
|
|
|
tags[key] = tag
|
|
|
|
end
|
2022-12-15 13:07:37 +00:00
|
|
|
end
|
|
|
|
local lines = { [[!_TAG_FILE_ENCODING utf-8 //]] }
|
2022-12-21 17:23:27 +00:00
|
|
|
Util.foreach(tags, function(_, tag)
|
2022-12-15 13:07:37 +00:00
|
|
|
table.insert(lines, ("%s\t%s\t/%s"):format(tag.tag, tag.file, tag.line))
|
2022-12-21 17:23:27 +00:00
|
|
|
end)
|
2022-12-15 13:07:37 +00:00
|
|
|
Util.write_file(docs .. "/tags", table.concat(lines, "\n"))
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|