Skip to content

Commit 4a58ec7

Browse files
committed
fix: clean render logic, idea for a content validater...
1 parent 11ee5b0 commit 4a58ec7

File tree

6 files changed

+110
-68
lines changed

6 files changed

+110
-68
lines changed

lua/feed/commands.lua

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ local cmds = {}
1111
-- 1. unjam: stop the connection pool?
1212
-- 2. add_feed: add to database
1313
-- 3. db_unload: not needed? file handles are just closed... but is that efficient?
14-
-- 4. load_opml: need to load locally
15-
-- 5. export_opml
1614
-- 6. tag / untag: save to db
1715
-- 7. show: its a better name lol
1816

17+
--- TODO: config.feeds and opml imported should be <link, table> to avoid dup
18+
1919
---load opml file to list of sources
2020
---@param filepath string
2121
function cmds.load_opml(filepath)
@@ -24,6 +24,7 @@ function cmds.load_opml(filepath)
2424
local index_opml = opml.import(config.opml)
2525
vim.list_extend(index_opml.outline, outlines)
2626
index_opml:export(config.opml)
27+
vim.list_extend(config.feeds, outlines)
2728
end
2829

2930
---index buffer commands
@@ -33,10 +34,11 @@ function cmds.show_in_browser()
3334
end
3435

3536
function cmds.show_in_w3m()
37+
local render = require "feed.render"
3638
if ut.check_command "W3m" then
3739
vim.cmd("W3m " .. render.current_entry().link)
3840
else
39-
vim.notify "[rss.nvim]: need w3m.nvim installed"
41+
vim.notify "[feed.nvim]: need w3m.nvim installed"
4042
end
4143
end
4244

@@ -99,12 +101,11 @@ function cmds.show_index()
99101
config.og_colorscheme = vim.g.colors_name
100102
if not render.state.in_entry then
101103
if not render.buf.index then
102-
print "reerer"
103104
render.prepare_bufs(cmds)
104105
end
105106
render.show_index()
106107
else
107-
if render.state.rendered_once then
108+
if render.state.index_rendered then
108109
if render.state.in_split then
109110
vim.cmd "q"
110111
render.state.in_split = false
@@ -117,14 +118,14 @@ end
117118

118119
function cmds.show_next()
119120
local render = require "feed.render"
121+
local db = require("feed.db").db(config.db_dir)
120122
if render.current_index == #db.index then
121123
return
122124
end
123125
render.current_index = render.current_index + 1
124126
render.show_entry(render.current_index)
125127
end
126128

127-
-- TODO: properly do 'ring' navigation, ie. wrap around
128129
function cmds.show_prev()
129130
local render = require "feed.render"
130131
if render.current_index == 1 then
@@ -176,7 +177,7 @@ function cmds:add_feed(str) end
176177

177178
function cmds:telescope()
178179
if ut.check_command "Telescope" then
179-
vim.cmd "Telescope rss"
180+
vim.cmd "Telescope feed"
180181
end
181182
end
182183

lua/feed/db.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ end
6464

6565
function db_mt:update_index()
6666
local ok, res = pcall(loadfile, self.dir .. "/index")
67-
if ok then
67+
if ok and res then
6868
self.index = res()
6969
else
70-
print "failed to update index?"
70+
print("[feed.nvim]: failed to load index: ", ok)
7171
end
7272
end
7373

@@ -118,9 +118,15 @@ end
118118

119119
---@param dir string
120120
local function db(dir)
121+
local index
121122
dir = vim.fn.expand(dir)
122123
local index_path = dir .. "/index"
123-
local index = loadfile(index_path)()
124+
local ok, res = pcall(loadfile, index_path)
125+
if ok and res then
126+
index = res()
127+
else
128+
print("[feed.nvim]: failed to load index: ", ok)
129+
end
124130
return setmetatable({ dir = dir, index = index }, db_mt)
125131
end
126132

