Skip to content

Instantly share code, notes, and snippets.

@jimmycuadra
Created December 30, 2011 09:47
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 jimmycuadra/1539035 to your computer and use it in GitHub Desktop.
Save jimmycuadra/1539035 to your computer and use it in GitHub Desktop.
Insert Open/Close Tag (With Current Word)
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment