robert
robert

Reputation: 23

Does an Indesign syntax highlighter exist?

Does anybody know of a non-manual method of highlighting syntax when pasted in InDesign?

I'm trying to show code of a project in an InDesign documentation but don't want to have to manually highlight the code, and preferably add numbered rows too. Is there a plugin to achieve this?

This is the style in which I'd like to format the code. The style I would like to achieve

Cheers

Upvotes: 0

Views: 1166

Answers (2)

Ronald Huereca
Ronald Huereca

Reputation: 266

If had luck pasting syntax highlighted code into a Rich Text editor like Libre Office and then pasting it into Indesign. Just make sure whatever font your syntax highlighted code is in is also in InDesign because you'll get font missing errors when you pre-flight the book.

Upvotes: 0

Alex
Alex

Reputation: 179

Not sure if you worked out an answer to this, but there's no magic button that will solve your problem.

However, InDesign does have a facility in each Paragraph style called GREP that can do what you're looking for.

enter image description here

This lets you write 'regex' or 'regular expressions' that are just rules for what to apply a given character style to. Yes, they look about as meaningful as Harry Potter incantations at first glance, but 2 or 3 simple regexes will get you a long way.

For instance:

    (\<|<)!--\s*.*?\s*--(\>|>)

Will target HTML comments only.

    (?<=").*?(?=")

This will target anything wrapped in straight double quotes.

    (?<=\().*?(?=\))

This will target any text inside parentheses ().

There's an '@' symbol button in that GREP style next to where you type the regex - that gives you a drop-down menu that is almost like a 'Regex Wizard'. Try that too.

When you've got a regex that works, create a new character style for the text color and select it in the 'Apply Style' input.

Regexr.com has a tool that is good for testing this stuff. Paste your code sample in the bottom panel and your line of regex in the top. The bits that it targets will turn blue.

There is a searchable community panel on the left where people have probably already written expressions like the one you need.

I'm working out a JavaScript highlighter at the moment. It's a shame there's no communal 'Indesign style sharing library'.

Best of luck.

Upvotes: 1

Related Questions