1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
- import os
- import re
- import sys
- def adjust_image_paths(markdown_content, original_md_file_path, root_folder):
- """
- Adjusts image paths in the markdown content to be relative to the root folder.
- This includes markdown image syntax and HTML <img> tags.
- :param markdown_content: The content of the markdown file.
- :param original_md_file_path: The file path of the original markdown file.
- :param root_folder: The root folder that contains all the markdown files.
- :return: Updated markdown content with adjusted image paths.
- """
- def adjust_path(match):
- original_path = match.group(1)
- # Construct the absolute path to the image
- absolute_image_path = os.path.abspath(os.path.join(os.path.dirname(original_md_file_path), original_path))
- # Make the path relative to the root folder
- relative_image_path = os.path.relpath(absolute_image_path, root_folder)
- # Ensure the path starts with './'
- if not relative_image_path.startswith('.'):
- relative_image_path = './' + relative_image_path
- return match.group(0).replace(original_path, relative_image_path)
- # Adjust paths in markdown image syntax
- adjusted_content = re.sub(r'!\[.*?\]\((.*?)\)', adjust_path, markdown_content)
- # Adjust paths in HTML <img> tags
- adjusted_content = re.sub(r'<img\s+[^>]*?src=["\'](.*?)["\'][^>]*>', adjust_path, adjusted_content)
- return adjusted_content
- def concatenate_markdown_files(root_folder, output_file):
- """
- Searches for markdown (.md) files starting from the root folder, adjusts their image paths,
- concatenates their contents, and writes them to a specified output markdown file.
- :param root_folder: The root directory to search for markdown files.
- :param output_file: The file path for the output markdown file.
- """
- concatenated_content = ""
- for subdir, dirs, files in os.walk(root_folder):
- for file in files:
- if file.endswith('.md'):
- file_path = os.path.join(subdir, file)
- with open(file_path, 'r') as md_file:
- file_content = md_file.read()
- # Adjust image paths
- file_content = adjust_image_paths(file_content, file_path, root_folder)
- concatenated_content += file_content + '\n\n'
- with open(output_file, 'w') as output_md_file:
- output_md_file.write(concatenated_content)
- if __name__ == "__main__":
- if len(sys.argv) != 3:
- print("Usage: python script.py <root_folder> <output_file>")
- sys.exit(1)
- root_folder = sys.argv[1]
- output_file = sys.argv[2]
- concatenate_markdown_files(root_folder, output_file)
- print(f"All markdown files from {root_folder} have been concatenated into {output_file}.")
|