File size: 2,150 Bytes
b58f685
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from __future__ import annotations

import io
import os
from flask import Flask, render_template, request, send_file, jsonify

app = Flask(__name__)


@app.get("/")
def index():
    return render_template("index.html")


@app.post("/export/md")
def export_md():
    content = request.json.get("markdown", "")
    buf = io.BytesIO(content.encode("utf-8"))
    return send_file(buf, as_attachment=True, download_name="document.md", mimetype="text/markdown")


@app.post("/export/html")
def export_html():
    html = request.json.get("html", "")
    with_styles = bool(request.args.get("withStyles", "1") not in ("0", "false", "False"))
    if with_styles:
        wrapped = f"""<!DOCTYPE html><html><head>

            <meta charset='utf-8'>

            <meta name='viewport' content='width=device-width, initial-scale=1'>

            <link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css' rel='stylesheet'>

            <link href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css' rel='stylesheet'>

            <link href='https://unpkg.com/@primer/css@21.0.7/dist/primer.css' rel='stylesheet'>

            <style> body{{padding:2rem}} </style>

            </head><body class='markdown-body'>{html}</body></html>"""
    else:
        wrapped = html

    buf = io.BytesIO(wrapped.encode("utf-8"))
    return send_file(buf, as_attachment=True, download_name="document.html", mimetype="text/html")


@app.post("/import/html")
def import_html():
    file = request.files.get("file")
    if not file:
        return jsonify({"error": "No file"}), 400
    text = file.read().decode("utf-8", errors="ignore")
    return jsonify({"html": text})


@app.post("/import/md")
def import_md():
    file = request.files.get("file")
    if not file:
        return jsonify({"error": "No file"}), 400
    text = file.read().decode("utf-8", errors="ignore")
    return jsonify({"markdown": text})


if __name__ == "__main__":
    port = int(os.environ.get("PORT", "7860"))
    host = os.environ.get("HOST", "0.0.0.0")
    app.run(host=host, port=port)