Matching Typefaces

Tutorial

Text is great for finding and replacing text, but it can also manipulate text based on style and that is what we will do for this example.

With this tutorial, we will focus on using conditionals to match text with specific styles that we can then work on. To not lose focus, we will break the issue into multiple tasks. These can later be combined.

We will focus on a common request seen on the internet, covering bold and italic text to the specified HTML tags.

Here is some text that is bold or italic or both. It can even be underlined or ~~strikethru~~.

That’s a lot of style action here, but each of these is straightforward enough.

To get started, create a new cleaner called “Text Styles to HTML Tags”

Part 1 - Bold and Italic

We will tackle the bolded text first. We need to match bold text and apply actions on that match. We do that with a Conditional.

Conditionals allow you to match a subset of the text and perform actions just on that subset. In this case, we want to use the conditional “If Font Matches”.

The “If Font Matches” conditionally allows us to match specific font families, typefaces (like bold, italic) and sizes.

Since we are initially looking for bold text, we check the “Typeface” option and “Bold”.

Conditionals can contain other actions (which operates on matched subset of text). To add embed actions to a conditional, drag the action on top of the conditional. This will add the action at the end of the list of embedded actions.

Alternatively, you can drag the action as a child (note the insertion marker). This is useful if you want to place an action somewhere other than at the end of the list of embedded actions.

The action we add is the “HTML Tag: Bold” which will surround the text with a <b> and </b> tag. And you could use “HTML Tag: Strong” if desired.

Let’s do bold first. We add the conditional, set the typeface option and bold option. This will now match any bold text in our sample.

We will tag it with the <b> tag. You could also choose <em> tag if you like. Find it in the actions (type in ‘bold’ in the filter) and drag ‘HTML Tag: Bold’ onto to conditional

Now we test it out against our sample:

OK, now we do the same thing for italicized text. We add an “If Font Matches”, check “Typeface” and “Italics” and embed the “HTML Tag: Italic” action. Again, if so desired, you could use “HTML Tag: Emphasized” action instead.

And as we test it out again, we see that both italic text and bold text are tagged.

While it may not matter if the text remains bold or italic, we might have other cleaners that don’t want those styles if they’ve already been processed. So we add an action at the end of the cleaner to remove the typeface. To do this, we use “Set Font”, check “Typeface” and then we check “Plain”.

“Plain” is a special option which will strip off the other typefaces options. We can again test it with our sample text.