Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add macro to inline SVG files #1761

Merged
merged 6 commits into from
Nov 17, 2022
Merged

Conversation

wout
Copy link
Contributor

@wout wout commented Nov 15, 2022

Purpose

Adds a macro to inline SVG files at compile time.

Description

SVG logos and icons can be inlined with the inline_svg macro in pages and components:

button
  inline_svg("buttons/round.svg")
end

This will:

  • inline the SVG file from src/svgs at compile-time (path configurable with an annotation)
  • strip the XML declaration, comments newlines and superfluous whitespace
  • optionally (and by default) strip styling attributes so the icons can be styled with CSS (also configurable with an annotation)
  • raise a compile-time exception when an SVG file is missing
  • add a data-inline-svg attribute with the path of the icon (e.g. <svg data-inline-svg="buttons/round.svg">)

Icons can then be styled with:

[data-inline-svg] {
  fill: none;
  stroke: #222;
  stroke-width: 2px;
}

Or:

[data-inline-svg="buttons/round.svg"] {
  fill: #f03;
}

I realise Lucky does not use annotations a lot, but I think they are helpful in this case. The settings defined by the annotations are reasonable defaults, I think, but if necessary, users can jump in and override the defaults.

This PR implements #1760.

Checklist

  • - An issue already exists detailing the issue/or feature request that this PR fixes
  • - All specs are formatted with crystal tool format spec src
  • - Inline documentation has been added and/or updated
  • - Lucky builds on docker with ./script/setup
  • - All builds and specs pass on docker with ./script/test

@wout
Copy link
Contributor Author

wout commented Nov 15, 2022

Forgot to mention I'll add the necessary documentation on the website as well when this is approved.

Copy link
Contributor

@robacarp robacarp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this idea. I use a lot of SVGs and have lamented that I can't easily inline them as part of compilation.

inlined_svg.should contain %(fill="none" stroke="#2a2a2a" class="logo")
inlined_svg.should_not contain %(<?xml version="1.0" encoding="UTF-8"?>)
inlined_svg.should_not contain %(<!-- lucky logo -->)
inlined_svg.should_not contain "\n"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that this strips out un-needed stuff from the XML. Should these should_not cases be broken out into an it "strips out comments" and it "strips out the xml header" set of tests? As it is, if something comes up and someone needs to modify the filter, they don't have a test which directly correlates to that feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I'll add separate specs for those cases separately.

end

@[Lucky::SvgInliner::Path("src/svgs")]
@[Lucky::SvgInliner::StripRegex(/(class|fill|stroke|stroke-width|style)="[^"]+" ?/)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you help me understand what's going on with these annotations? It seems like a roundabout way to accomplish arguments to the macro.

Copy link
Contributor Author

@wout wout Nov 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I think having those parameters passed as arguments would be tedious. Let's say the user wants to use a different directory for the SVG files, then it suffices to override the annotation in an initializer:

# config/svg_inliner.cr
@[Lucky::SvgInliner::Path("src/icons")]
module Lucky::SvgInliner
end

As opposed to (for every single SVG):

inline_svg("menu-social/twitter.svg", false, "src/icons")
inline_svg("menu-social/discord.svg", false, "src/icons")

Does that answer your question? Or am I missing the point?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh interesting, I hadn't considered that. I'm not opposed. Another possibility for this would be to have a Habitat config parameter. @jwoertink @stephendolan thoughts?

Copy link
Contributor Author

@wout wout Nov 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried that first, but settings from Habitat are not available at compile-time. As far as I know, the only way to allow compile-time configuration is by using annotations or constants. And using constants is not favourable because it's impossible to override them.

I think these are sensible defaults, so in 99% of the cases, it wouldn't be necessary to change them. The annotations are just an escape hatch.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's different for sure, but I'm willing to give it a shot. I don't have a strong opinion on it vs just using Habitat.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wout yeah, you present a use case I can't argue with. Thank you for walking me through it. Let's go!

Copy link
Member

@jwoertink jwoertink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it looks good 👍

@jwoertink jwoertink merged commit dbd66f9 into luckyframework:main Nov 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants