public
Created

Insert Open/Close Tag (With Current Word)

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#!/usr/bin/env ruby
#
# This script will expand the current word into: <word></word>
# It will recognize HTML 4.0 tags that need no close tag.
#
# With no current word, it will insert: <p></p> and allows you
# to overwrite the tag name and add potential arguments.
#
# The result is inserted as a snippet, so it's
# possible to tab through the place holders.
 
# single tags
single_no_arg = /^(?:br|hr)$/i
single = /^(?:img|meta|link|input|base|area|col|frame|param)$/i
 
# we are not in HTML mode, so let’s scrap the above hardcoded tag lists
unless ENV.has_key? 'TM_HTML_EMPTY_TAGS' then
single_no_arg = /(?=not)possible/
single = /(?=not)possible/
end
 
# handle the case where caret is in the middle of a word, assume only the left part is the tag
index = ENV['TM_LINE_INDEX'].to_i - ENV['TM_INPUT_START_LINE_INDEX'].to_i
tag, suffix = STDIN.read, ''
if index < tag.length && !ENV['TM_SELECTED_TEXT']
tag, suffix = tag[0...index], tag[index..-1]
end
 
xhtml = ENV['TM_XHTML'].to_s
 
print case tag
when single_no_arg then "<#{tag}#{xhtml}>"
when single then "<#{tag} $1#{xhtml}>"
when '' then "<${1:#{ENV['TM_DEFAULT_TAG'] || 'p'}}>$2</${1/\\s.*//}>"
else "<#{tag}>$1</#{tag.strip[/^\S+/]}>"
end
 
print suffix

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.