Tuchuanhuhuhu commited on
Commit
6213fb0
·
1 Parent(s): 849fb0d

解决用户输入中的HTML被渲染的问题

Browse files
Files changed (3) hide show
  1. modules/overwrites.py +6 -5
  2. modules/presets.py +2 -0
  3. modules/utils.py +11 -4
modules/overwrites.py CHANGED
@@ -30,11 +30,12 @@ def postprocess(
30
  """
31
  if y is None or y == []:
32
  return []
33
- tag_regex = re.compile(r"^<\w+>[^<]+</\w+>")
34
- if tag_regex.search(y[-1][1]):
35
- y[-1] = (convert_user(y[-1][0]), y[-1][1])
36
- else:
37
- y[-1] = (convert_user(y[-1][0]), convert_mdtext(y[-1][1]))
 
38
  return y
39
 
40
  with open("./assets/custom.js", "r", encoding="utf-8") as f, open("./assets/Kelpy-Codos.js", "r", encoding="utf-8") as f2:
 
30
  """
31
  if y is None or y == []:
32
  return []
33
+ user, bot = y[-1]
34
+ if not detect_converted_mark(user):
35
+ user = convert_asis(user)
36
+ if not detect_converted_mark(bot):
37
+ bot = convert_mdtext(bot)
38
+ y[-1] = (user, bot)
39
  return y
40
 
41
  with open("./assets/custom.js", "r", encoding="utf-8") as f, open("./assets/Kelpy-Codos.js", "r", encoding="utf-8") as f2:
modules/presets.py CHANGED
@@ -100,6 +100,8 @@ Reply in {reply_language}
100
  If the context isn't useful, return the original answer.
101
  """
102
 
 
 
103
  small_and_beautiful_theme = gr.themes.Soft(
104
  primary_hue=gr.themes.Color(
105
  c50="#02C160",
 
100
  If the context isn't useful, return the original answer.
101
  """
102
 
103
+ ALREADY_CONVERTED_MARK = "<!-- ALREADY CONVERTED BY PARSER. -->"
104
+
105
  small_and_beautiful_theme = gr.themes.Soft(
106
  primary_hue=gr.themes.Color(
107
  c50="#02C160",
modules/utils.py CHANGED
@@ -9,6 +9,7 @@ import hashlib
9
  import csv
10
  import requests
11
  import re
 
12
 
13
  import gradio as gr
14
  from pypinyin import lazy_pinyin
@@ -105,16 +106,22 @@ def convert_mdtext(md_text):
105
  if code.strip():
106
  # _, code = detect_language(code) # 暂时去除代码高亮功能,因为在大段代码的情况下会出现问题
107
  # code = code.replace("\n\n", "\n") # 暂时去除代码中的空行,因为在大段代码的情况下会出现问题
108
- code = f"```{code}\n\n```"
109
  code = markdown_to_html_with_syntax_highlight(code)
110
  result.append(code)
111
  result = "".join(result)
 
112
  return result
113
 
114
 
115
- def convert_user(userinput):
116
- userinput = userinput.replace("\n", "<br>")
117
- return f"<pre>{userinput}</pre>"
 
 
 
 
 
118
 
119
 
120
  def detect_language(code):
 
9
  import csv
10
  import requests
11
  import re
12
+ import html
13
 
14
  import gradio as gr
15
  from pypinyin import lazy_pinyin
 
106
  if code.strip():
107
  # _, code = detect_language(code) # 暂时去除代码高亮功能,因为在大段代码的情况下会出现问题
108
  # code = code.replace("\n\n", "\n") # 暂时去除代码中的空行,因为在大段代码的情况下会出现问题
109
+ code = f"\n```{code}\n\n```"
110
  code = markdown_to_html_with_syntax_highlight(code)
111
  result.append(code)
112
  result = "".join(result)
113
+ result += ALREADY_CONVERTED_MARK
114
  return result
115
 
116
 
117
+ def convert_asis(userinput):
118
+ return f"<pre>{html.escape(userinput)}</pre>"+ALREADY_CONVERTED_MARK
119
+
120
+ def detect_converted_mark(userinput):
121
+ if userinput.endswith(ALREADY_CONVERTED_MARK):
122
+ return True
123
+ else:
124
+ return False
125
 
126
 
127
  def detect_language(code):