1616from pathlib import Path
1717
1818
19- def extract_lines (file_path : Path , start_line : int , end_line : int ) -> str :
20- """Extract lines from a file.
21-
22- Args:
23- file_path: Path to the file
24- start_line: Starting line number (1-based)
25- end_line: Ending line number (1-based, inclusive)
26-
27- Returns:
28- The extracted lines
29- """
30- content = file_path .read_text ()
31- lines = content .splitlines ()
32- # Convert to 0-based indexing
33- start_idx = max (0 , start_line - 1 )
34- end_idx = min (len (lines ), end_line )
35- return "\n " .join (lines [start_idx :end_idx ])
36-
37-
38- def get_github_url (file_path : str , start_line : int , end_line : int ) -> str :
39- """Generate a GitHub URL for the file with line highlighting.
19+ def get_github_url (file_path : str ) -> str :
20+ """Generate a GitHub URL for the file.
4021
4122 Args:
4223 file_path: Path to the file relative to repo root
43- start_line: Starting line number
44- end_line: Ending line number
4524
4625 Returns:
47- GitHub URL with line highlighting
26+ GitHub URL
4827 """
4928 base_url = "https://github.com/modelcontextprotocol/python-sdk/blob/main"
50- url = f"{ base_url } /{ file_path } "
51-
52- if start_line == end_line :
53- url += f"#L{ start_line } "
54- else :
55- url += f"#L{ start_line } -L{ end_line } "
56-
57- return url
29+ return f"{ base_url } /{ file_path } "
5830
5931
6032def process_snippet_block (match : re .Match , check_mode : bool = False ) -> str :
@@ -70,32 +42,20 @@ def process_snippet_block(match: re.Match, check_mode: bool = False) -> str:
7042 full_match = match .group (0 )
7143 indent = match .group (1 )
7244 file_path = match .group (2 )
73- start_line = match .group (3 ) # May be None
74- end_line = match .group (4 ) # May be None
7545
7646 try :
77- # Read and extract the code
47+ # Read the entire file
7848 file = Path (file_path )
7949 if not file .exists ():
8050 print (f"Warning: File not found: { file_path } " )
8151 return full_match
8252
83- if start_line and end_line :
84- # Line range specified
85- start_line = int (start_line )
86- end_line = int (end_line )
87- code = extract_lines (file , start_line , end_line )
88- github_url = get_github_url (file_path , start_line , end_line )
89- line_ref = f"#L{ start_line } -L{ end_line } "
90- else :
91- # No line range - use whole file
92- code = file .read_text ().rstrip ()
93- github_url = f"https://github.com/modelcontextprotocol/python-sdk/blob/main/{ file_path } "
94- line_ref = ""
53+ code = file .read_text ().rstrip ()
54+ github_url = get_github_url (file_path )
9555
9656 # Build the replacement block
9757 indented_code = code .replace ('\n ' , f'\n { indent } ' )
98- replacement = f"""{ indent } <!-- snippet-source { file_path } { line_ref } -->
58+ replacement = f"""{ indent } <!-- snippet-source { file_path } -->
9959{ indent } ```python
10060{ indent } { indented_code }
10161{ indent } ```
@@ -105,7 +65,7 @@ def process_snippet_block(match: re.Match, check_mode: bool = False) -> str:
10565 # In check mode, only check if code has changed
10666 if check_mode :
10767 # Extract existing code from the match
108- existing_content = match .group (5 )
68+ existing_content = match .group (3 )
10969 if existing_content is not None :
11070 existing_lines = existing_content .strip ().split ("\n " )
11171 # Find code between ```python and ```
@@ -119,13 +79,15 @@ def process_snippet_block(match: re.Match, check_mode: bool = False) -> str:
11979 elif in_code :
12080 code_lines .append (line )
12181 existing_code = "\n " .join (code_lines ).strip ()
122- if existing_code == code .strip ():
82+ # Need to remove the indent from existing code for comparison
83+ dedented_existing = "\n " .join (line .lstrip () for line in existing_code .split ("\n " ))
84+ if dedented_existing == code .strip ():
12385 return full_match
12486
12587 return replacement
12688
12789 except Exception as e :
128- print (f"Error processing { file_path } #L { start_line } -L { end_line } : { e } " )
90+ print (f"Error processing { file_path } : { e } " )
12991 return full_match
13092
13193
@@ -146,12 +108,11 @@ def update_readme_snippets(readme_path: Path = Path("README.md"), check_mode: bo
146108 content = readme_path .read_text ()
147109 original_content = content
148110
149- # Pattern to match snippet-source blocks with optional line ranges
111+ # Pattern to match snippet-source blocks
150112 # Matches: <!-- snippet-source path/to/file.py -->
151- # or: <!-- snippet-source path/to/file.py#L10-L20 -->
152113 # ... any content ...
153114 # <!-- /snippet-source -->
154- pattern = r"^(\s*)<!-- snippet-source ([^# \s]+)(?:#L(\d+)-L(\d+))? -->\n" r"(.*?)" r"^\1<!-- /snippet-source -->"
115+ pattern = r"^(\s*)<!-- snippet-source ([^\s]+) -->\n" r"(.*?)" r"^\1<!-- /snippet-source -->"
155116
156117 # Process all snippet-source blocks
157118 updated_content = re .sub (
0 commit comments