Is there a tool (similar to the markup here on StackOverflow) that transforms code (based on syntax and keywords) into markup’ed code? In other words, take a blot of code and add the appropriate tags and styles to have it show all pretty in colour (and indented maybe)?
Specifically, I’d like something that’s language specific (or language aware) for at least C# and SQL. It can be a plugin to WordPress or just a formatter that I could post my code into and then copy paste to WordPress (or elsewhere. Portability is nice).
Vim is a remarkably good tool for this. You don’t have to use it as an editor (although every serous developer should consider doing just that–but I digress), you can use it as a pure syntax-aware code-to-html processor.
For example take this Python3 script (syntax coloring is by StackOverflow in this listing):
Using Vim color scheme Light this method produces:
Note that each syntax element is given a color in the CSS style section, which you can tweak in the html itself if you don’t want to fiddle with Vim color schemes.
Here’s what you’ll see in your browser. I’ve clipped this as an image to avoid color changes from StackOverflow:
Here’s how to do it:
~/.vim/colors
directory, or for Windows in%USERPROPFILE%/vimfiles/colors
.:e <file path>
where<file path>
is the location of the source file. Vim gui’s will also provide menu commands to open a file, you might prefer to do it that way so you can browse you file system.:color <name>
where<name>
is the color scheme you saved in step 2, without the.vim
extension. For example, I use:color synic
to load the color scheme in~/.vim/colors/synic.vim
. (In Vim, the ‘:’ puts you into “command mode”. When you enter a command and press<return>
you are back in what is called “normal mode”.):TOhtml
. A new window will open up and you’ll see the html markup. Be sure to use the right capitalization when executing _Vim_commands.:w <file path>
Optionally, if you want line numbers to display in the generated HTML, use the command
:set number
before:TOhtml
.If you don’t see the syntax coloring show up after you set your color scheme, try the command
:syntax on
. If you’re doing syntax coloring for a language or text format that vim doesn’t recognize you can probably find syntax files for that language on the web. Details of how to install syntax files are beyond what I want to write about here, but your favorite search engine has plenty on the topic. Try a search such asvim <mylang> syntax
. To see what language Vim thinks you have, use the command “:set ft?”.If, like me, you prefer to use the terminal form of
Vim
, there’s a little more to consider. You can not create colors that the terminal doesn’t support. Most terminal apps provide either 2, 16 or 256 colors. Get Vim to output in 256 colors (if it doesn’t already) using the instructions here. Not all color schemes work well on terminals out of the box, but instructions on this page help substantially, although they are written with a more experienced Vim user in mind. It’s a good idea to run the Vim tutorial (:help tutorial
) if you have to do more than I outline in my steps above.I, personally, like this script: http://softwaremaniacs.org/soft/highlight/en/