From 815edd1d09c02239068d905a06900b6c9b813179 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=A5=EB=AA=85=ED=98=84?= <gabang002@gmail.com> Date: Wed, 2 Apr 2025 12:53:52 +0900 Subject: [PATCH] =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EB=B3=80=ED=99=98=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C(python)=20=EB=B0=8F=20pipeline=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitlab-ci.yml | 20 +++++++++++++++ MDtoHTML.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 .gitlab-ci.yml create mode 100644 MDtoHTML.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..1652560 --- /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 0000000..649723f --- /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 -- GitLab