SHARE
TWEET

POO POO

a guest Dec 8th, 2019 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- docgenx.lua
  2. -- check if synapse x injected
  3. -- written by Louka
  4.  
  5. local docgen = {}
  6.  
  7. function docgen.tree()
  8.     local tree = {}
  9.     tree.categories = {}
  10.     return tree
  11. end
  12.  
  13. function docgen.category(tree, name)
  14.     tree.categories[name] = {}
  15.     return tree.categories[name]
  16. end
  17.  
  18. function docgen.method(f_return, f_name, f_arguments, f_description, f_example)
  19.     local method = {}
  20.     method.retn = f_return
  21.     method.exam = f_example
  22.     method.args = f_arguments
  23.     method.desc = f_description
  24.     method.name = f_name:gsub("%*prp%s*", "")
  25.     method.proprietary = f_name:find("%*prp") and true
  26.     return method
  27. end
  28.  
  29. function docgen.entry(category, name, method)
  30.     category[name] = method
  31. end
  32.  
  33. function docgen.filestr(path)
  34.     local io_f = io.open(path, "r")
  35.     local f_str = io_f:read("*a")
  36.     io_f:close()
  37.     return f_str
  38. end
  39.  
  40. function docgen.typename(str)
  41.     return str:gsub("<", "<span class=\"CodeTypenameVariant\">&lt;"):gsub(">", "&gt;</span>")
  42. end
  43.  
  44. function docgen.build(tree, prefix)
  45.     local document = docgen.header
  46.     local css = docgen.filestr("docstyle.css")
  47.     local body = ""
  48.  
  49.     for cname, cval in pairs(tree.categories) do
  50.         -- do something with categories. for now, not necessary
  51.         body = body .. "<h1 class=\"CategoryTitle\">" .. cname .. "</h1>"
  52.         for mname, mval in pairs(cval) do
  53.             local methodargs = ""
  54.             local bodyentry = docgen.entry
  55.             if mval.args:len() ~= 0 then -- build argument list
  56.                 for argvt, argvn in mval.args:gmatch("([%w/<>]+) ([%w%.]+)") do
  57.                     methodargs = methodargs .. ("<span class=\"CodeTypename\">%s</span> %s, ")
  58.                     :format(docgen.typename(argvt), argvn)
  59.                 end
  60.                 methodargs = methodargs:gsub(",%s*$", "") -- erase extra comma
  61.             else
  62.                 -- void argument list
  63.                 methodargs = "<span class=\"CodeTypename\">void</span>"
  64.             end
  65.             if mval.proprietary then
  66.                 bodyentry = bodyentry:format(mval.name:gsub("%*_", prefix), "CodeDefinitionProprietary", docgen.typename(mval.retn), '#' .. mval.name:gsub("%*_", prefix), mval.name:gsub("%*_", prefix), methodargs, mval.desc:gsub("%*_", prefix), mval.exam)
  67.                 body = body .. bodyentry
  68.             else
  69.                 bodyentry = bodyentry:format(mval.name:gsub("%*_", prefix), "CodeDefinition", docgen.typename(mval.retn), '#' .. mval.name:gsub("%*_", prefix), mval.name:gsub("%*_", prefix), methodargs, mval.desc:gsub("%*_", prefix), mval.exam)
  70.                 body = body .. bodyentry
  71.             end
  72.         end
  73.     end
  74.  
  75.     document = document:format(css, body)
  76.     return document
  77. end
  78.  
  79. function docgen.loadapidef(path)
  80.     local f = io.open(path, "r")
  81.     local a = f:read("*a")
  82.     local fn, err = loadstring(a)
  83.     if fn then
  84.         f:close()
  85.         return fn()
  86.     else
  87.         f:close()
  88.         error("shit happened, please fix: " .. err)
  89.     end
  90. end
  91.  
  92. function docgen.main(path, prefix)
  93.     assert(path, "path to api definition is missing")
  94.     local prefix = prefix or "api."
  95.     local api = docgen.loadapidef(path)
  96.     local tree = docgen.tree()
  97.     for k1, v1 in pairs(api) do
  98.         local category = docgen.category(tree, k1)
  99.         for k2, v2 in pairs(v1) do
  100.             local f_retn, f_name, f_args, f_desc, f_ex = v2[1], v2[2], v2[3], v2[4], v2[5] or "No example provided"
  101.             docgen.entry(category, f_name, docgen.method(f_retn, f_name, f_args, f_desc, f_ex))
  102.         end
  103.     end
  104.  
  105.     docgen.header = docgen.filestr("base_index.html")
  106.     docgen.entry = docgen.filestr("base_entry.html")
  107.     local document_string = docgen.build(tree, prefix)
  108.     local out = io.open("docs/index.html", "w")
  109.     out:write(document_string)
  110.     out:close()
  111. end
  112.  
  113. return docgen.main(...)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top