Printing code with syntax highlighting?

后端 未结 13 2583
轻奢々
轻奢々 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: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 
     blocks
        perl -i -wpe '/$/&&($_.="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  
    

提交回复
热议问题