Skip to content

Repository files navigation

css-inline

Crystal bindings for css-inline — a high-performance library for inlining CSS into HTML. Perfect for preparing HTML emails or embedding HTML into third-party web pages.

Prerequisites

  • Rust toolchain (the C library is compiled from source during installation)

Installation

Add the dependency to your shard.yml:

dependencies:
css-inline:
github: osbre/css-inline

Run shards install — this will automatically build the native library.

Usage

require"css-inline"# Inline CSS from <style> tags
html ="<html><head><style>h1 { color: blue; }</style></head><body><h1>Hello</h1></body></html>"
result =CssInline.inline(html)
# => <html><head></head><body><h1 style="color: blue;">Hello</h1></body></html># Inline a CSS fragment
result =CssInline.inline_fragment("<h1>Hello</h1>", "h1 { color: blue; }")
# => <h1 style="color: blue;">Hello</h1>

Options

inliner =CssInline::CSSInliner.new(
keep_style_tags:true,
extra_css:"h1 { font-size: 2em; }",
minify_css:true,
)
result = inliner.inline(html)

All available options:

OptionDefaultDescription
inline_style_tagstrueInline CSS from <style> tags
keep_style_tagsfalseKeep <style> tags after inlining
keep_link_tagsfalseKeep <link> tags after inlining
keep_at_rulesfalseKeep @media and other at-rules
load_remote_stylesheetstrueFetch and inline remote stylesheets
minify_cssfalseMinify the inlined CSS
remove_inlined_selectorsfalseRemove selectors that were inlined
apply_width_attributesfalseSet width HTML attributes from CSS
apply_height_attributesfalseSet height HTML attributes from CSS
base_urlnilBase URL for resolving relative URLs
extra_cssnilAdditional CSS to inline
cachenilStylesheet cache (see below)

Stylesheet Cache

Reuse parsed stylesheets across multiple calls:

cache =CssInline::StylesheetCache.new(size:64)
inliner =CssInline::CSSInliner.new(cache: cache)
inliner.inline(html1)
inliner.inline(html2) # reuses cached stylesheets

Contributing

  1. Fork it (https://github.com/osbre/css-inline/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

About

Crystal bindings to high-performance library for inlining CSS into HTML 'style' attributes.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages