Class: Prawn::Icon::Parser

Inherits:
Object
  • Object
show all
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

Class Method Details

.config_from_tokens(tokens) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/prawn/icon/parser.rb', line 57

def config_from_tokens(tokens)
  array = []

  tokens.each do |token|
    # Skip the closing tag
    next if token =~ /<\/icon>/i
    icon = {}

    # 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

    icon.merge!(attrs)
    array << icon
  end

  array
end

.format(document, string) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# 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)
  tags    = icon_tags(icons)

  string.gsub(TAG_REGEX).with_index do |_, i|
    tags[i]
  end
end

.icon_tags(icons) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/prawn/icon/parser.rb', line 78

def icon_tags(icons)
  tags = []

  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
      tags  << "#{opening}>#{content}</font>"
      next
    end

    opening += " size=\"#{size}\"" if size
    content = "<color rgb=\"#{color}\">#{content}</color>" if color

    opening += '>'
    tags << "#{opening}#{content}</font>"
  end

  tags
end

.keys_to_unicode(document, content, config) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/prawn/icon/parser.rb', line 107

def keys_to_unicode(document, content, config)
  icons = []

  content.each_with_index do |icon, index|
    options ||= {}
    options = config[index] if config.any?
    info = {
      set:     FontData.specifier_from_key(icon),
      size:    options[:size],
      color:   options[:color],
      content: FontData.unicode_from_key(document, icon)
    }
    icons << info
  end

  icons
end