Module: SimpleIDN

Defined in:
lib/motion-markdown-it/common/simpleidn.rb

Defined Under Namespace

Modules: Punycode

Class Method Summary collapse

Class Method Details

.to_ascii(domain) ⇒ Object

Converts a UTF-8 unicode string to a punycode ACE string.

Example

SimpleIDN.to_ascii("møllerriis.com")
 => "xn--mllerriis-l8a.com"


216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/motion-markdown-it/common/simpleidn.rb', line 216

def to_ascii(domain)
  domain_array = domain.split(".") rescue []
  return domain if domain_array.length == 0
  out = []
  i = 0
  while i < domain_array.length
    s = domain_array[i]
    out << (s =~ /[^A-Z0-9\-*_]/i ? "xn--" + Punycode.encode(s) : s)
    i += 1
  end
  return out.join(".")
end

.to_unicode(domain) ⇒ Object

Converts a punycode ACE string to a UTF-8 unicode string.

Example

SimpleIDN.to_unicode("xn--mllerriis-l8a.com")
 => "møllerriis.com"


233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/motion-markdown-it/common/simpleidn.rb', line 233

def to_unicode(domain)
  domain_array = domain.split(".") rescue []
  return domain if domain_array.length == 0
  out = []
  i = 0
  while i < domain_array.length
    s = domain_array[i]
    out << (s =~ /^xn\-\-/i ? Punycode.decode(s.gsub('xn--','')) : s)
    i += 1
  end
  return out.join(".")
end