Styling Code Snippets with GREP Styles
I received an urgent plea for help from a designer in Romania who’d tracked me down through a web forum where I’ve posted answers to a great many InDesign questions. He was on a very tight deadline, and he’d suddenly hit a problem he couldn’t crack.
He had a 90-page book with many examples of XML code that needed to be styled in a particular way. While his GREP Style expression was working for most of his code examples, it wasn’t working for items that spanned multiple lines.
He wanted to know why a multi-line paragraph wouldn’t accept the GREP Style, and (of course) how to fix it. He’d been digging though every GREP resource he could find, but wasn’t finding an answer.
The GREP expression he was using was this: <\/?(.|\s|\S)*?>
. It shouldn’t have been problematic, yet it “wasn’t working.”
The clue is that this is a code snippet. Code snippets come from programmers’ text editors like BBEdit, or Notepad++, which generally don’t care whether a line break is a newline character (a forced line break) or an true end-of-paragraph “hard return.” Copy that text into InDesign, and you’re likely to have hard returns at the end of every line, which become paragraph marks in InDesign.
Sure enough:
To a programmer, those five lines might seem like “one paragraph,” but as far as InDesign is concerned there are five paragraphs, and none of them contain a match for the GREP expression, which is looking for angle brackets at each end. So, naturally, the GREP Style never shows up.
Changing the first four hard returns to forced line breaks turns that section of code into something InDesign sees as a single paragraph. At that point, InDesign’s GREP Style waves its magic wand, and all is as it should be.
The lesson from all this (you knew there’d be a moral somewhere) is, it’s not always the complicated thing that is the problem, which is what we all tend to assume; sometimes it’s something really simple. When something just doesn’t seem to work, it’s often worthwhile to step back, take a deep breath, and see if something else—something you’ve been taking for granted—might be the droid you’re actually looking for.
Hi Alan
That’s a good tip – and very obvious thing to change – but wouldn’t like to search through tonnes of code looking for paragraph returns.
However, I haven’t tested this, this might work in selecting the paragraphs of code and styling them automatically.
(?s)
(?s)\
Gah the forum is stripping my code lol
(?s)
Altogether.
Sigh.
(?s)
less than sign
full stop
plus sign
question mark
greater than sign
It could also be the opposite… put the all the text red, but use a Style Grep to turn the data black when between opening and closing .
This grep code will do that if you don’t have nested … No need to digg and change return to force return.
)\K.+?(?=<\/\1)
Grrr! See the code here…
https://dl.dropboxusercontent.com/u/1482399/code.jpg
The grep expression isn’t what I’d have used, and like you I’d have styled the text, not the code. (As David’s often remarked, when you find yourself applying a character style to a whole paragraph, it should be a paragraph style.)
In this case, the implied question was “Why doesn’t my grep expression work?”, but it wasn’t the grep that was the problem.
True – the expression did work, exactly how it was written. GREP has to be exactly written to work as intended.
When it doesn’t work as intended you have two options. Alter the content it’s finding. Or alter the code to fix the issues.
I always go for altering the code, but sometimes there is only such much grep you can alter before you figure it’s never going to work, then it’s plan B – alter the text it’s finding.
Hello
For this problem, you can use this regex :
You don’t need to change hard returns with forced lines breaks turns.
\X is undocumented.
Where is my regex ?
\
So, my regex is :
Open bracket character \X + ? close bracket character
without space
Paul, so the \X+? is the equivalent of (?s).+?
Correct?
How do you call the \X
in french : caractère combinatoire.
That is mean : \X Matches a single Unicode grapheme, whether encoded as a single code point or multiple code points using combining marks. A grapheme most closely resembles the everyday concept of a “character”. \X matches à encoded as U+0061 U+0300, à encoded as U+00E0, ©, etc
It can be equivalent of (?s)
Merci Paul! ;-)
Avec plaisir Jean-Claude. Mais moi, c’est Laurent ;-)
Oh, merde! Je suis désolé Laurent! :-(
Y’a vraiment pas de mal.
Hi Jean,
Could you help me figure out how to use the \K in Indesign GREP. I tried yours code and didn’t succeed. However, I checked the Tournier regex and it is working perfect.