pre-commit-html 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Robotz213
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,90 @@
1
+ Metadata-Version: 2.3
2
+ Name: pre_commit_html
3
+ Version: 0.1.1
4
+ Summary: Format results from pre-commit cmd to HTML file
5
+ License: MIT
6
+ Author: Robotz213
7
+ Author-email: nicholas@robotz.dev
8
+ Requires-Python: >=3.13
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.13
12
+ Requires-Dist: jinja2 (>=3.1.5,<4.0.0)
13
+ Requires-Dist: pre-commit (>=4.1.0,<5.0.0)
14
+ Description-Content-Type: text/markdown
15
+
16
+ # Pre-Commit Formatter
17
+
18
+ > Portuguese version [here](docs/README-pt.md)(Link only works on the project page).
19
+
20
+ This project is designed to run pre-commit hooks and format the results into an HTML file for easy viewing.
21
+
22
+ ## Summary
23
+
24
+ - [How It Works](#how-it-works)
25
+ - [Installation](#installation)
26
+ - [Usage](#usage)
27
+ - [Project Structure](#project-structure)
28
+ - [Templates](#templates)
29
+ - [Example](#example)
30
+ - [License](#license)
31
+ - [Contributing](CONTRIBUTING.md)
32
+ - [Code of Conduct](CODE_OF_CONDUCT.md)
33
+
34
+ ## How It Works
35
+
36
+ 1. **Run Pre-Commit Hooks**: The `PreCommitParser` class runs the pre-commit hooks using the `pre-commit run --all-files` command.
37
+ 2. **Parse Output**: The output from the pre-commit hooks is parsed to extract relevant information such as file paths, line numbers, column numbers, and error messages.
38
+ 3. **Generate HTML**: The parsed information is then formatted into an HTML file using Jinja2 templates. The HTML file includes links to the relevant lines in the source code and references to the documentation for the errors.
39
+
40
+ ## Installation
41
+
42
+ To use this project, you need to have Python and `pre-commit` installed. You can install the required dependencies using pip:
43
+
44
+ ```sh
45
+ poetry install
46
+ ```
47
+
48
+ ## Usage
49
+
50
+ To run the pre-commit formatter, execute the following command:
51
+
52
+ ```sh
53
+ python -m pre_commit_html
54
+ ```
55
+
56
+ This will generate an HTML file named `result_pre_commit.html` in the project directory.
57
+
58
+ ## Project Structure
59
+
60
+ - `pre_commit_html/__main__.py`: Contains the main function to run the pre-commit formatter.
61
+ - `pre_commit_html/__init__.py`: Contains the `PreCommitParser` class which handles running the pre-commit hooks and formatting the output.
62
+ - `site/templates/`: Contains the Jinja2 templates used to generate the HTML file.
63
+
64
+ ## Templates
65
+
66
+ The project uses Jinja2 templates to format the HTML output. The templates are located in the `site/templates/` directory and include:
67
+
68
+ - `code_part.jinja`: Template for formatting code parts.
69
+ - `code_error.jinja`: Template for formatting code errors.
70
+ - `html_content.jinja`: Template for the overall HTML content.
71
+
72
+ ## Example
73
+
74
+ Here is an example of how the HTML output might look:
75
+
76
+ ```html
77
+ <h3>File: <a href="./path/to/file.py:10:5">path/to/file.py:10:5</a></h3>
78
+ <p>
79
+ Error: <a href="https://docs.astral.sh/ruff/rules/#E123">E123</a> Some error
80
+ message
81
+ </p>
82
+ <pre>
83
+ | Some code part |
84
+ </pre>
85
+ ```
86
+
87
+ ## License
88
+
89
+ This project is licensed under the [MIT License](./LICENSE).
90
+
@@ -0,0 +1,74 @@
1
+ # Pre-Commit Formatter
2
+
3
+ > Portuguese version [here](docs/README-pt.md)(Link only works on the project page).
4
+
5
+ This project is designed to run pre-commit hooks and format the results into an HTML file for easy viewing.
6
+
7
+ ## Summary
8
+
9
+ - [How It Works](#how-it-works)
10
+ - [Installation](#installation)
11
+ - [Usage](#usage)
12
+ - [Project Structure](#project-structure)
13
+ - [Templates](#templates)
14
+ - [Example](#example)
15
+ - [License](#license)
16
+ - [Contributing](CONTRIBUTING.md)
17
+ - [Code of Conduct](CODE_OF_CONDUCT.md)
18
+
19
+ ## How It Works
20
+
21
+ 1. **Run Pre-Commit Hooks**: The `PreCommitParser` class runs the pre-commit hooks using the `pre-commit run --all-files` command.
22
+ 2. **Parse Output**: The output from the pre-commit hooks is parsed to extract relevant information such as file paths, line numbers, column numbers, and error messages.
23
+ 3. **Generate HTML**: The parsed information is then formatted into an HTML file using Jinja2 templates. The HTML file includes links to the relevant lines in the source code and references to the documentation for the errors.
24
+
25
+ ## Installation
26
+
27
+ To use this project, you need to have Python and `pre-commit` installed. You can install the required dependencies using pip:
28
+
29
+ ```sh
30
+ poetry install
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ To run the pre-commit formatter, execute the following command:
36
+
37
+ ```sh
38
+ python -m pre_commit_html
39
+ ```
40
+
41
+ This will generate an HTML file named `result_pre_commit.html` in the project directory.
42
+
43
+ ## Project Structure
44
+
45
+ - `pre_commit_html/__main__.py`: Contains the main function to run the pre-commit formatter.
46
+ - `pre_commit_html/__init__.py`: Contains the `PreCommitParser` class which handles running the pre-commit hooks and formatting the output.
47
+ - `site/templates/`: Contains the Jinja2 templates used to generate the HTML file.
48
+
49
+ ## Templates
50
+
51
+ The project uses Jinja2 templates to format the HTML output. The templates are located in the `site/templates/` directory and include:
52
+
53
+ - `code_part.jinja`: Template for formatting code parts.
54
+ - `code_error.jinja`: Template for formatting code errors.
55
+ - `html_content.jinja`: Template for the overall HTML content.
56
+
57
+ ## Example
58
+
59
+ Here is an example of how the HTML output might look:
60
+
61
+ ```html
62
+ <h3>File: <a href="./path/to/file.py:10:5">path/to/file.py:10:5</a></h3>
63
+ <p>
64
+ Error: <a href="https://docs.astral.sh/ruff/rules/#E123">E123</a> Some error
65
+ message
66
+ </p>
67
+ <pre>
68
+ | Some code part |
69
+ </pre>
70
+ ```
71
+
72
+ ## License
73
+
74
+ This project is licensed under the [MIT License](./LICENSE).
@@ -0,0 +1,131 @@
1
+ """Module for formatting pre-commit results into an HTML file."""
2
+
3
+ from pathlib import Path
4
+ import subprocess
5
+ from jinja2 import Template, Environment, FileSystemLoader # noqa: F401
6
+
7
+ env = Environment(
8
+ loader=FileSystemLoader(Path(__file__).parent.resolve().joinpath("site/templates"))
9
+ )
10
+
11
+ render_template = env.get_template
12
+
13
+ path_result_content = Path("result_pre_commit.html")
14
+
15
+
16
+ class PreCommitParser:
17
+ """Class to parse and format pre-commit results."""
18
+
19
+ @classmethod
20
+ def run_pre_commit(cls) -> str:
21
+ """Run the pre-commit command and capture its output.
22
+
23
+ Returns:
24
+ str: The output of the pre-commit command.
25
+ Raises:
26
+ subprocess.CalledProcessError: If the pre-commit command fails.
27
+ """
28
+ try:
29
+ result = subprocess.run(
30
+ ["pre-commit", "run", "--all-files"],
31
+ capture_output=True,
32
+ text=True,
33
+ )
34
+ return result.stdout
35
+ except subprocess.CalledProcessError as e:
36
+ return f"Erro ao executar pre-commit: {e.stderr}"
37
+
38
+ @classmethod
39
+ def pre_commit_html(cls) -> None:
40
+ """Format the pre-commit output into an HTML file.
41
+
42
+ This method runs the pre-commit command, processes its output, and writes the formatted
43
+ results into an HTML file.
44
+ """
45
+ content = cls.run_pre_commit()
46
+
47
+ content_splitlines = content.splitlines()
48
+
49
+ html_content: list[str] = []
50
+
51
+ code_part: list[str] = []
52
+
53
+ code_error: list[str] = []
54
+
55
+ for line in content_splitlines:
56
+ if "\\" in line and ":" in line and len(code_part) == 0:
57
+ h3_file = line.replace("\\", "/")
58
+
59
+ path_code_file = h3_file.split(":")[0]
60
+ line_code = h3_file.split(":")[1]
61
+ column_code = h3_file.split(":")[2]
62
+ message = h3_file.split(":")[3]
63
+
64
+ ruff_ref = message.split(" ")[1]
65
+
66
+ code_error.append(
67
+ "".join(
68
+ (
69
+ f'<h3>File: <a href="./{path_code_file}:{line_code}:',
70
+ f'{column_code}">{path_code_file}:{line_code}:{column_code}</a></h3>',
71
+ )
72
+ )
73
+ )
74
+ code_error.append(
75
+ f'<p>Error: <a href="https://docs.astral.sh/ruff/rules/#{ruff_ref}">{ruff_ref}</a>{message}</p>'
76
+ )
77
+
78
+ elif "\\" in line and ":" in line and len(code_part) > 0:
79
+ h3_file = line.replace("\\", "/")
80
+
81
+ code_part_html = render_template("code_part.jinja").render(
82
+ code_part=code_part
83
+ )
84
+
85
+ code_error.append(code_part_html)
86
+ to_html = render_template("code_error.jinja").render(
87
+ code_error=code_error
88
+ )
89
+
90
+ html_content.append(to_html)
91
+
92
+ code_part.clear()
93
+ code_error.clear()
94
+
95
+ path_code_file = h3_file.split(":")[0]
96
+ line_code = h3_file.split(":")[1]
97
+ column_code = h3_file.split(":")[2]
98
+ message = h3_file.split(":")[3]
99
+
100
+ ruff_ref = message.split(" ")[1]
101
+
102
+ code_error.append(
103
+ "".join(
104
+ (
105
+ f'<h3>File: <a href="./{path_code_file}:{line_code}:',
106
+ f'{column_code}">{path_code_file}:{line_code}:{column_code}</a></h3>',
107
+ )
108
+ )
109
+ )
110
+ code_error.append(
111
+ f'<p>Error: <a href="https://docs.astral.sh/ruff/rules/#{ruff_ref}">{ruff_ref}</a>{message}</p>'
112
+ )
113
+ continue
114
+
115
+ if "|" in line:
116
+ code_part.append(line)
117
+
118
+ if path_result_content.exists():
119
+ path_result_content.unlink()
120
+
121
+ path_result_content.touch()
122
+
123
+ html_content = render_template("html_content.jinja").render(
124
+ content=html_content
125
+ )
126
+
127
+ with open(path_result_content, "w", encoding="utf-8") as f:
128
+ f.write(html_content)
129
+
130
+
131
+ PreCommitParser.pre_commit_html()
@@ -0,0 +1,11 @@
1
+ """This module contains the main function to run the pre-commit formatter."""
2
+
3
+ from . import PreCommitParser
4
+
5
+
6
+ def main() -> None:
7
+ """Main function to run the pre-commit formatter.
8
+
9
+ This function calls the pre_commit_html method from the PreCommitParser class.
10
+ """
11
+ PreCommitParser.pre_commit_html()
@@ -0,0 +1 @@
1
+ just inchident
@@ -0,0 +1,3 @@
1
+ {% for item in code_error %}
2
+ {{ item }}
3
+ {% endfor %}
@@ -0,0 +1,7 @@
1
+ <pre>
2
+ <code class="python">
3
+ {% for item in code_part %}
4
+ {{ item }}
5
+ {% endfor %}
6
+ </code>
7
+ </pre>
@@ -0,0 +1,38 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+
4
+ <head>
5
+ <meta charset="UTF-8">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+ <title>Pre-Commit Result</title>
8
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/styles/a11y-dark.min.css"
9
+ integrity="sha512-Vj6gPCk8EZlqnoveEyuGyYaWZ1+jyjMPg8g4shwyyNlRQl6d3L9At02ZHQr5K6s5duZl/+YKMnM3/8pDhoUphg=="
10
+ crossorigin="anonymous" referrerpolicy="no-referrer" />
11
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/highlight.min.js"
12
+ integrity="sha512-EBLzUL8XLl+va/zAsmXwS7Z2B1F9HUHkZwyS/VKwh3S7T/U0nF4BaU29EP/ZSf6zgiIxYAnKLu6bJ8dqpmX5uw=="
13
+ crossorigin="anonymous" referrerpolicy="no-referrer"></script>
14
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/languages/python.min.js"
15
+ integrity="sha512-/uCTceIDOniHf+VUKbCnP/x6GQSRrm4GwUtQYMgKa9yIZPGzlR04flSsD+2or7bPn44VY9inIHI4cwNCcZmJDw=="
16
+ crossorigin="anonymous" referrerpolicy="no-referrer"></script>
17
+ </head>
18
+
19
+ <body>
20
+ <h1>Pre-Commit Result</h1>
21
+ <p>Pre-Commit Result: <span id="result"></span></p>
22
+
23
+ <ul>
24
+ {% for item in content %}
25
+ {{ item }}
26
+ {% endfor %}
27
+ </ul>
28
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css">
29
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
30
+
31
+ <!-- and it's easy to individually load additional languages -->
32
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/go.min.js"></script>
33
+
34
+ <script>hljs.highlightAll();</script>
35
+
36
+ </body>
37
+
38
+ </html>
@@ -0,0 +1,17 @@
1
+ [project]
2
+ name = "pre_commit_html"
3
+ version = "0.1.1"
4
+ description = "Format results from pre-commit cmd to HTML file"
5
+ authors = [{ name = "Robotz213", email = "nicholas@robotz.dev" }]
6
+ readme = "README.md"
7
+ license = "MIT"
8
+ requires-python = ">=3.13"
9
+ dependencies = ["pre-commit (>=4.1.0,<5.0.0)", "jinja2 (>=3.1.5,<4.0.0)"]
10
+
11
+
12
+ [build-system]
13
+ requires = ["poetry-core>=2.0.0,<3.0.0"]
14
+ build-backend = "poetry.core.masonry.api"
15
+
16
+ [tool.poetry.group.dev.dependencies]
17
+ ruff = "^0.9.5"