1111from __future__ import annotations
1212
1313import argparse
14+ import re
1415import sys
1516from dataclasses import fields
1617from pathlib import Path
2627
2728DOCS_PATH = Path (__file__ ).parent .parent / "docs" / "supported_formats.md"
2829
30+ # Markers for auto-generated content
31+ BEGIN_MARKER = "<!-- BEGIN AUTO-GENERATED SUPPORTED FEATURES -->"
32+ END_MARKER = "<!-- END AUTO-GENERATED SUPPORTED FEATURES -->"
33+
2934# Status emoji mapping
3035STATUS_EMOJI = {
3136 "supported" : "✅" ,
@@ -114,10 +119,9 @@ def print_features_summary() -> None:
114119 print ()
115120
116121
117- def generate_supported_features_table () -> str :
118- """Generate the supported features table for documentation."""
122+ def generate_supported_features_content () -> str :
123+ """Generate the supported features content for documentation (without markers) ."""
119124 lines = [
120- "<!-- BEGIN AUTO-GENERATED SUPPORTED FEATURES -->" ,
121125 "" ,
122126 "### Supported Features (from code)" ,
123127 "" ,
@@ -136,11 +140,68 @@ def generate_supported_features_table() -> str:
136140 openapi_features = [
137141 (name , meta ) for name , meta in get_feature_metadata (OpenAPISchemaFeatures ) if name not in json_field_names
138142 ]
139- lines .extend ((generate_feature_table (openapi_features ), "" , "<!-- END AUTO-GENERATED SUPPORTED FEATURES -->" ))
143+ lines .extend ((generate_feature_table (openapi_features ), "" ))
140144
141145 return "\n " .join (lines )
142146
143147
148+ def generate_supported_features_table () -> str :
149+ """Generate the supported features table with markers for documentation."""
150+ return f"{ BEGIN_MARKER } { generate_supported_features_content ()} { END_MARKER } "
151+
152+
153+ def update_docs_file (* , check : bool = False ) -> int :
154+ """Update docs/supported_formats.md with auto-generated content.
155+
156+ Args:
157+ check: If True, only check if content would change (returns 1 if different).
158+
159+ Returns:
160+ 0 on success, 1 on failure or if check mode detects changes.
161+ """
162+ if not DOCS_PATH .exists ():
163+ print (f"Error: { DOCS_PATH } does not exist" , file = sys .stderr )
164+ return 1
165+
166+ current_content = DOCS_PATH .read_text (encoding = "utf-8" )
167+ new_generated = generate_supported_features_content ()
168+
169+ # Pattern to match content between markers (including markers)
170+ pattern = re .compile (
171+ rf"{ re .escape (BEGIN_MARKER )} .*?{ re .escape (END_MARKER )} " ,
172+ re .DOTALL ,
173+ )
174+
175+ if pattern .search (current_content ):
176+ # Replace existing auto-generated section
177+ new_content = pattern .sub (
178+ f"{ BEGIN_MARKER } { new_generated } { END_MARKER } " ,
179+ current_content ,
180+ )
181+ else :
182+ print (f"Warning: Markers not found in { DOCS_PATH } " , file = sys .stderr )
183+ print ("Please add the following markers to the file:" , file = sys .stderr )
184+ print (f" { BEGIN_MARKER } " , file = sys .stderr )
185+ print (f" { END_MARKER } " , file = sys .stderr )
186+ return 1
187+
188+ if check :
189+ if current_content != new_content :
190+ print (f"Schema docs are out of date: { DOCS_PATH } " , file = sys .stderr )
191+ print ("Run 'python scripts/build_schema_docs.py' to update." , file = sys .stderr )
192+ return 1
193+ print ("Schema docs are up to date." )
194+ return 0
195+
196+ if current_content == new_content :
197+ print ("Schema docs are already up to date." )
198+ return 0
199+
200+ DOCS_PATH .write_text (new_content , encoding = "utf-8" )
201+ print (f"Updated { DOCS_PATH } " )
202+ return 0
203+
204+
144205def main () -> int :
145206 """Parse arguments and build documentation."""
146207 parser = argparse .ArgumentParser (description = "Build schema documentation from code metadata" )
@@ -160,21 +221,7 @@ def main() -> int:
160221 print_features_summary ()
161222 return 0
162223
163- print ("Schema Documentation Builder" )
164- print ("-" * 40 )
165-
166- # For now, just print the generated table
167- print ("\n Generated Supported Features Table:" )
168- print ()
169- print (generate_supported_features_table ())
170-
171- if args .check :
172- print ("\n [Check mode] No files modified." )
173- else :
174- print ("\n [Info] This script currently outputs to stdout." )
175- print (" Future versions will update docs/supported_formats.md directly." )
176-
177- return 0
224+ return update_docs_file (check = args .check )
178225
179226
180227if __name__ == "__main__" :
0 commit comments