/************************************************************************ * * Copyright 2020 by Sean Conner. All Rights Reserved. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Comments, questions and criticisms can be sent to: sean@conman.org * *************************************************************************/ #include #include #include #include #include #include #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 # error You need to compile against Lua 5.3 or higher #endif /**************************************************************************/ enum { UPV_ENTITY = 1, UPV_UTF8 = 2, }; struct _yycontext; static int readchars (struct _yycontext *,char *,size_t); static void push_string (struct _yycontext *,char const *,size_t); static void tagi (struct _yycontext *,char const *); static void tagb (struct _yycontext *,char const *); static void endtag (struct _yycontext *); static void setfield (struct _yycontext *,char const *); static void deentifyn (struct _yycontext *,char const *,int); static void deentify (struct _yycontext *,char const *); #define YY_CTX_LOCAL #define YY_PARSE(T) static T #define YY_CTX_MEMBERS \ FILE *in; \ lua_State *L; \ luaL_Buffer buf; \ bool pre; #define YY_INPUT(yy,buf,result,max) result = readchars(yy,buf,max) #include "html.i" /**************************************************************************/ static int readchars(yycontext *yy,char *dest,size_t dlen) { int c = fgetc(yy->in); if (c == EOF) return 0; *dest = c; return 1; } /**************************************************************************/ static void tagi(yycontext *yy,char const *tag) { lua_createtable(yy->L,0,0); lua_pushstring(yy->L,tag); lua_setfield(yy->L,-2,"tag"); lua_pushboolean(yy->L,true); lua_setfield(yy->L,-2,"inline"); lua_createtable(yy->L,0,0); lua_setfield(yy->L,-2,"attributes"); } /**************************************************************************/ static void tagb(yycontext *yy,char const *tag) { lua_createtable(yy->L,0,0); lua_pushstring(yy->L,tag); lua_setfield(yy->L,-2,"tag"); lua_pushboolean(yy->L,true); lua_setfield(yy->L,-2,"block"); lua_createtable(yy->L,0,0); lua_setfield(yy->L,-2,"attributes"); } /**************************************************************************/ static void endtag(yycontext *yy) { lua_pushinteger(yy->L,luaL_len(yy->L,-2) + 1); lua_insert(yy->L,-2); lua_settable(yy->L,-3); } /**************************************************************************/ static void setfield(yycontext *yy,char const *name) { lua_pushstring(yy->L,name); lua_insert(yy->L,-2); lua_getfield(yy->L,-3,"attributes"); lua_insert(yy->L,-3); lua_settable(yy->L,-3); lua_pop(yy->L,1); } /**************************************************************************/ static void deentifyn(yycontext *yy,char const *num,int base) { size_t len; char const *s; lua_Integer v = strtoul(num,NULL,base); lua_getfield(yy->L,lua_upvalueindex(UPV_UTF8),"char"); lua_pushinteger(yy->L,v); lua_call(yy->L,1,1); s = lua_tolstring(yy->L,-1,&len); luaL_addlstring(&yy->buf,s,len); lua_pop(yy->L,1); } /**************************************************************************/ static void deentify(yycontext *yy,char const *label) { char const *entity; size_t len; lua_getfield(yy->L,lua_upvalueindex(UPV_ENTITY),label); entity = lua_tolstring(yy->L,-1,&len); luaL_addlstring(&yy->buf,entity,len); lua_pop(yy->L,1); } /**************************************************************************/ static int parse(lua_State *L) { yycontext yyctx; FILE **fp = luaL_checkudata(L,1,LUA_FILEHANDLE); int rc; lua_settop(L,1); lua_createtable(L,0,0); memset(&yyctx,0,sizeof(yyctx)); yyctx.in = *fp; yyctx.L = L; rc = yyparse(&yyctx); yyrelease(&yyctx); if (rc == 0) lua_pushnil(L); return 1; } /**************************************************************************/ int luaopen_org_conman_app_port70_handlers_blog_html(lua_State *L) { lua_getglobal(L,"require"); lua_pushliteral(L,"org.conman.const.entity"); lua_call(L,1,1); lua_getglobal(L,"require"); lua_pushliteral(L,"utf8"); lua_call(L,1,1); lua_pushcclosure(L,parse,2); return 1; }