6 Replies Latest reply on Sep 22, 2010 11:24 AM by Pet Re

    Am I fooled by GREP or ...

    Pet Re

      I have documents with ancient Corel Ventura markers in plain text for index topics. By script I convert the markers to topics, crossreferences etc. The script might look familiair: it's a modified version of index_rebuild by Peter Kahrel. Although the script seems to work quite well, I discovered some odd behaviour.

      Two different Ventura markers not seperated by any character are individually recognized if I GREP them manually. After running the script only the first marker has been converted to an Indesign index topic (and can be found in the Index panel).

      The Ventura markers look like this: <$I[]Topic;sub topic>

      The situation discribed above looks like this: <$I[]Windows 7;64-bits><$I[]Windows 7;32-bits>

       

      Could anyone please explain why the second marker isn't converted?

       

      var myDoc = app.activeDocument;
      if (myDoc.indexes.length == 0) myDoc.indexes.add();
      var index = myDoc.indexes[0];
      app.findGrepPreferences = NothingEnum.nothing;
      app.changeGrepPreferences = NothingEnum.nothing;
      app.findChangeGrepOptions.includeFootnotes = false;
      app.findChangeGrepOptions.includeHiddenLayers = false;
      app.findChangeGrepOptions.includeLockedLayersForFind = false;
      app.findChangeGrepOptions.includeLockedStoriesForFind = false;
      app.findChangeGrepOptions.includeMasterPages = false;
      app.findGrepPreferences.findWhat = '<[$][AIS]?\\[\\]([-.\\d\\w\\s]*);*([-.\\d\\w\\s]*)>';
      var entries = myDoc.findGrep();

      for (i=entries.length-1;i>=0;i--){
          var entry = entries[i];
          var indexType = 0;
          var str = entry.contents;
          if (str.indexOf('<$I') !=-1){
              indexType = 0; // gewone indexingang
          }
          else if (str.indexOf('<$A') !=-1){
              indexType = 1; // See also
          }
          else if (str.indexOf('<$S') !=-1){
              indexType = 2; // See
          }

        switch (indexType){
          case 0:

          var array = str.substring(str.indexOf(']')+1, str.indexOf('>')).split(';');
          var new_top = index.topics.add (array[0]); 
          for (var k = 1; k < array.length; k++)
              new_top = new_top.topics.add (array[k]);
              new_top.pageReferences.add(entry, PageReferenceType.currentPage);
          break;
          case 1:
          var array = str.substring(str.indexOf(']')+1, str.indexOf('>')).split(';');
          var new_top = index.topics.add (array[0]); 
          var new_ref = index.topics.add (array[1]);
          var new_crossRef = new_top.crossReferences.add(new_ref, CrossReferenceType.seeAlso);
          break;
          case 2:
          var array = str.substring(str.indexOf(']')+1, str.indexOf('>')).split(';');
          var new_top = index.topics.add (array[0]); 
          var new_ref = index.topics.add (array[1]);
          var new_crossRef = new_top.crossReferences.add(new_ref, CrossReferenceType.see);
          break;

        }   
      }

      app.findGrepPreferences.findWhat = '<[$][AIS]?\\[\\]([-.\\d\\w\\s]*);*([-.\\d\\w\\s]*)>';
      app.changeGrepPreferences.changeTo = '';
      myDoc.changeGrep ();


        • 1. Re: Am I fooled by GREP or ...
          Peter Kahrel Adobe Community Professional & MVP

          Your GREP expression is not the problem, nor how the script collects the matches. The problem is a bizarre bug (which I didn't know about). You can see what happens if you comment out the last three lines in the script so that your codes stay in the text:

           

          looks like this: |<$I[]Windows 7;64-bits|><$I[]Windows 7;32-bits>

           

          Here the vertical bars show where the script places the index markers. These markers are always placed at the first insertion point of the referenced object, except when that insertion point is preceded by <>()= and a handful of other symbols. That's why the first index marker is placed correctly right before <, the second one is placed one insertion point too early, before >< instead of after it.

           

          The solution seems to be to place the leading < in a lookbehind:

           

          app.findGrepPreferences.findWhat = '(?=<)[$][AIS]?\\[\\]([-.\\d\\w\\s]*);*([-.\\d\\w\\s]*)>';

           

          Peter

          1 person found this helpful
          • 2. Re: Am I fooled by GREP or ...
            Pet Re Level 1

            Hi Peter, thanks for your suggestion.

            I could reproduce your findings and see the incorrect placement of the second marker.

            However, adjusting the GREP expression with the (?=<) part results in no findings at all. But you have pointed me in the right direction, so I'll be looking for another way of GREPping the code. Or perhaps you might have another suggestion. GREP moves by mysterious ways, at least to me.

            • 3. Re: Am I fooled by GREP or ...
              Peter Kahrel Adobe Community Professional & MVP

              Ah, sorry -- that should be (?<=<)

               

              Peter

              1 person found this helpful
              • 4. Re: Am I fooled by GREP or ...
                Pet Re Level 1

                No, I think it should work the other way around (I'll be working on my knowledge of GREP). Now the index markers are being placed just behind the opening <, so inside the code that is to be deleted. That would result in deleting all the markers as well.

                (What actually happens when I change both the GREP findWhat expressions in '(?<=<)[$][AIS]?\\[\\]([-.\\d\\w\\s]*);*([-.\\d\\w\\s]*)>', is that everything after the < and the index marker is being deleted, but the < and the marker are not.)

                • 5. Re: Am I fooled by GREP or ...
                  Peter Kahrel Adobe Community Professional & MVP

                  I see. The cop-out is to replace the <$[ ] code in your text with e.g. £$[ ] (so £ instead of <) and change your GREP accordingly: findGrepPreferences.findWhat = '£$[AIS]? etc. Not exactly a satisfactory solution, but it works well.

                   

                  Peter

                  • 6. Re: Am I fooled by GREP or ...
                    Pet Re Level 1

                    Thanks a lot, Peter.

                    And the rebuild_index script was very helpful for understanding the principle of creating index topics.

                     

                    Peter