static void set_attributes(lua_State *L, const GumboVector *attrs) { static const char attrnsmap[][6] = {"none", "xlink", "xml", "xmlns"}; const unsigned int length = attrs->length; if (length > 0) { lua_createtable(L, length, length); for (unsigned int i = 0; i < length; i++) { const GumboAttribute *attr = (const GumboAttribute *)attrs->data[i]; if (attr->attr_namespace == GUMBO_ATTR_NAMESPACE_NONE) { lua_createtable(L, 0, 5); } else { lua_createtable(L, 0, 6); set_string(L, "prefix", attrnsmap[attr->attr_namespace]); } set_string(L, "name", attr->name); set_string(L, "value", attr->value); set_sourcepos(L, attr->name_start); lua_pushvalue(L, -1); lua_setfield(L, -3, attr->name); setmetatable(L, Attribute); lua_rawseti(L, -2, i + 1); } setmetatable(L, AttributeList); lua_setfield(L, -2, "attributes"); } }
static void push_node(lua_State *L, const GumboNode *node) { luaL_checkstack(L, 10, "Unable to allocate Lua stack space"); switch (node->type) { case GUMBO_NODE_ELEMENT: { const GumboElement *element = &node->v.element; lua_createtable(L, 0, 7); set_tag(L, element); set_sourcepos(L, element->start_pos); if (node->parse_flags != GUMBO_INSERTION_NORMAL) { set_integer(L, "parseFlags", node->parse_flags); } set_attributes(L, &element->attributes); set_children(L, &element->children, 1); setmetatable(L, Element); return; } case GUMBO_NODE_TEMPLATE: { const GumboElement *element = &node->v.element; lua_createtable(L, 0, 8); set_literal(L, "localName", "template"); set_sourcepos(L, element->start_pos); set_attributes(L, &element->attributes); lua_createtable(L, 0, 0); setmetatable(L, NodeList); lua_setfield(L, -2, "childNodes"); lua_createtable(L, 0, 1); set_children(L, &element->children, 1); setmetatable(L, DocumentFragment); lua_setfield(L, -2, "content"); setmetatable(L, Element); return; } case GUMBO_NODE_TEXT: create_text_node(L, &node->v.text, Text); return; case GUMBO_NODE_WHITESPACE: create_text_node(L, &node->v.text, Text); set_literal(L, "type", "whitespace"); return; case GUMBO_NODE_COMMENT: create_text_node(L, &node->v.text, Comment); return; case GUMBO_NODE_CDATA: create_text_node(L, &node->v.text, Text); set_literal(L, "type", "cdata"); return; default: luaL_error(L, "GumboNodeType value out of bounds: %d", node->type); return; } }
LUA_API void luaS_pushVector2(lua_State *L, float x, float y) { lua_newtable(L); lua_pushnumber(L, x); lua_rawseti(L, -2, 1); lua_pushnumber(L, y); lua_rawseti(L, -2, 2); setmetatable(L, -2, MT_VEC2); }
LUA_API void luaS_pushVector2(lua_State *L, float x, float y) { lua_newtable(L); lua_pushnumber(L, x); lua_setfield(L, -2, "x"); lua_pushnumber(L, y); lua_setfield(L, -2, "y"); setmetatable(L, -2, MT_VEC2); }
static int push_document(lua_State *L) { const GumboDocument *document = lua_touserdata(L, 1); lua_createtable(L, 0, 4); if (document->has_doctype) { set_integer(L, "quirksModeEnum", document->doc_type_quirks_mode); set_children(L, &document->children, 2); lua_getfield(L, -1, "childNodes"); lua_createtable(L, 0, 3); // doctype set_string(L, "name", document->name); set_string(L, "publicId", document->public_identifier); set_string(L, "systemId", document->system_identifier); setmetatable(L, DocumentType); lua_rawseti(L, -2, 1); // childNodes[1] = doctype lua_pop(L, 1); } else { set_children(L, &document->children, 1); } setmetatable(L, Document); return 1; }
int pushquat(lua_State *L, quat_t q) { size_t i; lua_newtable(L); setmetatable(L, QUAT_MT); for(i=0; i<4; i++) { lua_pushnumber(L, q[i]); lua_seti(L, -2, i+1); } return 1; }
LUA_API void luaS_pushColor(lua_State *L, float x, float y, float z, float w) { lua_newtable(L); lua_pushnumber(L, x); lua_rawseti(L, -2, 1); lua_pushnumber(L, y); lua_rawseti(L, -2, 2); lua_pushnumber(L, z); lua_rawseti(L, -2, 3); lua_pushnumber(L, w); lua_rawseti(L, -2, 4); setmetatable(L, -2, MT_COLOR); }
static void set_children(lua_State *L, const GumboVector *vec, unsigned int start) { const unsigned int length = vec->length; lua_createtable(L, length, 0); setmetatable(L, NodeList); for (unsigned int i = 0; i < length; i++) { push_node(L, (const GumboNode *)vec->data[i]); set_value(L, "parentNode", -3); // child.parentNode = parent lua_rawseti(L, -2, i + start); // parent.childNodes[i+start] = child } lua_setfield(L, -2, "childNodes"); }
LUA_API void luaS_pushColor(lua_State *L, float x, float y, float z, float w) { lua_newtable(L); lua_pushnumber(L, x); lua_setfield(L, -2, "r"); lua_pushnumber(L, y); lua_setfield(L, -2, "g"); lua_pushnumber(L, z); lua_setfield(L, -2, "b"); lua_pushnumber(L, w); lua_setfield(L, -2, "a"); setmetatable(L, -2, MT_COLOR); }
LUA_API void luaS_pushQuaternion(lua_State *L, float x, float y, float z, float w) { lua_newtable(L); lua_pushnumber(L, x); lua_setfield(L, -2, "x"); lua_pushnumber(L, y); lua_setfield(L, -2, "y"); lua_pushnumber(L, z); lua_setfield(L, -2, "z"); lua_pushnumber(L, w); lua_setfield(L, -2, "w"); setmetatable(L, -2, MT_Q); }
static void create_text_node(lua_State *L, const GumboText *t, Upvalue i) { lua_createtable(L, 0, 5); set_string(L, "data", t->text); set_sourcepos(L, t->start_pos); setmetatable(L, i); }