Skip to content

Commit

Permalink
fix: :Neorg journal today would fail on alternative path separators (
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLeoP authored Feb 17, 2023
1 parent 9766bef commit e7a5054
Showing 1 changed file with 144 additions and 145 deletions.
289 changes: 144 additions & 145 deletions lua/neorg/modules/core/norg/journal/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ module.private = {

local path = os.date(
type(module.config.public.strategy) == "function" and module.config.public.strategy(os.date("*t", time))
or module.config.public.strategy,
or module.config.public.strategy,
time
)

Expand All @@ -139,8 +139,7 @@ module.private = {

module.required["core.norg.dirman"].create_file(folder_name .. neorg.configuration.pathsep .. path, workspace)

if
not journal_file_exists
if not journal_file_exists
and module.config.public.use_template
and module.required["core.norg.dirman"].file_exists(
workspace_path .. "/" .. folder_name .. "/" .. template_name
Expand Down Expand Up @@ -212,7 +211,7 @@ module.private = {
local get_fs_handle = function(path)
path = path or ""
local handle = vim.loop.fs_scandir(workspace_path .. neorg.configuration.pathsep
.. folder_name .. neorg.configuration.pathsep .. path)
.. folder_name .. neorg.configuration.pathsep .. path)

if type(handle) ~= "userdata" then
error(neorg.lib.lazy_string_concat("Failed to scan directory '", workspace, path, "': ", handle))
Expand All @@ -229,169 +228,169 @@ module.private = {
return title
end

vim.loop.fs_scandir( workspace_path .. neorg.configuration.pathsep
.. folder_name .. neorg.configuration.pathsep,
function(err, handle)
assert(
not err,
neorg.lib.lazy_string_concat("Unable to generate TOC for directory '", folder_name, "' - ", err)
)

while true do
-- Name corresponds to either a YYYY-mm-dd.norg file, or just the year ("nested" strategy)
local name, type = vim.loop.fs_scandir_next(handle)

if not name then
break
end

-- Handle nested entries
if type == "directory" then
local years_handle = get_fs_handle(name)
while true do
-- mname is the month
local mname, mtype = vim.loop.fs_scandir_next(years_handle)
vim.loop.fs_scandir(workspace_path .. neorg.configuration.pathsep
.. folder_name .. neorg.configuration.pathsep,
function(err, handle)
assert(
not err,
neorg.lib.lazy_string_concat("Unable to generate TOC for directory '", folder_name, "' - ", err)
)

if not mname then
break
end
while true do
-- Name corresponds to either a YYYY-mm-dd.norg file, or just the year ("nested" strategy)
local name, type = vim.loop.fs_scandir_next(handle)

if mtype == "directory" then
local months_handle = get_fs_handle(name .. neorg.configuration.pathsep .. mname)
while true do
-- dname is the day
local dname, dtype = vim.loop.fs_scandir_next(months_handle)
if not name then
break
end

if not dname then
break
end
-- Handle nested entries
if type == "directory" then
local years_handle = get_fs_handle(name)
while true do
-- mname is the month
local mname, mtype = vim.loop.fs_scandir_next(years_handle)

-- If it's a .norg file, also ensure it is a day entry
if dtype == "file" and string.match(dname, "%d%d%.norg") then
-- Split the file name
local file = vim.split(dname, ".", { plain = true })
if not mname then
break
end

vim.schedule(function()
-- Get the title from the metadata, else, it just defaults to the name of the file
local title = get_title(
name
if mtype == "directory" then
local months_handle = get_fs_handle(name .. neorg.configuration.pathsep .. mname)
while true do
-- dname is the day
local dname, dtype = vim.loop.fs_scandir_next(months_handle)

if not dname then
break
end

-- If it's a .norg file, also ensure it is a day entry
if dtype == "file" and string.match(dname, "%d%d%.norg") then
-- Split the file name
local file = vim.split(dname, ".", { plain = true })

vim.schedule(function()
-- Get the title from the metadata, else, it just defaults to the name of the file
local title = get_title(
name
.. neorg.configuration.pathsep
.. mname
.. neorg.configuration.pathsep
.. dname
) or file[1]

-- Insert a new entry
table.insert(toc_entries, {
tonumber(name),
tonumber(mname),
tonumber(file[1]),
"{:$"
.. workspace_name_for_links
.. neorg.configuration.pathsep
.. module.config.public.journal_folder
.. neorg.configuration.pathsep
.. name
.. neorg.configuration.pathsep
.. mname
.. neorg.configuration.pathsep
.. file[1]
.. ":}",
title,
})
end)
) or file[1]

-- Insert a new entry
table.insert(toc_entries, {
tonumber(name),
tonumber(mname),
tonumber(file[1]),
"{:$"
.. workspace_name_for_links
.. neorg.configuration.pathsep
.. module.config.public.journal_folder
.. neorg.configuration.pathsep
.. name
.. neorg.configuration.pathsep
.. mname
.. neorg.configuration.pathsep
.. file[1]
.. ":}",
title,
})
end)
end
end
end
end
end
end

-- Handles flat entries
-- If it is a .norg file, but it's not any user generated file.
-- The match is here to avoid handling files made by the user, like a template file, or
-- the toc file
if type == "file" and string.match(name, "%d+-%d+-%d+%.norg") then
-- Split yyyy-mm-dd to a table
local file = vim.split(name, ".", { plain = true })
local parts = vim.split(file[1], "-")

-- Convert the parts into numbers
for k, v in pairs(parts) do
parts[k] = tonumber(v)
end
-- Handles flat entries
-- If it is a .norg file, but it's not any user generated file.
-- The match is here to avoid handling files made by the user, like a template file, or
-- the toc file
if type == "file" and string.match(name, "%d+-%d+-%d+%.norg") then
-- Split yyyy-mm-dd to a table
local file = vim.split(name, ".", { plain = true })
local parts = vim.split(file[1], "-")

-- Convert the parts into numbers
for k, v in pairs(parts) do
parts[k] = tonumber(v)
end

vim.schedule(function()
-- Get the title from the metadata, else, it just defaults to the name of the file
local title = get_title(name) or parts[3]

-- And insert a new entry that corresponds to the file
table.insert(toc_entries, {
parts[1],
parts[2],
parts[3],
"{:$"
.. workspace_name_for_links
.. neorg.configuration.pathsep
.. module.config.public.journal_folder
.. neorg.configuration.pathsep
.. file[1]
.. ":}",
title,
})
end)
vim.schedule(function()
-- Get the title from the metadata, else, it just defaults to the name of the file
local title = get_title(name) or parts[3]

-- And insert a new entry that corresponds to the file
table.insert(toc_entries, {
parts[1],
parts[2],
parts[3],
"{:$"
.. workspace_name_for_links
.. neorg.configuration.pathsep
.. module.config.public.journal_folder
.. neorg.configuration.pathsep
.. file[1]
.. ":}",
title,
})
end)
end
end
end

vim.schedule(function()
-- Gets a default format for the entries
local format = module.config.public.toc_format
or function(entries)
local months_text = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
}
-- Convert the entries into a certain format to be written
local output = {}
local current_year
local current_month
for _, entry in ipairs(entries) do
-- Don't print the year and month if they haven't changed
if not current_year or current_year < entry[1] then
current_year = entry[1]
table.insert(output, "* " .. current_year)
end
if not current_month or current_month < entry[2] then
current_month = entry[2]
table.insert(output, "** " .. months_text[current_month])
vim.schedule(function()
-- Gets a default format for the entries
local format = module.config.public.toc_format
or function(entries)
local months_text = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
}
-- Convert the entries into a certain format to be written
local output = {}
local current_year
local current_month
for _, entry in ipairs(entries) do
-- Don't print the year and month if they haven't changed
if not current_year or current_year < entry[1] then
current_year = entry[1]
table.insert(output, "* " .. current_year)
end
if not current_month or current_month < entry[2] then
current_month = entry[2]
table.insert(output, "** " .. months_text[current_month])
end

-- Prints the file link
table.insert(output, entry[4] .. string.format("[%s]", entry[5]))
end

-- Prints the file link
table.insert(output, entry[4] .. string.format("[%s]", entry[5]))
return output
end

return output
end

module.required["core.norg.dirman"].create_file(
folder_name .. neorg.configuration.pathsep .. index,
workspace or module.required["core.norg.dirman"].get_current_workspace()[1]
)
module.required["core.norg.dirman"].create_file(
folder_name .. neorg.configuration.pathsep .. index,
workspace or module.required["core.norg.dirman"].get_current_workspace()[1]
)

-- The current buffer now must be the toc file, so we set our toc entries there
vim.api.nvim_buf_set_lines(0, 0, -1, false, format(toc_entries))
vim.cmd("w")
-- The current buffer now must be the toc file, so we set our toc entries there
vim.api.nvim_buf_set_lines(0, 0, -1, false, format(toc_entries))
vim.cmd("w")
end)
end)
end)
end,
}

Expand Down Expand Up @@ -428,7 +427,7 @@ module.config.public = {
module.config.private = {
strategies = {
flat = "%Y-%m-%d.norg",
nested = "%Y/%m/%d.norg",
nested = "%Y" .. neorg.configuration.pathsep .. "%m" .. neorg.configuration.pathsep .. "%d.norg",
},
}

Expand Down

0 comments on commit e7a5054

Please sign in to comment.