Skip to content

Commit

Permalink
ref: start refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro committed Feb 3, 2023
1 parent 36cc153 commit b4d8839
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 1,015 deletions.
111 changes: 111 additions & 0 deletions docgen/docgen.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
local docgen = {}

-- Create the directory if it does not exist
docgen.output_dir = "wiki"
pcall(vim.fn.mkdir, docgen.output_dir)

require("neorg").setup({
load = {
["core.defaults"] = {},
["core.integrations.treesitter"] = {
config = {
configure_parsers = false,
},
},
},
})

-- Start neorg
neorg.org_file_entered(false)

-- Extract treesitter utility functions provided by Neorg and nvim-treesitter.ts_utils
local ts = neorg.modules.get_module("core.integrations.treesitter")
assert(ts, "treesitter not available")

local ts_utils = ts.get_ts_utils()

--- Aggregates all the available modules.
---@return table #A list of paths to every module's `module.lua` file
docgen.aggregate_module_files = function()
return vim.fs.find("module.lua", {
path = vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":p:h"),
type = "file",
limit = math.huge,
})
end

--- Opens a file from a given path in a new buffer
---@param path string #The path of the file to open
---@return number #The buffer ID of the opened file
docgen.open_file = function(path)
local uri = vim.uri_from_fname(path)
local buf = vim.uri_to_bufnr(uri)

return buf
end

--- Get the first comment (at line 0) from a module and get it's content
--- @param buf number #The buffer number to read from
--- @return table? #A table of lines
docgen.get_module_top_comment = function(buf)
local node = ts.get_first_node_recursive("comment", { buf = buf, ft = "lua" })

if not node then
return
end

-- Verify if it's the first line
local start_row = ts_utils.get_node_range(node)
if start_row ~= 0 then
return
end

local comment = vim.split(ts.get_node_text(node, buf), "\n")

-- Stops execution if it's not a multiline comment
if not comment[1] == "--[[" or not comment[#comment] == "--]]" then
return
end

-- Removes first and last braces
table.remove(comment, 1)
table.remove(comment, #comment)

return comment
end

---@alias TopComment { file: string, title: string, summary: string, markdown: string[] }

--- Parses the top comment
---@param comment string[] #The comment
---@return TopComment #The parsed comment
docgen.parse_top_comment = function(comment)
---@type TopComment
local result = {
-- file = "",
-- title = "",
-- summary = "",
markdown = {},
}
local can_have_options = true

for _, line in ipairs(comment) do
if line:match("^%s*%-%-%-%s*$") then
can_have_options = false
else
local option_name, value = line:match("^%s*(%w+):%s*(.+)")

if option_name and can_have_options then
result[option_name:lower()] = value
else
table.insert(result.markdown, line)
end
end
end

return result
end



return docgen
Loading

0 comments on commit b4d8839

Please sign in to comment.