Skip to content

Commit

Permalink
ref(summary)!: refactor of the core.norg.dirman.summary module
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro committed Apr 11, 2023
1 parent 7419cbb commit a2fe3ee
Showing 1 changed file with 23 additions and 57 deletions.
80 changes: 23 additions & 57 deletions lua/neorg/modules/core/norg/dirman/summary/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
DIRMAN SUMMARY
module to generate a summary of a workspace inside a note
--]]

require("neorg.modules.base")
require("neorg.modules")
require("neorg.external.helpers")
local log = require("neorg.external.log")

local module = neorg.modules.create("core.norg.dirman.summary")

module.setup = function()
Expand All @@ -23,9 +24,26 @@ module.load = function()
name = "dirman.summary",
},
})

module.config.public.strategy = neorg.lib.match(module.config.public.strategy) {
metadata = neorg.lib.wrap(function(files)
-- local metadata =
end),
headings = neorg.lib.wrap(function()
end),
} or module.config.public.strategy
end

module.config.public = {}
module.config.public = {
-- The strategy to use to generate a summary.
--
-- Possible options are:
-- - "metadata" - read the metadata to categorize and annotate files. Files
-- without metadata will be ignored.
-- - "headings" - read the top level heading and use that as the title.
-- files in subdirectories are treated as subheadings.
strategy = "metadata",
}

module.public = {}

Expand All @@ -34,61 +52,9 @@ module.events.subscribed = {
["dirman.summary"] = true,
},
}

module.on_event = function(event)
local dirman = module.required["core.norg.dirman"]
if event.type == "core.neorgcmd.events.dirman.summary" then
local dir = dirman.get_current_workspace()[1]
local files = dirman.get_norg_files(dir)
local output = vim.defaulttable()
local function gen_string(tbl, name, all_parents, heading_level)
local str = { heading_level .. " " .. name }
if type(tbl) ~= "table" then
return name
end
for i, v in ipairs(tbl) do
tbl[i] = nil
table.insert(str, "{:" .. all_parents .. tostring(v) .. ":}[" .. tostring(v) .. "]")
end
for subdir, path in pairs(tbl) do
vim.list_extend(str, gen_string(path, subdir, all_parents .. subdir .. "/", heading_level .. "*"))
end
return str
end
for _, v in ipairs(files) do
v = string.reverse(string.gsub(string.reverse(v), "gron.", "", 1))
local path_list = vim.split(v, "/")
local tbl = output
for i, p in ipairs(path_list) do
path_list[i] = nil
if vim.tbl_isempty(path_list) then
table.insert(tbl, p)
else
tbl = tbl[p]
end
end
end
local str = gen_string(output, "Index", "", "*")
local index = dirman.get_index()
dirman.open_file(dir, index)
local tstree = vim.treesitter.get_parser(0)
local buflc = vim.api.nvim_buf_line_count(0)
local query = neorg.utils.ts_parse_query(
"norg",
[[
(heading1 (heading1_prefix) title: (paragraph_segment) @title (#eq? @title "Index"))
]]
)
for _, tree in ipairs(tstree:parse()) do
for _, match, _ in query:iter_matches(tree:root(), 0, 1, buflc) do
for _, node in pairs(match) do
local parent = node:parent()
local start_row, _, end_row, _ = parent:range()
vim.api.nvim_buf_set_lines(0, start_row, end_row, true, str)
end
break
end
break
end
end

end

return module

0 comments on commit a2fe3ee

Please sign in to comment.