Jun 03, 2021 #4 2021-06-03T13:19+00:00
I find the element name
f ap ge instead of
f pa ge curious, but the script below searches for the values of element
f ap ge .
There was nothing written about a requirement for supporting multiple occurrences of the elements
f ap ge and
lpage and
book-page-count with attribute
count in active file in initial script coding request. There was also nothing written about a requirement for supporting invalid
Roman numerals . Roman numerals must be written with the upper case letters IVXLCDM.
The not commented script below should work for the task if there is not one more not mentioned requirement in which case you have to modify the script code by yourself as I don´t like it writing code more than once because of an incomplete list of requirements for a coding task.
Code: Select all
// The function below is from https://stackoverflow.com/a/17534350/3074564 converted from Java to JavaScript.
function ToArabic(sRomanNumeral)
{
if (!sRomanNumeral.length) return 0;
var sFirstChar = sRomanNumeral.charAt(0);
var sTwoChars = sRomanNumeral.substr(0,2);
if (sFirstChar == "M") return 1000 + ToArabic(sRomanNumeral.substr(1));
if (sTwoChars == "CM") return 900 + ToArabic(sRomanNumeral.substr(2));
if (sFirstChar == "D") return 500 + ToArabic(sRomanNumeral.substr(1));
if (sTwoChars == "CD") return 400 + ToArabic(sRomanNumeral.substr(2));
if (sFirstChar == "C") return 100 + ToArabic(sRomanNumeral.substr(1));
if (sTwoChars == "XC") return 90 + ToArabic(sRomanNumeral.substr(2));
if (sFirstChar == "L") return 50 + ToArabic(sRomanNumeral.substr(1));
if (sTwoChars == "XL") return 40 + ToArabic(sRomanNumeral.substr(2));
if (sFirstChar == "X") return 10 + ToArabic(sRomanNumeral.substr(1));
if (sTwoChars == "IX") return 9 + ToArabic(sRomanNumeral.substr(2));
if (sFirstChar == "V") return 5 + ToArabic(sRomanNumeral.substr(1));
if (sTwoChars == "IV") return 4 + ToArabic(sRomanNumeral.substr(2));
if (sFirstChar == "I") return 1 + ToArabic(sRomanNumeral.substr(1));
return 10000;
}
if (UltraEdit.document.length > 0)
{
UltraEdit.insertMode();
if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
UltraEdit.activeDocument.top();
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;
}
UltraEdit.activeDocument.findReplace.preserveCase=false;
UltraEdit.activeDocument.findReplace.replaceAll=false;
UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
var nFirstPageNumber;
var nLastPageNumber;
var nPageRangeCount;
var sElementValue;
while (UltraEdit.activeDocument.findReplace.find("<fapge>\\K[^<]+"))
{
sElementValue = UltraEdit.activeDocument.selection;
if (sElementValue.search(/^[0-9]+$/) == 0)
{
nFirstPageNumber = parseInt(sElementValue,10);
}
else
{
sElementValue = sElementValue.toUpperCase();
if (sElementValue.search(/^[CDILMVX]+$/) == 0)
{
nFirstPageNumber = ToArabic(sElementValue);
}
else nFirstPageNumber = -1;
}
if (UltraEdit.activeDocument.findReplace.find("<lpage>\\K[^<]*"))
{
sElementValue = UltraEdit.activeDocument.selection;
if (sElementValue.search(/^[0-9]+$/) == 0)
{
nLastPageNumber = parseInt(sElementValue,10);
}
else
{
sElementValue = sElementValue.toUpperCase();
if (sElementValue.search(/^[CDILMVX]+$/) == 0)
{
nLastPageNumber = ToArabic(sElementValue);
}
else nFirstPageNumber = -1;
}
}
else nFirstPageNumber = -1;
if ((nFirstPageNumber >= 0) && (nLastPageNumber >= nFirstPageNumber))
{
nPageRangeCount = nLastPageNumber - nFirstPageNumber + 1;
sElementValue = nPageRangeCount.toString(10);
UltraEdit.activeDocument.findReplace.replace('<book-page-count count="\\K[^"]*',sElementValue);
}
}
UltraEdit.activeDocument.top();
}
Best regards from an UC/UE/UES for Windows user from Austria