go_api_w_render.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from .tool.func import *
  2. class api_w_render_include:
  3. def __init__(self, data_option):
  4. self.include_change_list = data_option
  5. def __call__(self, match):
  6. match_org = match.group(0)
  7. match = match.groups()
  8. if len(match) < 3:
  9. match = list(match) + ['']
  10. if match[2] == '\\':
  11. return match_org
  12. else:
  13. slash_add = ''
  14. if match[0]:
  15. if len(match[0]) % 2 == 1:
  16. slash_add = '\\' * (len(match[0]) - 1)
  17. else:
  18. slash_add = match[0]
  19. if match[1] in self.include_change_list:
  20. return slash_add + self.include_change_list[match[1]]
  21. else:
  22. return slash_add + match[2]
  23. async def api_w_render(name = '', tool = '', request_method = '', request_data = {}):
  24. with get_db_connect() as conn:
  25. curs = conn.cursor()
  26. flask_data = flask_data_or_variable(flask.request.form, request_data)
  27. request_method = flask.request.method if request_method == '' else request_method
  28. if request_method == 'POST':
  29. name = flask_data.get('name', '')
  30. data_org = flask_data.get('data', '')
  31. data_option = flask_data.get('option', '')
  32. markup = ''
  33. if tool in ('', 'from', 'include'):
  34. curs.execute(db_change("select set_data from data_set where doc_name = ? and set_name = 'document_markup'"), [name])
  35. db_data = curs.fetchall()
  36. if db_data and db_data[0][0] != '' and db_data[0][0] != 'normal':
  37. markup = db_data[0][0]
  38. if markup == '':
  39. curs.execute(db_change('select data from other where name = "markup"'))
  40. db_data = curs.fetchall()
  41. markup = db_data[0][0] if db_data else 'namumark'
  42. data_type = ''
  43. if tool == '':
  44. data_type = 'api_view'
  45. elif tool == 'from':
  46. data_type = 'api_from'
  47. elif tool == 'include':
  48. data_type = 'api_include'
  49. elif tool == 'backlink':
  50. data_type = 'backlink'
  51. else:
  52. data_type = 'api_thread'
  53. if markup in ('', 'namumark', 'namumark_beta') and data_option != '':
  54. data_option = orjson.loads(data_option)
  55. data_option_func = api_w_render_include(data_option)
  56. # parameter replace
  57. data_org = re.sub(r'(\\+)?@([ㄱ-힣a-zA-Z0-9_]+)=((?:\\@|[^@\n])+)@', data_option_func, data_org)
  58. data_org = re.sub(r'(\\+)?@([ㄱ-힣a-zA-Z0-9_]+)@', data_option_func, data_org)
  59. # remove end br
  60. data_org = re.sub('^\n+', '', data_org)
  61. if markup in ('', 'namumark'):
  62. data_pas = render_set(conn,
  63. doc_name = name,
  64. doc_data = data_org,
  65. data_type = data_type
  66. )
  67. return flask.jsonify({
  68. "data" : data_pas[0],
  69. "js_data" : data_pas[1]
  70. })
  71. else:
  72. other_set = {}
  73. other_set["doc_name"] = name
  74. other_set["render_type"] = data_type
  75. other_set["data"] = data_org
  76. return flask.Response(response = (await python_to_golang(sys._getframe().f_code.co_name, other_set)), status = 200, mimetype = 'application/json')
  77. else:
  78. return flask.jsonify({})