Class: Prawn::Icon::Parser
- Inherits:
-
Object
- Object
- Prawn::Icon::Parser
- Defined in:
- lib/prawn/icon/parser.rb
Overview
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.
Constant Summary collapse
- PARSER_REGEX =
Regexp.new \ '<icon[^>]*>|</icon>', Regexp::IGNORECASE | Regexp::MULTILINE
- CONTENT_REGEX =
/<icon[^>]*>(?<content>[^<]*)<\/icon>/mi
- TAG_REGEX =
/<icon[^>]*>[^<]*<\/icon>/mi
- ATTR_REGEX =
/(?<attr>[a-zA-Z]*)=["|'](?<val>(\w*[^["|']]))["|']/mi
Class Method Summary collapse
- .config_from_tokens(tokens) ⇒ Object
- .format(document, string) ⇒ Object
- .icon_tags(icons) ⇒ Object
- .keys_to_unicode(document, content, config) ⇒ Object
Class Method Details
.config_from_tokens(tokens) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/prawn/icon/parser.rb', line 55 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 |
.format(document, string) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/prawn/icon/parser.rb', line 45 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) = (icons) string.gsub(TAG_REGEX).with_index { |_, i| [i] } end |
.icon_tags(icons) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/prawn/icon/parser.rb', line 72 def (icons) [].tap do || icons.each do |icon| # Mandatory content = icon[:content] set = icon[:set] # Optional color = icon[:color] size = icon[:size] opening = "<font name=\"#{set}\"" unless color || size << "#{opening}>#{content}</font>" next end opening += " size=\"#{size}\"" if size content = "<color rgb=\"#{color}\">#{content}</color>" if color opening += '>' << "#{opening}#{content}</font>" end end end |
.keys_to_unicode(document, content, config) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/prawn/icon/parser.rb', line 99 def keys_to_unicode(document, content, config) [].tap do |icons| content.each_with_index do |icon, index| key = Compatibility.new(key: icon).translate ||= {} = config[index] if config.any? info = { set: FontData.specifier_from_key(key), size: [:size], color: [:color], content: FontData.unicode_from_key(document, key) } icons << info end end end |