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.
This commit is contained in:
Paul Gauthier 2023-07-22 09:44:58 -03:00
parent 11dd753a8c
commit 6c7b6fc9e4

View file

@ -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):