2021-03-05 10:08:05 +00:00
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- - Vimwiki reviews extension library -
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
2021-03-06 21:23:05 +00:00
|
|
|
local Path = require('plenary.path')
|
|
|
|
local scandir = require('plenary.scandir')
|
|
|
|
|
2021-03-05 10:08:05 +00:00
|
|
|
-- Gets path to reviews dir of provided vimwiki (by index)
|
2021-03-10 13:26:49 +00:00
|
|
|
local function get_reviews_dir(vimwiki_index)
|
2021-03-05 10:08:05 +00:00
|
|
|
local vimwiki = {}
|
2021-03-10 13:26:49 +00:00
|
|
|
if (vimwiki_index == 0)
|
2021-03-05 10:08:05 +00:00
|
|
|
then
|
2021-03-10 13:26:49 +00:00
|
|
|
local current_index = vim.fn['vimwiki#vars#get_bufferlocal']('wiki_nr')
|
|
|
|
if (current_index < 0)
|
|
|
|
then
|
|
|
|
current_index = 0
|
|
|
|
end
|
|
|
|
vimwiki = vim.g.vimwiki_list[current_index + 1]
|
2021-03-05 10:08:05 +00:00
|
|
|
else
|
2021-03-10 13:26:49 +00:00
|
|
|
vimwiki = vim.g.vimwiki_list[vimwiki_index]
|
2021-03-05 10:08:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return vimwiki.path .. 'reviews/'
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Finds review template path for provided review type
|
|
|
|
local function get_review_template_path(vimwiki_reviews_path, review_type)
|
|
|
|
return vimwiki_reviews_path .. 'template-' .. review_type .. '.md'
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Reads template for provided review type into current buffer
|
|
|
|
-- Uses overrides in form of files in reviews directory
|
|
|
|
-- Looks for file named template-{review_type}.md:
|
|
|
|
-- - template-week.md
|
|
|
|
-- - template-month.md
|
|
|
|
-- - template-year.md
|
|
|
|
-- Templates can use variables using %variable% syntax
|
|
|
|
-- Currently supported variables are:
|
|
|
|
-- - %date% (Puts different date based on review type)
|
|
|
|
local function read_review_template_into_buffer(vimwiki_reviews_path, review_type)
|
|
|
|
local template_path = get_review_template_path(vimwiki_reviews_path, review_type)
|
2021-03-10 13:26:49 +00:00
|
|
|
if (vim.fn.filereadable(vim.fn.glob(template_path)) ~= 0)
|
2021-03-05 10:08:05 +00:00
|
|
|
then
|
|
|
|
vim.cmd('read ' .. template_path)
|
2021-03-06 21:23:05 +00:00
|
|
|
vim.cmd('0d')
|
2021-03-05 10:08:05 +00:00
|
|
|
else
|
|
|
|
if (review_type == 'week')
|
|
|
|
then
|
|
|
|
vim.fn.setline(1, '# %date% Weekly Review')
|
|
|
|
elseif (review_type == 'month')
|
|
|
|
then
|
|
|
|
vim.fn.setline(1, '# %date% Monthly Review')
|
|
|
|
elseif (review_type == 'year')
|
|
|
|
then
|
|
|
|
vim.fn.setline(1, '# %date% Yearly Review')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-06 21:23:05 +00:00
|
|
|
local function file_exists_or_open(file_name)
|
|
|
|
local exists = Path:new(Path:new(file_name):expand()):exists()
|
|
|
|
local open = vim.fn.bufwinnr(file_name) > 0
|
|
|
|
return exists or open
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_filename_from_path(path)
|
|
|
|
local _, filename, _ = string.match(path, "(.-)([^\\/]-%.?([^%.\\/]*))$")
|
|
|
|
return filename
|
|
|
|
end
|
|
|
|
|
2021-03-05 10:08:05 +00:00
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- - Public API -
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
local M = {}
|
|
|
|
|
|
|
|
-- Edits weekly review template
|
2021-03-10 13:26:49 +00:00
|
|
|
function M.open_review_weekly_template(vimwiki_index)
|
|
|
|
local reviews_dir = get_reviews_dir(vimwiki_index)
|
2021-03-05 10:08:05 +00:00
|
|
|
vim.cmd('edit ' .. get_review_template_path(reviews_dir, 'week'))
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Edits monthly review template
|
2021-03-10 13:26:49 +00:00
|
|
|
function M.open_review_monthly_template(vimwiki_index)
|
|
|
|
local reviews_dir = get_reviews_dir(vimwiki_index)
|
2021-03-05 10:08:05 +00:00
|
|
|
vim.cmd('edit ' .. get_review_template_path(reviews_dir, 'month'))
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Edits yearly review template
|
2021-03-10 13:26:49 +00:00
|
|
|
function M.open_review_yearly_template(vimwiki_index)
|
|
|
|
local reviews_dir = get_reviews_dir(vimwiki_index)
|
2021-03-05 10:08:05 +00:00
|
|
|
vim.cmd('edit ' .. get_review_template_path(reviews_dir, 'year'))
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Open current week weekly review file
|
|
|
|
-- Created buffer is dated to Sunday of current week
|
|
|
|
-- Opens current week because Sunday is good time to do this review
|
2021-03-10 13:26:49 +00:00
|
|
|
function M.vimwiki_weekly_review(vimwiki_index)
|
|
|
|
local reviews_dir = get_reviews_dir(vimwiki_index)
|
2021-03-05 10:08:05 +00:00
|
|
|
local days_to_sunday = 7 - tonumber(os.date('%u'))
|
|
|
|
local week_date = os.date('%Y-%m-%d', os.time() + days_to_sunday * 24 * 60 * 60)
|
|
|
|
local file_name = reviews_dir .. week_date .. '-week.md'
|
2021-03-06 21:23:05 +00:00
|
|
|
local exists = file_exists_or_open(file_name)
|
2021-03-05 10:08:05 +00:00
|
|
|
vim.cmd('edit ' .. file_name)
|
|
|
|
if (exists == false)
|
|
|
|
then
|
|
|
|
read_review_template_into_buffer(reviews_dir, 'week')
|
|
|
|
vim.cmd('%substitute/%date%/' .. week_date)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Open past month monthly review file
|
|
|
|
-- Created buffer is dated to previous month
|
|
|
|
-- Previous month is calculated in an erroneous way
|
|
|
|
-- 28 days are subtracted from current time to get previous month
|
2021-03-10 13:26:49 +00:00
|
|
|
function M.vimwiki_monthly_review(vimwiki_index)
|
|
|
|
local reviews_dir = get_reviews_dir(vimwiki_index)
|
2021-03-05 10:08:05 +00:00
|
|
|
local month_time = os.time() - 28 * 24 * 60 * 60
|
|
|
|
local month_date = os.date('%Y-%m', month_time)
|
|
|
|
local file_name = reviews_dir .. month_date .. '-month.md'
|
2021-03-06 21:23:05 +00:00
|
|
|
local exists = file_exists_or_open(file_name)
|
2021-03-05 10:08:05 +00:00
|
|
|
vim.cmd('edit ' .. file_name)
|
|
|
|
if (exists == false)
|
|
|
|
then
|
|
|
|
read_review_template_into_buffer(reviews_dir, 'month')
|
|
|
|
vim.cmd('%substitute/%date%/' .. os.date('%Y %B', month_time))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Open past year yearly review file
|
|
|
|
-- Created buffer is dated to previous year
|
2021-03-10 13:26:49 +00:00
|
|
|
function M.vimwiki_yearly_review(vimwiki_index)
|
|
|
|
local reviews_dir = get_reviews_dir(vimwiki_index)
|
2021-03-05 10:08:05 +00:00
|
|
|
local year_date = (tonumber(os.date('%Y')) - 1)
|
|
|
|
local file_name = reviews_dir .. year_date .. '-year.md'
|
2021-03-06 21:23:05 +00:00
|
|
|
local exists = file_exists_or_open(file_name)
|
2021-03-05 10:08:05 +00:00
|
|
|
vim.cmd('edit ' .. file_name)
|
|
|
|
if (exists == false)
|
|
|
|
then
|
|
|
|
read_review_template_into_buffer(reviews_dir, 'year')
|
|
|
|
vim.cmd('%substitute/%date%/' .. year_date)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-06 21:23:05 +00:00
|
|
|
-- Generates review index table
|
|
|
|
-- Takes arguments similar to all vimwiki calls
|
|
|
|
-- (no args, or 1 arg representing vimwiki index)
|
2021-03-10 13:26:49 +00:00
|
|
|
function M.get_review_index(vimwiki_index)
|
|
|
|
local reviews_dir = get_reviews_dir(vimwiki_index)
|
2021-03-06 21:23:05 +00:00
|
|
|
local reviews_path = Path:new(reviews_dir):expand()
|
|
|
|
|
|
|
|
local entries = scandir.scan_dir(
|
|
|
|
reviews_path,
|
|
|
|
{
|
|
|
|
hidden = false,
|
|
|
|
add_dirs = false,
|
|
|
|
respect_gitignore = true,
|
|
|
|
depth = 1
|
|
|
|
})
|
|
|
|
|
|
|
|
local index = {}
|
|
|
|
-- TODO better default values handling
|
|
|
|
for _,entry in pairs(entries) do
|
|
|
|
local filename = get_filename_from_path(entry)
|
|
|
|
if (string.match(filename, '^%d*-year.md$'))
|
|
|
|
then
|
|
|
|
local year = string.match(filename, '(%d*)-year')
|
|
|
|
if (index[year] == nil)
|
|
|
|
then
|
|
|
|
index[year] = {
|
|
|
|
months = {}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
index[year].year = filename
|
|
|
|
elseif (string.match(filename, '^%d*-%d*-month.md$'))
|
|
|
|
then
|
|
|
|
local year = string.match(filename, '(%d*)-%d*-month')
|
|
|
|
local month = string.match(filename, '(%d*)-month')
|
|
|
|
if (index[year] == nil)
|
|
|
|
then
|
|
|
|
index[year] = {
|
|
|
|
months = {}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
if (index[year].months[month] == nil)
|
|
|
|
then
|
|
|
|
index[year].months[month] = {
|
|
|
|
weeks = {}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
index[year].months[month].month = filename
|
|
|
|
elseif (string.match(filename, '^%d*-%d*-%d*-week.md$'))
|
|
|
|
then
|
|
|
|
local year = string.match(filename, '(%d*)-%d*-%d*-week')
|
|
|
|
local month = string.match(filename, '(%d*)-%d*-week')
|
|
|
|
local week = string.match(filename, '(%d*)-week')
|
|
|
|
if (index[year] == nil)
|
|
|
|
then
|
|
|
|
index[year] = {
|
|
|
|
months = {}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
if (index[year].months[month] == nil)
|
|
|
|
then
|
|
|
|
index[year].months[month] = {
|
|
|
|
weeks = {}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
index[year].months[month].weeks[week] = filename
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return index
|
|
|
|
end
|
|
|
|
|
2021-03-05 10:08:05 +00:00
|
|
|
-- Open reviews index file
|
2021-03-10 13:26:49 +00:00
|
|
|
function M.vimwiki_review_index(vimwiki_index)
|
|
|
|
local reviews_dir = get_reviews_dir(vimwiki_index)
|
2021-03-05 10:08:05 +00:00
|
|
|
vim.cmd('edit ' .. reviews_dir .. 'reviews.md')
|
2021-03-06 21:23:05 +00:00
|
|
|
|
2021-03-10 13:26:49 +00:00
|
|
|
local index = M.get_review_index(vimwiki_index)
|
2021-03-06 21:23:05 +00:00
|
|
|
|
|
|
|
local lines = {
|
|
|
|
'# Reviews',
|
|
|
|
'',
|
|
|
|
}
|
|
|
|
|
|
|
|
local month_names = {
|
|
|
|
'January',
|
|
|
|
'February',
|
|
|
|
'March',
|
|
|
|
'April',
|
|
|
|
'May',
|
|
|
|
'June',
|
|
|
|
'July',
|
|
|
|
'August',
|
|
|
|
'September',
|
|
|
|
'October',
|
|
|
|
'November',
|
|
|
|
'December'
|
|
|
|
}
|
|
|
|
|
|
|
|
-- Sort index by year
|
|
|
|
local years = {}
|
|
|
|
for k in pairs(index) do table.insert(years, k) end
|
|
|
|
table.sort(years, function(a, b) return a > b end)
|
|
|
|
|
|
|
|
local tablelength = function(T)
|
|
|
|
local count = 0
|
|
|
|
for _ in pairs(T) do count = count + 1 end
|
|
|
|
return count
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Add years
|
|
|
|
for _, year in pairs(years) do
|
|
|
|
table.insert(lines, '## ' .. year)
|
|
|
|
table.insert(lines, '')
|
|
|
|
if (index[year].year)
|
|
|
|
then
|
|
|
|
table.insert(lines, '- [Yearly review](' .. index[year].year .. ')')
|
|
|
|
table.insert(lines, '')
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Sort months
|
|
|
|
local months = {}
|
|
|
|
for k in pairs(index[year].months) do table.insert(months, k) end
|
|
|
|
table.sort(months, function(a, b) return a > b end)
|
|
|
|
|
|
|
|
-- Add months
|
|
|
|
for _, month in pairs(months) do
|
|
|
|
table.insert(lines, '### ' .. month_names[tonumber(month)])
|
|
|
|
table.insert(lines, '')
|
|
|
|
if (index[year].months[month].month)
|
|
|
|
then
|
|
|
|
table.insert(lines, '- [Monthly review](' .. index[year].months[month].month .. ')')
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Sort weeks
|
|
|
|
local weeks = index[year].months[month].weeks
|
2021-03-12 15:45:29 +00:00
|
|
|
local sorted = {}
|
|
|
|
for k in pairs(weeks) do table.insert(sorted, k) end
|
|
|
|
table.sort(sorted, function(a, b) return a > b end)
|
2021-03-06 21:23:05 +00:00
|
|
|
|
|
|
|
-- Add weeks
|
2021-03-12 15:45:29 +00:00
|
|
|
local count = tablelength(sorted)
|
|
|
|
for _, week in pairs(sorted) do
|
|
|
|
table.insert(lines, '- [Week #' .. count .. ' Review](' .. weeks[week] .. ')')
|
2021-03-06 21:23:05 +00:00
|
|
|
count = count - 1
|
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(lines, '')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local buf = vim.api.nvim_get_current_buf()
|
|
|
|
vim.api.nvim_buf_set_lines(buf, 0, -1, false, {}) -- Clear out
|
|
|
|
vim.api.nvim_buf_set_lines(buf, 0, 0, false, lines) -- Put new contents
|
2021-03-05 10:08:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|