Version 0.1.1 rise to flask app factory
This commit is contained in:
35
Html/apps/views/markdown.py
Normal file
35
Html/apps/views/markdown.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import os
|
||||
from flask import Blueprint, jsonify, current_app, send_from_directory
|
||||
from ..services.markdown_service import (
|
||||
convert_markdown_to_html, wrap_with_styles,
|
||||
save_html, copy_images
|
||||
)
|
||||
|
||||
bp = Blueprint("markdown", __name__)
|
||||
|
||||
@bp.route("/<course_id>-<filename>-markdown", methods=["GET"])
|
||||
def convert_md(course_id, filename):
|
||||
md_file_path = os.path.join(current_app.config["MARKDOWN_DIR"], course_id, f"{filename}.md")
|
||||
|
||||
if not os.path.exists(md_file_path):
|
||||
return jsonify({"error": "Markdown file not found"}), 404
|
||||
|
||||
# 转换 markdown -> html
|
||||
html = convert_markdown_to_html(md_file_path)
|
||||
html_with_styles = wrap_with_styles(html)
|
||||
|
||||
# 保存 HTML 文件到 static
|
||||
html_output_path = os.path.join(current_app.config["STATIC_DIR"], f"{filename}.html")
|
||||
save_html(html_with_styles, html_output_path)
|
||||
|
||||
# 拷贝图片资源
|
||||
image_source = os.path.join(current_app.config["MARKDOWN_DIR"], course_id, current_app.config["IMAGE_DIR"], filename)
|
||||
image_target = os.path.join(current_app.config["STATIC_DIR"], current_app.config["IMAGE_DIR"], filename)
|
||||
copy_images(image_source, image_target)
|
||||
|
||||
return jsonify({"html_url": f"/static/{filename}.html"})
|
||||
|
||||
# 提供静态文件访问
|
||||
@bp.route("/static/<path:filename>")
|
||||
def serve_static(filename):
|
||||
return send_from_directory(current_app.config["STATIC_DIR"], filename)
|
||||
Reference in New Issue
Block a user