nvim-sourcer/lua/sourcer/init.lua

191 lines
4.5 KiB
Lua

local M = {}
local api = vim.api
local buf, win
local function open_window()
buf = api.nvim_create_buf(false, true)
api.nvim_buf_set_option(buf, "bufhidden", "wipe")
-- get dimensions
local width = api.nvim_get_option("columns")
local height = api.nvim_get_option("lines")
-- calculate our floating window size
local win_height = math.ceil(height * 0.8 - 4)
local win_width = math.ceil(width * 0.8)
-- and its starting position
local row = math.ceil((height - win_height) / 2 - 1)
local col = math.ceil((width - win_width) / 2)
-- set some options
local opts = {
style = "minimal",
relative = "editor",
width = win_width,
height = win_height,
row = row,
col = col,
}
-- and finally create it with buffer attached
win = api.nvim_open_win(buf, true, opts)
end
local function httprequest(url)
local command = string.format("wget -q -O - '%s'", url)
local handle = io.popen(command)
local response = ""
if handle ~= nil then
response = handle:read("*a")
handle:close()
else
response = "ERROR"
end
return response
end
function M.lookup()
local keywords = vim.fn.getreg("*") -- receive yanked text
local url = "https://dl.acm.org/action/doSearch?AllField=" .. keywords
url, empty = url:gsub(" ", "+")
open_window()
-- enable write to buffer
api.nvim_buf_set_option(buf, "modifiable", true)
api.nvim_buf_set_lines(buf, 0, 1, false, {
"Interesting ACM Publications",
})
api.nvim_buf_set_lines(buf, 1, 2, false, {
"Requesting papers from ACM...",
})
-- Setup a http socket and request
local command = string.format("wget -q -O - '%s'", url)
-- Execute the wget command and capture its output
local handle = io.popen(command)
local response = ""
if handle ~= nil then
response = handle:read("*a")
handle:close()
else
response = ""
end
-- extract the rss feed link
local rss_url = response:match('"https://dl%.acm%.org/action/showFeed%?.-"')
if rss_url == nil then
api.nvim_buf_set_lines(buf, 3, 4, false, {
"ERROR: Could not locate Feed!",
})
return
end
rss_url = rss_url:sub(2, #rss_url - 1)
api.nvim_buf_set_lines(buf, 3, 4, false, {
"Feed found at: " .. rss_url,
})
local rss_content = httprequest(rss_url)
api.nvim_buf_set_lines(buf, 4, 5, false, {
"Received feed content",
})
-- skip initial block
local b, b_start = rss_content:find("</image>", 1, false)
-- select titles from response
b = b_start
local titles = {}
while true do
local x, y = rss_content:find("<title>.-</title>", b, false)
-- stop if no further occurrences are found
if x == nil or y == nil then
break
end
-- save substring to titles
table.insert(titles, rss_content:sub(x + 7, y - 8))
b = y
end
-- select links from rss_content
b = b_start
local links = {}
while true do
local x, y = rss_content:find("<link>.-</link>", b, false)
-- stop if no further occurrences are found
if x == nil or y == nil then
break
end
-- save substring to links
table.insert(links, rss_content:sub(x + 6, y - 7))
b = y
end
-- select dates from rss_content
b = b_start
local dates = {}
while true do
local x, y = rss_content:find("<dc:date>.-</dc:date>", b, false)
-- stop if no further occurrences are found
if x == nil or y == nil then
break
end
-- save substring to dates
table.insert(dates, rss_content:sub(x + 9, y - 10))
b = y
end
-- select descriptions from rss_content
b = b_start
local descriptions = {}
while true do
local x, y = rss_content:find("<description>.-</description>", b, false)
-- stop if no further occurrences are found
if x == nil or y == nil then
break
end
-- save substring to descriptions
table.insert(descriptions, rss_content:sub(x + 12, y - 13))
b = y
end
api.nvim_buf_set_lines(buf, 1, -1, false, {
"Found entries: " .. #dates,
})
local current_line = 4
for x = 1, #dates do
api.nvim_buf_set_lines(buf, current_line, -1, false, { "Title: " .. titles[x] })
current_line = current_line + 1
api.nvim_buf_set_lines(buf, current_line, -1, false, { "Date: " .. dates[x] })
current_line = current_line + 1
api.nvim_buf_set_lines(buf, current_line, -1, false, { "Description: " .. descriptions[x] })
current_line = current_line + 1
api.nvim_buf_set_lines(buf, current_line, -1, false, { "Website: " .. links[x] })
current_line = current_line + 1
api.nvim_buf_set_lines(buf, current_line, -1, false, { "" })
current_line = current_line + 2
end
api.nvim_buf_set_lines(buf, current_line, -1, false, { "End of feed entries." })
-- disable write / make buffer readonly
api.nvim_buf_set_option(buf, "modifiable", false)
end
return M