Module: Emoji
- Defined in:
- lib/emoji.rb,
lib/emoji/index.rb,
lib/emoji/railtie.rb,
lib/emoji/version.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
"1.0.7"
Class Method Summary collapse
- .alt_tag_for_moji(moji) ⇒ Object
- .asset_host ⇒ Object
- .asset_host=(asset_host) ⇒ Object
- .asset_host_for_asset(asset_path) ⇒ Object
- .asset_path ⇒ Object
- .asset_path=(path) ⇒ Object
- .escape_html(string) ⇒ Object
- .extract_port_string(uri) ⇒ Object
- .extract_uri_scheme_string(asset_host_spec, uri) ⇒ Object
- .image_url_for_name(name) ⇒ Object
- .image_url_for_unicode_moji(moji) ⇒ Object
- .index ⇒ Object
- .parse_and_validate_asset_host(asset_host_spec) ⇒ Object
- .parse_asset_host_uri(asset_host_spec) ⇒ Object
- .replace_named_moji_with_unicode(string) ⇒ Object
- .replace_unicode_moji_with_images(string) ⇒ Object
- .replace_unicode_moji_with_name(string) ⇒ Object
- .safe_string(string) ⇒ Object
- .use_plaintext_alt_tags ⇒ Object
- .use_plaintext_alt_tags=(bool) ⇒ Object
Class Method Details
.alt_tag_for_moji(moji) ⇒ Object
114 115 116 117 118 |
# File 'lib/emoji.rb', line 114 def self.alt_tag_for_moji(moji) return moji unless emoji = index.find_by_moji(moji) emoji['name'] end |
.asset_host ⇒ Object
22 23 24 |
# File 'lib/emoji.rb', line 22 def self.asset_host @asset_host || 'http://localhost:3000' end |
.asset_host=(asset_host) ⇒ Object
26 27 28 |
# File 'lib/emoji.rb', line 26 def self.asset_host=(asset_host) @asset_host = parse_and_validate_asset_host(asset_host) end |
.asset_host_for_asset(asset_path) ⇒ Object
97 98 99 100 101 |
# File 'lib/emoji.rb', line 97 def self.asset_host_for_asset(asset_path) return asset_host if asset_host.kind_of?(String) asset_host.call(asset_path) end |
.asset_path ⇒ Object
81 82 83 |
# File 'lib/emoji.rb', line 81 def self.asset_path @asset_path || '/' end |
.asset_path=(path) ⇒ Object
85 86 87 |
# File 'lib/emoji.rb', line 85 def self.asset_path=(path) @asset_path = path end |
.escape_html(string) ⇒ Object
177 178 179 |
# File 'lib/emoji.rb', line 177 def self.escape_html(string) @escaper.escape_html(string) end |
.extract_port_string(uri) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/emoji.rb', line 73 def self.extract_port_string(uri) return nil unless uri.port return nil if uri.port == 80 && uri.scheme == 'http' return nil if uri.port == 443 && uri.scheme == 'https' return ":#{uri.port}" end |
.extract_uri_scheme_string(asset_host_spec, uri) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/emoji.rb', line 61 def self.extract_uri_scheme_string(asset_host_spec, uri) # Special Case for Protocol Relative Scheme: //hostname.com/ if asset_host_spec.size >= 2 && asset_host_spec[0..1] == '//' return '//' end # Extract Protocol from asset_host_spec URI or default to HTTP scheme = uri.scheme || 'http' "#{ scheme }://" end |
.image_url_for_name(name) ⇒ Object
103 104 105 106 107 |
# File 'lib/emoji.rb', line 103 def self.image_url_for_name(name) path = "#{ File.join(asset_path, name) }.png" host = asset_host_for_asset(path) "#{host}#{path}" end |
.image_url_for_unicode_moji(moji) ⇒ Object
109 110 111 112 |
# File 'lib/emoji.rb', line 109 def self.image_url_for_unicode_moji(moji) emoji = index.find_by_moji(moji) image_url_for_name(emoji['name']) end |
.parse_and_validate_asset_host(asset_host_spec) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/emoji.rb', line 30 def self.parse_and_validate_asset_host(asset_host_spec) return '' unless asset_host_spec return asset_host_spec if asset_host_spec.respond_to?(:call) unless asset_host_spec.kind_of?(String) raise 'Invalid Emoji.asset_host, should be a hostname or URL prefix' end return '' unless asset_host_spec.size >= 1 # Special Case for 'hostname:port' style URIs, not parse properly by URI.parse if asset_host_spec.match(/^[^:\/]+:\d+$/) components = asset_host_spec.split(':') scheme_string = 'http://' hostname = components.first port_string = ":#{components.last}" else uri = parse_asset_host_uri(asset_host_spec) scheme_string = extract_uri_scheme_string(asset_host_spec, uri) hostname = uri.hostname || uri.path port_string = extract_port_string(uri) end "#{ scheme_string }#{ hostname }#{ port_string }" end |
.parse_asset_host_uri(asset_host_spec) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/emoji.rb', line 54 def self.parse_asset_host_uri(asset_host_spec) URI.parse(asset_host_spec) rescue URI::InvalidURIError raise 'Invalid Emoji.asset_host, should be a hostname or URL prefix' end |
.replace_named_moji_with_unicode(string) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/emoji.rb', line 135 def self.replace_named_moji_with_unicode(string) return string unless string unless string.match(index.named_moji_regex) return safe_string(string) end safe_string = safe_string(string.dup) safe_string.gsub!(index.named_moji_regex) do |name_exp| name = name_exp[1..-2] # Convert :heart: to heart, strips first/last chars emoji = index.find_by_name(name) emoji['moji'] end safe_string = safe_string.html_safe if safe_string.respond_to?(:html_safe) safe_string end |
.replace_unicode_moji_with_images(string) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/emoji.rb', line 120 def self.replace_unicode_moji_with_images(string) return string unless string unless string.match(index.unicode_moji_regex) return safe_string(string) end safe_string = safe_string(string.dup) safe_string.gsub!(index.unicode_moji_regex) do |moji| %Q{<img alt="#{alt_tag_for_moji(moji)}" class="emoji" src="#{ image_url_for_unicode_moji(moji) }">} end safe_string = safe_string.html_safe if safe_string.respond_to?(:html_safe) safe_string end |
.replace_unicode_moji_with_name(string) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/emoji.rb', line 153 def self.replace_unicode_moji_with_name(string) return string unless string unless string.match(index.unicode_moji_regex) return safe_string(string) end safe_string = safe_string(string.dup) safe_string.gsub!(index.unicode_moji_regex) do |moji| emoji = index.find_by_moji(moji) ":#{emoji['name']}:" end safe_string = safe_string.html_safe if safe_string.respond_to?(:html_safe) safe_string end |
.safe_string(string) ⇒ Object
169 170 171 172 173 174 175 |
# File 'lib/emoji.rb', line 169 def self.safe_string(string) if string.respond_to?(:html_safe?) && string.html_safe? string else escape_html(string) end end |
.use_plaintext_alt_tags ⇒ Object
89 90 91 |
# File 'lib/emoji.rb', line 89 def self. @use_plaintext_alt_tags || false end |
.use_plaintext_alt_tags=(bool) ⇒ Object
93 94 95 |
# File 'lib/emoji.rb', line 93 def self.(bool) @use_plaintext_alt_tags = bool end |