GithubHelp home page GithubHelp logo

cheapsteak / css-inline Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stranger6667/css-inline

0.0 1.0 0.0 142 KB

Inline CSS into style attributes

License: MIT License

Rust 75.27% Python 23.40% CSS 0.03% TypeScript 1.31%

css-inline's Introduction

css-inline

ci Crates.io docs.rs

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>

To use it in your project add the following line to your dependencies section in project's Cargo.toml file:

css-inline = "0.4"

Usage

use css_inline;

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)?;
    // Do something with inlined HTML, e.g. send an email
    Ok(())
}

Features & Configuration

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

use css_inline;

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

Command Line Interface

css-inline provides a command-line interface:

$ css-inline --help

css-inline inlines CSS into HTML documents.

USAGE:
   css-inline [OPTIONS] [PATH ...]
   command | css-inline [OPTIONS]

ARGS:
    <PATH>...
        An HTML document to process. In each specified document "css-inline" will look for
        all relevant "style" and "link" tags, will load CSS from them and then will inline it
        to the HTML tags, according to the relevant CSS selectors.
        When multiple documents are specified, they will be processed in parallel and each inlined
        file will be saved with "inlined." prefix. E.g. for "example.html", there will be
        "inlined.example.html".

OPTIONS:
    --inline-style-tags
        Whether to inline CSS from "style" tags. The default value is `true`. To disable inlining
        from "style" tags use `-inline-style-tags=false`.

    --remove-style-tags
        Remove "style" tags after inlining.

    --base-url
        Used for loading external stylesheets via relative URLs.

    --load-remote-stylesheets
        Whether remote stylesheets should be loaded or not.

    --extra-css
        Additional CSS to inline.

css-inline's People

Contributors

cheapsteak avatar stranger6667 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.