lua/feed/feedparser.lua

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,53 @@ local date = require "feed.date"
33
local sha1 = require "feed.sha1"
44
local ut = require "feed.utils"
55

6+
local function make_sure_list(t)
7+
if #t == 0 and not vim.islist(t) then
8+
t = { t }
9+
end
10+
return t
11+
end
12+
13+
local entry_validator_mt = {}
14+
15+
local expected_types = {
16+
title = "string",
17+
link = "string",
18+
id = "string",
19+
feed = "string",
20+
author = "string",
21+
content = "string",
22+
time = "number",
23+
}
24+
25+
local default = {
26+
title = "<>",
27+
link = "<>",
28+
id = "<>",
29+
feed = "<>",
30+
author = "<>",
31+
content = "<>",
32+
time = 222222,
33+
}
34+
35+
-- function entry_validator_mt:__newindex(k, v)
36+
-- if not (type(v) == expected_types[k]) then
37+
-- print("expected " .. expected_types[k] .. " got: " .. type(v))
38+
-- rawset(self, k, default[k]) -- HACK:
39+
-- end
40+
-- rawset(self, k, v)
41+
-- end
42+
43+
local function V(t)
44+
return setmetatable(t, entry_validator_mt)
45+
end
46+
47+
local function remove_meta(t)
48+
local mt = getmetatable(t)
49+
mt.__index = nil
50+
return t
51+
end
52+
653
---check if json
754
---@param str string
855
---@return boolean
@@ -68,13 +115,6 @@ local function reify_entry(entry, feedtype, title)
68115
return res
69116
end
70117

71-
local function make_sure_list(t)
72-
if #t == 0 and not vim.islist(t) then
73-
t = { t }
74-
end
75-
return t
76-
end
77-
78118
---walk the ast and retrive usefull info for all three types
79119
---@param ast table
80120
---@return feed.feed

lua/feed/render.lua

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ local M = {
44
},
55
}
66

7-
local flatdb = require "feed.db"
87
local config = require "feed.config"
8+
local db = require("feed.db").db(config.db_dir)
99
local ut = require "feed.utils"
10-
local db = flatdb.db(config.db_dir)
1110
local format = require "feed.format"
1211

1312
---@return feed.entry
14-
function M.current_entry()
13+
function M.get_entry_under_cursor()
1514
local row = vim.api.nvim_win_get_cursor(0)[1]
1615
return db.index[row - 1]
1716
end
@@ -50,13 +49,10 @@ end
5049
function M.prepare_bufs(cmds)
5150
M.buf = {
5251
index = vim.api.nvim_create_buf(false, true),
53-
entry = {},
52+
entry = vim.api.nvim_create_buf(false, true),
5453
}
55-
for i = 1, 3 do
56-
M.buf.entry[i] = vim.api.nvim_create_buf(false, true)
57-
for rhs, lhs in pairs(config.keymaps.entry) do
58-
ut.push_keymap(M.buf.entry[i], lhs, cmds[rhs], rhs)
59-
end
54+
for rhs, lhs in pairs(config.keymaps.entry) do
55+
ut.push_keymap(M.buf.entry, lhs, cmds[rhs], rhs)
6056
end
6157
for rhs, lhs in pairs(config.keymaps.index) do
6258
ut.push_keymap(M.buf.index, lhs, cmds[rhs], rhs)
@@ -65,29 +61,30 @@ end
6561

6662
---render whole db as flat list
6763
function M.show_index()
68-
if ut.check_command "ZenMode" then
69-
vim.cmd "ZenMode"
70-
end
7164
db:update_index()
7265
local lines = {}
7366
lines[1] = M.show_hint()
7467
for i, entry in ipairs(db.index) do
7568
lines[i + 1] = format.entry_name(entry)
7669
end
7770
M.show(lines, M.buf.index, ut.highlight_index)
78-
M.state.rendered_once = true
71+
M.state.index_rendered = true
7972
end
8073

