template_lualib.c 2.7 KB

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