Skip to content

Commit

Permalink
Merge pull request #4 from raphaelmansuy/feat/lot1
Browse files Browse the repository at this point in the history
Feat/lot1
  • Loading branch information
raphaelmansuy committed Jun 28, 2024
2 parents 8ea6888 + 4650df0 commit f17aeb1
Show file tree
Hide file tree
Showing 11 changed files with 370 additions and 146 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Alternatively, you can install Code2Prompt using pipx, a tool for installing and

2. Install Code2Prompt using pipx:
```
pipx install git+https://github.com/raphael.mansuy/code2prompt.git
pipx install git+https://github.com/raphaelmansuy/code2prompt.git
```

This command will clone the Code2Prompt repository and install it in an isolated environment managed by pipx.
Expand Down
5 changes: 0 additions & 5 deletions TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,3 @@ This template groups files by language and creates separate sections for Python
3. Create user-defined variables for dynamic content that you want to input at runtime.
4. Use the `files` list to iterate over all processed files and access their properties.
5. Remember that the `content` of each file is already processed according to the command-line options (e.g., comments stripped if `--suppress-comments` was used).
By leveraging these templating capabilities, you can create highly customized outputs tailored to your specific needs for code analysis and documentation.
Citations:
[1] https://ppl-ai-file-upload.s3.amazonaws.com/web/direct-files/585370/cbd51bc7-ed1f-4321-9a7b-b756920ca298/paste.txt
21 changes: 21 additions & 0 deletions code2prompt/count_tokens.py
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
77 changes: 77 additions & 0 deletions code2prompt/create_template_directory.py
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}")
26 changes: 26 additions & 0 deletions code2prompt/generate_content.py
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'])
27 changes: 27 additions & 0 deletions code2prompt/get_gitignore_patterns.py
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
Loading

0 comments on commit f17aeb1

Please sign in to comment.