164 lines
3.5 KiB
Lua
164 lines
3.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()
|
|
|
|
print("Starting the request")
|
|
|
|
-- 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
|
|
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("</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, y))
|
|
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, y))
|
|
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, y))
|
|
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, y))
|
|
b = y
|
|
end
|
|
|
|
print("Keywords: " .. keywords)
|
|
print("URL: " .. url)
|
|
print("Title: " .. #titles)
|
|
print("Links: " .. #links)
|
|
print("Descriptions: " .. #descriptions)
|
|
print("Dates: " .. #dates)
|
|
end
|
|
|
|
-- Hardware analysis
|
|
|
|
return M
|