Skip to content

Instantly share code, notes, and snippets.

@treytomes
Created January 30, 2024 20:54
Show Gist options
  • Save treytomes/ffa096dd9aab6c6d15dd755264cbbf6e to your computer and use it in GitHub Desktop.
Save treytomes/ffa096dd9aab6c6d15dd755264cbbf6e to your computer and use it in GitHub Desktop.
Mini Micro Help
import "json"
import "stringUtil"
wikiApiUrl = "https://miniscript.org/w/api.php"
formatParams = function(params)
query = []
for key in params.indexes
query.push "{0}={1}".fill([ key, params[key] ])
end for
return query.join("&")
end function
getIndex = function
indexParams = {
"action": "query",
"format": "json",
"list": "allpages",
"aplimit": "max",
}
data = http.get("{0}?{1}".fill([ wikiApiUrl, formatParams(indexParams) ]))
data = json.parse(data)
return data.query.allpages
end function
getPage = function(pageTitle)
pageParams = {
"action": "query",
"format": "json",
"titles": pageTitle,
"prop": "revisions",
"rvprop": "content",
}
data = http.get("{0}?{1}".fill([ wikiApiUrl, formatParams(pageParams) ]))
data = json.parse(data)
if data.query.indexes.indexOf("pages") < 0 then return ""
texts = []
for page in data.query.pages
texts.push page.value.revisions[0]["*"]
end for
return texts.join
end function
// Return a list of page infos containing the search text.
// `what`: ['nearmatch', 'text', 'title']
searchBy = function(searchText, what)
searchParams = {
"action": "query",
"format": "json",
"list": "search",
"srwhat": what,
"srsearch": searchText,
"prop": "info",
}
data = "{0}?{1}".fill([ wikiApiUrl, formatParams(searchParams) ])
data = http.get("{0}?{1}".fill([ wikiApiUrl, formatParams(searchParams) ]))
data = json.parse(data)
result = []
for page in data.query.search.values
result.push page.title
end for
return result
end function
search = function(searchText)
searchText = searchText.lower.trim
nearMatches = searchBy(searchText, "nearmatch")
texts = searchBy(searchText, "text")
titles = searchBy(searchText, "title")
results = nearMatches + titles + texts
finalResults = []
for result in results
if finalResults.indexOf(result) != null then continue
if result.lower.trim == searchText then
// Try returning just an exact match if you can.
return [ result ]
else
finalResults.push result
end if
end for
finalResults.sort
return finalResults
end function
help = function(searchText)
results = search(searchText)
if results.len > 1 then
print "Please choose a topic:"
n = 1
for result in results
print "" + n + ". " + result
n += 1
end for
else
print getPage(results[0])
end if
end function
if locals == globals then
// index = getIndex
// for i in range(5)
// print index[i]
// end for
//
// page = getPage("Abs")
// print "==="
// print page
// print "==="
// print search("Abs")
end if
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment