Class: Unmagic::Icon
- Inherits:
-
Object
show all
- Defined in:
- lib/unmagic/icon.rb,
lib/unmagic/icon/web.rb,
lib/unmagic/icon/engine.rb,
lib/unmagic/icon/library.rb,
lib/unmagic/icon/version.rb,
lib/unmagic/icon/configuration.rb,
lib/unmagic/icon/library/source.rb,
lib/unmagic/icon/library/registry.rb,
lib/unmagic/icon/action_view_helpers.rb,
lib/unmagic/icon/library/source/silk.rb,
lib/unmagic/icon/library/source/lucide.rb,
lib/unmagic/icon/library/source/tabler.rb,
lib/unmagic/icon/library/source/feather.rb,
lib/unmagic/icon/library/source/devicons.rb,
lib/unmagic/icon/library/source/heroicons.rb,
lib/unmagic/icon/library/source/simple_icons.rb,
lib/unmagic/icon/library/source/material_file_icons.rb
Defined Under Namespace
Modules: ActionViewHelpers
Classes: Configuration, Engine, Error, IconNotFoundError, Library, LibraryNotFoundError, Web
Constant Summary
collapse
- VERSION =
"0.1.0"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name:, path:) ⇒ Icon
Returns a new instance of Icon.
60
61
62
63
|
# File 'lib/unmagic/icon.rb', line 60
def initialize(name:, path:)
@name = name
@path = path
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
58
59
60
|
# File 'lib/unmagic/icon.rb', line 58
def name
@name
end
|
#path ⇒ Object
Returns the value of attribute path.
58
59
60
|
# File 'lib/unmagic/icon.rb', line 58
def path
@path
end
|
Class Method Details
31
32
33
34
|
# File 'lib/unmagic/icon.rb', line 31
def configure
yield(configuration) if block_given?
configuration
end
|
.find(reference) ⇒ Object
44
45
46
47
48
|
# File 'lib/unmagic/icon.rb', line 44
def find(reference)
library_path, icon_name = parse_reference(reference)
Unmagic::Icon::Library::Registry.find(library_path).find(icon_name)
end
|
.init {|configuration| ... } ⇒ Object
22
23
24
25
|
# File 'lib/unmagic/icon.rb', line 22
def init
yield(configuration) if block_given?
@initialized = true
end
|
.initialized? ⇒ Boolean
27
28
29
|
# File 'lib/unmagic/icon.rb', line 27
def initialized?
@initialized == true
end
|
.parse_reference(reference) ⇒ Object
Parse a “library/name” reference into [library, name], tolerating the emoji-style “:library/name:” decoration so both kinds share one syntax.
52
53
54
55
|
# File 'lib/unmagic/icon.rb', line 52
def parse_reference(reference)
*library_parts, name = reference.to_s.gsub(/\A:|:\z/, "").split("/")
[ library_parts.join("/"), name ]
end
|
Instance Method Details
#as_json ⇒ Object
77
78
79
|
# File 'lib/unmagic/icon.rb', line 77
def as_json
{ name: name, svg: to_svg }
end
|
#attributes ⇒ Object
69
70
71
|
# File 'lib/unmagic/icon.rb', line 69
def attributes
[:attributes]
end
|
#contents ⇒ Object
73
74
75
|
# File 'lib/unmagic/icon.rb', line 73
def contents
[:contents].strip
end
|
#doc ⇒ Object
65
66
67
|
# File 'lib/unmagic/icon.rb', line 65
def doc
Nokogiri::XML(raw_svg_content)
end
|
#render(options = {}) ⇒ Object
Render the asset to HTML, dispatching on its kind. SVG inlines today; a future raster asset (png/gif) would emit an <img> here, so callers stay render-agnostic.
84
85
86
87
88
89
90
|
# File 'lib/unmagic/icon.rb', line 84
def render(options = {})
case File.extname(@path).downcase
when ".svg" then to_svg(options)
else
raise Unmagic::Icon::Error, "Don't know how to render #{@path}"
end
end
|
#to_svg(options = {}) ⇒ Object
Render the SVG with a ‘unmagic-icon` class (plus any caller class) and a `data-unmagic-icon` marker. Any other options are merged verbatim as attributes on the <svg> — so the caller controls accessibility (`aria-hidden`, `aria-label`, `role`), `id`, `data-*`, etc.
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/unmagic/icon.rb', line 96
def to_svg(options = {})
return @svg_cache if options.empty? && @svg_cache
attributes = options.transform_keys(&:to_s)
css_classes = [ "unmagic-icon", attributes.delete("class") ].compact.join(" ")
svg = raw_svg_content.dup
svg.sub!(/<svg(\s+[^>]*)?>/i) do
existing = (::Regexp.last_match(1) || "").gsub(/\sclass=["'][^"']*["']/, "")
= attributes.map { |name, value| %(#{name}="#{CGI.escapeHTML(value.to_s)}") }
%(<svg#{existing} class="#{css_classes}" data-unmagic-icon="#{@name}"#{.empty? ? "" : " #{.join(' ')}"}>)
end
svg = svg.html_safe
@svg_cache = svg if options.empty?
svg
end
|