开发者

Printing code with syntax highlighting? [closed]

开发者 https://www.devze.com 2022-12-10 03:28 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citati开发者_Go百科ons.

Closed 6 days ago.

Improve this question

I have occasion to need to print code (Horrors!!! ;) ), and i was wondering what editor or tool would i use to print that code out with proper formatting and syntax highlighting? If it's important, it will be c# code but ideally the tool will work for as many languages as possible. Can Notepad++ or something handle this?


You can use Vim! It's probably installed already if you're on modern Linux/MacOS and an easy install if not.

:syntax will turn syntax highlighting on and :hardcopy will print it. There's syntax highlighting definitions for many languages out there. The default look is usually optimised for screen display, but you can fix that.

Simply open the file on command line with vim <filename>, type :syntax on<ENTER>, then :hardcopy<ENTER> to print it. Quit Vim with :q!<ENTER>.

There's also the :TOhtml command which will open the current selection as HTML in a new Vim window. Capture the entire document with :%y<ENTER> followed by :TOhtml<ENTER> to open it.


Yes, Notepad++ can certainly print code with syntax highlighting.

Colour printing would obviously be preferable, but on the occasions when I've printed in black and white, the subtle differences in colour [rendered as shades of grey, of course] can be difficult to distinguish.

However, I think a little customisation of the colour schemes should make this less of a problem.


Use vim. Its the easiest method to do it in my experience by far, that is, once you know how.

Vim comes pre-installed on Macs, btw. And I know how you special people like colors, so I'm going to make this impatientbusinessman-proof for the benefit of all.

1.) open file

vim filename.m

2.) enable syntax coloration (mine did not have enabled by default)

:syntax on

3.) print

:hardcopy

Vim will choose your system's default printer without asking you so make sure you set that up first.

4.) exit the program (this is actually not a given)

:q

OR

Use Aptana. Aptana is pretty sweet, its free and it has great ftp functionality.


http://pygments.org/ is one option. It supports a ton of languages, and since it's written as a python library, you can script the conversion process however you want.


Visual Studio will, and allows you have a completely separate configuration for printing.


I recently compared the 2 solutions already mentioned : vim & pygments. They both give great results, but there is how you can practically use them quickly:

  • pygments does not provide direct export to PDF. Hence, the simplest solution I found was to export to HTML and then use wkhtmltopdf. You can combine both operations using the following bash script:
src2pdf () {
    local noext="${1%.*}"
    pygmentize -O full -o "$noext.html" "$1"
    # enabling line wrapping in <pre> blocks
    perl -i -wpe '/<style.*>$/&&($_.="pre{white-space:pre-wrap;}\n")' "$noext.html"
    wkhtmltopdf "$noext.html" "$noext.pdf"
    rm "$noext.html"
}
  • for vim, it's as simple as this: TERM=xterm-256color vim '+hardcopy >out.ps' +q code.src I found out that the $TERM environment variable can affect the output colors, so I prefer to set it explicitly. And finally, you may need to tweak your .vimrc a little:
set printfont=:h9  
set printoptions=number:y,left:5pc  


The tool called enscript is very much the tool for doing this. It is very powerful, is not tied to an editor nor a language and you can create PDF's with syntax highlighting.

The documentation pretty much says it all.

enscript man page


Under unix you might want to try a2ps. It is flexible and produces nice results.


I while ago I created a household python script that wraps pygments into a small console utility. It works with any language supported by pygments.

Also if you happen to use eclipse, you could simply copy the selected text in the editor and then paste it in a RTF-aware editor like MS Word - it will preserve all the colors, fonts and formatting.


If you have problems with Visual Studio 2012 concerning the highlighted printing an handeling the described problem:

  • Download and install this Power Tool which implements the color printing, besides some other features and bug fixes. Works for me!


Solution For Bash Shell

  1. Add this line to ~/.bashrc if you are using UBUNTU
    or, to ~/.bash_profile if you are using MAC
    If that file does not exists, create it.

    alias lprc='vim -me -c ":syntax on" -c ":hardcopy" -c ":q"'

  2. source ~/.bashrc or source ~/.bash_profile

  3. To print colored hello.py just do this:
    lprc hello.py instead of lpr hello.py


Use ConTEXT programming editor (which is free). I am using it for both generating .pdf-s with syntax highlighted source code and printing to paper.

There are many ConTEXT syntax highlihting definitions to download and you can make your own highligher file which will BTW be highlighted using the ConTEXT Highlighter Files highlighter definition.


I do it downloading js and css files from

https://prismjs.com/

There are so many 5-7 options to select the theme and language highlighter. Once you select a theme and download the tiny js/css files the next thing you need to do is rename the code file to html, and call the css/js files. Open the html in a browser and print it. Done!


You can also use this in case you've to print with bnw syntax highlighting https://github.com/SGI-CAPP-AT2/code-highlight-n-print

0

精彩评论

暂无评论...
验证码 换一张
取 消