You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

app.py 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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><source src="{}" type="video/{}"></video>"""
  13. RE_SOUNDTRACK = re.compile("""\[soundtrack ["']([^['"]*?)["']\]""")
  14. TEMPLATE_SOUNDTRACK = """<audio data-autoplay><source src="{}" type="audio/{}"></audio>"""
  15. def preprocess_markdown(md):
  16. if md:
  17. content = markdown(md)
  18. content = RE_VID.sub(lambda m: TEMPLATE_VID.format(m.group(1), m.group(1).split('.')[-1]), content)
  19. content = RE_SOUNDTRACK.sub(lambda m: TEMPLATE_SOUNDTRACK.format(m.group(1), m.group(1).split('.')[-1]), content)
  20. return content
  21. return ""
  22. def preprocess_payload(payload):
  23. for column in payload.get("columns", []):
  24. column["content"] = preprocess_markdown(column.get("markdown"))
  25. for slide in column.get("slides", []):
  26. slide["content"] = preprocess_markdown(slide.get("markdown"))
  27. load_dotenv()
  28. application = Flask(__name__)
  29. @application.get("/")
  30. def home():
  31. payload = json.load(open("static/slides.json"))
  32. preprocess_payload(payload)
  33. return render_template("slides.html", **payload)
  34. @application.post("/update")
  35. def update():
  36. shutil.copy(
  37. "static/slides.json",
  38. time.strftime(
  39. "archive/slides-%Y-%m-%d-%H.%M.%S.json",
  40. time.localtime()))
  41. payload = request.get_json()
  42. print(type(payload))
  43. json.dump(payload, sys.stdout, indent=4)
  44. json.dump(payload, open("static/slides.json", "w"), indent=4)
  45. return({"status": "success"})
  46. @application.get("/enum/<topic>")
  47. def choices(topic):
  48. if topic in ["img", "bg", "bg-video"]:
  49. return {"type": "string", "enum": glob("static/{}/*.*".format(topic))}
  50. abort(404)
  51. @application.get("/schema.json")
  52. def schema():
  53. return render_template("slides.schema.json", backgrounds=choices("bg"))
  54. @application.get("/editor")
  55. def editor():
  56. return render_template("editor.html")
  57. @application.route("/favicon.ico")
  58. def favicon():
  59. return send_from_directory(os.path.join(application.root_path, "static"),
  60. "favicon.ico", mimetype="image/vnd.microsoft.icon")