nvim-sourcer/lua/sourcer/init.lua

44 lines
979 B
Lua

local M = {}
function M.lookup()
local keywords = vim.fn.getreg("*") -- receive yanked text
keywords = keywords:sub(2, #keywords)
local url = "https://dl.acm.org/action/doSearch?AllField=" .. keywords
url, empty = url:gsub(" ", "+")
-- Setup a http socket and request
local command = string.format("curl -s '%s'", url)
-- Execute the curl 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
-- select doi links from response
local b = 1
local doi_links = {}
while true do
local x, y = response:find("//doi/[a-zA-Z]+/10.[0-9]+/[0-9.]+/gm", 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))
b = y
end
print("Keywords: " .. keywords)
print("URL: " .. url)
print("links: " .. doi_links)
end
return M