33Inject minified code into documentation markdown files.
44This script reads minified versions from cp-algo/min/ and cp-algo/min-bundled/
55and adds minifiedCode and minifiedBundledCode fields to the documentation markdown files.
6+ For test files, generates minified versions on-the-fly without storing them.
67"""
78
89import os
1112import re
1213import json
1314import yaml
15+ import subprocess
1416
1517
1618class _LiteralDumper (yaml .SafeDumper ):
@@ -27,6 +29,48 @@ def _str_presenter(dumper, data):
2729_LiteralDumper .add_representer (str , _str_presenter )
2830
2931
32+ def minify_code (code ):
33+ """Minify C++ code on-the-fly using the minify utility."""
34+ try :
35+ result = subprocess .run (
36+ ['python3' , 'cp-algo/util/minify.py' ],
37+ input = code ,
38+ capture_output = True ,
39+ text = True ,
40+ check = True
41+ )
42+ return result .stdout
43+ except subprocess .CalledProcessError as e :
44+ print (f"Warning: Failed to minify code: { e } " , file = sys .stderr )
45+ return None
46+ except FileNotFoundError :
47+ print ("Warning: minify.py not found, skipping minification" , file = sys .stderr )
48+ return None
49+
50+
51+ def bundle_and_minify (source_file ):
52+ """Bundle and minify a test file using oj-bundle and minify.py."""
53+ try :
54+ # First bundle with oj-bundle
55+ bundle_result = subprocess .run (
56+ ['oj-bundle' , str (source_file )],
57+ capture_output = True ,
58+ text = True ,
59+ check = True
60+ )
61+ bundled_code = bundle_result .stdout
62+
63+ # Then minify
64+ minified = minify_code (bundled_code )
65+ return minified
66+ except subprocess .CalledProcessError as e :
67+ print (f"Warning: Failed to bundle/minify { source_file } : { e } " , file = sys .stderr )
68+ return None
69+ except FileNotFoundError :
70+ print ("Warning: oj-bundle not found, skipping bundled minification" , file = sys .stderr )
71+ return None
72+
73+
3074def inject_minified_to_markdown (markdown_file , minified_code = None , minified_bundled_code = None ):
3175 """Inject minified code into markdown file's front matter using proper YAML parsing."""
3276 try :
@@ -131,10 +175,15 @@ def main():
131175 rel_path = md_file .relative_to (markdown_dir )
132176 path_without_ext = str (rel_path )[:- 3 ] # Remove trailing .md
133177
178+ # Determine if this is a test file or library file
179+ is_test_file = path_without_ext .startswith ('verify/' )
180+
134181 # Drop the original source extension so we don't end up with double extensions
182+ original_ext = None
135183 for src_ext in ('.hpp' , '.cpp' , '.h' ):
136184 if path_without_ext .endswith (src_ext ):
137185 path_without_ext = path_without_ext [: - len (src_ext )]
186+ original_ext = src_ext
138187 break
139188
140189 # Strip cp-algo/ prefix if present since min/min-bundled dirs don't duplicate it
@@ -146,33 +195,48 @@ def main():
146195 minified_code = None
147196 minified_bundled_code = None
148197
149- # Try to find corresponding minified source file
150- possible_extensions = ['hpp' , 'cpp' , 'h' ]
151-
152- for ext in possible_extensions :
153- if minified_code is None and minified_dir .exists ():
154- minified_file = minified_dir / f"{ path_in_min } .{ ext } "
155- if minified_file .exists ():
156- with open (minified_file , 'r' , encoding = 'utf-8' ) as f :
157- minified_code = f .read ()
158- break
159-
160- # Try to find corresponding minified bundled file
161- # Try both with and without cp-algo/ prefix for backwards compatibility
162- for ext in possible_extensions :
163- if minified_bundled_code is None and minified_bundled_dir .exists ():
164- # Try without prefix first (correct structure after fix)
165- minified_bundled_file = minified_bundled_dir / f"{ path_in_min } .{ ext } "
166- if minified_bundled_file .exists ():
167- with open (minified_bundled_file , 'r' , encoding = 'utf-8' ) as f :
168- minified_bundled_code = f .read ()
169- break
170- # Also try with cp-algo/ prefix (old nested structure)
171- minified_bundled_file = minified_bundled_dir / f"cp-algo/{ path_in_min } .{ ext } "
172- if minified_bundled_file .exists ():
173- with open (minified_bundled_file , 'r' , encoding = 'utf-8' ) as f :
174- minified_bundled_code = f .read ()
175- break
198+ if is_test_file :
199+ # For test files, generate minified versions on-the-fly from the source file
200+ # Reconstruct source file path
201+ if original_ext :
202+ source_file = Path (path_without_ext + original_ext )
203+ if source_file .exists ():
204+ # Read the source and minify directly
205+ with open (source_file , 'r' , encoding = 'utf-8' ) as f :
206+ source_code = f .read ()
207+ minified_code = minify_code (source_code )
208+
209+ # Generate bundled+minified version
210+ minified_bundled_code = bundle_and_minify (source_file )
211+ else :
212+ # For library files, use pre-generated minified versions
213+ # Try to find corresponding minified source file
214+ possible_extensions = ['hpp' , 'cpp' , 'h' ]
215+
216+ for ext in possible_extensions :
217+ if minified_code is None and minified_dir .exists ():
218+ minified_file = minified_dir / f"{ path_in_min } .{ ext } "
219+ if minified_file .exists ():
220+ with open (minified_file , 'r' , encoding = 'utf-8' ) as f :
221+ minified_code = f .read ()
222+ break
223+
224+ # Try to find corresponding minified bundled file
225+ # Try both with and without cp-algo/ prefix for backwards compatibility
226+ for ext in possible_extensions :
227+ if minified_bundled_code is None and minified_bundled_dir .exists ():
228+ # Try without prefix first (correct structure after fix)
229+ minified_bundled_file = minified_bundled_dir / f"{ path_in_min } .{ ext } "
230+ if minified_bundled_file .exists ():
231+ with open (minified_bundled_file , 'r' , encoding = 'utf-8' ) as f :
232+ minified_bundled_code = f .read ()
233+ break
234+ # Also try with cp-algo/ prefix (old nested structure)
235+ minified_bundled_file = minified_bundled_dir / f"cp-algo/{ path_in_min } .{ ext } "
236+ if minified_bundled_file .exists ():
237+ with open (minified_bundled_file , 'r' , encoding = 'utf-8' ) as f :
238+ minified_bundled_code = f .read ()
239+ break
176240
177241 # Only inject if we found at least one minified version
178242 if (minified_code or minified_bundled_code ):
0 commit comments