feat(componentes): conversor reutilizable md-a-documento (Word/PDF) (#21) - #37
feat(componentes): conversor reutilizable md-a-documento (Word/PDF) (#21)#37blippip69 wants to merge 1 commit into
Conversation
| def esc(text: str) -> str: | ||
| return text.replace("&", "&").replace("<", "<").replace(">", ">") | ||
|
|
||
| story = [] | ||
| for kind, payload in blocks: | ||
| if kind == "h1": | ||
| story.append(Paragraph(esc(payload), h1)) | ||
| elif kind == "h2": | ||
| story.append(Paragraph(esc(payload), h2)) | ||
| elif kind == "h3": | ||
| story.append(Paragraph(f"<b>{esc(payload)}</b>", body)) | ||
| elif kind == "ul": | ||
| for item in payload: | ||
| story.append(Paragraph("\u2022 " + esc(item), body)) | ||
| story.append(Spacer(1, 6)) |
There was a problem hiding this comment.
⚠️ Bug: PDF output shows literal * and instead of bold/italic
In build_pdf the text is passed through esc() only, which escapes &<> but leaves the Markdown emphasis markers untouched. Unlike build_docx (which parses */ into runs via add_runs), the PDF path never converts **texto** / *texto* into reportlab's <b>/<i> tags, so the generated PDF displays the raw asterisks and no bold/italic formatting. Convert the inline markers to tags before building each Paragraph, e.g. run esc() first then re.sub the marker pairs into <b>/<i>.
Escape XML first, then translate ** and * emphasis into reportlab <b>/<i> tags so the PDF renders formatting instead of literal asterisks.:
def esc(text: str) -> str:
text = text.replace("&", "&").replace("<", "<").replace(">", ">")
text = re.sub(r"\*\*(.+?)\*\*", r"<b>\1</b>", text)
text = re.sub(r"\*(.+?)\*", r"<i>\1</i>", text)
return text
- Apply fix
Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎
| try: | ||
| from reportlab.lib.styles import getSampleStyleSheet as _gss | ||
| except ImportError: | ||
| from reportlab.lib.styles import get_sample_stylesheet as _gss |
There was a problem hiding this comment.
💡 Quality: reportlab fallback import references nonexistent function
The except ImportError branch imports get_sample_stylesheet, but reportlab has always exported this as getSampleStyleSheet (never a snake_case variant across v3/v4/v5). The primary import therefore always succeeds and the fallback is dead code; if it ever executed it would raise ImportError again. Drop the try/except and import getSampleStyleSheet directly.
Use the single correct import name and remove the misleading fallback.:
from reportlab.lib.styles import getSampleStyleSheet as _gss
- Apply fix
Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎
| def verify(blocks, docx_path: Path, pdf_path: Path) -> list[str]: | ||
| """Verificación automática: el texto plano extraído debe contener cada | ||
| bloque del .md origen (normalizado). Devuelve lista de faltantes.""" | ||
| problems: list[str] = [] | ||
| expected = [ | ||
| normalize(line) | ||
| for line in plain_text(blocks).split("\n") | ||
| if line.strip() | ||
| ] | ||
| try: | ||
| from docx import Document as _Doc | ||
|
|
||
| docx_text = normalize( | ||
| "\n".join(p.text for p in _Doc(str(docx_path)).paragraphs) | ||
| ) |
There was a problem hiding this comment.
💡 Quality: verify() takes pdf_path but never checks the PDF
verify() accepts pdf_path and its docstring claims to validate the generated document, but it only extracts and compares text from the .docx; the PDF is never verified. This means a corrupt or content-incomplete PDF still yields 'VERIFICACIÓN OK'. Either drop the unused pdf_path parameter or actually extract and diff the PDF text (e.g. via pdfminer/pypdf).
Was this helpful? React with 👍 / 👎
Code Review
|
| Auto-apply | Compact |
|
|
Important
Your trial ends in 7 days — upgrade now to keep code review, CI analysis, auto-apply, custom automations, and more.
Was this helpful? React with 👍 / 👎 | Gitar
feat(componentes): conversor reutilizable Markdown → Word/PDF (#21)
ES
Agrega
componentes/md-a-documento.py, el conversor reutilizable que pide laissue — se termina de reescribir un script Python por cada escrito.
#/##/###, listas con viñeta y numeradas)..docx(python-docx, Times New Roman 12, interlineado 1.5,justificado, márgenes A4) y
.pdf(reportlab, mismo criterio).(sin escapes manuales
\u201c).comprueba que cada bloque del .md esté presente; si falta algo, lista las
diferencias y sale con código 2.
python md-a-documento.py escrito.md [--solo docx|pdf] [--out carpeta/].argentina-buclesahora lo referencia como método estándar en la entrega.Probado end-to-end con un escrito de muestra (encabezados, negritas,
cursivas, listas numeradas/viñetas, acentos y comillas): genera ambos archivos
y la verificación pasa (
VERIFICACIÓN OK). Compatible con reportlab ≥4/5(fallback de import para el renombre de
getSampleStyleSheet).EN
Adds
componentes/md-a-documento.py: a reusable Markdown → DOCX/PDF converterwith consistent styling, proper typographic quote/accent handling, and an
automatic content-verification pass that diffs the generated document against
the source markdown.
argentina-buclesnow references it as the standardclosing step. Tested end to end.