-- 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 = ''
end
local paragraphs = {}
for line in (s.extract or ""):gmatch("[^\n]+") do
table.insert(paragraphs, "
" .. escape(line) .. "
") end local page = '