2022-12-15 13:08:04 +00:00
|
|
|
local Util = require("lazy.util")
|
2022-12-14 23:22:40 +00:00
|
|
|
|
2022-12-15 13:08:04 +00:00
|
|
|
local M = {}
|
2022-12-14 23:22:40 +00:00
|
|
|
|
|
|
|
function M.indent(str, indent)
|
|
|
|
local lines = vim.split(str, "\n")
|
|
|
|
for l, line in ipairs(lines) do
|
|
|
|
lines[l] = (" "):rep(indent) .. line
|
|
|
|
end
|
|
|
|
return table.concat(lines, "\n")
|
|
|
|
end
|
|
|
|
|
2022-12-15 07:46:25 +00:00
|
|
|
function M.toc(md)
|
|
|
|
local toc = {}
|
|
|
|
local lines = vim.split(md, "\n")
|
|
|
|
local toc_found = false
|
|
|
|
for _, line in ipairs(lines) do
|
|
|
|
local hash, title = line:match("^(#+)%s*(.*)")
|
|
|
|
if hash then
|
|
|
|
if toc_found then
|
|
|
|
local anchor = string.gsub(title:lower(), "[^\32-\126]", "")
|
|
|
|
anchor = string.gsub(anchor, " ", "-")
|
|
|
|
toc[#toc + 1] = string.rep(" ", #hash - 1) .. "- [" .. title .. "](#" .. anchor .. ")"
|
|
|
|
end
|
|
|
|
if title:find("Table of Contents") then
|
|
|
|
toc_found = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return M.fix_indent(table.concat(toc, "\n"))
|
|
|
|
end
|
|
|
|
|
2022-12-14 23:22:40 +00:00
|
|
|
---@param str string
|
|
|
|
function M.fix_indent(str)
|
|
|
|
local lines = vim.split(str, "\n")
|
|
|
|
|
|
|
|
local width = 120
|
|
|
|
for _, line in ipairs(lines) do
|
|
|
|
width = math.min(width, #line:match("^%s*"))
|
|
|
|
end
|
|
|
|
|
|
|
|
for l, line in ipairs(lines) do
|
|
|
|
lines[l] = line:sub(width)
|
|
|
|
end
|
|
|
|
return table.concat(lines, "\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param contents table<string, string>
|
|
|
|
function M.save(contents)
|
2022-12-15 13:08:04 +00:00
|
|
|
local readme = Util.read_file("README.md")
|
2022-12-17 08:59:29 +00:00
|
|
|
-- contents.toc = M.toc(readme)
|
2022-12-14 23:22:40 +00:00
|
|
|
for tag, content in pairs(contents) do
|
|
|
|
content = M.fix_indent(content)
|
|
|
|
content = content:gsub("%%", "%%%%")
|
|
|
|
content = vim.trim(content)
|
2022-12-15 07:46:25 +00:00
|
|
|
local pattern = "(<%!%-%- " .. tag .. ":start %-%->).*(<%!%-%- " .. tag .. ":end %-%->)"
|
2022-12-14 23:22:40 +00:00
|
|
|
if not readme:find(pattern) then
|
|
|
|
error("tag " .. tag .. " not found")
|
|
|
|
end
|
2022-12-18 10:42:54 +00:00
|
|
|
if tag == "toc" or tag == "commands" then
|
2022-12-15 07:46:25 +00:00
|
|
|
readme = readme:gsub(pattern, "%1\n\n" .. content .. "\n\n%2")
|
|
|
|
else
|
|
|
|
readme = readme:gsub(pattern, "%1\n\n```lua\n" .. content .. "\n```\n\n%2")
|
|
|
|
end
|
2022-12-14 23:22:40 +00:00
|
|
|
end
|
|
|
|
|
2022-12-15 13:08:04 +00:00
|
|
|
Util.write_file("README.md", readme)
|
2022-12-14 23:22:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@return string
|
|
|
|
function M.extract(file, pattern)
|
2022-12-15 13:08:04 +00:00
|
|
|
local init = Util.read_file(file)
|
2022-12-14 23:22:40 +00:00
|
|
|
return assert(init:match(pattern))
|
|
|
|
end
|
|
|
|
|
2022-12-18 10:42:54 +00:00
|
|
|
function M.commands()
|
|
|
|
local commands = require("lazy.view.commands").commands
|
|
|
|
local modes = require("lazy.view").modes
|
|
|
|
local lines = {
|
|
|
|
{ "Command", "Lua", "Key Mapping", "Description" },
|
|
|
|
{ "---", "---", "---", "---" },
|
|
|
|
}
|
|
|
|
for _, mode in ipairs(modes) do
|
|
|
|
if not mode.plugin and commands[mode.name] then
|
|
|
|
lines[#lines + 1] = {
|
2022-12-19 13:19:10 +00:00
|
|
|
("`:Lazy %s`"):format(mode.name),
|
2022-12-18 10:42:54 +00:00
|
|
|
([[`require("lazy").%s()`]]):format(mode.name),
|
|
|
|
mode.key and ("`<%s>`"):format(mode.key) or "",
|
|
|
|
mode.desc,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local ret = {}
|
|
|
|
for _, line in ipairs(lines) do
|
|
|
|
ret[#ret + 1] = "| " .. table.concat(line, " | ") .. " |"
|
|
|
|
end
|
|
|
|
return table.concat(ret, "\n")
|
|
|
|
end
|
|
|
|
|
2022-12-14 23:22:40 +00:00
|
|
|
function M.update()
|
|
|
|
local cache_config = M.extract("lua/lazy/core/cache.lua", "\nM%.config = ({.-\n})")
|
|
|
|
local config = M.extract("lua/lazy/core/config.lua", "\nM%.defaults = ({.-\n})")
|
|
|
|
config = config:gsub(
|
|
|
|
"\n%s*%-%-%-@type LazyCacheConfig.*cache = nil,",
|
|
|
|
"\n" .. M.indent("cache = " .. cache_config .. ",", 4)
|
|
|
|
)
|
|
|
|
config = config:gsub("%s*debug = false.\n", "\n")
|
|
|
|
M.save({
|
|
|
|
bootstrap = M.extract("lua/lazy/init.lua", "function M%.bootstrap%(%)\n(.-)\nend"),
|
|
|
|
config = config,
|
2022-12-15 13:08:04 +00:00
|
|
|
spec = Util.read_file("lua/lazy/example.lua"),
|
2022-12-18 10:42:54 +00:00
|
|
|
commands = M.commands(),
|
2022-12-14 23:22:40 +00:00
|
|
|
})
|
|
|
|
vim.cmd.checktime()
|
|
|
|
end
|
|
|
|
|
|
|
|
M.update()
|
|
|
|
|
|
|
|
return M
|