Files
toolshed-extensions/wikipedia/init.lua
T

134 lines
5.2 KiB
Lua

-- Wikipedia: "wiki rust" lists matching articles; Enter shows an article's summary, and Enter
-- there opens it. ⌘K opens it or copies its link from either. The last jig opens Wikipedia's
-- own search for what was typed.
-- Wikimedia asks automated clients to say what they are.
local agent = "Toolshed-Wikipedia-extension/1.0"
local encode = toolshed.url_encode
local escape = toolshed.escape
local function get(url, max_seconds)
return toolshed.json.decode(toolshed.exec({ "curl", "-sSfL", "--max-time", tostring(max_seconds), "-A", agent, url }))
end
local function site(settings)
return "https://" .. settings.language .. ".wikipedia.org"
end
local function article_address(data)
return "https://" .. data.language .. ".wikipedia.org/wiki/" .. encode(data.key)
end
return toolshed.toolbox {
key = "wikipedia",
title = "Wikipedia",
description = "i18n:toolbox.description",
icon = "assets/wikipedia.svg",
tint = "gray",
settings = {
{ key = "language", title = "i18n:setting.language.title", description = "i18n:setting.language.description", type = "choice", default = "en",
options = {
{ value = "en", title = "English" },
{ value = "no", title = "Norsk (bokmål)" },
{ value = "nn", title = "Norsk (nynorsk)" },
{ value = "sv", title = "Svenska" },
{ value = "da", title = "Dansk" },
{ value = "de", title = "Deutsch" },
{ value = "fr", title = "Français" },
{ value = "es", title = "Español" },
} },
},
tools = {
{
key = "search",
title = "i18n:tool.search.title",
words = { "wiki" },
examples = { "wiki rust", "wiki oslo" },
keywords = { "wikipedia", "encyclopedia", "leksikon" },
-- Wikipedia is asked once typing pauses, not for every letter on the way.
debounce = true,
search = function(rest, settings)
if rest == "" then return {} end
local jigs = {}
-- Offline or slow, the search page is still offered.
local ok, found = pcall(get, site(settings) .. "/w/rest.php/v1/search/title?limit=8&q=" .. encode(rest), 1.5)
for _, page in ipairs(ok and found.pages or {}) do
local thumb = page.thumbnail and page.thumbnail.url
table.insert(jigs, {
title = page.title,
subtitle = page.description,
image = thumb and ("https:" .. thumb) or nil,
tool = "article",
data = { key = page.key, title = page.title, language = settings.language },
})
end
table.insert(jigs, {
title = toolshed.t("search-for", { query = rest }),
action = "open-search",
data = { query = rest, language = settings.language },
})
return jigs
end,
actions = {
{
key = "open-search", title = "i18n:action.search.open-search",
run = function(ctx)
toolshed.open("https://" .. ctx.data.language .. ".wikipedia.org/w/index.php?fulltext=1&search=" .. encode(ctx.data.query))
end,
},
},
},
{
key = "article",
title = "i18n:tool.article.title",
hidden = true,
items = true,
actions = {
{
-- The summary is a page with jigs for what to do next: Enter opens the article.
key = "summary", title = "i18n:action.article.summary",
run = function(ctx)
local s = get("https://" .. ctx.data.language .. ".wikipedia.org/api/rest_v1/page/summary/" .. encode(ctx.data.key), 10)
local picture = ""
if s.thumbnail and s.thumbnail.source then
picture = '<img src="' .. escape(s.thumbnail.source) .. '" alt="" style="float:right;max-width:40%;max-height:220px;margin:0 0 12px 16px;border-radius:8px">'
end
local paragraphs = {}
for line in (s.extract or ""):gmatch("[^\n]+") do
table.insert(paragraphs, "<p style=\"margin:0 0 10px\">" .. escape(line) .. "</p>")
end
local page = '<div style="padding:20px 24px;color:var(--text);line-height:1.5;overflow:auto;height:100%;box-sizing:border-box">'
.. picture
.. '<h2 style="margin:0 0 2px;font-size:20px;font-weight:600">' .. escape(s.title) .. '</h2>'
.. '<div style="margin-bottom:14px;color:var(--text-secondary)">' .. escape(s.description) .. '</div>'
.. table.concat(paragraphs)
.. '</div>'
return { html = page, jigs = {
{ title = toolshed.t("action.article.open"), icon = "external-link", action = "open", data = ctx.data },
{ title = toolshed.t("action.article.copy-link"), icon = "link", action = "copy-link", data = ctx.data },
} }
end,
},
{
key = "open", title = "i18n:action.article.open", shortcut = "cmd+enter",
run = function(ctx) toolshed.open(article_address(ctx.data)) end,
},
{
key = "copy-link", title = "i18n:action.article.copy-link", shortcut = "cmd+shift+c",
run = function(ctx)
toolshed.clipboard.copy(article_address(ctx.data))
return toolshed.t("link-copied")
end,
},
},
},
},
}