Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dryan/171308 to your computer and use it in GitHub Desktop.
Save dryan/171308 to your computer and use it in GitHub Desktop.
TextMate command to convert all entities to their hex equivalents
/* Convert Selection to Entities */
#!/usr/bin/env ruby
$KCODE = 'U'
$char_to_entity = { }
File.open("#{ENV['TM_BUNDLE_SUPPORT']}/entities.txt").read.scan(/^(\d+)\t(.+)$/) do |key, value|
$char_to_entity[[key.to_i].pack('U')] = value
end
def encode (text)
text.gsub(/[^\x00-\x7F]|["'<>&]/) do |ch|
if ENV['TM_XHTML'] then
ent = sprintf("&#x%02X;", ch.unpack("U")[0])
else
ent = $char_to_entity[ch]
ent ? "&amp;#{ent};" : sprintf("&amp;#x%02X;", ch.unpack("U")[0])
end
end
end
print encode(STDIN.read)
/* Convert Selection to Entities Excluding Tags */
#!/usr/bin/env ruby
$KCODE = 'U'
$char_to_entity = { }
File.open("#{ENV['TM_BUNDLE_SUPPORT']}/entities.txt").read.scan(/^(\d+)\t(.+)$/) do |key, value|
$char_to_entity[[key.to_i].pack('U')] = value
end
def encode (text)
text.gsub(/[^\x00-\x7F]|["'<>&]/) do |ch|
if ENV['TM_XHTML'] then
ent = sprintf("&#x%02X;", ch.unpack("U")[0])
else
ent = $char_to_entity[ch]
ent ? "&amp;#{ent};" : sprintf("&amp;#x%02X;", ch.unpack("U")[0])
end
end
end
STDIN.read.scan(/(?x)
( <\?(?:[^?
|\?(?!>))
\?>
| <!— (?m:.*?) —>
| <\/? (?i:a|abbr|acronym|address|applet|area|b|base|basefont|bdo|big|blockquote|body|br|button|caption|center|cite|code|col|colgroup|dd|del|dfn|dir|div|dl|dt|em|fieldset|font|form|frame|frameset|h1|h2|h3|h4|h5|h6|head|hr|html|i|iframe|img|input|ins|isindex|kbd|label|legend|li|link|map|menu|meta|noframes|noscript|object|ol|optgroup|option|p|param|pre|q|s|samp|script|select|small|span|strike|strong|style|sub|sup|table|tbody|td|textarea|tfoot|th|thead|title|tr|tt|u|ul|var)\b
(?:[^>"']|"[^"
"|'[^']
')*
>
| &(?:[a-zA-Z0-9
|\#[0-9]
|\#x[0-9a-fA-F]+);
)
|([^<&]+|[<&])
/x) do |tag, text|
print tag.to_s, encode(text.to_s)
end
/* Convert Named Entities to Hex */
#!/usr/bin/env ruby
$KCODE = 'U'
$entity_to_char = { }
File.open("#{ENV['TM_BUNDLE_SUPPORT']}/entities.txt").read.scan(/^(\d+)\t(.+)$/) do |key, value|
$entity_to_char[value] = [key.to_i].pack('U')
end
$char_to_entity = { }
File.open("#{ENV['TM_BUNDLE_SUPPORT']}/entities.txt").read.scan(/^(\d+)\t(.+)$/) do |key, value|
$char_to_entity[[key.to_i].pack('U')] = value
end
def decode ( text )
text.gsub(/&(?:([a-z0-9
)|#([0-9]
)|#x([0-9A-F]+));/i) do |m|
if $1 then
$entity_to_char[$1] || m
else
[$2 ? $2.to_i : $3.hex].pack("U")
end
end
end
def encode( text )
text.gsub(/[^\x00-\x7F]|["'<>&]/) do |ch|
if ENV['TM_XHTML'] then
text = sprintf("&#x%02X;", ch.unpack("U")[0])
else
text = $char_to_entity[ch]
text ? "&amp;#{text};" : sprintf("&amp;#x%02X;", ch.unpack("U")[0])
end
end
end
def transcode( text )
encode( decode( text ) )
end
STDIN.read.scan(/(?x)
( <\?(?:[^?
|\?(?!>))
\?>
| <!— (?m:.*?) —>
| <\/? (?i:a|abbr|acronym|address|applet|area|article|aside|audio|b|base|basefont|bdo|big|blockquote|body|br|button|caption|center|cite|code|col|colgroup|command|datagrid|datalist|datatemplate|dd|del|details|dfn|dialog|dir|div|dl|dt|em|fieldset|font|footer|form|frame|frameset|h1|h2|h3|h4|h5|h6|head|header|hr|html|i|iframe|img|input|ins|isindex|kbd|label|legend|li|link|map|menu|meta|nav|noframes|noscript|object|ol|optgroup|option|p|param|pre|q|s|samp|script|section|select|small|span|strike|strong|style|sub|sup|table|tbody|td|textarea|tfoot|th|thead|title|tr|tt|u|ul|var|video)\b
(?:[^>"']|"[^"
"|'[^']
')*
>
| &(?:[a-zA-Z0-9
|\#[0-9]
|\#x[0-9a-fA-F]+);
)
|([^<&]+|[<&])
/x) do |tag, text|
print tag.to_s, transcode(text.to_s)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment