On May 18, 2006, at 7:32 PM, Dave Foy wrote:
I'm having trouble creating a custom snippet that, for me, would be really useful.
I want to set up a snippet to create an unordered list nav from a plain text list, e.g.
foo bar coo tar
I'd like to highlight the whole list, hit a key combo, which would wrap each list item in <li>, wrap the whole lot in <ul>, and even wrap the text within each list item in <a href=""></a> for good measure.
Can anyone help?
This should get you started, i.e. you should be able to easily modify it to do what you want it to, even if you don't know much ruby. Create a command with input selected text with fallback line, output replace selected text (or possibly insert as snippet if you want to do something more clever) and text:
#!/usr/bin/env ruby # print "<ul>\n" STDIN.read.each_line { |x| print "\t<li>\n\t\t<p>#{x.chomp}</p>\n\t</li>\n" } print "</ul>\n"
I hope this is (at least relatively) clear and you should be able to modify it to do stuff. the syntax #{x.chomp} which might seem familiar tells Ruby: "I know you are inside a string now, but execute the thing in the {} as a ruby program and put its output in the string right here". x.chomp returns the string x with the possible newline removed.
Many thanks in advance.
Haris