Skip to content

Instantly share code, notes, and snippets.

@hello-42
Last active October 6, 2022 13:35
Show Gist options
  • Save hello-42/bbf04304fbf32d9d467dc7839a1f7e26 to your computer and use it in GitHub Desktop.
Save hello-42/bbf04304fbf32d9d467dc7839a1f7e26 to your computer and use it in GitHub Desktop.
--[=[
Copyright (c) 2022- https://github.com/hello-42
MIT Licensing. All rights reserved.
]=]
-- Services
local HttpService = game:GetService("HttpService")
-- Internal
function jsonToTableString(json)
local cache, stack, depth = {}, {}, 1
local results = "{\n"
while true do
local index, size = 1, 0
for _ in json do
size += 1
end
for k, v in json do
if not cache[json] or (index >= cache[json]) then
local key = if typeof(k) ~= "string" then "[" .. tostring(k) .. "]" else k
local len = string.len(results)
results ..= (if string.find(results, "}", len) then ",\n"
elseif not (string.find(results, "\n", len)) then "\n"
else "") .. string.rep("\t", depth) .. key .. " = "
if typeof(v) == "number" or typeof(v) == "boolean" then
results ..= tostring(v)
elseif typeof(v) == "table" then
results ..= "{\n"
table.insert(stack, json)
table.insert(stack, v)
cache[json] = (index + 1)
break
else
local value = string.gsub(tostring(v), "]]", "]] .. [[ ")
results ..= if string.find(value, "\n") then "[[" .. value .. "]]" else "'" .. value .. "'"
end
results ..= if index == size
then "\n" .. string.rep("\t", (depth - 1)) .. "}"
else ","
elseif index == size then
results ..= "\n" .. string.rep("\t", (depth - 1)) .. "}"
end
index += 1
end
results ..= if (size == 0) then "\n" .. string.rep("\t", (depth - 1)) .. "}" else ""
if (#stack > 0) then
json = stack[#stack]
stack[#stack] = nil
depth = if (cache[json] == nil) then (depth + 1) else (depth - 1)
else
break
end
end
return results
end
-- Constants
local API_START_FLAG = 'wrap">{'
local API_STOP_FLAG = '</pre></details>'
local function getMoonwaveDocSite(url)
local source = HttpService:GetAsync(url)
local rawApi = source:sub(
(source:find(API_START_FLAG) + 6), (source:find(API_STOP_FLAG) - 1))
rawApi = rawApi:gsub('&quot;','"')
rawApi = rawApi:gsub('&#x27;','')
rawApi = rawApi:gsub('&lt;','<')
rawApi = rawApi:gsub('&gt;','>')
return jsonToTableString(HttpService:JSONDecode(rawApi))
end
--[[ Example usage:
local moonwaveExampleSites = {
{ name = "Promise", url = "https://eryn.io/roblox-lua-promise/api/Promise" },
{ name = "Plasma", url = "https://eryn.io/plasma/api/Plasma" },
{ name = "Binder", url = "https://quenty.github.io/NevermoreEngine/api/Binder" },
{ name = "Matter", url = "https://eryn.io/matter/api/Matter" },
}
for _, site in moonwaveExampleSites do
local output = Instance.new("ModuleScript")
output.Name = site.name
output.Source = ("local Promise = " .. (getMoonwaveDocSite(site.url) or ""))
output.Parent = workspace
end
end
]]
return getMoonwaveDocSite
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment