[][src]Crate css_inline

css-inline

A crate for inlining CSS into HTML documents. When you send HTML emails you need to use "style" attributes instead of "style" tags.

For example, this HTML:

<html>
    <head>
        <title>Test</title>
        <style>h1 { color:blue; }</style>
    </head>
    <body>
        <h1>Big Text</h1>
    </body>
</html>

Will be turned into this:

<html>
    <head><title>Test</title></head>
    <body>
        <h1 style="color:blue;">Big Text</h1>
    </body>
</html>

Usage

const HTML: &str = r#"<html>
<head>
    <title>Test</title>
    <style>h1 { color:blue; }</style>
</head>
<body>
    <h1>Big Text</h1>
</body>
</html>"#;

fn main() -> Result<(), css_inline::InlineError> {
   let inlined = css_inline::inline(HTML)?;  // shortcut with default options
   // Do something with inlined HTML, e.g. send an email
   Ok(())
}

Features & Configuration

css-inline can be configured by using InlineOptions and CSSInliner:

const HTML: &str = r#"<html>
<head>
    <title>Test</title>
    <style>h1 { color:blue; }</style>
</head>
<body>
    <h1>Big Text</h1>
</body>
</html>"#;

fn main() -> Result<(), css_inline::InlineError> {
    let options = css_inline::InlineOptions {
        load_remote_stylesheets: false,
        ..Default::default()
    };
    let inliner = css_inline::CSSInliner::new(options);
    let inlined = inliner.inline(HTML);
    // Do something with inlined HTML, e.g. send an email
    Ok(())
}
  • inline_style_tags. Whether to inline CSS from "style" tags. Default: true
  • remove_style_tags. Remove "style" tags after inlining. Default: false
  • base_url. Base URL to resolve relative URLs. Default: None
  • load_remote_stylesheets. Whether remote stylesheets should be loaded or not. Default: true
  • extra_css. Additional CSS to inline. Default: None

Re-exports

pub use error::InlineError;

Modules

error

Errors that may happen during inlining.

Structs

CSSInliner

Customizable CSS inliner.

InlineOptions

Configuration options for CSS inlining process.

Url

A parsed URL record.

Enums

ParseError

Errors that can occur during parsing.

Functions

inline

Shortcut for inlining CSS with default parameters.

inline_to

Shortcut for inlining CSS with default parameters and writing the output to a generic writer.