Skip to content
Snippets Groups Projects
Commit 815edd1d authored by 장명현's avatar 장명현
Browse files

파일 변환 코드(python) 및 pipeline 추가

parent 6b978716
Branches
No related tags found
No related merge requests found
Pipeline #11352 skipped
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 # 선택: 필요할 때만 실행
### 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment