css-inline 0.21.0__cp310-abi3-win_amd64.whl

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.
css_inline/__init__.py ADDED
@@ -0,0 +1,5 @@
1
+ from .css_inline import *
2
+
3
+ __doc__ = css_inline.__doc__
4
+ if hasattr(css_inline, "__all__"):
5
+ __all__ = css_inline.__all__
@@ -0,0 +1,83 @@
1
+ class StylesheetCache:
2
+ def __init__(self, size: int = 8) -> None: ...
3
+
4
+ class CSSInliner:
5
+ def __init__(
6
+ self,
7
+ inline_style_tags: bool = True,
8
+ keep_style_tags: bool = False,
9
+ keep_link_tags: bool = False,
10
+ keep_at_rules: bool = False,
11
+ minify_css: bool = False,
12
+ base_url: str | None = None,
13
+ load_remote_stylesheets: bool = True,
14
+ cache: StylesheetCache | None = None,
15
+ extra_css: str | None = None,
16
+ preallocate_node_capacity: int | None = 32,
17
+ remove_inlined_selectors: bool = False,
18
+ ) -> None: ...
19
+ def inline(self, html: str) -> str: ...
20
+ def inline_fragment(self, html: str, css: str) -> str: ...
21
+ def inline_many(self, html: list[str]) -> list[str]: ...
22
+ def inline_many_fragments(self, html: list[str], css: list[str]) -> list[str]: ...
23
+
24
+ def inline(
25
+ html: str,
26
+ inline_style_tags: bool = True,
27
+ keep_style_tags: bool = False,
28
+ keep_link_tags: bool = False,
29
+ keep_at_rules: bool = False,
30
+ minify_css: bool = False,
31
+ base_url: str | None = None,
32
+ load_remote_stylesheets: bool = True,
33
+ cache: StylesheetCache | None = None,
34
+ extra_css: str | None = None,
35
+ preallocate_node_capacity: int | None = 32,
36
+ remove_inlined_selectors: bool = False,
37
+ ) -> str: ...
38
+ def inline_fragment(
39
+ html: str,
40
+ css: str,
41
+ inline_style_tags: bool = True,
42
+ keep_style_tags: bool = False,
43
+ keep_link_tags: bool = False,
44
+ keep_at_rules: bool = False,
45
+ minify_css: bool = False,
46
+ base_url: str | None = None,
47
+ load_remote_stylesheets: bool = True,
48
+ cache: StylesheetCache | None = None,
49
+ extra_css: str | None = None,
50
+ preallocate_node_capacity: int | None = 32,
51
+ remove_inlined_selectors: bool = False,
52
+ ) -> str: ...
53
+ def inline_many(
54
+ html: list[str],
55
+ inline_style_tags: bool = True,
56
+ keep_style_tags: bool = False,
57
+ keep_link_tags: bool = False,
58
+ keep_at_rules: bool = False,
59
+ minify_css: bool = False,
60
+ base_url: str | None = None,
61
+ load_remote_stylesheets: bool = True,
62
+ cache: StylesheetCache | None = None,
63
+ extra_css: str | None = None,
64
+ preallocate_node_capacity: int | None = 32,
65
+ remove_inlined_selectors: bool = False,
66
+ ) -> list[str]: ...
67
+ def inline_many_fragments(
68
+ html: list[str],
69
+ css: list[str],
70
+ inline_style_tags: bool = True,
71
+ keep_style_tags: bool = False,
72
+ keep_link_tags: bool = False,
73
+ keep_at_rules: bool = False,
74
+ minify_css: bool = False,
75
+ base_url: str | None = None,
76
+ load_remote_stylesheets: bool = True,
77
+ cache: StylesheetCache | None = None,
78
+ extra_css: str | None = None,
79
+ preallocate_node_capacity: int | None = 32,
80
+ remove_inlined_selectors: bool = False,
81
+ ) -> list[str]: ...
82
+
83
+ class InlineError(ValueError): ...
Binary file
css_inline/py.typed ADDED
File without changes
@@ -0,0 +1,331 @@
1
+ Metadata-Version: 2.4
2
+ Name: css-inline
3
+ Version: 0.21.0
4
+ Classifier: Development Status :: 4 - Beta
5
+ Classifier: Intended Audience :: Developers
6
+ Classifier: License :: OSI Approved :: MIT License
7
+ Classifier: Operating System :: MacOS :: MacOS X
8
+ Classifier: Operating System :: Microsoft :: Windows
9
+ Classifier: Operating System :: POSIX
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Classifier: Programming Language :: Python :: 3.14
16
+ Classifier: Programming Language :: Python :: Implementation :: CPython
17
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
18
+ Classifier: Programming Language :: Rust
19
+ License-File: LICENSE
20
+ Summary: High-performance library for inlining CSS into HTML 'style' attributes
21
+ Keywords: css,html,email,stylesheet,inlining
22
+ Author-email: Dmitry Dygalo <dmitry@dygalo.dev>
23
+ Requires-Python: >=3.10
24
+ Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
25
+ Project-URL: homepage, https://github.com/Stranger6667/css-inline/tree/master/bindings/python
26
+ Project-URL: repository, https://github.com/Stranger6667/css-inline
27
+
28
+ # css_inline
29
+
30
+ [<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/Stranger6667/css-inline/build.yml?style=flat-square&labelColor=555555&logo=github" height="20">](https://github.com/Stranger6667/css-inline/actions/workflows/build.yml)
31
+ [<img alt="pypi" src="https://img.shields.io/pypi/v/css_inline.svg?style=flat-square" height="20">](https://pypi.org/project/css_inline/)
32
+ [<img alt="versions" src="https://img.shields.io/pypi/pyversions/css_inline.svg?style=flat-square" height="20">](https://pypi.org/project/css_inline/)
33
+ [<img alt="license" src="https://img.shields.io/pypi/l/css_inline.svg?style=flat-square" height="20">](https://opensource.org/licenses/MIT)
34
+ [<img alt="codecov.io" src="https://img.shields.io/codecov/c/gh/Stranger6667/css-inline?logo=codecov&style=flat-square&token=tOzvV4kDY0" height="20">](https://app.codecov.io/github/Stranger6667/css-inline)
35
+ [<img alt="gitter" src="https://img.shields.io/gitter/room/Stranger6667/css-inline?style=flat-square" height="20">](https://gitter.im/Stranger6667/css-inline)
36
+
37
+ `css_inline` is a high-performance library for inlining CSS into HTML 'style' attributes.
38
+
39
+ This library is designed for scenarios such as preparing HTML emails or embedding HTML into third-party web pages.
40
+
41
+ For instance, the library transforms HTML like this:
42
+
43
+ ```html
44
+ <html>
45
+ <head>
46
+ <style>h1 { color:blue; }</style>
47
+ </head>
48
+ <body>
49
+ <h1>Big Text</h1>
50
+ </body>
51
+ </html>
52
+ ```
53
+
54
+ into:
55
+
56
+ ```html
57
+ <html>
58
+ <head></head>
59
+ <body>
60
+ <h1 style="color:blue;">Big Text</h1>
61
+ </body>
62
+ </html>
63
+ ```
64
+
65
+ - Uses reliable components from Mozilla's Servo project
66
+ - 10-500x faster than alternatives
67
+ - Inlines CSS from `style` and `link` tags
68
+ - Removes `style` and `link` tags
69
+ - Resolves external stylesheets (including local files)
70
+ - Optionally caches external stylesheets
71
+ - Can process multiple documents in parallel
72
+ - Works on Linux, Windows, macOS and in the browser via PyOdide
73
+ - Supports HTML5 & CSS3
74
+ - Tested on CPython 3.10, 3.11, 3.12, 3.13, 3.14 and PyPy 3.11.
75
+
76
+ ## Playground
77
+
78
+ If you'd like to try `css-inline`, you can check the WebAssembly-powered [playground](https://css-inline.org/) to see the results instantly.
79
+
80
+ ## Installation
81
+
82
+ Install with `pip`:
83
+
84
+ ```shell
85
+ pip install css_inline
86
+ ```
87
+
88
+ Pre-compiled wheels are available for most popular platforms.
89
+ If not available for your platform, a Rust compiler will be needed to build this package from source. Rust version 1.65 or higher is required.
90
+
91
+ ## Usage
92
+
93
+ ```python
94
+ import css_inline
95
+
96
+ HTML = """<html>
97
+ <head>
98
+ <style>h1 { color:blue; }</style>
99
+ </head>
100
+ <body>
101
+ <h1>Big Text</h1>
102
+ </body>
103
+ </html>"""
104
+
105
+ inlined = css_inline.inline(HTML)
106
+ # HTML becomes this:
107
+ #
108
+ # <html>
109
+ # <head>
110
+ # <style>h1 { color:blue; }</style>
111
+ # </head>
112
+ # <body>
113
+ # <h1 style="color:blue;">Big Text</h1>
114
+ # </body>
115
+ # </html>
116
+ ```
117
+
118
+ Note that `css-inline` automatically adds missing `html` and `body` tags, so the output is a valid HTML document.
119
+
120
+ Alternatively, you can inline CSS into an HTML fragment. Structural tags (`<html>`, `<head>`, `<body>`) are stripped from the output; only their contents are preserved. Use `inline` if you need to keep the full document structure:
121
+
122
+ ```python
123
+ FRAGMENT = """<main>
124
+ <h1>Hello</h1>
125
+ <section>
126
+ <p>who am i</p>
127
+ </section>
128
+ </main>"""
129
+
130
+ CSS = """
131
+ p {
132
+ color: red;
133
+ }
134
+
135
+ h1 {
136
+ color: blue;
137
+ }
138
+ """
139
+
140
+ inlined = css_inline.inline_fragment(FRAGMENT, CSS)
141
+ # HTML becomes this:
142
+ # <main>
143
+ # <h1 style="color: blue;">Hello</h1>
144
+ # <section>
145
+ # <p style="color: red;">who am i</p>
146
+ # </section>
147
+ # </main>
148
+ ```
149
+
150
+ When there is a need to inline multiple HTML documents simultaneously, `css_inline` offers `inline_many` and `inline_many_fragments` functions.
151
+ This feature allows for concurrent processing of several inputs, significantly improving performance when dealing with a large number of documents.
152
+
153
+ ```python
154
+ import css_inline
155
+
156
+ css_inline.inline_many(["<...>", "<...>"])
157
+ ```
158
+
159
+ Under the hood, `inline_many`, spawns threads at the Rust layer to handle the parallel processing of inputs.
160
+ This results in faster execution times compared to employing parallel processing techniques at the Python level.
161
+
162
+ **Note**: To fully benefit from `inline_many`, you should run your application on a multicore machine.
163
+
164
+ ### Configuration
165
+
166
+ For configuration options use the `CSSInliner` class:
167
+
168
+ ```python
169
+ import css_inline
170
+
171
+ inliner = css_inline.CSSInliner(keep_style_tags=True)
172
+ inliner.inline("...")
173
+ ```
174
+
175
+ - `inline_style_tags`. Specifies whether to inline CSS from "style" tags. Default: `True`
176
+ - `keep_style_tags`. Specifies whether to keep "style" tags after inlining. Default: `False`
177
+ - `keep_link_tags`. Specifies whether to keep "link" tags after inlining. Default: `False`
178
+ - `keep_at_rules`. Specifies whether to keep "at-rules" (starting with `@`) after inlining. Default: `False`
179
+ - `minify_css`. Specifies whether to remove trailing semicolons and spaces between properties and values. Default: `False`
180
+ - `base_url`. The base URL used to resolve relative URLs. If you'd like to load stylesheets from your filesystem, use the `file://` scheme. Default: `None`
181
+ - `load_remote_stylesheets`. Specifies whether remote stylesheets should be loaded. Default: `True`
182
+ - `cache`. Specifies caching options for external stylesheets (for example, `StylesheetCache(size=5)`). Default: `None`
183
+ - `extra_css`. Extra CSS to be inlined. Default: `None`
184
+ - `preallocate_node_capacity`. **Advanced**. Preallocates capacity for HTML nodes during parsing. This can improve performance when you have an estimate of the number of nodes in your HTML document. Default: `32`
185
+ - `remove_inlined_selectors`. Specifies whether to remove selectors that were successfully inlined from `<style>` blocks. Default: `False`
186
+ - `apply_width_attributes`. Specifies whether to add `width` HTML attributes from CSS `width` properties on supported elements (`table`, `td`, `th`, `img`). Default: `False`
187
+ - `apply_height_attributes`. Specifies whether to add `height` HTML attributes from CSS `height` properties on supported elements (`table`, `td`, `th`, `img`). Default: `False`
188
+
189
+ You can also skip CSS inlining for an HTML tag by adding the `data-css-inline="ignore"` attribute to it:
190
+
191
+ ```html
192
+ <head>
193
+ <style>h1 { color:blue; }</style>
194
+ </head>
195
+ <body>
196
+ <!-- The tag below won't receive additional styles -->
197
+ <h1 data-css-inline="ignore">Big Text</h1>
198
+ </body>
199
+ ```
200
+
201
+ The `data-css-inline="ignore"` attribute also allows you to skip `link` and `style` tags:
202
+
203
+ ```html
204
+ <head>
205
+ <!-- Styles below are ignored -->
206
+ <style data-css-inline="ignore">h1 { color:blue; }</style>
207
+ </head>
208
+ <body>
209
+ <h1>Big Text</h1>
210
+ </body>
211
+ ```
212
+
213
+ Alternatively, you may keep `style` from being removed by using the `data-css-inline="keep"` attribute.
214
+ This is useful if you want to keep `@media` queries for responsive emails in separate `style` tags.
215
+ Such tags will be kept in the resulting HTML even if the `keep_style_tags` option is set to `false`.
216
+
217
+ ```html
218
+ <head>
219
+ <!-- Styles below are not removed -->
220
+ <style data-css-inline="keep">h1 { color:blue; }</style>
221
+ </head>
222
+ <body>
223
+ <h1>Big Text</h1>
224
+ </body>
225
+ ```
226
+
227
+ Another possibility is to set `keep_at_rules` option to `true`. At-rules cannot be inlined into HTML therefore they
228
+ get removed by default. This is useful if you want to keep at-rules, e.g. `@media` queries for responsive emails in
229
+ separate `style` tags but inline any styles which can be inlined.
230
+ Such tags will be kept in the resulting HTML even if the `keep_style_tags` option is explicitly set to `false`.
231
+
232
+ ```html
233
+ <head>
234
+ <!-- With keep_at_rules=true "color:blue" will get inlined into <h1> but @media will be kept in <style> -->
235
+ <style>h1 { color: blue; } @media (max-width: 600px) { h1 { font-size: 18px; } }</style>
236
+ </head>
237
+ <body>
238
+ <h1>Big Text</h1>
239
+ </body>
240
+ ```
241
+
242
+ If you set the the `minify_css` option to `true`, the inlined styles will be minified by removing trailing semicolons
243
+ and spaces between properties and values.
244
+
245
+ ```html
246
+ <head>
247
+ <!-- With minify_css=True, the <h1> will have `style="color:blue;font-weight:bold"` -->
248
+ <style>h1 { color: blue; font-weight: bold; }</style>
249
+ </head>
250
+ <body>
251
+ <h1>Big Text</h1>
252
+ </body>
253
+ ```
254
+
255
+ If you'd like to load stylesheets from your filesystem, use the `file://` scheme:
256
+
257
+ ```python
258
+ import css_inline
259
+
260
+ # styles/email is relative to the current directory
261
+ inliner = css_inline.CSSInliner(base_url="file://styles/email/")
262
+ inliner.inline("...")
263
+ ```
264
+
265
+ You can also cache external stylesheets to avoid excessive network requests:
266
+
267
+ ```python
268
+ import css_inline
269
+
270
+ inliner = css_inline.CSSInliner(
271
+ cache=css_inline.StylesheetCache(size=5)
272
+ )
273
+ inliner.inline("...")
274
+ ```
275
+
276
+ Caching is disabled by default.
277
+
278
+ ## XHTML compatibility
279
+
280
+ If you'd like to work around some XHTML compatibility issues like closing empty tags (`<hr>` vs. `<hr/>`), you can use the following snippet that involves `lxml`:
281
+
282
+ ```python
283
+ import css_inline
284
+ from lxml import html, etree
285
+
286
+ document = "..." # Your HTML document
287
+ inlined = css_inline.inline(document)
288
+ tree = html.fromstring(inlined)
289
+ inlined = etree.tostring(tree).decode(encoding="utf-8")
290
+ ```
291
+
292
+ ## Performance
293
+
294
+ `css-inline` is powered by efficient tooling from Mozilla's Servo project and significantly outperforms other Python alternatives in terms of speed.
295
+ Most of the time it achieves over a **10x** speed advantage compared to the next fastest alternative.
296
+
297
+ Here is the performance comparison:
298
+
299
+ | | Size | `css_inline 0.19.0` | `premailer 3.10.0` | `toronado 0.1.0` | `inlinestyler 0.2.5` | `pynliner 0.8.0` |
300
+ |-------------|---------|---------------------|------------------------|-------------------------|------------------------|-------------------------|
301
+ | Basic | 230 B | 4.27 µs | 85.05 µs (**19.93x**) | 495.30 µs (**116.05x**) | 1.02 ms (**238.87x**) | 867.79 µs (**203.32x**) |
302
+ | Realistic-1 | 8.58 KB | 80.59 µs | 1.03 ms (**12.76x**) | 11.55 ms (**143.29x**) | 26.37 ms (**327.21x**) | 11.71 ms (**145.36x**) |
303
+ | Realistic-2 | 4.3 KB | 46.88 µs | 1.44 ms (**30.73x**) | ERROR | 17.71 ms (**377.77x**) | ERROR |
304
+ | GitHub page | 1.81 MB | 17.57 ms | 10.78 s (**613.48x**) | ERROR | ERROR | ERROR |
305
+
306
+ The "Basic" case was obtained by benchmarking the example from the Usage section.
307
+ Note that the `toronado`, `inlinestyler`, and `pynliner` libraries encountered errors when used to inline CSS in the last scenarios.
308
+
309
+ The benchmarking code is available in the `benches/bench.py` file. The benchmarks were conducted using the stable `rustc 1.91`, Python `3.14.2` on Ryzen 9 9950X.
310
+
311
+ ## Comparison with other libraries
312
+
313
+ Besides performance, `css-inline` differs from other Python libraries for CSS inlining.
314
+
315
+ - Generally supports more CSS features than other libraries (for example, `toronado` and `pynliner` do not support pseudo-elements);
316
+ - It has fewer configuration options and is not as flexible as `premailer`;
317
+ - Works on fewer platforms than LXML-based libraries (`premailer`, `inlinestyler`, `toronado`, and optionally `pynliner`);
318
+ - Does not have debug logs yet;
319
+ - Supports only HTML 5.
320
+
321
+ ## Further reading
322
+
323
+ If you want to know how this library was created & how it works internally, you could take a look at these articles:
324
+
325
+ - [Rust crate](https://dygalo.dev/blog/rust-for-a-pythonista-2/)
326
+ - [Python bindings](https://dygalo.dev/blog/rust-for-a-pythonista-3/)
327
+
328
+ ## License
329
+
330
+ This project is licensed under the terms of the [MIT license](https://opensource.org/licenses/MIT).
331
+
@@ -0,0 +1,9 @@
1
+ css_inline/__init__.py,sha256=Idodv4vLn31heDtdkdsRJjLUiFhqZ98AVCXzfNsuGgc,123
2
+ css_inline/__init__.pyi,sha256=oELQofaJ8kiPiqZjmaEZFN1S3DiqyEXp4hNoBNVJPro,2869
3
+ css_inline/css_inline.pyd,sha256=zfRcz97W4OncMdMGO0_-aosLfoo2Vl9zhlwI58E2W3Y,4282880
4
+ css_inline/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ css_inline-0.21.0.dist-info/METADATA,sha256=FiLY8Ixh2pzzg2AEiK401vjnu4kJiZoF5SXcfzpDZoA,13080
6
+ css_inline-0.21.0.dist-info/WHEEL,sha256=OudrOkgSd5TAMiYUmv0po5aZeINNCjLAn4tWEN0V1Jc,96
7
+ css_inline-0.21.0.dist-info/licenses/LICENSE,sha256=dE2b3DgsGRyrpy48m_gfzgxdni8MGnvE8iDDyi_1jCY,1096
8
+ css_inline-0.21.0.dist-info/sboms/css-inline-python.cyclonedx.json,sha256=1hIFuDTwGOyoNF9RPgblPafkbzfNz0r9O4EPH4oVl9c,202140
9
+ css_inline-0.21.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: maturin (1.14.0)
3
+ Root-Is-Purelib: false
4
+ Tag: cp310-abi3-win_amd64
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020-2023 Dmitry Dygalo
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.