ソースを参照

feat: 优化单词下载机制

SongZihuan 3 年 前
コミット
5e3ac8f865
2 ファイル変更27 行追加9 行削除
  1. 2 9
      app/test.py
  2. 25 0
      templates/download.html

+ 2 - 9
app/test.py

@@ -139,15 +139,8 @@ def download(word: str):
     w = user.find_word(word, False)
     if w is None:
         abort(404)
-    w_str = f"{w.name}\n"
-    for i in w.comment:
-        comment = w.comment[i]
-        w_str += f"  {comment.part}\n  {comment.english}\n  {comment.chinese}\n"
-        for a in comment.eg:
-            e, c = a.split("##")
-            w_str += f"    {e}\n    {c}\n"
-    response = make_response(w_str)
-    response.headers["Content-Disposition"] = f"attachment;filename={word}.henglish.txt"
+    response = make_response(render_template("download.html", w=w))
+    response.headers["Content-Disposition"] = f"attachment;filename={word}.html"
     return response
 
 

+ 25 - 0
templates/download.html

@@ -0,0 +1,25 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title> {{ w.name }} - HEnglish </title>
+</head>
+<body>
+    <h1> {{ w.name }} <small> (box: {{ w.box }}) </small> </h1>
+    {% for c in w.comment %}
+        <h2> {{ w.comment[c].chinese }} </h2>
+        <h3> English: {{ w.comment[c].english }} </h3>
+        <h4> Part: {{ w.comment[c].part }} </h4>
+        <div class="eg">
+            <ul>
+                {% for e in w.comment[c].eg %}
+                    <li>
+                        <p> {{ e.split("##")[0] }} </p>
+                        <p> {{ e.split("##")[1] }} </p>
+                    </li>
+                {% endfor %}
+            </ul>
+        </div>
+    {% endfor %}
+</body>
+</html>