1+ import glob
2+ import os
13import sys
24from pathlib import Path
35
6+
7+ def get_version ():
8+ """Read version from inference/core/version.py"""
9+ current_path = Path (__file__ ).resolve ()
10+ for parent in current_path .parents :
11+ if (parent / 'inference' ).is_dir ():
12+ repo_root = parent
13+ break
14+ else :
15+ raise FileNotFoundError ("Could not find repository root with 'inference' directory" )
16+
17+ version_file_path = repo_root .joinpath ('inference' , 'core' , 'version.py' )
18+
19+ try :
20+ namespace = {}
21+ with open (version_file_path , 'r' ) as f :
22+ exec (f .read (), namespace )
23+ return namespace ['__version__' ]
24+ except Exception as e :
25+ print (f"Warning: Could not read version from { version_file_path } : { e } " )
26+ return "unknown"
27+
28+
429def define_env (env ):
530 """Hook function to define macros for MkDocs."""
6-
7- @env .macro
8- def get_version ():
9- """Read version from inference/core/version.py"""
10- # Find the root of the repository by iterating up parent directories
11- current_path = Path (__file__ ).resolve ()
12- for parent in current_path .parents :
13- # Check if this directory contains the 'inference' subdirectory
14- if (parent / 'inference' ).is_dir ():
15- repo_root = parent
16- break
17- else :
18- raise FileNotFoundError ("Could not find repository root with 'inference' directory" )
19-
20- version_file_path = repo_root .joinpath ('inference' , 'core' , 'version.py' )
21-
22- try :
23- # Execute the version.py file and extract __version__
24- namespace = {}
25- with open (version_file_path , 'r' ) as f :
26- exec (f .read (), namespace )
27- return namespace ['__version__' ]
28- except Exception as e :
29- print (f"Warning: Could not read version from { version_file_path } : { e } " )
30- return "unknown"
31-
32- # Make VERSION available globally to all templates
33- env .variables ['VERSION' ] = get_version ()
31+ env .macro (get_version )
32+ env .variables ['VERSION' ] = get_version ()
33+
34+
35+ def replace_in_docs (docs_dir : str ) -> int :
36+ """Replace ``{{ VERSION }}`` in all Markdown files under *docs_dir*.
37+
38+ Zensical does not run MkDocs plugins, so this can be called as a
39+ pre-build step to perform the substitution that mkdocs-macros would
40+ normally handle.
41+
42+ Returns the number of files modified.
43+ """
44+ version = get_version ()
45+ replacements = {
46+ "{{ VERSION }}" : version ,
47+ }
48+
49+ modified = 0
50+ for md_file in sorted (
51+ glob .glob (os .path .join (docs_dir , "**" , "*.md" ), recursive = True )
52+ ):
53+ with open (md_file , encoding = "utf-8" ) as fh :
54+ content = fh .read ()
55+
56+ new_content = content
57+ for placeholder , value in replacements .items ():
58+ new_content = new_content .replace (placeholder , value )
59+
60+ if new_content != content :
61+ with open (md_file , "w" , encoding = "utf-8" ) as fh :
62+ fh .write (new_content )
63+ modified += 1
64+ print (f" replaced variables in { os .path .relpath (md_file , docs_dir )} " )
65+
66+ return modified
67+
68+
69+ if __name__ == "__main__" :
70+ docs_dir = sys .argv [1 ] if len (sys .argv ) > 1 else os .path .join (
71+ os .path .dirname (__file__ ), ".." ,
72+ )
73+ docs_dir = os .path .abspath (docs_dir )
74+
75+ version = get_version ()
76+ print (f"Replacing template variables in { docs_dir } (VERSION={ version } ) ..." )
77+ count = replace_in_docs (docs_dir )
78+ print (f"Done - { count } file(s) updated." )
0 commit comments