diff --git a/lua/sourcer/init.lua b/lua/sourcer/init.lua index dd67ea8..77e3946 100644 --- a/lua/sourcer/init.lua +++ b/lua/sourcer/init.lua @@ -1,14 +1,65 @@ 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() + + print("Starting the request") -- Setup a http socket and request - local command = string.format("curl -s '%s'", url) + local command = string.format("wget -q -O - '%s'", url) - -- Execute the curl command and capture its output + -- Execute the wget command and capture its output local handle = io.popen(command) local response = "" if handle ~= nil then @@ -18,25 +69,93 @@ function M.lookup() response = "" end - -- select doi links from response - local b = 1 - local doi_links = {} + -- extract the rss feed link + local rss_url = response:match('"https://dl%.acm%.org/action/showFeed%?.-"') + if rss_url == nil then + print("ERROR in URL") + return + end + rss_url = rss_url:sub(2, #rss_url - 1) + + print("Feed found at: " .. rss_url) + + local rss_content = httprequest(rss_url) + print("Received feed content") + print(rss_content) + + -- skip initial block + local b, b_start = rss_content:find("", 1, false) + + -- select titles from response + b = b_start + local titles = {} while true do - local x, y = response:find("doi/[a-zA-Z]+/10.[0-9]+/[0-9.]+", b, false) + local x, y = rss_content:find("