JairoDanielMT commited on
Commit
ebb7ae8
·
verified ·
1 Parent(s): d20426c

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +12 -8
main.py CHANGED
@@ -12,30 +12,32 @@ app = FastAPI()
12
  # Control de descargas por archivo
13
  descargas = {}
14
 
15
-
16
  class MarkdownInput(BaseModel):
17
  content: str
18
 
19
-
20
  def limpiar_lineas_hr(markdown_text: str) -> str:
21
  return re.sub(r'^\s*---\s*$', '\n', markdown_text, flags=re.MULTILINE)
22
 
 
 
 
 
 
23
 
24
  def save_markdown_to_file(content: str, path: str):
25
  with open(path, "w", encoding="utf-8") as f:
26
  f.write(content)
27
 
28
-
29
  def convert_markdown_to_docx(input_md: str, output_docx: str):
30
  pypandoc.convert_file(
31
  source_file=input_md,
32
  to="docx",
33
  format="md",
34
  outputfile=output_docx,
35
- extra_args=["--mathjax"]
36
  )
37
 
38
-
39
  def limpieza():
40
  for ext in ["*.docx", "*.md"]:
41
  for file in glob.glob(ext):
@@ -44,7 +46,6 @@ def limpieza():
44
  except Exception as e:
45
  print(f"No se pudo eliminar {file}: {e}")
46
 
47
-
48
  @app.post("/convert/")
49
  def convert(data: MarkdownInput):
50
  try:
@@ -54,17 +55,20 @@ def convert(data: MarkdownInput):
54
  output_docx = f"{uid}.docx"
55
 
56
  contenido_limpio = limpiar_lineas_hr(data.content)
 
57
  save_markdown_to_file(contenido_limpio, input_md)
58
  convert_markdown_to_docx(input_md, output_docx)
59
  os.remove(input_md)
60
 
61
  descargas[uid] = 0
62
- return {"message": "Archivo generado exitosamente, para descargar acceder a la URL", "url": f"https://jairodanielmt-markdown-docx.hf.space/download/{uid}"}
 
 
 
63
 
64
  except Exception as e:
65
  return JSONResponse(content={"error": str(e)}, status_code=500)
66
 
67
-
68
  @app.get("/download/{file_id}")
69
  def download(file_id: str):
70
  output_docx = f"{file_id}.docx"
 
12
  # Control de descargas por archivo
13
  descargas = {}
14
 
 
15
  class MarkdownInput(BaseModel):
16
  content: str
17
 
18
+ # Reemplaza '---' por saltos de línea
19
  def limpiar_lineas_hr(markdown_text: str) -> str:
20
  return re.sub(r'^\s*---\s*$', '\n', markdown_text, flags=re.MULTILINE)
21
 
22
+ # Convierte \[...\] y \(...\) a $$...$$ y $...$
23
+ def normalizar_ecuaciones(md: str) -> str:
24
+ md = re.sub(r'\\\[\s*(.*?)\s*\\\]', r'$$\1$$', md, flags=re.DOTALL)
25
+ md = re.sub(r'\\\(\s*(.*?)\s*\\\)', r'$\1$', md, flags=re.DOTALL)
26
+ return md
27
 
28
  def save_markdown_to_file(content: str, path: str):
29
  with open(path, "w", encoding="utf-8") as f:
30
  f.write(content)
31
 
 
32
  def convert_markdown_to_docx(input_md: str, output_docx: str):
33
  pypandoc.convert_file(
34
  source_file=input_md,
35
  to="docx",
36
  format="md",
37
  outputfile=output_docx,
38
+ extra_args=["--standalone"]
39
  )
40
 
 
41
  def limpieza():
42
  for ext in ["*.docx", "*.md"]:
43
  for file in glob.glob(ext):
 
46
  except Exception as e:
47
  print(f"No se pudo eliminar {file}: {e}")
48
 
 
49
  @app.post("/convert/")
50
  def convert(data: MarkdownInput):
51
  try:
 
55
  output_docx = f"{uid}.docx"
56
 
57
  contenido_limpio = limpiar_lineas_hr(data.content)
58
+ contenido_limpio = normalizar_ecuaciones(contenido_limpio)
59
  save_markdown_to_file(contenido_limpio, input_md)
60
  convert_markdown_to_docx(input_md, output_docx)
61
  os.remove(input_md)
62
 
63
  descargas[uid] = 0
64
+ return {
65
+ "message": "Archivo generado exitosamente, para descargar acceder a la URL",
66
+ "url": f"https://jairodanielmt-markdown-docx.hf.space/download/{uid}"
67
+ }
68
 
69
  except Exception as e:
70
  return JSONResponse(content={"error": str(e)}, status_code=500)
71
 
 
72
  @app.get("/download/{file_id}")
73
  def download(file_id: str):
74
  output_docx = f"{file_id}.docx"