-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathembed_code_final.py
More file actions
129 lines (96 loc) · 4.34 KB
/
embed_code_final.py
File metadata and controls
129 lines (96 loc) · 4.34 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
"""
Final Code Embedding Script
Embeds actual code content from code files into Markdown files
"""
import os
import re
import glob
from pathlib import Path
class FinalCodeEmbedder:
def __init__(self):
self.markdown_dir = "manuscript"
self.code_dir = "code"
def embed_code_in_markdown(self, markdown_file):
"""Embed actual code content into Markdown file"""
with open(markdown_file, 'r', encoding='utf-8') as f:
content = f.read()
# Find all code block patterns and embed actual code
content = self.embed_code_blocks(content)
return content
def embed_code_blocks(self, content):
"""Embed actual code content into code blocks"""
# Pattern to find code blocks with file references
pattern = r'```cpp\n// File: ([^\n]+)\n```'
def replace_code_block(match):
file_path = match.group(1)
# Clean up the file path
if file_path.startswith('../code/'):
file_path = file_path[8:] # Remove '../code/'
elif file_path.startswith('code/'):
file_path = file_path[5:] # Remove 'code/'
# Construct full path
full_path = os.path.join(self.code_dir, file_path)
if os.path.exists(full_path):
try:
with open(full_path, 'r', encoding='utf-8') as f:
code_content = f.read()
return f'```cpp\n{code_content}\n```'
except Exception as e:
print(f"Error reading {full_path}: {e}")
return match.group(0)
else:
print(f"File not found: {full_path}")
return match.group(0)
# Replace all code blocks
content = re.sub(pattern, replace_code_block, content)
# Also handle patterns without the "// File:" prefix
pattern2 = r'```cpp\n# File: ([^\n]+)\n```'
def replace_code_block2(match):
file_path = match.group(1)
# Clean up the file path
if file_path.startswith('../code/'):
file_path = file_path[8:] # Remove '../code/'
elif file_path.startswith('code/'):
file_path = file_path[5:] # Remove 'code/'
# Construct full path
full_path = os.path.join(self.code_dir, file_path)
if os.path.exists(full_path):
try:
with open(full_path, 'r', encoding='utf-8') as f:
code_content = f.read()
return f'```cpp\n{code_content}\n```'
except Exception as e:
print(f"Error reading {full_path}: {e}")
return match.group(0)
else:
print(f"File not found: {full_path}")
return match.group(0)
# Replace all code blocks
content = re.sub(pattern2, replace_code_block2, content)
return content
def process_all_markdown_files(self):
"""Process all Markdown files to embed code"""
# Get all .md files
md_files = glob.glob(os.path.join(self.markdown_dir, "*.md"))
for md_file in md_files:
filename = os.path.basename(md_file)
print(f"Processing {filename}...")
try:
updated_content = self.embed_code_in_markdown(md_file)
with open(md_file, 'w', encoding='utf-8') as f:
f.write(updated_content)
print(f"✓ Successfully updated {filename}")
except Exception as e:
print(f"✗ Error processing {filename}: {e}")
def main():
embedder = FinalCodeEmbedder()
print("🔄 Starting final code embedding in Markdown files...")
print("=" * 50)
# Process all Markdown files
embedder.process_all_markdown_files()
print("=" * 50)
print("✅ Final code embedding completed!")
print("\n📁 Check the 'manuscript' directory for updated files with embedded code.")
if __name__ == "__main__":
main()