14

12/10

HTML Syntax Highlighting

13:30 by rleahy. Filed under: Random,Technology
Tags: , ,

I’ve been magically using syntax highlighting in some of my blog posts.

No, I don’t accomplish this by having no life and doing it by hand.  There are some nice tools available that’ll allow you to get HTML syntax-highlighted code snippets.

The first of these is very straightforward. PHP has a built in syntax highlighter, and, if enabled, will automatically spit out HTML syntax-highlighted PHP code when you visit a *.phps file.

To enable this behaviour—and it’s not recommended on production servers since sensitive data can hide in source code—you have to add a handler mapping in IIS for “*.phps” files, and map it to php-cgi.exe with the command line option -s.

Once you’ve enabled it, rename the *.php file that you want the syntax-highlighted source for to a *.phps file, visit it in a web browser, use “View Source“, copy and paste.

For C#/XML, I use this handy tool (also in “Links” in the sidebar).  You do need to embed his stylesheet in the <head> of your page, but he provides the source to do that (make sure you download and re-host his *.css file).  If you’re using WordPress, you’re going to need to find the header.php file for your theme, and stick it in there.