Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
0

Convert simple html elements to indesign text with character styles

Community Beginner ,
Oct 22, 2017 Oct 22, 2017

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

TOPICS
Scripting
2.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 23, 2017 Oct 23, 2017

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.

Translate
People's Champ ,
Oct 22, 2017 Oct 22, 2017

Grep styles could be used here. Or you could map tags to styles if you actually placed xml tagged content.

Up to you.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 22, 2017 Oct 22, 2017

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).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 23, 2017 Oct 23, 2017

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 23, 2017 Oct 23, 2017

Read on from "Step 3: Now you need to hide the tags ..."

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 23, 2017 Oct 23, 2017

I did I still  would like to delete the tags and not hide with me ? : )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 23, 2017 Oct 23, 2017

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 23, 2017 Oct 23, 2017

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.

See those invisible characters might be deleted in which case the styling would disappear.

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,

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 23, 2017 Oct 23, 2017

Care to give me an example  : ) ?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 23, 2017 Oct 23, 2017
LATEST

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 );

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines