1 Reply Latest reply on Mar 14, 2014 6:19 AM by LeftHead

    nested xml styles

    LeftHead

      Me again, but as everybody seems to be motivated, will try my extra question

       

      Do you have any advice or ressources to managed nested Xml Styles?

       

      For example:

       

      text in italic text in italic/bold text in bold

       

      If I'm tagging first italic than just tag bold, the result will be:

       

      <i>text in italic</i><b>text in italic/bold text in bold</b> (and it's of course not what I need!)

       

      So, looking for any ideas to:

       

      1. Finding a style (it's ok with findText)
      2. test if a xml tag is already apply on everything or a part of the findText result
      3. if not, apply the xml tag
      4. if yes, find where the 2 styles are both present and apply both and find where only the new one is apply and apply him only
      5. restart from 1 for any additional style
      6. everything not with 2000 code lines

       

      The aim is to get:

       

      <i>text in italic</i><i><b>text in italic/bold</b></i><b> text in bold</b>

       

      I know I will get mad but it's for a good cause

        • 1. Re: nested xml styles
          LeftHead Level 1

          Crazy life

           

          By combining and adapting different ideas from Uwe (.+ grep search) and Ariel (backToNoStyle function), I've found a quite easy solution. Will have to test it further to be sure it's steady but first tries are working.

           

          Here's the code for those who are interested. I just give the "Italic" part but copy/paste (or better loop it) the lines in bold to manage others properties (smallcaps, superscript...).

           

          The basic idea is to remove all formating on the tags markers that you are adding. Doing this, the findGrep results are cut into chunks that cannot have multiples nested styles.

           

           

          function caraToXml() {
              app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
              app.findChangeTextOptions.caseSensitive = false;
              app.findChangeTextOptions.includeFootnotes = app.findChangeGrepOptions.includeFootnotes = false;
              app.findChangeTextOptions.includeHiddenLayers = false;
              app.findChangeTextOptions.includeLockedLayersForFind = false;
              app.findChangeTextOptions.includeLockedStoriesForFind = false;
              app.findChangeTextOptions.includeMasterPages = false;
              app.findChangeTextOptions.wholeWord = false;
              // italic conversion
              // need to be developed for other styles
              // pay attention to nested xml tags problems
              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
              app.findGrepPreferences.findWhat = ".+";
              app.findGrepPreferences.fontStyle = "Italic";
              var myFindTexts = myDoc.findGrep();
              var myShift = 0;
              if (myFindTexts.length > 0) {
                  for (var j = 0; j < myFindTexts.length; j++) {
                      var myText = myFindTexts[j];
                      var myStartIndex = myText.characters[0].index;
                      var myEndIndex = myText.characters[-1].index;
                      myXmlElement = myXmlElement.xmlElements.add({markupTag:"i", xmlContent:myText});
                      myXmlElement = myXmlElement.parent;
                      backToNoStyle(myText.parent.characters[myStartIndex]);
                      backToNoStyle(myText.parent.characters[myEndIndex+ 2 + myShift]);
                      myShift+=2;
                  }
              }
          }

          //
          function backToNoStyle(xmlMarker) {
              xmlMarker.fontStyle = "Regular";
              xmlMarker.capitalization = Capitalization.NORMAL;
              xmlMarker.position = Position.NORMAL;
              xmlMarker.underline = false;
              xmlMarker.strikeThru = false;
          }