feat: Replace wordcount with find_original_update_blocks in testsr.py

This commit is contained in:
Paul Gauthier (aider) 2024-08-22 06:34:19 -07:00
parent 3272a2b84e
commit 7e763094b3

View file

@ -1,11 +1,7 @@
import re
import sys
def wordcount(text):
"""Count the number of words in the given text."""
return len(text.split())
import json
from aider.coders.editblock_coder import find_original_update_blocks, DEFAULT_FENCE
def process_markdown(filename):
try:
@ -15,6 +11,7 @@ def process_markdown(filename):
# Split the content into sections based on '####' headers
sections = re.split(r"(?=####\s)", content)
results = []
for section in sections:
if section.strip(): # Ignore empty sections
# Extract the header (if present)
@ -22,19 +19,41 @@ def process_markdown(filename):
# Get the content (everything after the header)
content = "\n".join(section.split("\n")[1:]).strip()
# Count words
count = wordcount(content)
# Process the content with find_original_update_blocks
blocks = list(find_original_update_blocks(content, DEFAULT_FENCE))
print(f"{header}: {count} words")
# Create a dictionary for this section
section_result = {
"header": header,
"blocks": []
}
for block in blocks:
if block[0] is None: # This is a shell command block
section_result["blocks"].append({
"type": "shell",
"content": block[1]
})
else: # This is a SEARCH/REPLACE block
section_result["blocks"].append({
"type": "search_replace",
"filename": block[0],
"original": block[1],
"updated": block[2]
})
results.append(section_result)
# Output the results as JSON
print(json.dumps(results, indent=2))
except FileNotFoundError:
print(f"Error: File '{filename}' not found.")
print(json.dumps({"error": f"File '{filename}' not found."}))
except Exception as e:
print(f"An error occurred: {e}")
print(json.dumps({"error": f"An error occurred: {str(e)}"}))
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python testsr.py <markdown_filename>")
print(json.dumps({"error": "Usage: python testsr.py <markdown_filename>"}))
else:
process_markdown(sys.argv[1])