May 25 2010 • 2:10 AM

Using GREP Styles to Format HTML Code in InDesign

[This is an article written by Claudio Marconato from Italy and reprinted with his permission. We've made only minor edits. Claudio presented this at the Ignite InDesign event at the Print and ePublshing Conference in Seattle a couple of weeks ago. We were impressed, so he graciously wrote it up and sent it to us.]

GREP Styles is one of the most powerful features added in InDesign CS4. Using GREP Styles, you can set a GREP expression to apply a character style to a range of characters in a very smart and dynamic way.

Recently I’ve discovered the possibility of using multiple GREP expressions to select the a range of characters and apply a sort of “cascading character styles”. You can’t apply multiple styles to the same range of characters using the standard character styles panel, but with GREP style you can, so you can use character styles in a way that is very similar to the HTML and CSS way.

With this technique you can set one character style that contains only the definition of the text color (e.g. red) and another one that contains only the definition of the text weight (e.g. bold). After that you can apply those styles combining the definitions.

Let me show you an example.

Here is a very simple piece of HTML code:

<p>This is a paragraph of plain text</p>
<p>In this paragraph there’s <strong>some bold text with some <em>italic text</em> inside</strong></p>
<p>Using GREP styles you can apply multiple styles at the same characters, for example you can apply a <font color=”red”>red color and also <strong>bold text</strong> using two separate styles with only one attribute each</font> as you can do with HTML and CSS</p>
<p>Enjoy</p>

If you save this text in a text file and save it with an .html extension, you can open it in Safari or Firefox and look how those web browsers render the HTML code, it’s something like this screenshot:

Of course, InDesign does not read HTML. But you can simulate this same rendering with InDesign by applying some character styles with GREP, leaving the original text untouched.

Step 1: copy and paste from a text editor to InDesign with all the HTML code. Here I have made a paragraph style called “plain text” to simulate a text editor:

Step 2: I’ve created a paragraph style called “syntax highlight” to simulate an HTML editor like Dreamweaver that highlights the HTML tags, you can do this with a single GREP expression that select all the tags, and you can add as many tags as you need:
(<\/*p>)|(<\/*strong>)|(<\/*em>)|(<\/*font( color="red")*>)

This is the result:

Step 3: Now you need to hide the tags and apply some styles to simulate a web browser. I have used the same GREP expression as before to apply a character style with a size of 0.1 pt (the minimum I can set with InDesign), a horizontal and vertical scale of 1%, and a color of “none”. With these settings the text of the tags become invisible!

Step 4: Finally, I can add some other GREP expression for formatting the text:

(<strong>).+(</strong>) for bold (that is, I apply a bold character style to that code in grep styles)

(<em>).+(</em>) for italic

(<font color=”red”>).+(</font>) for red color

(<em>).+(</em>)(?=.+</strong>) for bold italic

After creating each of these grep styles inside my paragraph styles, here is the result:

As you can see, I’ve defined one style for red text and one style for bold text. Then I applied red and bold separately, and as a result I’ve obtained a text that is red and also bold, two properties of the text that are defined in two different styles.

There are many instances in InDesign where you want to apply multiple characters styles to the same text. I hope you find this technique useful!

14 Responses discussing this post. Add yours below.

  1. May 25th, 2010 • 4:48 am • Link

    This looks really good, but all I can think of is You Can’t Parse HTML With Regular Expressions :)

  2. May 25th, 2010 • 5:43 am • Link

    That is funny.
    Yes, it is true that this grep styles regex “parser” is very simple and of course won’t work for many html pages. But the basic idea is interesting and can be reworked for specific purposes.

  3. May 25th, 2010 • 5:47 am • Link

    Verrry gooood !

  4. May 25th, 2010 • 7:15 am • Link

    Good example again…

    But your last screen capture is not what peoples will get when they apply the grep in a styles because it will not remove the various , in the text file. To remove them they will need be run (and modified) as a series of Grep Queries.

  5. May 25th, 2010 • 7:17 am • Link

    One small issue with the regular expressions though.

    Imagine in “this is some text” everything have a character style applied.. “.+” can be a bit greedy.

    .*?” will work better because “.*?” is not greedy.

    Also the parentheses are not needed :)

    Anyways, I love how it is possible to add multiple character styles to text using grep styles! Just have to take note in what order styles are added to the text depending on the order the GREP styles are arranged.

  6. May 25th, 2010 • 8:50 am • Link

    Ok, now I feel stupid to have jump to the end of the article and not having seen your “trick” to hide the coding in steps 3.

  7. Claudio Marconato
    May 25th, 2010 • 1:55 pm • Link

    Thank you David for the opportunity, and thank to all of you for your comments!

  8. May 25th, 2010 • 3:04 pm • Link

    Hello,

    Just for the fun, have a look on indigrep.com to have an example of “Grep Styles and HTML dynamic”
    http://bit.ly/9sMi9z
    Best

  9. May 25th, 2010 • 3:19 pm • Link

    @Tournier: That is great! Thanks for the link and making a movie of the result.

  10. May 25th, 2010 • 11:46 pm • Link

    @Tournier: very cool

    @Thomas Silkjær: thank you for digging on GREP syntax

    @Skilldrick: sorry, probably I had to write “XHTML”

  11. November 22nd, 2010 • 4:49 pm • Link

    This is great for applying character styles with GREP. My immediate need though would be to apply Paragraph styles with GREP. Is there a way to do so?

  12. November 22nd, 2010 • 5:23 pm • Link

    @Evan: You can open the find/change dialog box, switch to the GREP tab, type the grep code in Find What, then leave Change to blank. Then use the Change to Formatting field (in the More Options section) to apply a paragraph style. That will find the grep and then apply the style.

  13. Martin
    April 13th, 2011 • 6:36 pm • Link

    @Thomas — thanks for the non-greedy *? tweak…

  14. June 30th, 2011 • 6:16 am • Link

    Wow. thanks for this tutorial, this really rocks! We have DB output with formating in HTML and formating based on this HTML was done in 3 minutes. Thanks!

Subscribe to the Discussion

Get the ongoing discussion surrounding "Using GREP Styles to Format HTML Code in InDesign" delivered to you. Click here to subscribe via RSS.

Leave a Reply

You can use limited HTML tags, such as <em></em> for emphasis/italics and <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .

InDesignSecrets reserves the right to edit and/or remove posts and comments.