Formatting Code in InDesign with GREP Styles

You don't have to be a geek to love the power of GREP styles.

If you’ve seen issue 32 of InDesign Magazine, you might have noticed that I am a fan of IDML. I am excited by its potential, and geek out on how everything in an InDesign document can be represented in XML that you can actually read and edit. I’ve been working on some IDML documentation, including a collection of simple documents showing certain page elements and their corresponding IDML.

IDML for a placed image

To make my guide, I used to just open the IDML in Oxygen and take a screenshot of the code and paste that into InDesign. But this doesn’t work well for long pieces of code. So I switched to copy and pasting the XML into InDesign text frame. Just one problem: I could only paste without formatting, and it’s hard to find stuff in a sea of black 12 pt Monaco. If I turned on InDesign’s Clipboard Handling preference to paste all information (including the code colors), I got nothing. The Paste command just didn’t work. Even tried copy and pasting through an intermediate application, and that didn’t work the way I wanted.

But every obstacle is also an opportunity, right? This was a perfect time to whip up some GREP styles. IDML (or any well-formed XML) is a predictable, structured string of text. Elements always reside within tags, tags always reside within brackets, attributes are always followed by an equals sign, and then a value in quotes.

 

And the party always starts with a declaration, stating this is XML.

By matching these patterns with GREP styles, I can create any kind of look for my code and apply it with a single click. I chose to imitate the default look of XML in Oxygen, since I’m familiar with that: elements in black, tags in blue, attributes in orange, quoted values in brown, and the declaration in purple. So I first created character styles to apply all the colors except for black (which will be applied by the paragraph style).

Now I’ll admit, I don’t live and breathe GREP every day, so I expect one of the GREPmeisters out there can offer improvements on the following code, but it works for what I wanted to accomplish.

I have one paragraph style that sets the foundation: 12 pt Monaco in black.

Then to that paragraph style I added a GREP style to color the tags blue. It starts with (?x) to let me space out the parts of the expression for better readability.

It matches an opening bracket, an optional forward slash, one or more word characters, an optional colon, one or more word characters or an optional forward slash and a closing bracket. This fellow matches all varieties of opening and closing tags, including ones with a namespace prefix (indicated by the colon).

Next comes a GREP style to color the quoted values brown. It matches everything in quotes, plus the quotes themselves.

Next I added a GREP style to color the attributes orange. It matches one or more word characters, an optional colon, one or more word characters again, and an equals sign.

Finally,  a GREP style to make the declaration purple.

It matches an opening bracket followed by a question mark and any characters that come after, up to the end of the line. By placing this style at the bottom of the list, I ensure it’s applied after the others, so it overrides them. Here they are all together.

Now I can copy IDML (or other XML), paste it into my document, unformatted…

…and apply my IDML Code paragraph style to the whole thing. Bingo. It’s all styled instantly.

Truly a thing of geek beauty. I unstyle it and restyle it just to watch the magic over again. Yes, I am easily amused.

Now I’m guessing you probably don’t need to style XML like this, but I love how it illustrates the power of GREP styles. Try ’em out. Then when someone makes a template with dozens of paragraph styles, you can say, ‘Tsk, tsk, I would’ve just used one.’

Bookmark
Please login to bookmark Close

This article was last modified on March 15, 2023

Comments (7)

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Uwe Laubender

    @Mike: Give “Springy” a try. You can open zip files (of course IDML as well). Select the xml-file you want to edit, rightclick and choose “Edit with”. Now select the text editor of your choice. Very handy.
    https://www.springyarchiver.com/

  2. Mike Rankin

    Thanks, Fritz-

    BBEdit rocks. It’s cool for editing single IDML files, like snippets, but I haven’t found a way to get it to work with an IDML package (without unzipping it). One of the best things about Oxygen is that it can open IDML files directly and give you access to their innards. Then again, I don’t have the most recent version of BBEdit, maybe it can open IDML files.

    I also love BBEdit’s most humble motto:
    “BBEdit: It Doesn’t Suck.” Wish all software could live up to that standard.

    You may be right about the color coding. If the app can do Find on GREP, it makes sense it could apply formatting that way.

  3. James Fritz

    Great example Mike,

    Your stylized code reminds me of open I open XML or HTML in BBedit.

    Have you ever tired using BBedit to edit your IDML?

    I wonder if BBedit uses some crazy form of GREP to color all of the tags.

  4. Mike Rankin

    Gabriel and Gfx-Dzine-
    Thanks for the comments :)

    Laurent-
    Thanks for the link. There is so much good Creative Suite content en français, I have to revisit my high school French lessons so I can really read it. Maybe I should switch my CS4 to the French version to get started. Happily, GREP is our common language.

    Also, I fixed the supersized art. The old version of WordPress would automatically scale art to fit in the column. I think this new version requires us to manually scale it (which obviously I didn’t till now).

  5. Laurent Tournier

    Hello,

    You can see an other example for JavaScript here (Marc Autret’s blog) :
    https://marcautret.free.fr/geek/ikono/grepcolor/index.php
    Best
    Laurent

  6. Great example Mike!

    I myself just started to get my head around GREP & GREP-styles in Indesign (with the help of David amongst others ;) ) for a book I’m formatting and the results I was able to get untill now are already really satisfying.

    A few things that are on my wishlist for CS5 is the flexebility of the GREP Find/Change implemented in GREP Paragraph Styles – more specific that you can select text and ‘reorder’ it ($1 etc.)

    And a less technical way to build the queries would be nice, also to make it more accessable.

    And ofcourse a better way to make text disappear.

    Thanks again for the article,
    Mike.

    __
    P.S. I viewed the article on both Firefox and Internet Explorer and it’s really wide – the advertisement banners at the top right are overlapping.

  7. Gabriel Powell

    Great tip Mike! This example definitely demonstrates the power of GREP styles. And I find it practical for the kind of work I do as well.