81-
---@param index integer
82-
function M.show_entry(index)
83-
local entry = M.get_entry(index)
84-
M.show(format.entry(entry, db:get(entry)), M.buf.entry[2], ut.highlight_entry)
74+
local function apply_formatter(buf)
8575
local ok, conform = pcall(require, "conform")
8676
if ok then
87-
vim.api.nvim_set_option_value("modifiable", true, { buf = M.buf.entry[2] })
88-
conform.format { bufnr = M.buf.entry[2] }
89-
vim.api.nvim_set_option_value("modifiable", false, { buf = M.buf.entry[2] })
77+
vim.api.nvim_set_option_value("modifiable", true, { buf = buf })
78+
conform.format { bufnr = buf }
79+
vim.api.nvim_set_option_value("modifiable", false, { buf = buf })
9080
end
81+
end
82+
83+
---@param index integer
84+
function M.show_entry(index)
85+
local entry = M.get_entry(index)
86+
M.show(format.entry(entry, db:get(entry)), M.buf.entry, ut.highlight_entry)
87+
apply_formatter(M.buf.entry)
9188
entry.tags.unread = nil
9289
db:save()
9390
end

lua/telescope/_extensions/feed.lua

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,35 @@ end
2323

2424
local function feed(opts)
2525
pickers
26-
.new(opts, {
27-
prompt_title = "Feeds",
28-
previewer = previewers.new_buffer_previewer {
29-
--- TODO: attach highlighter! format content on disk to markdown
30-
define_preview = function(self, entry, _)
31-
local db_entry = db:address(db.index[entry.index])
32-
conf.buffer_previewer_maker(db_entry, self.state.bufnr, {
33-
bufname = self.state.bufname,
34-
winid = self.state.winid,
35-
preview = opts.preview,
36-
file_encoding = opts.file_encoding,
37-
})
38-
end,
39-
},
40-
finder = finders.new_table {
41-
results = lines,
42-
},
43-
attach_mappings = function(prompt_bufnr, map)
44-
actions.select_default:replace(function()
45-
actions.close(prompt_bufnr)
46-
local selection = action_state.get_selected_entry()
47-
local entry = db.index[selection.index]
48-
render.show(format.entry(entry, db:get(entry)), render.buf.entry[2], ut.highlight_entry)
49-
end)
50-
return true
51-
end,
52-
sorter = conf.generic_sorter(opts), -- TODO: sort by date?
53-
})
54-
:find()
26+
.new(opts, {
27+
prompt_title = "Feeds",
28+
previewer = previewers.new_buffer_previewer {
29+
--- TODO: attach highlighter! format content on disk to markdown
30+
define_preview = function(self, entry, _)
31+
local db_entry = db:address(db.index[entry.index])
32+
conf.buffer_previewer_maker(db_entry, self.state.bufnr, {
33+
bufname = self.state.bufname,
34+
winid = self.state.winid,
35+
preview = opts.preview,
36+
file_encoding = opts.file_encoding,
37+
})
38+
end,
39+
},
40+
finder = finders.new_table {
41+
results = lines,
42+
},
43+
attach_mappings = function(prompt_bufnr, map)
44+
actions.select_default:replace(function()
45+
actions.close(prompt_bufnr)
46+
local selection = action_state.get_selected_entry()
47+
local entry = db.index[selection.index]
48+
render.show(format.entry(entry, db:get(entry)), render.buf.entry[2], ut.highlight_entry)
49+
end)
50+
return true
51+
end,
52+
sorter = conf.generic_sorter(opts), -- TODO: sort by date?
53+
})
54+
:find()
5555
end
5656

5757
return telescope.register_extension {

lua/treedoc/utils.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,4 @@ ut.list_tostring = function(obj)
5151
return ("%s [ %s ]"):format(obj.tag, table.concat(buf, " "))
5252
end
5353

54-
ut.list_unhandled_tags "/home/n451/Plugins/feed.nvim/tree-sitter-html/src/grammar.json"
55-
5654
return ut

0 commit comments

Comments
 (0)