reportsmith 0.1.0__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.
- reportsmith-0.1.0/LICENSE +21 -0
- reportsmith-0.1.0/PKG-INFO +150 -0
- reportsmith-0.1.0/README.md +121 -0
- reportsmith-0.1.0/pyproject.toml +39 -0
- reportsmith-0.1.0/reportsmith/__init__.py +60 -0
- reportsmith-0.1.0/reportsmith/elements.py +190 -0
- reportsmith-0.1.0/reportsmith/inline.py +59 -0
- reportsmith-0.1.0/reportsmith/renderers/__init__.py +5 -0
- reportsmith-0.1.0/reportsmith/renderers/html.py +203 -0
- reportsmith-0.1.0/reportsmith/renderers/markdown.py +131 -0
- reportsmith-0.1.0/reportsmith/renderers/pdf.py +323 -0
- reportsmith-0.1.0/reportsmith/report.py +98 -0
- reportsmith-0.1.0/reportsmith.egg-info/PKG-INFO +150 -0
- reportsmith-0.1.0/reportsmith.egg-info/SOURCES.txt +21 -0
- reportsmith-0.1.0/reportsmith.egg-info/dependency_links.txt +1 -0
- reportsmith-0.1.0/reportsmith.egg-info/requires.txt +13 -0
- reportsmith-0.1.0/reportsmith.egg-info/top_level.txt +1 -0
- reportsmith-0.1.0/setup.cfg +4 -0
- reportsmith-0.1.0/tests/test_elements.py +149 -0
- reportsmith-0.1.0/tests/test_inline.py +55 -0
- reportsmith-0.1.0/tests/test_pdf.py +115 -0
- reportsmith-0.1.0/tests/test_renderers.py +226 -0
- reportsmith-0.1.0/tests/test_reportsmith.py +160 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Michal Stojke
|
|
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,150 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: reportsmith
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A small, dependency-free library for building reports and exporting them to Markdown and HTML
|
|
5
|
+
Author: Michal Stojke
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/mstojke/reportsmith
|
|
8
|
+
Project-URL: Documentation, https://github.com/mstojke/reportsmith/blob/main/docs/user-guide.md
|
|
9
|
+
Project-URL: Changelog, https://github.com/mstojke/reportsmith/blob/main/CHANGELOG.md
|
|
10
|
+
Project-URL: Issues, https://github.com/mstojke/reportsmith/issues
|
|
11
|
+
Keywords: report,markdown,html,export,document
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Topic :: Text Processing :: Markup :: Markdown
|
|
14
|
+
Classifier: Topic :: Text Processing :: Markup :: HTML
|
|
15
|
+
Requires-Python: >=3.9
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Provides-Extra: pdf
|
|
19
|
+
Requires-Dist: fpdf2; extra == "pdf"
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: pytest; extra == "dev"
|
|
22
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
|
23
|
+
Requires-Dist: fpdf2; extra == "dev"
|
|
24
|
+
Provides-Extra: docs
|
|
25
|
+
Requires-Dist: sphinx; extra == "docs"
|
|
26
|
+
Requires-Dist: myst-parser; extra == "docs"
|
|
27
|
+
Requires-Dist: sphinx-rtd-theme; extra == "docs"
|
|
28
|
+
Dynamic: license-file
|
|
29
|
+
|
|
30
|
+
# reportsmith
|
|
31
|
+
|
|
32
|
+
A small, dependency-free Python library for building reports programmatically and exporting them to **Markdown**, a **standalone styled HTML** document, or (optionally) **PDF**.
|
|
33
|
+
|
|
34
|
+
## Features
|
|
35
|
+
|
|
36
|
+
- Fluent builder API with chainable methods and nested sections (`with report.section(...)`)
|
|
37
|
+
- Elements: headings, paragraphs, tables (with alignment and captions), bullet/numbered lists (nestable), code blocks, block quotes, images, horizontal rules, and format-specific raw passthrough
|
|
38
|
+
- Inline formatting helpers: `bold()`, `italic()`, `code()`, `link()` — render correctly in both formats
|
|
39
|
+
- Correct escaping everywhere: HTML entities, Markdown table pipes, code fences that widen when the source contains ` ``` `
|
|
40
|
+
- HTML export ships with a clean embedded stylesheet (light/dark aware) that you can replace with your own CSS
|
|
41
|
+
- Optional PDF export (`pip install reportsmith[pdf]`, via fpdf2): matching styling, PDF bookmarks from sections, clickable links, auto-detected Unicode system fonts
|
|
42
|
+
- No required dependencies; Python 3.9+
|
|
43
|
+
|
|
44
|
+
## Installation
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pip install -e .
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Or just copy the `reportsmith/` package into your project — it has no dependencies.
|
|
51
|
+
|
|
52
|
+
## Quick start
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from reportsmith import Report, bold, link
|
|
56
|
+
|
|
57
|
+
report = Report("Q3 Sales Report", author="Jane Doe", subtitle="Quarterly summary")
|
|
58
|
+
|
|
59
|
+
with report.section("Overview") as s:
|
|
60
|
+
s.paragraph(["Revenue was ", bold("up 12%"), " quarter over quarter."])
|
|
61
|
+
s.table(
|
|
62
|
+
["Region", "Revenue"],
|
|
63
|
+
[["EMEA", "$1.2M"], ["APAC", "$0.9M"]],
|
|
64
|
+
caption="Revenue by region",
|
|
65
|
+
align=["left", "right"],
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
with report.section("Next steps") as s:
|
|
69
|
+
s.numbered_list([
|
|
70
|
+
"Expand APAC sales team",
|
|
71
|
+
["Review ", link("pricing proposal", "https://example.com/pricing")],
|
|
72
|
+
])
|
|
73
|
+
|
|
74
|
+
print(report.to_markdown()) # Markdown string
|
|
75
|
+
print(report.to_html()) # standalone HTML page
|
|
76
|
+
report.save_markdown("report.md")
|
|
77
|
+
report.save_html("report.html")
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Run `python example.py` for a fuller demo that writes `example_report.md` and `example_report.html`.
|
|
81
|
+
|
|
82
|
+
## Documentation
|
|
83
|
+
|
|
84
|
+
- **[User Guide](https://github.com/mstojke/reportsmith/blob/main/docs/user-guide.md)** — a walkthrough of every feature with examples: sections, inline formatting, tables, lists, raw passthrough, HTML customization, and escaping behavior.
|
|
85
|
+
- **[API Reference](https://github.com/mstojke/reportsmith/blob/main/docs/api-reference.rst)** — signatures, parameters, and exceptions for every public class and function (reStructuredText, Sphinx-ready).
|
|
86
|
+
|
|
87
|
+
The docs are also a buildable Sphinx site:
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
pip install -e .[docs]
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
python -m sphinx -b html docs docs/_build/html
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Then open `docs/_build/html/index.html`.
|
|
98
|
+
|
|
99
|
+
## API overview
|
|
100
|
+
|
|
101
|
+
### `Report(title, author="", date=None, subtitle="")`
|
|
102
|
+
|
|
103
|
+
`date` accepts a string, `datetime.date`, or `datetime.datetime`; it defaults to today.
|
|
104
|
+
|
|
105
|
+
Builder methods (all chainable, also available on sections):
|
|
106
|
+
|
|
107
|
+
| Method | Adds |
|
|
108
|
+
| --- | --- |
|
|
109
|
+
| `section(title)` | Nested section; heading level tracks nesting depth. Usable as a context manager. |
|
|
110
|
+
| `heading(text, level=None)` | Explicit heading (level defaults to the container's depth + 1) |
|
|
111
|
+
| `paragraph(content)` | Paragraph |
|
|
112
|
+
| `quote(content)` | Block quote |
|
|
113
|
+
| `code(source, language="")` | Fenced/`<pre>` code block |
|
|
114
|
+
| `bullet_list(items)` / `numbered_list(items)` | List; items may be text or a nested `ListElement` |
|
|
115
|
+
| `table(headers, rows, caption="", align=None)` | Table; `align` entries are `"left"`, `"center"`, `"right"` |
|
|
116
|
+
| `image(src, alt="", title="")` | Image |
|
|
117
|
+
| `horizontal_rule()` | Rule |
|
|
118
|
+
| `raw(content, format="both")` | Verbatim content for `"markdown"`, `"html"`, or `"both"` |
|
|
119
|
+
| `add(element)` | Any pre-built element |
|
|
120
|
+
|
|
121
|
+
Anywhere text is accepted you may pass a plain string or a list mixing strings with `bold()`, `italic()`, `code()`, and `link(text, href)` spans.
|
|
122
|
+
|
|
123
|
+
### Export
|
|
124
|
+
|
|
125
|
+
- `report.to_markdown()` → GitHub-flavored Markdown string
|
|
126
|
+
- `report.to_html(full_document=True, css=None)` → HTML string; `full_document=False` returns just the body fragment, `css=` replaces the default stylesheet
|
|
127
|
+
- `report.to_pdf(fonts=None, mono_fonts=None)` → PDF bytes (requires the `pdf` extra); pass TTF path quadruples to control typefaces
|
|
128
|
+
- `report.save_markdown(path)` / `report.save_html(path, ...)` / `report.save_pdf(path, ...)` → write to a file, return the `Path`
|
|
129
|
+
|
|
130
|
+
## Running tests
|
|
131
|
+
|
|
132
|
+
The test suite (93 tests, 100% line coverage) lives in `tests/`:
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
python -m pytest
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
With a coverage report (requires `pytest-cov`):
|
|
139
|
+
|
|
140
|
+
```bash
|
|
141
|
+
python -m pytest --cov=reportsmith --cov-report=term-missing
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Changelog
|
|
145
|
+
|
|
146
|
+
See [CHANGELOG.md](https://github.com/mstojke/reportsmith/blob/main/CHANGELOG.md) — the format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|
147
|
+
|
|
148
|
+
## License
|
|
149
|
+
|
|
150
|
+
MIT
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# reportsmith
|
|
2
|
+
|
|
3
|
+
A small, dependency-free Python library for building reports programmatically and exporting them to **Markdown**, a **standalone styled HTML** document, or (optionally) **PDF**.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Fluent builder API with chainable methods and nested sections (`with report.section(...)`)
|
|
8
|
+
- Elements: headings, paragraphs, tables (with alignment and captions), bullet/numbered lists (nestable), code blocks, block quotes, images, horizontal rules, and format-specific raw passthrough
|
|
9
|
+
- Inline formatting helpers: `bold()`, `italic()`, `code()`, `link()` — render correctly in both formats
|
|
10
|
+
- Correct escaping everywhere: HTML entities, Markdown table pipes, code fences that widen when the source contains ` ``` `
|
|
11
|
+
- HTML export ships with a clean embedded stylesheet (light/dark aware) that you can replace with your own CSS
|
|
12
|
+
- Optional PDF export (`pip install reportsmith[pdf]`, via fpdf2): matching styling, PDF bookmarks from sections, clickable links, auto-detected Unicode system fonts
|
|
13
|
+
- No required dependencies; Python 3.9+
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install -e .
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Or just copy the `reportsmith/` package into your project — it has no dependencies.
|
|
22
|
+
|
|
23
|
+
## Quick start
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
from reportsmith import Report, bold, link
|
|
27
|
+
|
|
28
|
+
report = Report("Q3 Sales Report", author="Jane Doe", subtitle="Quarterly summary")
|
|
29
|
+
|
|
30
|
+
with report.section("Overview") as s:
|
|
31
|
+
s.paragraph(["Revenue was ", bold("up 12%"), " quarter over quarter."])
|
|
32
|
+
s.table(
|
|
33
|
+
["Region", "Revenue"],
|
|
34
|
+
[["EMEA", "$1.2M"], ["APAC", "$0.9M"]],
|
|
35
|
+
caption="Revenue by region",
|
|
36
|
+
align=["left", "right"],
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
with report.section("Next steps") as s:
|
|
40
|
+
s.numbered_list([
|
|
41
|
+
"Expand APAC sales team",
|
|
42
|
+
["Review ", link("pricing proposal", "https://example.com/pricing")],
|
|
43
|
+
])
|
|
44
|
+
|
|
45
|
+
print(report.to_markdown()) # Markdown string
|
|
46
|
+
print(report.to_html()) # standalone HTML page
|
|
47
|
+
report.save_markdown("report.md")
|
|
48
|
+
report.save_html("report.html")
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Run `python example.py` for a fuller demo that writes `example_report.md` and `example_report.html`.
|
|
52
|
+
|
|
53
|
+
## Documentation
|
|
54
|
+
|
|
55
|
+
- **[User Guide](https://github.com/mstojke/reportsmith/blob/main/docs/user-guide.md)** — a walkthrough of every feature with examples: sections, inline formatting, tables, lists, raw passthrough, HTML customization, and escaping behavior.
|
|
56
|
+
- **[API Reference](https://github.com/mstojke/reportsmith/blob/main/docs/api-reference.rst)** — signatures, parameters, and exceptions for every public class and function (reStructuredText, Sphinx-ready).
|
|
57
|
+
|
|
58
|
+
The docs are also a buildable Sphinx site:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pip install -e .[docs]
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
python -m sphinx -b html docs docs/_build/html
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Then open `docs/_build/html/index.html`.
|
|
69
|
+
|
|
70
|
+
## API overview
|
|
71
|
+
|
|
72
|
+
### `Report(title, author="", date=None, subtitle="")`
|
|
73
|
+
|
|
74
|
+
`date` accepts a string, `datetime.date`, or `datetime.datetime`; it defaults to today.
|
|
75
|
+
|
|
76
|
+
Builder methods (all chainable, also available on sections):
|
|
77
|
+
|
|
78
|
+
| Method | Adds |
|
|
79
|
+
| --- | --- |
|
|
80
|
+
| `section(title)` | Nested section; heading level tracks nesting depth. Usable as a context manager. |
|
|
81
|
+
| `heading(text, level=None)` | Explicit heading (level defaults to the container's depth + 1) |
|
|
82
|
+
| `paragraph(content)` | Paragraph |
|
|
83
|
+
| `quote(content)` | Block quote |
|
|
84
|
+
| `code(source, language="")` | Fenced/`<pre>` code block |
|
|
85
|
+
| `bullet_list(items)` / `numbered_list(items)` | List; items may be text or a nested `ListElement` |
|
|
86
|
+
| `table(headers, rows, caption="", align=None)` | Table; `align` entries are `"left"`, `"center"`, `"right"` |
|
|
87
|
+
| `image(src, alt="", title="")` | Image |
|
|
88
|
+
| `horizontal_rule()` | Rule |
|
|
89
|
+
| `raw(content, format="both")` | Verbatim content for `"markdown"`, `"html"`, or `"both"` |
|
|
90
|
+
| `add(element)` | Any pre-built element |
|
|
91
|
+
|
|
92
|
+
Anywhere text is accepted you may pass a plain string or a list mixing strings with `bold()`, `italic()`, `code()`, and `link(text, href)` spans.
|
|
93
|
+
|
|
94
|
+
### Export
|
|
95
|
+
|
|
96
|
+
- `report.to_markdown()` → GitHub-flavored Markdown string
|
|
97
|
+
- `report.to_html(full_document=True, css=None)` → HTML string; `full_document=False` returns just the body fragment, `css=` replaces the default stylesheet
|
|
98
|
+
- `report.to_pdf(fonts=None, mono_fonts=None)` → PDF bytes (requires the `pdf` extra); pass TTF path quadruples to control typefaces
|
|
99
|
+
- `report.save_markdown(path)` / `report.save_html(path, ...)` / `report.save_pdf(path, ...)` → write to a file, return the `Path`
|
|
100
|
+
|
|
101
|
+
## Running tests
|
|
102
|
+
|
|
103
|
+
The test suite (93 tests, 100% line coverage) lives in `tests/`:
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
python -m pytest
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
With a coverage report (requires `pytest-cov`):
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
python -m pytest --cov=reportsmith --cov-report=term-missing
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Changelog
|
|
116
|
+
|
|
117
|
+
See [CHANGELOG.md](https://github.com/mstojke/reportsmith/blob/main/CHANGELOG.md) — the format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|
118
|
+
|
|
119
|
+
## License
|
|
120
|
+
|
|
121
|
+
MIT
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "reportsmith"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "A small, dependency-free library for building reports and exporting them to Markdown and HTML"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "Michal Stojke" }]
|
|
14
|
+
keywords = ["report", "markdown", "html", "export", "document"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Topic :: Text Processing :: Markup :: Markdown",
|
|
18
|
+
"Topic :: Text Processing :: Markup :: HTML",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
Homepage = "https://github.com/mstojke/reportsmith"
|
|
23
|
+
Documentation = "https://github.com/mstojke/reportsmith/blob/main/docs/user-guide.md"
|
|
24
|
+
Changelog = "https://github.com/mstojke/reportsmith/blob/main/CHANGELOG.md"
|
|
25
|
+
Issues = "https://github.com/mstojke/reportsmith/issues"
|
|
26
|
+
|
|
27
|
+
[project.optional-dependencies]
|
|
28
|
+
pdf = ["fpdf2"]
|
|
29
|
+
dev = ["pytest", "pytest-cov", "fpdf2"]
|
|
30
|
+
docs = ["sphinx", "myst-parser", "sphinx-rtd-theme"]
|
|
31
|
+
|
|
32
|
+
[tool.setuptools.dynamic]
|
|
33
|
+
version = { attr = "reportsmith.__version__" }
|
|
34
|
+
|
|
35
|
+
[tool.setuptools.packages.find]
|
|
36
|
+
include = ["reportsmith*"]
|
|
37
|
+
|
|
38
|
+
[tool.pytest.ini_options]
|
|
39
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"""reportsmith — a small, dependency-free library for building reports.
|
|
2
|
+
|
|
3
|
+
Build a report programmatically from typed elements (headings, paragraphs,
|
|
4
|
+
tables, lists, code blocks, images, ...) and export it to Markdown or a
|
|
5
|
+
standalone styled HTML document.
|
|
6
|
+
|
|
7
|
+
Quick start::
|
|
8
|
+
|
|
9
|
+
from reportsmith import Report, bold, link
|
|
10
|
+
|
|
11
|
+
report = Report("Q3 Sales Report", author="Jane Doe")
|
|
12
|
+
with report.section("Overview") as s:
|
|
13
|
+
s.paragraph(["Revenue was ", bold("up 12%"), " quarter over quarter."])
|
|
14
|
+
s.table(["Region", "Revenue"], [["EMEA", "$1.2M"], ["APAC", "$0.9M"]])
|
|
15
|
+
|
|
16
|
+
report.save_markdown("report.md")
|
|
17
|
+
report.save_html("report.html")
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from .inline import Inline, bold, code, italic, link
|
|
21
|
+
from .elements import (
|
|
22
|
+
CodeBlock,
|
|
23
|
+
Heading,
|
|
24
|
+
HorizontalRule,
|
|
25
|
+
Image,
|
|
26
|
+
ListElement,
|
|
27
|
+
Paragraph,
|
|
28
|
+
Quote,
|
|
29
|
+
Raw,
|
|
30
|
+
Section,
|
|
31
|
+
Table,
|
|
32
|
+
)
|
|
33
|
+
from .report import Report
|
|
34
|
+
from .renderers.markdown import MarkdownRenderer
|
|
35
|
+
from .renderers.html import HtmlRenderer
|
|
36
|
+
from .renderers.pdf import PdfRenderer
|
|
37
|
+
|
|
38
|
+
__version__ = "0.1.0"
|
|
39
|
+
|
|
40
|
+
__all__ = [
|
|
41
|
+
"Report",
|
|
42
|
+
"Section",
|
|
43
|
+
"Heading",
|
|
44
|
+
"Paragraph",
|
|
45
|
+
"Table",
|
|
46
|
+
"ListElement",
|
|
47
|
+
"CodeBlock",
|
|
48
|
+
"Image",
|
|
49
|
+
"Quote",
|
|
50
|
+
"HorizontalRule",
|
|
51
|
+
"Raw",
|
|
52
|
+
"Inline",
|
|
53
|
+
"bold",
|
|
54
|
+
"italic",
|
|
55
|
+
"code",
|
|
56
|
+
"link",
|
|
57
|
+
"MarkdownRenderer",
|
|
58
|
+
"HtmlRenderer",
|
|
59
|
+
"PdfRenderer",
|
|
60
|
+
]
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"""Block-level report elements.
|
|
2
|
+
|
|
3
|
+
Every element knows nothing about output formats; rendering lives in
|
|
4
|
+
``reportsmith.renderers``. ``Section`` is a container element that also mixes in
|
|
5
|
+
the builder API, so content can be added to a report or any nested section
|
|
6
|
+
with the same methods.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from typing import Optional, Sequence
|
|
12
|
+
|
|
13
|
+
from .inline import InlineContent, TextLike, normalize
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Element:
|
|
17
|
+
"""Base class for all block elements."""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class Heading(Element):
|
|
21
|
+
def __init__(self, content: TextLike, level: int = 1):
|
|
22
|
+
if not 1 <= level <= 6:
|
|
23
|
+
raise ValueError(f"Heading level must be 1-6, got {level}")
|
|
24
|
+
self.parts: list[InlineContent] = normalize(content)
|
|
25
|
+
self.level = level
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class Paragraph(Element):
|
|
29
|
+
def __init__(self, content: TextLike):
|
|
30
|
+
self.parts: list[InlineContent] = normalize(content)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class Quote(Element):
|
|
34
|
+
def __init__(self, content: TextLike):
|
|
35
|
+
self.parts: list[InlineContent] = normalize(content)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class CodeBlock(Element):
|
|
39
|
+
def __init__(self, source: str, language: str = ""):
|
|
40
|
+
self.source = source
|
|
41
|
+
self.language = language
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class ListElement(Element):
|
|
45
|
+
"""Bullet or numbered list. Items may be plain/inline text or nested lists."""
|
|
46
|
+
|
|
47
|
+
def __init__(self, items: Sequence, ordered: bool = False):
|
|
48
|
+
self.ordered = ordered
|
|
49
|
+
self.items: list = []
|
|
50
|
+
for item in items:
|
|
51
|
+
if isinstance(item, ListElement):
|
|
52
|
+
self.items.append(item)
|
|
53
|
+
else:
|
|
54
|
+
self.items.append(normalize(item))
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class Table(Element):
|
|
58
|
+
def __init__(
|
|
59
|
+
self,
|
|
60
|
+
headers: Sequence[TextLike],
|
|
61
|
+
rows: Sequence[Sequence[TextLike]],
|
|
62
|
+
caption: str = "",
|
|
63
|
+
align: Optional[Sequence[str]] = None,
|
|
64
|
+
):
|
|
65
|
+
self.headers = [normalize(h) for h in headers]
|
|
66
|
+
self.rows = [[normalize(cell) for cell in row] for row in rows]
|
|
67
|
+
width = len(self.headers)
|
|
68
|
+
for i, row in enumerate(self.rows):
|
|
69
|
+
if len(row) != width:
|
|
70
|
+
raise ValueError(
|
|
71
|
+
f"Row {i} has {len(row)} cells, expected {width}"
|
|
72
|
+
)
|
|
73
|
+
self.caption = caption
|
|
74
|
+
if align is not None:
|
|
75
|
+
if len(align) != width:
|
|
76
|
+
raise ValueError("align must have one entry per column")
|
|
77
|
+
for a in align:
|
|
78
|
+
if a not in ("left", "center", "right"):
|
|
79
|
+
raise ValueError(f"Invalid alignment: {a!r}")
|
|
80
|
+
self.align = list(align) if align is not None else ["left"] * width
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class Image(Element):
|
|
84
|
+
def __init__(self, src: str, alt: str = "", title: str = ""):
|
|
85
|
+
self.src = src
|
|
86
|
+
self.alt = alt
|
|
87
|
+
self.title = title
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
class HorizontalRule(Element):
|
|
91
|
+
pass
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class Raw(Element):
|
|
95
|
+
"""Format-specific passthrough content.
|
|
96
|
+
|
|
97
|
+
Rendered verbatim when the output format matches (``"markdown"`` or
|
|
98
|
+
``"html"``); skipped otherwise. Use ``format="both"`` to always emit.
|
|
99
|
+
"""
|
|
100
|
+
|
|
101
|
+
def __init__(self, content: str, format: str = "both"):
|
|
102
|
+
if format not in ("markdown", "html", "both"):
|
|
103
|
+
raise ValueError(f"format must be 'markdown', 'html' or 'both', got {format!r}")
|
|
104
|
+
self.content = content
|
|
105
|
+
self.format = format
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
class Container(Element):
|
|
109
|
+
"""Mixin providing the fluent builder API shared by Report and Section."""
|
|
110
|
+
|
|
111
|
+
def __init__(self) -> None:
|
|
112
|
+
self.children: list[Element] = []
|
|
113
|
+
self._level = 0 # heading depth of this container
|
|
114
|
+
|
|
115
|
+
def add(self, element: Element) -> "Container":
|
|
116
|
+
"""Append a pre-built element. Returns self for chaining."""
|
|
117
|
+
self.children.append(element)
|
|
118
|
+
return self
|
|
119
|
+
|
|
120
|
+
def section(self, title: TextLike) -> "Section":
|
|
121
|
+
"""Add a nested section (usable as a context manager)."""
|
|
122
|
+
section = Section(title, level=self._level + 1)
|
|
123
|
+
self.children.append(section)
|
|
124
|
+
return section
|
|
125
|
+
|
|
126
|
+
def heading(self, content: TextLike, level: Optional[int] = None) -> "Container":
|
|
127
|
+
"""Add a heading. Level defaults to one below this container's title."""
|
|
128
|
+
self.children.append(Heading(content, level or min(self._level + 1, 6)))
|
|
129
|
+
return self
|
|
130
|
+
|
|
131
|
+
def paragraph(self, content: TextLike) -> "Container":
|
|
132
|
+
self.children.append(Paragraph(content))
|
|
133
|
+
return self
|
|
134
|
+
|
|
135
|
+
def quote(self, content: TextLike) -> "Container":
|
|
136
|
+
self.children.append(Quote(content))
|
|
137
|
+
return self
|
|
138
|
+
|
|
139
|
+
def code(self, source: str, language: str = "") -> "Container":
|
|
140
|
+
self.children.append(CodeBlock(source, language))
|
|
141
|
+
return self
|
|
142
|
+
|
|
143
|
+
def bullet_list(self, items: Sequence) -> "Container":
|
|
144
|
+
self.children.append(ListElement(items, ordered=False))
|
|
145
|
+
return self
|
|
146
|
+
|
|
147
|
+
def numbered_list(self, items: Sequence) -> "Container":
|
|
148
|
+
self.children.append(ListElement(items, ordered=True))
|
|
149
|
+
return self
|
|
150
|
+
|
|
151
|
+
def table(
|
|
152
|
+
self,
|
|
153
|
+
headers: Sequence[TextLike],
|
|
154
|
+
rows: Sequence[Sequence[TextLike]],
|
|
155
|
+
caption: str = "",
|
|
156
|
+
align: Optional[Sequence[str]] = None,
|
|
157
|
+
) -> "Container":
|
|
158
|
+
self.children.append(Table(headers, rows, caption, align))
|
|
159
|
+
return self
|
|
160
|
+
|
|
161
|
+
def image(self, src: str, alt: str = "", title: str = "") -> "Container":
|
|
162
|
+
self.children.append(Image(src, alt, title))
|
|
163
|
+
return self
|
|
164
|
+
|
|
165
|
+
def horizontal_rule(self) -> "Container":
|
|
166
|
+
self.children.append(HorizontalRule())
|
|
167
|
+
return self
|
|
168
|
+
|
|
169
|
+
def raw(self, content: str, format: str = "both") -> "Container":
|
|
170
|
+
self.children.append(Raw(content, format))
|
|
171
|
+
return self
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
class Section(Container):
|
|
175
|
+
"""A titled group of elements; nests to any depth.
|
|
176
|
+
|
|
177
|
+
Supports ``with`` syntax purely for readable indentation — entering and
|
|
178
|
+
exiting the context has no side effects.
|
|
179
|
+
"""
|
|
180
|
+
|
|
181
|
+
def __init__(self, title: TextLike, level: int = 1):
|
|
182
|
+
super().__init__()
|
|
183
|
+
self.title_parts: list[InlineContent] = normalize(title)
|
|
184
|
+
self._level = min(level, 6)
|
|
185
|
+
|
|
186
|
+
def __enter__(self) -> "Section":
|
|
187
|
+
return self
|
|
188
|
+
|
|
189
|
+
def __exit__(self, *exc_info) -> None:
|
|
190
|
+
return None
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""Inline (span-level) content: bold, italic, code, links.
|
|
2
|
+
|
|
3
|
+
Block elements accept either a plain string or a list mixing strings and
|
|
4
|
+
:class:`Inline` objects, so the same content renders correctly in both
|
|
5
|
+
Markdown and HTML.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from typing import Iterable, Sequence, Union
|
|
12
|
+
|
|
13
|
+
InlineContent = Union[str, "Inline"]
|
|
14
|
+
# What users may pass wherever inline text is expected.
|
|
15
|
+
TextLike = Union[str, "Inline", Sequence[InlineContent]]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True)
|
|
19
|
+
class Inline:
|
|
20
|
+
"""A span of text with a style: 'bold', 'italic', 'code', or 'link'."""
|
|
21
|
+
|
|
22
|
+
style: str
|
|
23
|
+
text: str
|
|
24
|
+
href: str = ""
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def bold(text: str) -> Inline:
|
|
28
|
+
"""Bold text span."""
|
|
29
|
+
return Inline("bold", text)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def italic(text: str) -> Inline:
|
|
33
|
+
"""Italic text span."""
|
|
34
|
+
return Inline("italic", text)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def code(text: str) -> Inline:
|
|
38
|
+
"""Inline code span."""
|
|
39
|
+
return Inline("code", text)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def link(text: str, href: str) -> Inline:
|
|
43
|
+
"""Hyperlink span."""
|
|
44
|
+
return Inline("link", text, href)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def normalize(content: TextLike) -> list[InlineContent]:
|
|
48
|
+
"""Coerce any accepted text input into a flat list of str/Inline parts."""
|
|
49
|
+
if isinstance(content, (str, Inline)):
|
|
50
|
+
return [content]
|
|
51
|
+
if isinstance(content, Iterable):
|
|
52
|
+
parts: list[InlineContent] = []
|
|
53
|
+
for item in content:
|
|
54
|
+
if isinstance(item, (str, Inline)):
|
|
55
|
+
parts.append(item)
|
|
56
|
+
else:
|
|
57
|
+
parts.append(str(item))
|
|
58
|
+
return parts
|
|
59
|
+
return [str(content)]
|