Skip to content

Instantly share code, notes, and snippets.

@andrewle
Created May 14, 2010 17:20
Show Gist options
  • Save andrewle/401392 to your computer and use it in GitHub Desktop.
Save andrewle/401392 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# This tag doesn't work: div.span-3col.column
#
full_line = STDIN.read
raw_tag = if full_line.strip.match(/\s/)
full_line.strip.reverse.match(/^.*?\s/).to_s.reverse.strip
else
full_line.strip.reverse.match(/^.*?\s?$/).to_s.reverse.strip
end
tag = raw_tag.match(/^\w+?\b/)
tag_classes = raw_tag.scan(/\.(\w+?)\b/)
tag_ids = raw_tag.scan(/#(\w+?)\b/)
attr_id = tag_ids.empty? ? "" : " id=\"#{tag_ids.last}\""
attr_class = tag_classes.empty? ? "" : " class=\"#{tag_classes.join(' ')}\""
single_no_arg = /(br)/i
single = /(img|meta|link|input|base|area|col|frame|param|hr)/i
out = case tag
when single_no_arg then "<#{tag} />"
when single then "<#{tag}#{attr_id}#{attr_class}$1 />"
when '' then "<${1:#{ENV['TM_DEFAULT_TAG'] || 'p'}}>$2</${1/\\s.*//}>"
else "<#{tag}#{attr_id}#{attr_class}>$1</#{tag}>"
end
if full_line.length != raw_tag.length
print full_line[0..(full_line.length - raw_tag.length-1)] + out
else
print out
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment