From 7fd54118ad368c311f413d0e2487f4d83760a3c5 Mon Sep 17 00:00:00 2001 From: yannickreiss Date: Mon, 19 Feb 2024 15:39:30 +0100 Subject: [PATCH] Add window function and receiving information functionality --- lua/sourcer/init.lua | 137 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 128 insertions(+), 9 deletions(-) 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(".-", b, false) -- stop if no further occurrences are found if x == nil or y == nil then break end - -- save substring to doi_links - doi_links:insert(response:sub(x, y)) + -- save substring to titles + table.insert(titles, rss_content:sub(x, y)) + b = y + end + + -- select links from rss_content + b = b_start + local links = {} + while true do + local x, y = rss_content:find(".-", 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, y)) + b = y + end + + -- select dates from rss_content + b = b_start + local dates = {} + while true do + local x, y = rss_content:find(".-", 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, y)) + b = y + end + + -- select descriptions from rss_content + b = b_start + local descriptions = {} + while true do + local x, y = rss_content:find(".-", 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, y)) b = y end print("Keywords: " .. keywords) print("URL: " .. url) - print("Links: " .. #doi_links) + print("Title: " .. #titles) + print("Links: " .. #links) + print("Descriptions: " .. #descriptions) + print("Dates: " .. #dates) end -- Hardware analysis