template_lualib.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * gluon-web Template - Lua binding
  3. *
  4. * Copyright (C) 2009 Jo-Philipp Wich <jow@openwrt.org>
  5. * Copyright (C) 2018 Matthias Schiffer <mschiffer@universe-factory.net>
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "template_parser.h"
  20. #include "template_utils.h"
  21. #include "template_lmo.h"
  22. #define TEMPLATE_LUALIB_META "gluon.web.template.parser"
  23. static int template_L_do_parse(lua_State *L, struct template_parser *parser, const char *chunkname)
  24. {
  25. int lua_status, rv;
  26. if (!parser)
  27. {
  28. lua_pushnil(L);
  29. lua_pushinteger(L, errno);
  30. lua_pushstring(L, strerror(errno));
  31. return 3;
  32. }
  33. lua_status = lua_load(L, template_reader, parser, chunkname);
  34. if (lua_status == 0)
  35. rv = 1;
  36. else
  37. rv = template_error(L, parser);
  38. template_close(parser);
  39. return rv;
  40. }
  41. static int template_L_parse(lua_State *L)
  42. {
  43. const char *file = luaL_checkstring(L, 1);
  44. struct template_parser *parser = template_open(file);
  45. return template_L_do_parse(L, parser, file);
  46. }
  47. static int template_L_parse_string(lua_State *L)
  48. {
  49. size_t len;
  50. const char *str = luaL_checklstring(L, 1, &len);
  51. struct template_parser *parser = template_string(str, len);
  52. return template_L_do_parse(L, parser, "[string]");
  53. }
  54. static int template_L_pcdata(lua_State *L)
  55. {
  56. size_t len = 0;
  57. const char *str = luaL_checklstring(L, 1, &len);
  58. char *res = pcdata(str, len);
  59. if (res != NULL)
  60. {
  61. lua_pushstring(L, res);
  62. free(res);
  63. return 1;
  64. }
  65. return 0;
  66. }
  67. static int template_L_load_catalog(lua_State *L) {
  68. const char *lang = luaL_optstring(L, 1, "en");
  69. const char *dir = luaL_optstring(L, 2, NULL);
  70. lua_pushboolean(L, !lmo_load_catalog(lang, dir));
  71. return 1;
  72. }
  73. static int template_L_translate(lua_State *L) {
  74. size_t len;
  75. char *tr;
  76. int trlen;
  77. const char *key = luaL_checklstring(L, 1, &len);
  78. switch (lmo_translate(key, len, &tr, &trlen))
  79. {
  80. case 0:
  81. lua_pushlstring(L, tr, trlen);
  82. return 1;
  83. case -1:
  84. return 0;
  85. }
  86. lua_pushnil(L);
  87. lua_pushstring(L, "no catalog loaded");
  88. return 2;
  89. }
  90. /* module table */
  91. static const luaL_reg R[] = {
  92. { "parse", template_L_parse },
  93. { "parse_string", template_L_parse_string },
  94. { "pcdata", template_L_pcdata },
  95. { "load_catalog", template_L_load_catalog },
  96. { "translate", template_L_translate },
  97. {}
  98. };
  99. LUALIB_API int luaopen_gluon_web_template_parser(lua_State *L) {
  100. luaL_register(L, TEMPLATE_LUALIB_META, R);
  101. return 1;
  102. }