Mike
Mike

Reputation: 24974

How can I print syntax-highlighted Ruby code?

I use TextMate for my Ruby editing, but when printing files, the code isn't syntax highlighted. Are there any good programs for printing out well-formatted color-highlighted Ruby code?

Upvotes: 3

Views: 1511

Answers (6)

boulder_ruby
boulder_ruby

Reputation: 39705

Apparently it is difficult to communicate to the printer that a *.txt-equivalent file needs to be colorized but it obviously can be done. TextMate's creator has communicated simply that he doesn't think it is important enough of a feature to have which basically means it is extremely difficult to implement.

Aptana can do this thing no problem but it takes forever to load.

Enter vim. vim is probably already installed on your computer.

vim filename.m
:syntax on
:hardcopy

Again, there won't be any prompt for which printer to use, so make sure your system's default printer is set correctly.

If you had to use the :syntax on command to get vim to colorize your code:

To set vim by default to do syntax-based coloring:

nano ~/.vimrc
syntax on

save & quit

Upvotes: 1

August
August

Reputation: 423

Thanks, boulder_ruby

A couple of additional points at the end of 2016 (2 years 10 months later):

If you're on Windows, you cannot count on VIM being already installed. Membership in Stack Overflow does improve the odds, but probably not to 90%). However VIM for Windows is easy to get. www.vim.org/download.php

Macs come with MacVim by default.

On a Mac, using MacVim, following your advice, I carefully set the default printer to one where I could empty the queue before wasting paper on testing.

Then I used the :hardcopy VIM command. The printed version went straight to PDF and opened in Preview. It never showed up in the default printer queue.

Also, edavey above points to http://biztos.blogspot.com/2008/06/printing-with-textmate-vim-and-friends.html which contains a link to the TextMate Help page on printing. The TextMate help says:

There are plans to improve the printing capabilities, but until then, there is also a command in the Source bundle (View Source as PDF) which produces a PDF from the current source using enscript and has syntax highlighting enabled for supported languages.

That suggests (I haven't figured it out the "Source bundle" yet) that you can go straight to PDF, with syntax coloring, from TextMate. If I figure that out, I'll add something here (eventually).

I hope this helps.

Upvotes: 0

Alex Wayne
Alex Wayne

Reputation: 187074

The syntax gem is pretty awesome.

require 'hpricot'               
require 'syntax/convertors/html'
def filter_content(content)
    h = Hpricot(content)
    c = Syntax::Convertors::HTML.for_syntax "ruby"
    h.search('//pre[@class="ruby"]') do |e|
        e.inner_html = c.convert(e.inner_text,false)
    end
    h.to_s
end

Edit: Oh, you are referring to printing... Well you could do convert it to HTML and then print it from your browser.

Upvotes: 1

edavey
edavey

Reputation: 394

A good solution which I use is to print from TextMate via vim which gives you a syntax-highlighted and line-numbered result (or however you choose to configure it.) In addition to vim it requires ps2pdf but these are easy to install with macports etc.

The only limitation is that the file needs to be saved first.

See this page, which shows how to set up the macro as a TextMate 'command'.

Upvotes: 2

kejadlen
kejadlen

Reputation: 1599

I'd probably use Ultraviolet to create an HTML file that's syntax-highlighted to print out...

Upvotes: 1

Mike Woodhouse
Mike Woodhouse

Reputation: 52326

I don't know if it's relevant in Mac-world, but the SciTE editor that's bundled with the one-click installer for Windows prints nicely in colour on our HP Laserjet. I haven't tried printing from any of the various IDEs - I ought to try it.

Upvotes: 1

Related Questions