regex - Escaping a Ruby String for TextMate's $DIALOG command -
calling regex gurus. i'm having trouble right escaping string in ruby can pass command line utility using exec
, %x{}
or similar. command line command textmate dialog feature, works (run shell script in textmate.):
$dialog -mp "items=('string1','string2', 'string3');" requestitem
but can use nib file specify "path/to/my/nib/file.nib" so, need pass whole command string exec
command = <<string $dialog -mp "items=('string1','string2', 'string3');" "path/to/my/nib/file.nib" string exec command
this works fine naturally. problem running components of items array determined programmatically, need programmatically escape string in way pass $dialog
command properly.
so far, have determined need escape single quotes, double quotes, , newline , tab characters. i've tried ton of potential regular expression solutions haven't yet been able land on reliable escaping mechanism. basically, need fill in
class string def escape_for_dialog self end end
with right self.gsub
calls escape strings putting parameters. so, let's assume params looked @ runtime, here's think need using same example above:
command = <<string $dialog -mp "items=('#{item1.escape_for_dialog}', '#{item2.escape_for_dialog}', '#{item3.escape_for_dialog}');" "path/to/my/nib/file.nib" string exec command
i have figured out heredoc style helps not having remember escape quotes, parameters still problematic i'm asking better string manipulation , regular expressions lend hand @ filling in string method. i'll grateful! :)
string.inspect escape delimiters & etc. in way makes shell happy:
itemlist = "(#{items.join(', ')})" command = [ $dialog, '-mp', itemlist.inspect, 'path/to/my/nib/file.nib', ].join(' ') system(command)
Comments
Post a Comment