Archive

Posts Tagged ‘syntax highlighting’

syntax highlight your code and paste in Word

April 14, 2016 1 comment

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:

sum

Thanks to Peter F. for the link.

HTML: add syntax-highlight to textarea

September 26, 2013 1 comment

Problem
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.

Solution
I tried EditArea, and it suits my needs. See this SO page for more alternatives.

EditArea

EditArea is a free javascript editor for source code. This editor is designed to edit souce code files in a textarea. The main goal is to allow text formatting, search and replace and real-time syntax highlight (for not too heavy text).” (source)

For Python support, I had to add these lines to the HTML source:

<script language="javascript" type="text/javascript" src="../editarea/edit_area/edit_area_full.js"></script>
<script language="javascript" type="text/javascript">
editAreaLoader.init({
    id : "src_input"       // textarea id
    ,syntax: "css"          // syntax to be uses for highgliting
    ,start_highlight: true      // to display with highlight mode on start-up
    ,syntax: "python"
    ,replace_tab_by_spaces: 4
});
</script>

Related work

  • Ace (it seems a more professional solution)

Syntax-highlighted “cat” in command line

May 29, 2013 1 comment

Problem
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?

Solution
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.

Example:

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.

Tip from here. Discussion @reddit here.

Links

Insert syntax highlighted source in Powerpoint

August 7, 2012 1 comment

Problem
You want to have syntax highlighted source in Powerpoint.

Solution
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:

py2rtf hello.py

The script is available here.

Update (20131030)
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 -S (here: 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']

Then include style.css in index.html:

<head>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>

Add syntax highlighting to “less”

August 10, 2011 Leave a comment

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
Categories: bash, vim Tags: , ,