Sunday 10 October 2010

HTML Syntax highlighting in InDesign


Whilst preparing some lessons I found myself doing something very silly trying to do syntax highlighting by hand. Rather than taking myself out back and giving myself a good kicking I spent the time getting to fiddle with the GREP style feature of inDesign’s paragraph styles.

Using GREP Style

Grep Style

From within the paragraph style that you want to use, choose GREP style from the options on the left hand side. When you do that you’ll be greeted with the following UI


From here it’s just a case adding some regular expressions to match the syntax and apply your selected character style to those matches.

I’ve used three regular expressions. The first matches tags with attributes, the second plain tags and the third matches comments.

This matches tags with attributes

 < !?/?\w+\s+[^>]*> 

This one matches plain tags


This one matches comments

 < !-- .+ --> 

Below you can see the styles in action, the tags are now automatically using the html syntax character style that I set up.

Grep Styles in action

Whilst regular expressions aren’t a mystery to me, neither are they my most familiar subject, however the ones above seem to get the job done. If you have a better way of doing them, please feel free to post a comment.