It is the second part of a series about Rust for Python users.
In this article, we will build a foundation for a Rust-powered Python library - a crate that implements CSS inlining.
It is a process of moving CSS rules from style
tags to the corresponding spots in the HTML body.
This approach to including styles is crucial for sending HTML emails or embedding HTML pages into 3rd party resources.
Our goal is to build a library that will transform this HTML:
<html>
<head>
<style>h1 { color:blue; }</style>
</head>
<body>
<h1>Big Text</h1>
</body>
</html>
into this:
<html>
<head>
<style>h1 { color:blue; }</style>
</head>
<body>
<h1 style="color:blue;">Big Text</h1>
</body>
</html>
We'll go through:
- How CSS inlining works
- Popular crates for HTML & CSS processing
- Configuration: The Builder pattern
- Searching in an HTML document
- Building a CSS parser for qualified rules
- Modifying nodes: Interior Mutability pattern
- Serializing the output into a generic writer
- Next steps & potential improvements
Target audience: Those who know Rust common principles and looking for practical examples. Some familiarity with trait bounds and generics is helpful.