-
-
Save biscuitvile/2577706 to your computer and use it in GitHub Desktop.
HTML entities? We don't need no stinkin' HTML entities.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
# | |
# Encode any codepoint outside the ASCII printable range to an HTML character | |
# reference (http://bit.ly/KNupLT). | |
def encode(string) | |
string.each_codepoint.inject("") do |buffer, cp| | |
cp = "&#x#{cp.to_s(16)};" unless cp >= 0x20 && cp <= 0x7E | |
buffer << cp | |
end | |
end | |
puts encode "Japan" | |
# => "Japan" | |
puts encode "日本" | |
# => "日本" | |
puts encode "Japón" | |
# => "Japón" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment