diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000000000000000000000000000000000000..16525602374318f5ba9653f7475d478e715be6b4 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,20 @@ +stages: + - convert + +convert_modified_md: + stage: convert + image: python:3.11 + script: + - pip install markdown playwright + - playwright install chromium + # π 1. μμ λ .md νμΌ μ°ΎκΈ° (μ»€λ° κΈ°μ€) + - | + for file in $(git diff --name-only $CI_COMMIT_BEFORE_SHA $CI_COMMIT_SHA | grep '\.md$'); do + echo "λ³ν μ€: $file" + python MDtoPDF.py "$file" + done + artifacts: + paths: + - "*.pdf" + - "*.html" + when: manual # μ ν: νμν λλ§ μ€ν diff --git a/MDtoHTML.py b/MDtoHTML.py new file mode 100644 index 0000000000000000000000000000000000000000..649723f069686ac67dc83d2a3b92df83a2f43eb9 --- /dev/null +++ b/MDtoHTML.py @@ -0,0 +1,67 @@ +### pip install markdown playwright +### playwright install chromium + +import os +import sys +import markdown +import asyncio +from playwright.async_api import async_playwright + +# 0. Markdown νμΌ μ΄λ¦ νμ +md_filename = sys.argv[1] if len(sys.argv) > 1 else "sample.md" + +print(f"μ λ¬λ°μ νμΌ: {md_filename}") +basename, _ = os.path.splitext(md_filename) +html_filename = f"{basename}.html" +pdf_filename = f"{basename}.pdf" + +# 1. κΈ°μ‘΄ html, pdf νμΌ μμ +for f in [html_filename, pdf_filename]: + if os.path.exists(f): + os.remove(f) + +# 2. Markdown β HTML λ³ν +with open(md_filename, "r", encoding="utf-8") as md_file: + md_content = md_file.read() + +html_content = markdown.markdown(md_content, extensions=["fenced_code", "codehilite"]) + +# 3. HTML νμΌλ‘ μ μ₯ (κ°λ¨ν HTML ν νλ¦Ώ ν¬ν¨) +full_html = f""" +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <title>{basename}</title> + <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/default.min.css"> + <style> + body {{ font-family: sans-serif; max-width: 800px; margin: auto; padding: 2rem; }} + pre code {{ background: #f5f5f5; padding: 1rem; display: block; overflow-x: auto; }} + h1, h2, h3 {{ border-bottom: 1px solid #ddd; padding-bottom: 0.3em; }} + </style> +</head> +<body> +{html_content} +</body> +</html> +""" + +# 4. HTML μ μ₯ +with open(html_filename, "w", encoding="utf-8") as html_file: + html_file.write(full_html) + +print(f"HTML λ³ν μλ£: {html_filename}") + +# 5. κ²½λ‘ μΆμ λ° HTML β PDF λ³ν +async def html_to_pdf(input_html, output_pdf): + abs_path = os.path.abspath(input_html).replace("\\", "/") + url = "file:///" + abs_path + async with async_playwright() as p: + browser = await p.chromium.launch() + page = await browser.new_page() + await page.goto(url) + await page.pdf(path=output_pdf, format="A4") + await browser.close() + print(f"PDF μ μ₯ μλ£: {output_pdf}") + +asyncio.run(html_to_pdf(html_filename, pdf_filename)) \ No newline at end of file