Printing code with syntax highlighting?

后端 未结 13 2183
轻奢々
轻奢々 2021-02-01 16:00

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

相关标签:
13条回答
  • 2021-02-01 16:33

    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.

    0 讨论(0)
  • 2021-02-01 16:34

    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.

    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2021-02-01 16:41

    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.

    0 讨论(0)
  • 2021-02-01 16:41

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

    0 讨论(0)
  • 2021-02-01 16:43

    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  
    
    0 讨论(0)
提交回复
热议问题