At http://planetb.ca/syntax-highlight-word you can find a nice syntax highlighter. It supports Python, HTML, Java, and several other languages. The rendered output can be copy-pasted to a Word document and it’ll look the same and remains editable in Word. Pretty cool.
It’s also useful if you want to make a screenshot of the syntax highlighted code. Here is an example:
Thanks to Peter F. for the link.
You have an HTML form with a textarea where you want to accept some source code. You want to turn this simple textarea into a fancy input area that adds syntax highlighting.
For Python support, I had to add these lines to the HTML source:
- Ace (it seems a more professional solution)
You have a source code whose content you want to display on the stdout. The natural way to do that is the “
cat” command but it’s monochrome. How to get a syntax-highlighted output?
Use pygments. If you install it (“
sudo apt-get install python-pygments“), you will have a command called “
pygmentize” that you can invoke from the command line.
pygmentize -f terminal256 -O style=native -g color_me.py
Tip: put an alias on it.
alias pcat="pygmentize -f terminal256 -O style=native -g"
There are different styles available, see “
pygmentize -L” for a list.
- Using Pygments with less (thanks rajbot)
You want to have syntax highlighted source in Powerpoint.
First, create an RTF file from the source:
pygmentize -f rtf -o hello.rtf hello.py
Open the RTF file in Word, select the source, then insert it in Powerpoint.
I made a basic script to automate the conversion. Usage:
The script is available here.
What if you want to insert colorized code in HTML?
$ pygmentize -f html -o index.html color_me.py $ pygmentize -f html -S default >style.css
The parameter of
default) is the name of the color scheme. Available color schemes:
>>> from pygments.styles import get_all_styles >>> list(get_all_styles()) ['monokai', 'manni', 'rrt', 'perldoc', 'borland', 'colorful', 'default', 'murphy', 'vs', 'trac', 'tango', 'fruity', 'autumn', 'bw', 'emacs', 'vim', 'pastie', 'friendly', 'native']
<head> <link rel="stylesheet" type="text/css" href="style.css"> </head>
This entry is based on this post: Redirecting wget to STDOUT – now with Syntax Highlighting.
Less is a program similar to more, but it allows backward movement in the file as well as forward movement. Example:
cat big_text_file.txt | less
If you have vim installed, it comes with a script called
less.sh that acts as a replacement of
less providing syntax highlighting. Here is how to use it:
$ locate less.sh /usr/share/vim/vim73/macros/less.sh $ sudo ln -s /usr/share/vim/vim73/macros/less.sh /usr/bin/vless $ curl http://www.python.org | vless