javascript to build index, accumulating character offset
HI script & index wonder-workers
I have a script that I run on single document of 160 or so pages from a list of terms to index.
It does the job but tends to misplace index markers progressively further offset from the real location of the real target word.
The direction of offset changes depending on if I increment up or down my list of txets returned from the story.search.
Using CS2, there are footnotes.
The offset is not exactly=number of footnotes encountered so far, but does broadly scale with about 60% of it (I've not checked this in detail) with 1000 marks in file some later pages have about a 50char offset from the word s in my list.
Very few footnotes havewords in my index, although 1 or 2 do I'm OK to just catch them.
I have tried reinvoking story.search to create a new list of texts after each (or 1 in 5) pageReferences.add
in case the insert of the index marker creates the offset - but this just makes it run slower and not fix the problem.
Most index refs come up correct but there will inevitably be some that drop from page x to page x-1
Any ideas where I should look?
Is the var myStory getting a copy or pointer to the changing story??
for (var s = 0 ; s < doc.stories.length ; s++) {
var myStory = doc.stories.item(s);
if (myStory.length > 800) { // avoid small story-objects (notes, headers etc)
toindex = myStory.search(myUpper); //Index entry with initial CAP in this story text
if (toindex.length > 0) // found the text at least once...
{
newtopic = doc.indexes[0].topics.add(myUpper);
r=0;
for( i = toindex.length-1; i > -1; i-- ) // this way marks DRIFT after word
// for( i = 0; i < toindex.length; i++ ) //marks DRIFT ahead of word
{
r++;
try
{
newtopic.pageReferences.add( toindex, PageReferenceType.currentPage );
if (r==5) {
toindex = doc.stories.item(s).search(myUpper); //costs time, helps ? reset DRIFT every 5th cycle
r=0;
}
}
catch(_)
{
// many of these will be footnote locations...
alert ("Index problem >" +myUpper+"<item "+i+ " may be in a footnote");
}