|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import markdown2 |
| 4 | + |
| 5 | +def adjust_image_paths(content, current_file_dir, output_base_dir): |
| 6 | + """ |
| 7 | + Adjust relative image paths to be relative to the output base directory and use forward slashes. |
| 8 | + """ |
| 9 | + lines = content.split('\n') |
| 10 | + for i, line in enumerate(lines): |
| 11 | + if '](' in line: |
| 12 | + #if ' + 2 |
| 14 | + end_link = line.find(')', start_link) |
| 15 | + relative_path = line[start_link:end_link] |
| 16 | + if not os.path.isabs(relative_path): |
| 17 | + # Calculate the relative path from the current file directory to the image |
| 18 | + image_path = os.path.normpath(os.path.join(current_file_dir, relative_path)) |
| 19 | + relative_to_output = os.path.relpath(image_path, output_base_dir) |
| 20 | + # Convert backslashes to forward slashes |
| 21 | + relative_to_output = relative_to_output.replace('\\', '/') |
| 22 | + lines[i] = line[:start_link] + relative_to_output + line[end_link:] |
| 23 | + return '\n'.join(lines) |
| 24 | + |
| 25 | +def assemble_markdown_files(index_file, output_file_base): |
| 26 | + markdown_content = "" |
| 27 | + toc_content = "" |
| 28 | + base_path = os.path.dirname(os.path.abspath(index_file)) |
| 29 | + output_base_dir = os.path.dirname(os.path.abspath(output_file_base + '.md')) |
| 30 | + |
| 31 | + with open(index_file, 'r') as file: |
| 32 | + lines = file.readlines() |
| 33 | + |
| 34 | + for line in lines: |
| 35 | + if ' [' in line: |
| 36 | + start_link = line.find('(') + 1 |
| 37 | + end_link = line.find(')') |
| 38 | + file_link = line[start_link:end_link].strip() |
| 39 | + file_path = os.path.normpath(os.path.join(base_path, file_link)) |
| 40 | + |
| 41 | + # Generate clean anchors for ToC links |
| 42 | + anchor_link = f'#{os.path.basename(file_link).replace(".md", "").replace(" ", "-").lower()}' |
| 43 | + toc_line = line.replace(file_link, anchor_link) |
| 44 | + toc_content += toc_line |
| 45 | + |
| 46 | + # Check file type and skip non-markdown files |
| 47 | + if file_link.lower().endswith('.md') and os.path.exists(file_path): |
| 48 | + with open(file_path, 'r') as infile: |
| 49 | + file_contents = infile.read() |
| 50 | + file_contents = adjust_image_paths(file_contents, os.path.dirname(file_path), output_base_dir) |
| 51 | + file_contents = file_contents.replace("\n#", "\n##") # Downgrade headers |
| 52 | + markdown_content += f"\n<a id='{os.path.basename(file_link).replace('.md', '').replace(' ', '-').lower()}'></a>\n\n----\n\n" |
| 53 | + markdown_content += file_contents + '\n' |
| 54 | + elif not file_link.lower().endswith('.md'): |
| 55 | + toc_content += f" (External file not included in compilation)\n" |
| 56 | + else: |
| 57 | + markdown_content += f"# File not found: {file_link}\n\n" |
| 58 | + else: |
| 59 | + toc_content += line |
| 60 | + markdown_content += line |
| 61 | + |
| 62 | + complete_markdown = toc_content + markdown_content |
| 63 | + with open(output_file_base + '.md', 'w') as outfile: |
| 64 | + outfile.write(complete_markdown) |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + index_filename = 'README.md' |
| 68 | + output_filename_base = 'Complete' |
| 69 | + |
| 70 | + if len(sys.argv) > 1: |
| 71 | + index_filename = sys.argv[1] |
| 72 | + if len(sys.argv) > 2: |
| 73 | + output_filename_base = sys.argv[2] |
| 74 | + |
| 75 | + assemble_markdown_files(index_filename, output_filename_base) |
0 commit comments