# J2Escape
[![.github/workflows/python-package.yml](https://github.com/jifox/j2escape/actions/workflows/python-package.yml/badge.svg?branch=main)](https://github.com/jifox/j2escape/actions/workflows/python-package.yml)
[![Python 3.7](https://img.shields.io/badge/python-3.7-blue.svg)](https://www.python.org/downloads/release/python-370/)
[![Python 3.8](https://img.shields.io/badge/python-3.8-blue.svg)](https://www.python.org/downloads/release/python-380/)
[![Python 3.9](https://img.shields.io/badge/python-3.9-blue.svg)](https://www.python.org/downloads/release/python-390/)
[![Python 3.10](https://img.shields.io/badge/python-3.10-blue.svg)](https://www.python.org/downloads/release/python-310/)
[![Python 3.11](https://img.shields.io/badge/python-3.11-blue.svg)](https://www.python.org/downloads/release/python-311/)
### Installation
```
pip install j2escape
```
### Overview
This module, written in Python, facilitates the storage of Jinja2 templates within a project managed by [Cookiecutter](https://github.com/cookiecutter/cookiecutter) or [Cruft](https://github.com/cruft/cruft).
Cookiecutter utilizes Jinja templates internally when substituting input variables in the source code. However, this can result in errors if a Jinja template, such as {% if config.allow_duplicates %}, is intended for the application rather than Cookiecutter or Cruft.
To circumvent this problem, this module employs the jinja2.lex() function to parse and appropriately escape the template blocks.
The transformation process is idempotent, meaning that an already escaped template will remain unchanged when escaped again.
Example:
| Template | Escaped Template |
|---|---|
| {{ variable }} | {{ '{{' }} variable {{ '}}' }} |
| {% if config.allow_duplicates %} | {{ '{%' }} if config.allow_duplicates {{ '%}' }} |
The module also provides a command-line interface that can be accessed using the `j2escape` command. This command can be utilized to escape jinja2 tags within a directory of templates.
### j2escape usage
```bash
j2escape --help
usage: j2escape [-h] [-t TEMPLATES] [-o OUTPUT_DIR] [--overwrite] [-c] [-l {DEBUG,INFO,WARNING,ERROR,CRITICAL}] [-f LOGFILE]
Escape jinja2 tags in a directory of templates.
options:
-h, --help show this help message and exit
-t TEMPLATES, --templates TEMPLATES
A comma-separated string of Jinja Templates (*.j2) or a directory with *.j2 files.
-o OUTPUT_DIR, --output-dir OUTPUT_DIR
Specifies the directory path where the escaped templates will be stored.
--overwrite Replaces the original templates. This is necessary if the --output-dir is not provided.
-c, --create-ok Generates the output directory if it doesn’t already exist.
-l {DEBUG,INFO,WARNING,ERROR,CRITICAL}, --loglevel {DEBUG,INFO,WARNING,ERROR,CRITICAL}
log level
-f LOGFILE, --logfile LOGFILE
Specifies the logfile. If not provided, the default is None
```
To use the module in Python code, you can import it as follows:
```python
import j2escape
template_directory = "./source-dir"
output_directory = "./excaped-templates"
allow_create_output_dir = True
j2e = J2Escape(template_directory)
j2e.save_to_directory(outputdir=output_directory, create_ok=allow_create_output_dir)
```
The static method `get_escaped()` can be used to escape templates in memory:
`escaped_tamplate_string = J2Escape.get_escaped(plain_template_string)`
### Development
Install [poetry](https://python-poetry.org/docs/#installation) and [pre-commit](https://pre-commit.com/#install) to manage the development environment.
Clone the repository and install the development dependencies:
```bash
git clone https://github.com/jifox/j2escape.git
cd j2escape
poetry install
```
`pre-commit` hooks are used to ensure code quality. To install the pre-commit hooks, run the following command:
```bash
pre-commit install
```
Use pre-commit to run the hooks on all files:
```bash
pre-commit run --all-files
```
Raw data
{
"_id": null,
"home_page": "https://github.com/jifox/j2escape.git",
"name": "j2escape",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "Cookiecutter,Cruft,Jinja2,Utility",
"author": "jifox",
"author_email": "josef.fuchs@j-fuchs.at",
"download_url": "https://files.pythonhosted.org/packages/fb/26/29145f0151260da552ec89dadbceef2be6b4fcd0cedbf03565a15aada726/j2escape-1.0.2.tar.gz",
"platform": null,
"description": "# J2Escape\n\n[![.github/workflows/python-package.yml](https://github.com/jifox/j2escape/actions/workflows/python-package.yml/badge.svg?branch=main)](https://github.com/jifox/j2escape/actions/workflows/python-package.yml)\n[![Python 3.7](https://img.shields.io/badge/python-3.7-blue.svg)](https://www.python.org/downloads/release/python-370/)\n[![Python 3.8](https://img.shields.io/badge/python-3.8-blue.svg)](https://www.python.org/downloads/release/python-380/)\n[![Python 3.9](https://img.shields.io/badge/python-3.9-blue.svg)](https://www.python.org/downloads/release/python-390/)\n[![Python 3.10](https://img.shields.io/badge/python-3.10-blue.svg)](https://www.python.org/downloads/release/python-310/)\n[![Python 3.11](https://img.shields.io/badge/python-3.11-blue.svg)](https://www.python.org/downloads/release/python-311/)\n\n\n### Installation\n\n```\npip install j2escape\n```\n\n### Overview\n\nThis module, written in Python, facilitates the storage of Jinja2 templates within a project managed by [Cookiecutter](https://github.com/cookiecutter/cookiecutter) or [Cruft](https://github.com/cruft/cruft).\n\nCookiecutter utilizes Jinja templates internally when substituting input variables in the source code. However, this can result in errors if a Jinja template, such as {% if config.allow_duplicates %}, is intended for the application rather than Cookiecutter or Cruft.\n\nTo circumvent this problem, this module employs the jinja2.lex() function to parse and appropriately escape the template blocks.\n\nThe transformation process is idempotent, meaning that an already escaped template will remain unchanged when escaped again.\n\nExample:\n\n| Template | Escaped Template |\n|---|---|\n| {{ variable }} | {{ '{{' }} variable {{ '}}' }} |\n| {% if config.allow_duplicates %} | {{ '{%' }} if config.allow_duplicates {{ '%}' }} |\n\nThe module also provides a command-line interface that can be accessed using the `j2escape` command. This command can be utilized to escape jinja2 tags within a directory of templates.\n\n### j2escape usage\n\n```bash\nj2escape --help\nusage: j2escape [-h] [-t TEMPLATES] [-o OUTPUT_DIR] [--overwrite] [-c] [-l {DEBUG,INFO,WARNING,ERROR,CRITICAL}] [-f LOGFILE]\n\nEscape jinja2 tags in a directory of templates.\n\noptions:\n -h, --help show this help message and exit\n -t TEMPLATES, --templates TEMPLATES\n A comma-separated string of Jinja Templates (*.j2) or a directory with *.j2 files.\n -o OUTPUT_DIR, --output-dir OUTPUT_DIR\n Specifies the directory path where the escaped templates will be stored.\n --overwrite Replaces the original templates. This is necessary if the --output-dir is not provided.\n -c, --create-ok Generates the output directory if it doesn\u2019t already exist.\n -l {DEBUG,INFO,WARNING,ERROR,CRITICAL}, --loglevel {DEBUG,INFO,WARNING,ERROR,CRITICAL}\n log level\n -f LOGFILE, --logfile LOGFILE\n Specifies the logfile. If not provided, the default is None\n```\n\nTo use the module in Python code, you can import it as follows:\n\n```python\nimport j2escape\n\ntemplate_directory = \"./source-dir\"\noutput_directory = \"./excaped-templates\"\nallow_create_output_dir = True\n\nj2e = J2Escape(template_directory)\nj2e.save_to_directory(outputdir=output_directory, create_ok=allow_create_output_dir)\n```\n\nThe static method `get_escaped()` can be used to escape templates in memory:\n\n`escaped_tamplate_string = J2Escape.get_escaped(plain_template_string)`\n\n\n### Development\n\nInstall [poetry](https://python-poetry.org/docs/#installation) and [pre-commit](https://pre-commit.com/#install) to manage the development environment.\n\nClone the repository and install the development dependencies:\n\n```bash\ngit clone https://github.com/jifox/j2escape.git\ncd j2escape\npoetry install\n```\n\n`pre-commit` hooks are used to ensure code quality. To install the pre-commit hooks, run the following command:\n\n```bash\npre-commit install\n```\n\nUse pre-commit to run the hooks on all files:\n\n```bash\npre-commit run --all-files\n```\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "Jinja2 Template Escaper",
"version": "1.0.2",
"project_urls": {
"Homepage": "https://github.com/jifox/j2escape.git",
"Repository": "https://github.com/jifox/j2escape.git"
},
"split_keywords": [
"cookiecutter",
"cruft",
"jinja2",
"utility"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "b8bc3cf4346a48718543c537c71dd397617fddab26eb3d6048b5907b405bfc62",
"md5": "6039b9c8e19c9da4088d0860bc903373",
"sha256": "6cc7a2c65c204947a2407f58f147173eab729702ce54e5e57be117a540ff9f46"
},
"downloads": -1,
"filename": "j2escape-1.0.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6039b9c8e19c9da4088d0860bc903373",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 9278,
"upload_time": "2023-10-04T03:31:27",
"upload_time_iso_8601": "2023-10-04T03:31:27.134281Z",
"url": "https://files.pythonhosted.org/packages/b8/bc/3cf4346a48718543c537c71dd397617fddab26eb3d6048b5907b405bfc62/j2escape-1.0.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "fb2629145f0151260da552ec89dadbceef2be6b4fcd0cedbf03565a15aada726",
"md5": "af306c3052c331504d3ecf323aa1a3e2",
"sha256": "1e92ce72ad63723cf05f71665ae82faeae37166b655d83d631942cfdb885cb51"
},
"downloads": -1,
"filename": "j2escape-1.0.2.tar.gz",
"has_sig": false,
"md5_digest": "af306c3052c331504d3ecf323aa1a3e2",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 9566,
"upload_time": "2023-10-04T03:31:28",
"upload_time_iso_8601": "2023-10-04T03:31:28.612354Z",
"url": "https://files.pythonhosted.org/packages/fb/26/29145f0151260da552ec89dadbceef2be6b4fcd0cedbf03565a15aada726/j2escape-1.0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-04 03:31:28",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "jifox",
"github_project": "j2escape",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "j2escape"
}