Hello all,
I started using TextMate 1.1b4 and really like it so far. I'm just beginning to learn about Ruby and TextMate is much better than BBEdit with it.
I was looking around the Web for some example code, and saw on someone's blog that they had colorized Ruby code examples with line numbers. I emailed them to ask them how they did it, and they told me they used Vim. They said in Vim, you open the file you want colorized and type ":runtime! syntax/2html.vim". Vim will then split the window with the original in the upper half and the colorized HTML version in the bottom half. He then copies what he needs out of that and pastes it into his blog entry. He says it will work with any language Vim understands.
What I'm wanting to know is if there's a way to do something like this in TextMate? Perhaps a command? I'm not familiar with the inner workings or power toys in TextMate, so any help would be appreciated.
Thanks, Chris
On Feb 5, 2005, at 0:43, Chris Ruzin wrote:
[...] Vim will then split the window with the original in the upper half and the colorized HTML version in the bottom half. He then copies what he needs out of that and pastes it into his blog entry. He says it will work with any language Vim understands.
What I'm wanting to know is if there's a way to do something like this in TextMate? Perhaps a command?
If you have a command/script that can convert Ruby code to HTML, then it's not a problem to have this run on your selected text and show the result as text, html, or copy to the clipboard.
But I am assuming that the person is actually using a VIM feature that does the actual conversion (using VIM's own syntax files) -- currently this is not possible with TextMate (I'll add it as a feature request though).
On Feb 5, 2005, at 20:05, Allan Odgaard wrote:
On Feb 5, 2005, at 0:43, Chris Ruzin wrote:
[...] Vim will then split the window with the original in the upper half and the colorized HTML version in the bottom half. He then copies what he needs out of that and pastes it into his blog entry. He says it will work with any language Vim understands.
What I'm wanting to know is if there's a way to do something like this in TextMate? Perhaps a command?
If you have a command/script that can convert Ruby code to HTML, then it's not a problem to have this run on your selected text and show the result as text, html, or copy to the clipboard.
Just for the record, there are some utilities out there that can help:
GNU Source-highlight http://www.gnu.org/software/src-highlite/
Beautifier http://www.beautifier.org/
Perltidy http://perltidy.sourceforge.net/
-- fxn
On Feb 5, 2005, at 20:46, Xavier Noria wrote:
Just for the record, there are some utilities out there that can help: GNU Source-highlight http://www.gnu.org/software/src-highlite/
Ah cool, and this is in Darwin Ports, so I did (Terminal.app): % sudo port install source-highlight
And after a minute of installing then I made this command (in TextMate): expand -3 | source-highlight -scpp -fhtml
With input: Selected text and output: Show as HTML (beta 5 has the “Open as new document”).
Since the OP wanted line numbers, I also did a small awk script to insert these, only tested with C++ output though: expand -3 | source-highlight -scpp -fhtml \ | awk '
/^<pre><tt>/ { sub("<pre><tt>", "<pre><tt> 1: "); print $0; next; }
/^</tt></pre>/ { print $0; next; }
{ printf("%3d: %s\n", ++line + 1, $0); } '
Remember that you may need to specify full path for source-highlight. And 'expand -3' is to convert tabs into spaces.
On Feb 5, 2005, at 21:25, Allan Odgaard wrote:
Remember that you may need to specify full path for source-highlight. And 'expand -3' is to convert tabs into spaces.
And if you have it working to your satisfaction, you probably want to change output to place on clipboard and bind a key equivalent to the command.
Allan Odgaard wrote:
On Feb 5, 2005, at 20:46, Xavier Noria wrote:
Just for the record, there are some utilities out there that can help: GNU Source-highlight http://www.gnu.org/software/src-highlite/
Since the OP wanted line numbers, I also did a small awk script to insert these, only tested with C++ output though: expand -3 | source-highlight -scpp -fhtml \ | awk '
/^<pre><tt>/ { sub("<pre><tt>", "<pre><tt> 1: "); print $0; next; }
/^</tt></pre>/ { print $0; next; }
{ printf("%3d: %s\n", ++line + 1, $0); } '
I can get the source-highlight to work perfectly. I changed the output to xhtml (you know, standards and all) and it looks great! However... whenever I try to pipe it through awk, I just get a single blank line as output. I copied and pasted your example above. Any ideas as to why it would do that? I know NOTHING about awk.
Also, is there a way to include the compiled source-highlight files inside of the TextMate application bundle in the future so this could be a built-in feature? And one other thing. ("No more!", they cry.) Right now, you have to staticly type in what the source is. Can TM have a current file extension variable (or something like that) so we can make this a single command that will work with any files source-highlight supports? So the line would instead be something like:
expand -3 | source-highlight -s$TM_EXT -fxhtml
Chris
On Feb 6, 2005, at 0:43, Chris Ruzin wrote:
Since the OP wanted line numbers, I also did a small awk script to insert these [...]
I can get the source-highlight to work perfectly. I changed the output to xhtml (you know, standards and all) and it looks great! However... whenever I try to pipe it through awk, I just get a single blank line as output. I copied and pasted your example above. Any ideas as to why it would do that?
No ideas, here's my command in its own bundle (which reminds me that bundle items should be drag-able to/from Mail for these types of situations ;) ):
I know NOTHING about awk.
Awk is great for processing text, it takes a 'program' which is basically a list of “regex { action }” where action is executed if regex matches the line (it applies all rules to all lines of the source.
In action you have the variables $1-n to access specific columns. Try “man awk”. You can also leave out the regex to have the action executed for all lines and there are also commands to skip the next actions etc. etc. Basically awk is an entire programming language, but with text processing in mind.
Also, is there a way to include the compiled source-highlight files inside of the TextMate application bundle in the future so this could be a built-in feature?
There is, and you can reach the path of the bundle to which the command belong with $TM_BUNDLE_PATH, so basically just copy it to a bin directory of your bundle and call it as $TM_BUNDLE_PATH/bin/source-highlight -- though I don't think I should include this with TextMate (because it's then a binary distribution of a GNU source and I don't want to argue over whether or not TextMate is derived work of source-highlight etc.), but I probably should look into that system people are requesting about having a Quicksilver-inspired system where people can 'look for more bundles'.
Right now, you have to staticly type in what the source is. Can TM have a current file extension variable (or something like that)
You can get the extension from the TM_FILEPATH like this:
TM_EXT=`echo $TM_FILEPATH|tr . '\n'|tail -1`
But you'd need to map it to the proper argument, e.g. rb -> ruby etc. Here's an example of how you could do that (inline):
TM_LANG=`grep ^$TM_EXT <<EOF |cut -d: -f2 cc:cpp mm:cpp h:cpp rb:ruby EOF`
So you'd just add mappings to the list.
There also is the TM_MODE variable which I introduced with the mode-dependent-lookup. This would be better to use for this, but the reason I don't mention it is, that the scope system will soon see the light of day, and that kind of makes this mode-stuff redundant.
So for future compatibility, I'd suggest not relying on TM_MODE. and in the future the command could be changed to use the scope stuff. But I guess for most other languages than C++, there are only one or two extensions in use, so the list above will be manageable.
I also just noticed that the piping through awk isn't necessary with source-highlight. Just add "-n" to the command and it will automatically output it with line numbers. Even better, if you add "--line-number-ref",
Doh! Well, it gave me a chance to talk about the often overlooked awk! ;)
Allan Odgaard wrote:
There is, and you can reach the path of the bundle to which the command belong with $TM_BUNDLE_PATH, so basically just copy it to a bin directory of your bundle and call it as $TM_BUNDLE_PATH/bin/source-highlight -- though I don't think I should include this with TextMate (because it's then a binary distribution of a GNU source and I don't want to argue over whether or not TextMate is derived work of source-highlight etc.), but I probably should look into that system people are requesting about having a Quicksilver-inspired system where people can 'look for more bundles'.
I've now started putting together a bundle for this, but whenever I use the $TM_BUNDLE_PATH, I get the following error:
/bin/bash: line 1: /Users/chris/Library/Application: No such file or directory
It looks like the $TM_BUNDLE_PATH isn't dealing with spaces correctly. If I manually add the path into the command, then it works.
Chris
At 2:34 PM -0600 2/6/05, Chris Ruzin wrote:
It looks like the $TM_BUNDLE_PATH isn't dealing with spaces correctly. If I manually add the path into the command, then it works.
If this is in a shell script try "$TM_BUNDLE_PATH". The quotes should fix things 99% of the time.
best, Eric
Eric Hsu wrote:
At 2:34 PM -0600 2/6/05, Chris Ruzin wrote:
It looks like the $TM_BUNDLE_PATH isn't dealing with spaces correctly. If I manually add the path into the command, then it works.
If this is in a shell script try "$TM_BUNDLE_PATH". The quotes should fix things 99% of the time.
This worked! Now to get the rest of the bundle going...
Allan Odgaard wrote:
On Feb 5, 2005, at 20:46, Xavier Noria wrote:
Just for the record, there are some utilities out there that can help: GNU Source-highlight http://www.gnu.org/software/src-highlite/
Since the OP wanted line numbers, I also did a small awk script to insert these, only tested with C++ output though: expand -3 | source-highlight -scpp -fhtml \ | awk '
/^<pre><tt>/ { sub("<pre><tt>", "<pre><tt> 1: "); print $0; next; }
/^</tt></pre>/ { print $0; next; }
{ printf("%3d: %s\n", ++line + 1, $0); } '
I also just noticed that the piping through awk isn't necessary with source-highlight. Just add "-n" to the command and it will automatically output it with line numbers. Even better, if you add "--line-number-ref", it will number all output lines and generate an anchor that can be referred to from another document! That could come in VERY handy.
Chris
I have now put together a bundle that will enable you to export selected text as either HTML or XHTML. It uses the source-highlight utility that Xavier pointed out. It's not foolproof though. A single quote in a commented line in a Perl document screwed up the result in one test, but there's nothing I can do about that.
This is my first attempt at a TM bundle, so go easy on me. Try it out and let me know if it works. I've tested it with C, C++, Perl, PHP and Ruby files and except for the above mentioned mistake, everything worked.
Thanks to Allan, Eric and Xavier for their help.
You can get it here:
www.chrisruzin.net/sh.tmbundle.tgz
- Chris
On Feb 6, 2005, at 23:23, Chris Ruzin wrote:
Xavier pointed out. It's not foolproof though. A single quote in a commented line in a Perl document screwed up the result in one test, but there's nothing I can do about that.
Yeah, Perltidy is the most robust Perl HTML colorizer AFAIK, the others I've tried are easily screwed up.
-- fxn