md檔案批次轉html

PyJava老鸟發表於2024-07-08

1.requirements.txt

beautifulsoup4==4.12.3
Markdown==3.6

2.main.py

import markdown
import os
import shutil
from bs4 import BeautifulSoup
import uuid
import argparse


def md2html(src_folder, dest_folder):
    for item in os.listdir(src_folder):
        item_path = os.path.join(src_folder, item)
        dest_item_path = os.path.join(dest_folder, item)

        # 如果是資料夾,則遞迴呼叫函式處理子資料夾
        if os.path.isdir(item_path):
            os.makedirs(dest_item_path, exist_ok=True)
            md2html(item_path, dest_item_path)

        # 如果是檔案且以.md結尾,則複製並轉換檔案
        elif os.path.isfile(item_path) and item.endswith('.md'):
            dest_item_path = dest_item_path[:-3] + '.html'  # 替換檔案字尾為.html
            with open(item_path, 'r', encoding='utf-8') as md_file:
                md_content = md_file.read()
            html_content = markdown.markdown(md_content)
            # 新增HTML結構
            # 使用BeautifulSoup庫解析html
            soup = BeautifulSoup(html_content, 'html.parser')

            # 遍歷所有的標籤,並新增id屬性
            for tag in soup.find_all(True):
                tag['id'] = str(uuid.uuid4())  # 生成一個隨機的UUID作為id

            # 輸出新增id屬性後的html
            html_with_uuid = str(soup)

            # 手動新增<html>, <head>, <title>, <meta> 和<body>標籤
            html_full_page = f"<!DOCTYPE html>\n<html id='{uuid.uuid4()}'>\n<head id='{uuid.uuid4()}'>\n<title id='{uuid.uuid4()}'>{os.path.basename(item_path)}</title>\n<meta id='{uuid.uuid4()}' charset='UTF-8'>\n</head>\n<body id='{uuid.uuid4()}'>\n{html_with_uuid}\n</body>\n</html>"

            with open(dest_item_path, 'w', encoding='utf-8') as html_file:
                html_file.write(html_full_page)

        # 非md檔案正常複製
        else:
            shutil.copy2(item_path, dest_item_path)


def main():
    parser = argparse.ArgumentParser(description='Convert markdown to html.')
    parser.add_argument('src_folder', type=str, help='The source folder that contains the markdown files.')
    parser.add_argument('dest_folder', type=str,
                        help='The destination folder that will store the generated html files.')

    args = parser.parse_args()

    md2html(args.src_folder, args.dest_folder)


if __name__ == "__main__":
    main()

3. 執行命令

python3 main.py $1 $2

相關文章