lazy.nvim/lua/build.lua

178 lines
4.3 KiB
Lua
Raw Normal View History

2024-06-19 06:59:45 +01:00
local Docs = require("lazy.docs")
local Util = require("lazy.util")
2024-06-18 22:17:53 +01:00
local M = {}
2024-06-19 06:59:45 +01:00
---@param path string
local function dir(path)
2024-06-23 09:09:41 +01:00
local plugin, extra = path:match("([^/]+)(.*)")
return require("lazy.core.config").plugins[plugin].dir .. extra
2024-06-19 06:59:45 +01:00
end
M.extract = {
2024-06-23 09:09:41 +01:00
["configuration/index"] = {
config = Docs.extract(dir("lazy.nvim/lua/lazy/core/config.lua"), "\nM%.defaults = ({.-\n})")
:gsub("%s*debug = false.\n", "\n"),
},
["configuration/highlights"] = {
colors = Docs.colors({
path = dir("lazy.nvim/lua/lazy/view/colors.lua"),
}),
},
["spec/examples"] = {
examples = Util.read_file(dir("lazy.nvim/lua/lazy/example.lua")),
},
["usage/index"] = {
stats = Docs.extract(dir("lazy.nvim/lua/lazy/stats.lua"), "\nM%._stats = ({.-\n})"),
commands = Docs.commands(),
},
2024-06-19 06:59:45 +01:00
}
2024-06-18 22:17:53 +01:00
local function exec(cmd)
2024-06-23 09:09:41 +01:00
return vim.system(vim.split(cmd, " "), { text = true }):wait()
2024-06-18 22:17:53 +01:00
end
function M.themes()
2024-06-23 09:09:41 +01:00
exec("rm -rf src/themes")
exec("mkdir -p src/themes")
exec("cp -r .nvim/plugins/tokyonight.nvim/extras/prism src/themes/prism")
end
function M.installation()
local install = Util.read_file("lua/tpl/install.lua")
local install_multi = install:gsub(
"spec = {}",
[[spec = {
-- import your plugins
{ import = "plugins" },
}]]
)
local install_single = install:gsub(
"spec = {}",
[[spec = {
-- add your plugins here
}]]
)
return {
install_single = {
content = install_single,
lang = 'lua title="~/.config/nvim/init.lua"',
},
install_multi = {
content = install_multi,
lang = 'lua title="~/.config/nvim/lua/config/lazy.lua"',
},
}
2024-06-18 22:17:53 +01:00
end
2024-06-19 06:59:45 +01:00
function M.docs()
2024-06-23 09:09:41 +01:00
M.extract.installation = M.installation()
for name, data in pairs(M.extract) do
local md = "docs/" .. name .. ".md"
if vim.uv.fs_stat(md .. "x") then
md = md .. "x"
end
print("Building " .. md)
Docs.save(data, md)
end
2024-06-19 06:59:45 +01:00
end
function M._old()
2024-06-23 09:09:41 +01:00
M.save({
stats = M.extract("lua/lazy/stats.lua", "\nM%._stats = ({.-\n})"),
commands = M.commands(),
})
2024-06-19 06:59:45 +01:00
end
2024-06-23 19:20:49 +01:00
---@param readme? string
---@param mds? string[]
2024-06-23 19:28:46 +01:00
---@param transform? fun(s:string):string
function M.readme(readme, mds, transform)
local is_sorted = mds ~= nil
2024-06-23 19:20:49 +01:00
readme = readme or "README.md"
mds = mds
or vim.fs.find(function(name, path)
return name:match(".*%.mdx?$")
end, { limit = math.huge, type = "file", path = "docs" })
2024-06-23 18:38:39 +01:00
local sorters = {
"intro",
"installation",
"spec",
"packages",
"configuration",
"usage",
"developers",
}
2024-06-23 19:28:46 +01:00
if not is_sorted then
table.sort(mds, function(a, b)
local aa = 0
local bb = 0
for i, name in ipairs(sorters) do
if a:match(name) then
aa = i
end
if b:match(name) then
bb = i
end
2024-06-23 18:38:39 +01:00
end
2024-06-23 19:28:46 +01:00
if aa == bb then
if a:match("index") then
return true
elseif b:match("index") then
return false
end
return a < b
2024-06-23 18:38:39 +01:00
end
2024-06-23 19:28:46 +01:00
return aa < bb
end)
end
2024-06-23 18:49:03 +01:00
local text = ""
2024-06-23 18:38:39 +01:00
for _, md in ipairs(mds) do
2024-06-23 19:02:04 +01:00
local _, level = md:gsub("/", "")
level = level - 1
if md:match("index") then
level = level - 1
end
2024-06-23 19:28:46 +01:00
level = math.max(0, level)
2024-06-23 18:49:03 +01:00
local t = Util.read_file(md) .. "\n\n"
-- remove frontmatter
t = t:gsub("^%-%-%-.-%-%-%-\n", "")
-- remove code block titles
t = t:gsub("```lua.-\n", "```lua\n")
-- remove markdown comments
t = t:gsub("<!--.-\n", "")
-- remove <Tabs>
t = t:gsub("</?Tabs>", "")
-- replace tab item with ## title
-- <TabItem value="multiple" label="Structured Setup">
t = t:gsub('[ \t]*<TabItem value="([^"]+)" label="([^"]+)">', "## %2")
t = t:gsub("</?TabItem>", "")
2024-06-23 19:20:49 +01:00
t = t:gsub("\nimport .-\n", "\n")
t = t:gsub("\nimport .-\n", "\n")
2024-06-23 18:49:03 +01:00
t = t:gsub("\n%s*\n", "\n\n")
2024-06-23 19:02:04 +01:00
t = "\n" .. t
-- fix headings
t = t:gsub("\n#", "\n" .. ("#"):rep(level + 1))
2024-06-23 19:20:49 +01:00
text = text .. "\n\n" .. vim.trim(t)
2024-06-23 18:38:39 +01:00
end
2024-06-23 18:49:03 +01:00
text = vim.trim(text)
2024-06-23 19:28:46 +01:00
text = transform and transform(text) or text
2024-06-23 19:20:49 +01:00
Util.write_file(readme, text)
2024-06-23 18:38:39 +01:00
end
2024-06-18 22:17:53 +01:00
function M.update()
2024-06-25 07:47:07 +01:00
M.themes()
M.docs()
2024-06-23 19:20:49 +01:00
M.readme("README.vim.md")
M.readme("README.md", {
"README.header.md",
"docs/intro.md",
"README.footer.md",
2024-06-23 19:28:46 +01:00
}, function(s)
return s:gsub("\n# 🚀 Getting Started", "\n")
end)
2024-06-23 18:38:39 +01:00
vim.cmd.checktime()
2024-06-18 22:17:53 +01:00
end
return M