@@ -29,7 +29,7 @@ def get_minified_code(file_path, minified_dir):
2929 return None
3030
3131
32- def inject_minified_to_markdown (markdown_file , minified_code ):
32+ def inject_minified_to_markdown (markdown_file , minified_code = None , minified_bundled_code = None ):
3333 """Inject minified code into markdown file's front matter."""
3434 try :
3535 with open (markdown_file , 'r' , encoding = 'utf-8' ) as f :
@@ -47,27 +47,65 @@ def inject_minified_to_markdown(markdown_file, minified_code):
4747 front_matter = parts [1 ]
4848 body = parts [2 ]
4949
50- # Parse front matter to check if minifiedCode already exists
51- if 'minifiedCode:' in front_matter :
52- # Replace existing minifiedCode
53- front_matter = re .sub (
54- r' minifiedCode: ".*?"(?=\n [a-zA-Z_]|\n$)' ,
55- f' minifiedCode: "{ minified_code } "' ,
56- front_matter ,
57- flags = re .DOTALL
58- )
59- else :
60- # Add minifiedCode after bundledCode if it exists
61- if 'bundledCode:' in front_matter :
50+ updated = False
51+
52+ # Handle minifiedCode
53+ if minified_code :
54+ if 'minifiedCode:' in front_matter :
55+ # Replace existing minifiedCode
6256 front_matter = re .sub (
63- r'( bundledCode : ".*?")( \n [a-zA-Z_]|\n$)' ,
64- rf'\1\n minifiedCode: "{ minified_code } "\2 ' ,
57+ r' minifiedCode : ".*?"(?= \n [a-zA-Z_]|\n$)' ,
58+ f' minifiedCode: "{ minified_code } "' ,
6559 front_matter ,
6660 flags = re .DOTALL
6761 )
6862 else :
69- # Add at the end of front matter
70- front_matter = front_matter .rstrip () + f'\n minifiedCode: "{ minified_code } "'
63+ # Add minifiedCode after bundledCode if it exists
64+ if 'bundledCode:' in front_matter :
65+ front_matter = re .sub (
66+ r'( bundledCode: ".*?")(\n [a-zA-Z_]|\n$)' ,
67+ rf'\1\n minifiedCode: "{ minified_code } "\2' ,
68+ front_matter ,
69+ flags = re .DOTALL
70+ )
71+ else :
72+ # Add at the end of front matter
73+ front_matter = front_matter .rstrip () + f'\n minifiedCode: "{ minified_code } "'
74+ updated = True
75+
76+ # Handle minifiedBundledCode
77+ if minified_bundled_code :
78+ if 'minifiedBundledCode:' in front_matter :
79+ # Replace existing minifiedBundledCode
80+ front_matter = re .sub (
81+ r' minifiedBundledCode: ".*?"(?=\n [a-zA-Z_]|\n$)' ,
82+ f' minifiedBundledCode: "{ minified_bundled_code } "' ,
83+ front_matter ,
84+ flags = re .DOTALL
85+ )
86+ else :
87+ # Add minifiedBundledCode after minifiedCode if it exists
88+ if 'minifiedCode:' in front_matter :
89+ front_matter = re .sub (
90+ r'( minifiedCode: ".*?")(\n [a-zA-Z_]|\n$)' ,
91+ rf'\1\n minifiedBundledCode: "{ minified_bundled_code } "\2' ,
92+ front_matter ,
93+ flags = re .DOTALL
94+ )
95+ elif 'bundledCode:' in front_matter :
96+ front_matter = re .sub (
97+ r'( bundledCode: ".*?")(\n [a-zA-Z_]|\n$)' ,
98+ rf'\1\n minifiedBundledCode: "{ minified_bundled_code } "\2' ,
99+ front_matter ,
100+ flags = re .DOTALL
101+ )
102+ else :
103+ # Add at the end of front matter
104+ front_matter = front_matter .rstrip () + f'\n minifiedBundledCode: "{ minified_bundled_code } "'
105+ updated = True
106+
107+ if not updated :
108+ return False
71109
72110 # Write updated content
73111 new_content = f'---{ front_matter } ---{ body } '
@@ -81,12 +119,19 @@ def inject_minified_to_markdown(markdown_file, minified_code):
81119
82120
83121def main ():
84- markdown_dir = Path ('.verify-helper/markdown ' )
122+ markdown_dir = Path ('_jekyll ' )
85123 minified_dir = Path ('cp-algo/min' )
124+ minified_bundled_dir = Path ('cp-algo/min-bundled' )
125+
126+ # If _jekyll doesn't exist, try the verify-helper path (for local testing)
127+ if not markdown_dir .exists ():
128+ markdown_dir = Path ('.verify-helper/markdown' )
86129
87130 if not minified_dir .exists ():
88131 print (f"Warning: { minified_dir } does not exist" , file = sys .stderr )
89- return 0
132+
133+ if not minified_bundled_dir .exists ():
134+ print (f"Warning: { minified_bundled_dir } does not exist" , file = sys .stderr )
90135
91136 if not markdown_dir .exists ():
92137 print (f"Error: { markdown_dir } does not exist" , file = sys .stderr )
@@ -95,29 +140,45 @@ def main():
95140 print ("Injecting minified code into documentation..." )
96141
97142 count = 0
98- # Find all markdown files with bundledCode
143+ # Find all markdown files
99144 for md_file in markdown_dir .rglob ('*.md' ):
100145 # Get relative path without .md extension
101146 rel_path = md_file .relative_to (markdown_dir )
102147 path_without_ext = str (rel_path )[:- 3 ] # Remove .md
103148
104- # Try to find corresponding minified file
105- possible_extensions = ['hpp' , 'cpp' , 'h' ]
106149 minified_code = None
150+ minified_bundled_code = None
151+
152+ # Try to find corresponding minified source file
153+ possible_extensions = ['hpp' , 'cpp' , 'h' ]
154+
155+ for ext in possible_extensions :
156+ if minified_code is None and minified_dir .exists ():
157+ minified_file = minified_dir / f"{ path_without_ext } .{ ext } "
158+ if minified_file .exists ():
159+ with open (minified_file , 'r' , encoding = 'utf-8' ) as f :
160+ code = f .read ()
161+ # Escape for YAML
162+ code = code .replace ('\\ ' , '\\ \\ ' )
163+ code = code .replace ('"' , '\\ "' )
164+ code = code .replace ('\n ' , '\\ n' )
165+ minified_code = code
107166
167+ # Try to find corresponding minified bundled file
108168 for ext in possible_extensions :
109- minified_file = minified_dir / f" { path_without_ext } . { ext } "
110- if minified_file . exists ():
111- with open ( minified_file , 'r' , encoding = 'utf-8' ) as f :
112- code = f . read ()
113- # Escape for YAML
114- code = code . replace ( ' \\ ' , ' \\ \\ ' )
115- code = code .replace ('" ' , '\\ " ' )
116- code = code .replace ('\n ' , '\\ n ' )
117- minified_code = code
118- break
169+ if minified_bundled_code is None and minified_bundled_dir . exists ():
170+ minified_bundled_file = minified_bundled_dir / f" { path_without_ext } . { ext } "
171+ if minified_bundled_file . exists () :
172+ with open ( minified_bundled_file , 'r' , encoding = 'utf-8' ) as f :
173+ code = f . read ()
174+ # Escape for YAML
175+ code = code .replace ('\\ ' , '\\ \\ ' )
176+ code = code .replace ('" ' , '\\ " ' )
177+ code = code . replace ( ' \n ' , ' \\ n' )
178+ minified_bundled_code = code
119179
120- if minified_code and inject_minified_to_markdown (md_file , minified_code ):
180+ # Only inject if we found at least one minified version
181+ if (minified_code or minified_bundled_code ) and inject_minified_to_markdown (md_file , minified_code , minified_bundled_code ):
121182 count += 1
122183 print (f" Updated: { path_without_ext } " )
123184
0 commit comments