from flask import Flask, request, render_template, \
redirect, url_for, session
import subprocess
import re
app = Flask(__name__)
def upload_pdf_file(pdf_file):
pdf_file.save("uploads/target.pdf")
def run_pdf2text():
cmd = "pdftotext -nopgbrk uploads/target.pdf uploads/target.md"
subprocess.run(cmd, shell=True, capture_output=True, text=True)
def run_textlint_and_get_result_list():
cmd = "npx textlint uploads/target.md"
result = subprocess.run(cmd, shell=True,
capture_output=True, text=True)
result_list = result.stdout.split("\n")
result_table = []
for result in result_list[2:-4]:
tmp_result = result.split(" error ")
if len(tmp_result) >= 2:
row = int(tmp_result[0].split(":")[0].strip())
colum = tmp_result[0].split(":")[1].strip()
error_data = re.sub("ja-technical-writing/[a-z | -]*",
"",tmp_result[1]).strip()
result_table.append([row, colum, error_data])
return result_table
def run_textlint_fix_data():
cmd = "npx textlint --fix ./uploads/target.md"
subprocess.run(cmd, shell=True, capture_output=True, text=True)
with open("./uploads/target.md", ) as md_file:
data_lines = md_file.read()
return data_lines
@app.route("/index", methods=["GET"])
def index():
return render_template("index.html")
@app.route("/", methods=["GET"])
def root():
return redirect(url_for("index"))
@app.route("/result", methods=["GET", "POST"])
def result():
if request.method == "POST":
pdf_file = request.files["pdf_file"]
upload_pdf_file(pdf_file)
run_pdf2text()
result_table = run_textlint_and_get_result_list()
result_text = run_textlint_fix_data()
return render_template("result.html",
result_table=result_table,
result_text=result_text)
return redirect(url_for('index'))
if __name__ == "__main__":
app.debug = True
app.run(host='0.0.0.0', port=5000)