class Prawn::Icon::Parser
Provides the necessary methods to enable the parsing of <icon> tags from input text.
Supported Tags:¶ ↑
- <icon></icon>
-
Place an icon key between the tags and the output will be translated into: <font name=“fa”>unicode</font>.
Supported Attributes:¶ ↑
Various attributes will be extracted from +<icon>+ tags:
- color
-
The hex representation of a color that the icon should be rendered as. If left nil, the document’s fill color will be used.
- size
-
The font size of a particular icon. If left nil, the document’s font size will be used.
Constants
- ATTR_REGEX
-
rubocop:disable Lint/MixedRegexpCaptureTypes
- CONTENT_REGEX
- PARSER_REGEX
- TAG_REGEX
Public Class Methods
Source
# File lib/prawn/icon/parser.rb, line 57 def config_from_tokens(tokens) [].tap do |array| tokens.each do |token| # Skip the closing tag next if token =~ /<\/icon>/i # Convert [[1,2], [3,4]] to { :1 => 2, :3 => 4 } attrs = token.scan(ATTR_REGEX).inject({}) do |k, v| val = attr_hash(v) k.merge!(val) end array << attrs end end end
Source
# File lib/prawn/icon/parser.rb, line 47 def format(document, string) tokens = string.scan(PARSER_REGEX) config = config_from_tokens(tokens) content = string.scan(CONTENT_REGEX).flatten icons = keys_to_unicode(document, content, config) tags = icon_tags(icons) string.gsub(TAG_REGEX).with_index { |_, i| tags[i] } end
Source
# File lib/prawn/icon/parser.rb, line 101 def keys_to_unicode(document, content, config) [].tap do |icons| content.each_with_index do |icon, index| key = Compatibility.new(key: icon).translate options ||= {} options = config[index] if config.any? info = { set: FontData.specifier_from_key(key), size: options[:size], color: options[:color], content: FontData.unicode_from_key(document, key) } icons << info end end end