Jump to content

Recommended Posts

Posted

This code will retrieve the entire Lua API and process it, outputting some interesting data into a JSON file. What can this be used for? I am glad you asked. Soon you will learn the answer. From start to finish, it took me 45 minutes to write this code.

#include "Leadwerks.h"
#include <regex>

using namespace Leadwerks;

void Load(table& j3, table& info)
{
    if (j3["hideFromLua"] == true) return;
    Print(j3["title"]);
    Print(j3["filename"]);

    table t = {};
    //t["name"] = j3["title"];
    t["page"] = j3["filename"];
    if (j3["filename"].is_string())
    {
        std::string s = j3["filename"];
        //if (s == "Abs") DebugBreak();
        auto stream = ReadFile("https://raw.githubusercontent.com/Leadwerks/Documentation/refs/heads/master/Lua/" + s + ".md");
        if (stream)
        {
            //if (s == "Entity_Move") DebugBreak();
            bool start = false;
            while (not stream->Eof())
            {
                auto s = stream->ReadLine();
                s = s.Trim();
                if (s == "## Syntax")
                {
                    start = true;
                    continue;
                }
                if (start)
                {
                    if (s.empty()) continue;
                    if (s.StartsWith("-"))
                    {
                        s = s.Right(s.size() - 1);
                    }
                    s = s.Trim();
                    s = s.Replace("xMat4", "Mat4");
                    s = s.Replace("xMat3", "Mat3");
                    s = s.Replace("xQuat", "Quat");
                    s = s.Replace("xPlane", "Plane");
                    s = s.Replace("xVec4", "Vec4");
                    s = s.Replace("xVec3", "Vec3");
                    s = s.Replace("xVec2", "Vec2");
                    s = s.Replace("[string](https://www.lua.org/manual/5.4/manual.html#6.4)", "string");
                    s = s.Replace("[table](https://www.lua.org/manual/5.4/manual.html#6.6)", "table");
                    s = s.Replace("*", "");

                    int p = s.Find(" ");
                    int p2 = s.Find("(");

                    // Regex to match [word](word.md) pattern
                    std::regex pattern(R"(\[([^\]]+)\]\([^\)]+\.md\))");

                    // Replace matched pattern with just the captured 'word'
                    s = std::regex_replace(s, pattern, "$1");

                    if (p > -1 and p < p2)
                    {                        
                        String rvalue = s.Left(p);
                        s = s.Right(s.size() - p);
                        p = rvalue.Find("(");
                        if (p > -1)
                        { 
                            rvalue = rvalue.Left(p);
                        //    rvalue = rvalue.Replace("[", "");
                         //   rvalue = rvalue.Replace("]", "");
                        }
                        rvalue = rvalue.Trim();
                        s = rvalue + " " + s.Trim();
                    }
                    s = s.Trim();
                    t["syntax"] = s;
                    break;
                }
            }
            info[std::string(j3["title"])] = t;
        }
    }

    if (j3["topics"].is_array())
    {
        for (auto t : j3["topics"])
        {
            Load(t.second, info);
        }
    }
}

int main(int argc, const char* argv[])
{
    auto j3 = LoadTable("https://raw.githubusercontent.com/Leadwerks/Documentation/refs/heads/master/toc.json");

    table info;
    
    for (auto t : j3["topics"])
    {
        if (std::string(t.second["title"]) == std::string("Programming Reference"))
        {
            for (auto child : t.second["topics"])
            {
                Load(child.second, info);
            }
        }
    }

    SaveTable(info, GetPath(PATH_DESKTOP) + "/Lua.json");

    return 0;
}

The output looks like this:

