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?
Many thanks in advance.
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
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.
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.
Here's the command I came up with:
#!/usr/bin/env python import sys
def wrap(string): return '\t<li><a href="">%s</a></li>' % string.split()
print "\n".join(["<ul>"] + [wrap(line) for line in sys.stdin] + ["</ul>"])
Set the input to selection or line. If you want some snippet tab action to enter something in as URL's for the <a>'s, that isn't too hard.
-Jacob
Jacob Rus wrote:
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.
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.
Here's the command I came up with:
#!/usr/bin/env python import sys def wrap(string): return '\t<li><a href="">%s</a></li>' % string.strip() print "\n".join(["<ul>"] + [wrap(line) for line in sys.stdin] + ["</ul>"])
Actually, the following is probably clearer and easier to change, without any knowledge of python:
#!/usr/bin/env python import sys
print "<ul>"
for line in sys.stdin: print '\t<li><a href="">' + line.strip() + '</a></li>'
print "</ul>"
Here's my entry:
As requested, it outputs the text wrapped in <a> tags. As a bonus, it outputs as a snippet, so you can tab through the href attributes to populate them.
On May 18, 2006, at 5: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?
Many thanks in advance.
For new threads USE THIS: textmate@lists.macromates.com (threading gets destroyed and the universe will collapse if you don't) http://lists.macromates.com/mailman/listinfo/textmate
Here's a modified version of Charilaos' script:
#!/usr/bin/env ruby
txt = "<ul>\n" STDIN.read.each_line do |x| txt = txt + "\t<li><a href="">#{x.strip}</a></li>\n" end txt = txt.sub(/\n\z/) { |match| "$0#{match}" } txt = txt + "</ul>" print txt
This is a command, where input is Selected Text or Nothing, and where output is Insert as Snippet. The substitution puts the carat at the end of the last list item. If nothing is selected then it prints: <ul> </ul>
with the carat just after the opening ul tag.
On 5/18/06, Charilaos Skiadas cskiadas@uchicago.edu wrote:
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
For new threads USE THIS: textmate@lists.macromates.com (threading gets destroyed and the universe will collapse if you don't) http://lists.macromates.com/mailman/listinfo/textmate