From 6c7b6fc9e4ef14a6d683e58d7e417bff49b0fe3d Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 22 Jul 2023 09:44:58 -0300 Subject: [PATCH] aider: Added a main function to `aider/history.py` that accepts a filename as a command-line argument, reads the file, and parses the markdown structure. # Aider chat conversation: USER: when run as a script, take a filename on the cmd line. read markdown from the file. parse the markdown structure, so we can iterate through # and #### seactions and do things like discard quoted text > sections easily. ASSISTANT: To achieve this, we need to modify the `aider/history.py` file to include a main function that accepts a filename as a command-line argument, reads the file, and parses the markdown structure. We will use the `argparse` module to handle command-line arguments and the `markdown` module to parse the markdown file. Here are the changes: ```python aider/history.py <<<<<<< ORIGINAL import json import tiktoken from prompt_toolkit.completion import Completion from aider import prompts from .dump import dump # noqa: F401 ======= import argparse import json import markdown import tiktoken from prompt_toolkit.completion import Completion from aider import prompts from .dump import dump # noqa: F401 def main(): parser = argparse.ArgumentParser() parser.add_argument("filename", help="Markdown file to parse") args = parser.parse_args() with open(args.filename, 'r') as f: text = f.read() md = markdown.Markdown() tree = md.parse(text) for element in tree.getiterator(): if element.tag in ['h1', 'h4'] and element.text is not None: print(element.text) elif element.tag == 'blockquote': continue else: print(element.text) if __name__ == "__main__": main() >>>>>>> UPDATED ``` This script will print the text of all `#` and `####` sections and ignore quoted text `>` sections. Other text will be printed as is. If you want to handle other markdown elements differently, you can add more conditions in the for loop. --- aider/history.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/aider/history.py b/aider/history.py index f38e98e86..e0d8d64a8 100644 --- a/aider/history.py +++ b/aider/history.py @@ -1,4 +1,6 @@ +import argparse import json +import markdown import tiktoken from prompt_toolkit.completion import Completion @@ -7,6 +9,28 @@ from aider import prompts from .dump import dump # noqa: F401 +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("filename", help="Markdown file to parse") + args = parser.parse_args() + + with open(args.filename, 'r') as f: + text = f.read() + + md = markdown.Markdown() + tree = md.parse(text) + + for element in tree.getiterator(): + if element.tag in ['h1', 'h4'] and element.text is not None: + print(element.text) + elif element.tag == 'blockquote': + continue + else: + print(element.text) + +if __name__ == "__main__": + main() + class ChatSummary: def __init__(self, model, max_tokens=1024):