Here is an UltraEdit script for this definitely not easy to code replacement of comma separated continuous references by a from/to reference range in active file.
Code: Select all
function updateReferences(nFirst, nLast)
{
// On first reference of a continuous reference series remove
// all trailing whitespaces and append an EN DASH character.
g_asReferences[nFirst] = g_asReferences[nFirstIndex].replace(/\s+$/,"");
g_asReferences[nFirst] += "–"
// On all references of the continuous reference series between first
// and last reference remove preceding and trailing whitespaces, the
// reference number in square brackets and change the element with a
// start and end tag to an empty element.
for (var nModIndex = nFirst+1; nModIndex < nLast; nModIndex++)
{
g_asReferences[nModIndex] = g_asReferences[nModIndex].replace(/^[^<]*(<.+)>\[\d+\]<\/xref>\s*/,"$1/>");
}
// On last reference of a continuous reference series
// remove the preceding whitespaces.
g_asReferences[nLast] = g_asReferences[nLast].replace(/^[^<]*(<.+)$/,"$1");
}
if (UltraEdit.document.length > 0) // Is any file opened?
{
// Define environment for this script.
UltraEdit.insertMode();
UltraEdit.columnModeOff();
// Move caret to top of the active file.
UltraEdit.activeDocument.top();
// A case-sensitive Perl regular expression is used to find 3 or more
// comma separated references executed in a loop up to end of file.
UltraEdit.perlReOn();
UltraEdit.activeDocument.findReplace.mode=0;
UltraEdit.activeDocument.findReplace.matchCase=true;
UltraEdit.activeDocument.findReplace.matchWord=false;
UltraEdit.activeDocument.findReplace.regExp=true;
UltraEdit.activeDocument.findReplace.searchDown=true;
if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
{
UltraEdit.activeDocument.findReplace.searchInColumn=false;
}
while(UltraEdit.activeDocument.findReplace.find('(?:<xref type="bibr" rid="ref\\d+">\\[\\d+\\]</xref>\\s*,\\s*){2,}<xref type="bibr" rid="ref\\d+">\\[\\d+\\]</xref>'))
{
// Copy the references into a globally referenced list of strings.
var g_asReferences = UltraEdit.activeDocument.selection.split(',');
var nNumber = -1;
var nRefIndex = 1;
var nFirstIndex = 0;
var bModified = false;
// Get first reference number from string inside the square
// brackets converted to an integer number using decimal system.
var nRefNumber = parseInt(g_asReferences[0].replace(/^.*\[(\d+)\].*$/,"$1"),10);
// Now process in a loop all references from list except the first one.
while (nRefIndex < g_asReferences.length)
{
// Get the reference number as integer of current refernce.
nNumber = parseInt(g_asReferences[nRefIndex].replace(/^.*\[(\d+)\].*$/,"$1"),10);
// Is this reference number NOT +1 of previous reference number?
if (nNumber != (nRefNumber+1))
{
// Are there at least 3 references with each reference number
// being +1 of the reference number of previous reference.
if ((nRefIndex - nFirstIndex) > 2)
{
bModified = true;
// The previous reference is the last one of the reference
// series and so 1 must be subtracted from current index.
// And there is one more reference and so a comma must be
// appended on last reference of the series.
updateReferences(nFirstIndex,nRefIndex-1);
}
// Append a comma to previous reference string.
g_asReferences[nRefIndex-1] += ",";
// The current reference is the first one of a perhaps new series.
nFirstIndex = nRefIndex;
nRefNumber = nNumber;
}
else
{
nRefNumber++; // Reference series continues.
}
nRefIndex++; // Process next reference.
}
// Belongs the last reference to a continuous reference series.
if ((nRefIndex - nFirstIndex) > 2)
{
bModified = true;
// The previous reference is the last one of the reference
// series and so 1 must be subtracted from current index.
updateReferences(nFirstIndex,nRefIndex-1);
}
else if (nFirstIndex != (nRefIndex-1))
{
// Append a comma to last but one reference string if just
// the last two references have continuous reference numbers.
g_asReferences[nRefIndex-2] += ",";
}
// Was anything in list of reference modified at all?
if (bModified)
{
UltraEdit.activeDocument.write(g_asReferences.join(""));
}
}
UltraEdit.activeDocument.top();
}
The input file used for testing this script was:
Code: Select all
I am jones, <xref type="bibr" rid="ref2">[2]</xref>, <xref type="bibr" rid="ref3">[3]</xref>, <xref type="bibr" rid="ref4">[4]</xref>, <xref type="bibr" rid="ref5">[5]</xref>, <xref type="bibr" rid="ref12">[12]</xref>, <xref type="bibr" rid="ref14">[14]</xref> and my age is <xref type="bibr" rid="ref5">[5]</xref>, <xref type="bibr" rid="ref6">[6]</xref>. How are you <xref type="bibr" rid="ref8">[8]</xref>, <xref type="bibr" rid="ref9">[9]</xref>, <xref type="bibr" rid="ref10">[10]</xref>, <xref type="bibr" rid="ref61">[61]</xref>, <xref type="bibr" rid="ref62">[62]</xref>?
I am jones, <xref type="bibr" rid="ref2">[2]</xref>, <xref type="bibr" rid="ref3">[3]</xref>, <xref type="bibr" rid="ref4">[4]</xref>, <xref type="bibr" rid="ref5">[5]</xref> and my age is <xref type="bibr" rid="ref5">[5]</xref>, <xref type="bibr" rid="ref6">[6]</xref>, <xref type="bibr" rid="ref7">[7]</xref>. How are you <xref type="bibr" rid="ref15">[5]</xref>, <xref type="bibr" rid="ref63">[63]</xref>?
The output file produced by the script was:
Code: Select all
I am jones, <xref type="bibr" rid="ref2">[2]</xref>–<xref type="bibr" rid="ref3"/><xref type="bibr" rid="ref4"/><xref type="bibr" rid="ref5">[5]</xref>, <xref type="bibr" rid="ref12">[12]</xref>, <xref type="bibr" rid="ref14">[14]</xref> and my age is <xref type="bibr" rid="ref5">[5]</xref>, <xref type="bibr" rid="ref6">[6]</xref>. How are you <xref type="bibr" rid="ref8">[8]</xref>–<xref type="bibr" rid="ref9"/><xref type="bibr" rid="ref10">[10]</xref>, <xref type="bibr" rid="ref61">[61]</xref>, <xref type="bibr" rid="ref62">[62]</xref>?
I am jones, <xref type="bibr" rid="ref2">[2]</xref>–<xref type="bibr" rid="ref3"/><xref type="bibr" rid="ref4"/><xref type="bibr" rid="ref5">[5]</xref> and my age is <xref type="bibr" rid="ref5">[5]</xref>–<xref type="bibr" rid="ref6"/><xref type="bibr" rid="ref7">[7]</xref>. How are you <xref type="bibr" rid="ref15">[5]</xref>, <xref type="bibr" rid="ref63">[63]</xref>?