Copy link to clipboard
Copied
Hi,
I have a simple html input I get from a database. It looks something like this
-----
Lorem ipsum dolor sit amet, <b>consectetur</b> adipiscing elit. Praesent mattis enim felis, quis laoreet <i>libero</i> fringilla dictum.
-----
I would like to add this text to a textFrame (I know how to do that bit) but I would like to apply the character style 'bold_text', 'italic_text' to the part of the text which is inclosed within tags <b> and <i> and of course remove the tags. I know how to remove the tags as well it is just the apply style I am missing really : )
Is there an easy way of doing this ? How would you accomplish the above ?
Thank you for helping
Geza
Ah the point is you don't want them in your text. (Why not? They will be invisibly small and, having the color [None], will not export to PDF either. Oh well.)
In that case then don't use GREP styles. You still can use those same GREP codes, but you must apply the styles with a search-and-replace. After that, you can safely remove the HTML codes.
The GREP codes to use for applying are in Step 4 in that same post.
Copy link to clipboard
Copied
Grep styles could be used here. Or you could map tags to styles if you actually placed xml tagged content.
Up to you.
Copy link to clipboard
Copied
Or type the title of this question into Google and pick this from the very first page: https://indesignsecrets.com/using-grep-styles-to-format-html-code-in-indesign.php (which indeed uses GREP styles).
Copy link to clipboard
Copied
Thank you. Indeed I have missed that post and it is an interesting read but I was not quite after what it does. I would like to "remove" the html tags.
You could not use his solution to do that because once you have removed the html elements the GREP style will not apply.
Copy link to clipboard
Copied
Read on from "Step 3: Now you need to hide the tags ..."
Copy link to clipboard
Copied
I did I still would like to delete the tags and not hide with me ? : )
Copy link to clipboard
Copied
Ah the point is you don't want them in your text. (Why not? They will be invisibly small and, having the color [None], will not export to PDF either. Oh well.)
In that case then don't use GREP styles. You still can use those same GREP codes, but you must apply the styles with a search-and-replace. After that, you can safely remove the HTML codes.
The GREP codes to use for applying are in Step 4 in that same post.
Copy link to clipboard
Copied
It is not up to me its more to do with the document is going to be further edited by somebody else and I guess it becomes not too obvious why that style applies when you do not see the characters anymore.
They would have no idea what they have just deleted and it would be confusing to them how to re-apply the style.
I guess my question is now narrowed down to how "How do I find the text between two html tags via grep".
And hear is where you are so right : ))
The grep codes are indeed there for that and I have been searching google for an hour or so : ))
Thank you for pointing that out I thought they would only work with the grep styles why I thought that I have no idea ...
Once ready I post the code here.
Thank you,
Copy link to clipboard
Copied
Care to give me an example : ) ?
Copy link to clipboard
Copied
So the final solution for me so far:
function replaceHtmlTags(myDoc){
var tags = [
{ styleGrep: '(<b>).+(</b>)', removeGrep: '(<\/*b>)', characterStyle:'html_bold'},
{ styleGrep: '(<i>).+(</i>)', removeGrep: '(<\/*i>)', characterStyle:'html_italic'},
];
var i = tags.length;
while(i--){
var style = myDoc.characterStyles.itemByName(tags.characterStyle);
if(!style.isValid){
alert('The character style "'+key+'" was not found within the document. Make sure it is not grouped and it exists.');
return;
}
// style text between tags
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.findWhat = tags.styleGrep;
app.changeGrepPreferences.appliedCharacterStyle = style;
myDoc.changeGrep();
// remove tags
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.findWhat = tags.removeGrep;
app.changeGrepPreferences.changeTo = '';
myDoc.changeGrep();
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
}
}
replaceHtmlTags( app.activeDocument );