import os
import re
def markdown_to_html(md_text, img_lines=None):
"""Convert simple Markdown syntax to HTML and optionally insert img_lines."""
# Convert headers
md_text = re.sub(r'###### (.*?)\n', r'
\1
\n', md_text)md_text = re.sub(r'##### (.*?)\n', r'
\1
\n', md_text)md_text = re.sub(r'#### (.*?)\n', r'
\1
\n', md_text)md_text = re.sub(r'### (.*?)\n', r'
\1
\n', md_text)md_text = re.sub(r'## (.*?)\n', r'
\1
\n', md_text)md_text = re.sub(r'# (.*?)\n', r'
\1
\n', md_text)# Insert img_lines after each
if img_lines:
img_iter = iter(img_lines)
md_text = re.sub(r'()', lambda match: f'{match.group(1)}\n{next(img_iter, "")}\n', md_text)
# Convert bold and italic
md_text = re.sub(r'\*\*(.*?)\*\*', r'\1', md_text)
md_text = re.sub(r'\*(.*?)\*', r'\1', md_text)
# Convert links
md_text = re.sub(r'\[(.*?)\]\((.*?)\)', r'\1', md_text)
# Convert images
md_text = re.sub(r'!\[(.*?)\]\((.*?)\)', r'', md_text)
# Convert paragraphs
md_text = re.sub(r'\n\n+', r'
\n', md_text)
md_text = re.sub(r'^\n+', '', md_text) # Remove leading newlines
md_text = re.sub(r'\n$', '', md_text) # Remove trailing newlines
md_text = f'
{md_text}
'return md_text
def convert_txt_to_html(input_path, output_path, img_lines):
"""Convert a .txt file with Markdown content to an .html file."""
with open(input_path, 'r', encoding='utf-8') as txt_file:
md_text = txt_file.read()
html = markdown_to_html(md_text, img_lines)
with open(output_path, 'w', encoding='utf-8') as html_file:
html_file.write(html)
def get_img_lines(directory):
"""Read the content of img.txt in the specified directory and return as a list of lines."""
img_file_path = os.path.join(directory, 'img.txt')
if os.path.exists(img_file_path):
with open(img_file_path, 'r', encoding='utf-8') as img_file:
return img_file.readlines()
return []
def batch_convert_txt_to_html(directory):
"""Batch convert all .txt files in the specified directory to HTML files."""
output_directory = os.path.join(directory, '已处理')
os.makedirs(output_directory, exist_ok=True) # Create the output directory if it doesn't exist
img_lines = get_img_lines(directory) # Get content from img.txt as a list of lines
for filename in os.listdir(directory):
if filename.endswith('.txt'):
input_path = os.path.join(directory, filename)
output_path = os.path.join(output_directory, filename.replace('.txt', '.html'))
convert_txt_to_html(input_path, output_path, img_lines)
print(f'Converted: {filename} -> {output_path}')
def main():
current_directory = os.getcwd()
batch_convert_txt_to_html(current_directory)
if __name__ == "__main__":
main()