選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

app.py 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from flask import Flask, render_template, request, abort, send_from_directory
  2. from dotenv import load_dotenv
  3. from markdown import markdown
  4. from glob import glob
  5. import json
  6. import os
  7. import sys
  8. import time
  9. import shutil
  10. import re
  11. RE_VID = re.compile("""\[video ["']([^['"]*?)["']\]""")
  12. TEMPLATE_VID = """<video controls class="centered"><source src="{}" type="video/{}"></video>"""
  13. RE_SOUNDTRACK = re.compile("""\[soundtrack ["']([^['"]*?)["']\]""")
  14. TEMPLATE_SOUNDTRACK = """<audio loop data-autoplay><source src="{}" type="audio/{}"></audio>"""
  15. def preprocess_content(defs):
  16. md = defs.get("markdown", "")
  17. if md:
  18. content = markdown(md)
  19. content = RE_VID.sub(lambda m: TEMPLATE_VID.format(m.group(1), m.group(1).split('.')[-1]), content)
  20. else:
  21. content = ""
  22. img = defs.get("image", {})
  23. match img.get("placement", "none"):
  24. case "above":
  25. content = """<img src="{source}" alt="{alt}" class="centered w-{width}"/><br/>\n""".format(**img)+content
  26. case "left":
  27. content = """<img src="{source}" alt="{alt}" class="float-left w-{width}"/><br/>\n""".format(**img)+content
  28. case "right":
  29. content = """<img src="{source}" alt="{alt}" class="float-right w-{width}"/><br/>\n""".format(**img)+content
  30. case "below":
  31. content = content+"""<br/>\n<img src="{source}" alt="{alt}" class="centered w-{width}"/><br/>\n""".format(**img)
  32. if defs.get('soundtrack'):
  33. content = """<audio loop data-autoplay><source src="{}" type="audio/{}"></audio>\n""".format(
  34. defs["soundtrack"], defs["soundtrack"].split(".")[-1])+content
  35. return content
  36. def preprocess_payload(payload):
  37. for column in payload.get("columns", []):
  38. column["content"] = preprocess_content(column)
  39. for slide in column.get("slides", []):
  40. slide["content"] = preprocess_content(slide)
  41. load_dotenv()
  42. application = Flask(__name__)
  43. @application.get("/")
  44. def home():
  45. payload = json.load(open("static/slides.json"))
  46. preprocess_payload(payload)
  47. return render_template("slides.html", **payload)
  48. @application.post("/update")
  49. def update():
  50. shutil.copy(
  51. "static/slides.json",
  52. time.strftime(
  53. "archive/slides-%Y-%m-%d-%H.%M.%S.json",
  54. time.localtime()))
  55. payload = request.get_json()
  56. print(type(payload))
  57. json.dump(payload, sys.stdout, indent=4)
  58. json.dump(payload, open("static/slides.json", "w"), indent=4)
  59. return({"status": "success"})
  60. @application.get("/enum/<topic>")
  61. def choices(topic):
  62. if topic in ["img", "bg", "bg-video", "audio"]:
  63. return {"type": "string", "enum": glob("static/{}/*.*".format(topic))}
  64. abort(404)
  65. @application.get("/schema.json")
  66. def schema():
  67. return render_template("slides.schema.json", backgrounds=choices("bg"))
  68. @application.get("/editor")
  69. def editor():
  70. return render_template("editor.html")
  71. @application.route("/favicon.ico")
  72. def favicon():
  73. return send_from_directory(os.path.join(application.root_path, "static"),
  74. "favicon.ico", mimetype="image/vnd.microsoft.icon")