forked from raphaelmansuy/code2prompt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request raphaelmansuy#4 from raphaelmansuy/feat/lot1
Feat/lot1
- Loading branch information
Showing
11 changed files
with
370 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import click | ||
import tiktoken | ||
|
||
|
||
def count_tokens(text: str, encoding: str) -> int: | ||
""" | ||
Count the number of tokens in the given text using the specified encoding. | ||
Args: | ||
text (str): The text to tokenize and count. | ||
encoding (str): The encoding to use for tokenization. | ||
Returns: | ||
int: The number of tokens in the text. | ||
""" | ||
try: | ||
encoder = tiktoken.get_encoding(encoding) | ||
return len(encoder.encode(text)) | ||
except Exception as e: | ||
click.echo(f"Error counting tokens: {str(e)}", err=True) | ||
return 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from pathlib import Path | ||
|
||
|
||
def create_templates_directory(): | ||
""" | ||
Create a 'templates' directory in the current working directory and | ||
populate it with example template files. | ||
""" | ||
# Define the path for the templates directory | ||
templates_dir = Path.cwd() / "templates" | ||
|
||
# Create the templates directory if it doesn't exist | ||
templates_dir.mkdir(exist_ok=True) | ||
|
||
# Define example templates | ||
example_templates = { | ||
"basic.j2": """# Code Summary | ||
{% for file in files %} | ||
## {{ file.path }} | ||
```{{ file.language }} | ||
{{ file.content }} | ||
``` | ||
{% endfor %} | ||
""", | ||
"detailed.j2": """# Project Code Analysis | ||
{% for file in files %} | ||
## File: {{ file.path }} | ||
- **Language**: {{ file.language }} | ||
- **Size**: {{ file.size }} bytes | ||
- **Last Modified**: {{ file.modified }} | ||
### Code: | ||
```{{ file.language }} | ||
{{ file.content }} | ||
``` | ||
### Analysis: | ||
[Your analysis for {{ file.path }} goes here] | ||
{% endfor %} | ||
""", | ||
"custom.md": """# {{ project_name }} | ||
{{ project_description }} | ||
{% for file in files %} | ||
## {{ file.path }} | ||
{{ file_purpose }} | ||
```{{ file.language }} | ||
{{ file.content }} | ||
``` | ||
{% endfor %} | ||
## Next Steps: | ||
{{ next_steps }} | ||
""", | ||
} | ||
|
||
# Write example templates to files | ||
for filename, content in example_templates.items(): | ||
file_path = templates_dir / filename | ||
with file_path.open("w") as f: | ||
f.write(content) | ||
|
||
print(f"Templates directory created at: {templates_dir}") | ||
print("Example templates added:") | ||
for filename, _ in example_templates.items(): | ||
print(f"- {filename}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from code2prompt.template_processor import get_user_inputs, load_template, process_template | ||
from code2prompt.utils.generate_markdown_content import generate_markdown_content | ||
|
||
|
||
def generate_content(files_data, options): | ||
""" | ||
Generate content based on the provided files data and options. | ||
This function either processes a Jinja2 template with the given files data and user inputs | ||
or generates markdown content directly from the files data, depending on whether a | ||
template option is provided. | ||
Args: | ||
files_data (list): A list of dictionaries containing processed file data. | ||
options (dict): A dictionary containing options such as template path and whether | ||
to wrap code inside markdown code blocks. | ||
Returns: | ||
str: The generated content as a string, either from processing a template or | ||
directly generating markdown content. | ||
""" | ||
if options['template']: | ||
template_content = load_template(options['template']) | ||
user_inputs = get_user_inputs(template_content) | ||
return process_template(template_content, files_data, user_inputs) | ||
return generate_markdown_content(files_data, options['no_codeblock']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from code2prompt.utils.parse_gitignore import parse_gitignore | ||
from pathlib import Path | ||
|
||
def get_gitignore_patterns(path, gitignore): | ||
""" | ||
Retrieve gitignore patterns from a specified path or a default .gitignore file. | ||
This function reads the .gitignore file located at the specified path or uses | ||
the default .gitignore file in the project root if no specific path is provided. | ||
It then parses the file to extract ignore patterns and adds a default pattern | ||
to ignore the .git directory itself. | ||
Args: | ||
path (Path): The root path of the project where the default .gitignore file is located. | ||
gitignore (Optional[str]): An optional path to a specific .gitignore file to use instead of the default. | ||
Returns: | ||
Set[str]: A set of gitignore patterns extracted from the .gitignore file. | ||
""" | ||
if gitignore: | ||
gitignore_path = Path(gitignore) | ||
else: | ||
gitignore_path = Path(path) / ".gitignore" | ||
|
||
patterns = parse_gitignore(gitignore_path) | ||
patterns.add(".git") | ||
return patterns |
Oops, something went wrong.