{
	"ACos":
	{
		"page": "ACos",
		"syntax": "number ACos(number value)"
	},
	"ASin":
	{
		"page": "ASin",
		"syntax": "number ASin(number value)"
	},
	"ASyncDownloadFile":
	{
		"page": "ASyncDownloadFile",
		"syntax": "ASyncDownloadFileInfo AsyncDownloadFile(string url, string localpath)"
	},
	"ATan":
	{
		"page": "ATan",
		"syntax": "number ATan(number value)"
	},
	"Aabb":
	{
		"page": "Aabb"
	},

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Posted

Updated to fetch the command description:
 

#include "Leadwerks.h"
#include <regex>

using namespace Leadwerks;

void Load(table& j3, table& info)
{
    if (j3["hideFromLua"] == true) return;
    Print(j3["title"]);
    Print(j3["filename"]);

    table t = {};
    //t["name"] = j3["title"];
    t["page"] = j3["filename"];
    if (j3["filename"].is_string())
    {
        std::string s = j3["filename"];
        //if (s == "Abs") DebugBreak();
        auto stream = ReadFile("https://raw.githubusercontent.com/Leadwerks/Documentation/refs/heads/master/Lua/" + s + ".md");
        if (stream)
        {
            //if (s == "Entity_Move") DebugBreak();
            bool start = false;
            String description;
            bool descactive = true;
            while (not stream->Eof())
            {
                auto s = stream->ReadLine();
                s = s.Trim();
                if (descactive)
                {
                    if (s.StartsWith("Base class:")) continue;
                    if (s.StartsWith("# ")) continue;
                    if (s.StartsWith("|") or s.StartsWith("##"))
                    {
                        descactive = false;
                    }
                    else
                    {
                        if (not s.empty()) description += s;
                    }
                }
                if (s == "## Syntax")
                {
                    start = true;
                    continue;
                }
                if (start)
                {
                    if (s.empty()) continue;
                    if (s.StartsWith("-"))
                    {
                        s = s.Right(s.size() - 1);
                    }
                    s = s.Trim();
                    s = s.Replace("xMat4", "Mat4");
                    s = s.Replace("xMat3", "Mat3");
                    s = s.Replace("xQuat", "Quat");
                    s = s.Replace("xPlane", "Plane");
                    s = s.Replace("xVec4", "Vec4");
                    s = s.Replace("xVec3", "Vec3");
                    s = s.Replace("xVec2", "Vec2");
                    s = s.Replace("[string](https://www.lua.org/manual/5.4/manual.html#6.4)", "string");
                    s = s.Replace("[table](https://www.lua.org/manual/5.4/manual.html#6.6)", "table");
                    s = s.Replace("*", "");

                    int p = s.Find(" ");
                    int p2 = s.Find("(");

                    // Regex to match [word](word.md) pattern
                    std::regex pattern(R"(\[([^\]]+)\]\([^\)]+\.md\))");

                    // Replace matched pattern with just the captured 'word'
                    s = std::regex_replace(s, pattern, "$1");

                    if (p > -1 and p < p2)
                    {                        
                        String rvalue = s.Left(p);
                        s = s.Right(s.size() - p);
                        p = rvalue.Find("(");
                        if (p > -1)
                        { 
                            rvalue = rvalue.Left(p);
                        //    rvalue = rvalue.Replace("[", "");
                         //   rvalue = rvalue.Replace("]", "");
                        }
                        rvalue = rvalue.Trim();
                        s = rvalue + " " + s.Trim();
                    }
                    s = s.Trim();
                    t["syntax"] = s;
                    break;
                }
            }
            if (not description.empty())
            {
                int p = description.Find(".");
                if (p != -1) description = description.Left(p + 1);
                t["description"] = description;
            }
            info[std::string(j3["title"])] = t;
        }
    }

    if (j3["topics"].is_array())
    {
        for (auto t : j3["topics"])
        {
            Load(t.second, info);
        }
    }
}

int main(int argc, const char* argv[])
{
    auto j3 = LoadTable("https://raw.githubusercontent.com/Leadwerks/Documentation/refs/heads/master/toc.json");

    table info;
    
    for (auto t : j3["topics"])
    {
        if (std::string(t.second["title"]) == std::string("Programming Reference"))
        {
            for (auto child : t.second["topics"])
            {
                Load(child.second, info);
            }
        }
    }

    SaveTable(info, GetPath(PATH_DESKTOP) + "/Lua.json");

    return 0;
}

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...