How to generate total page range count with a script file?

How to generate total page range count with a script file?

6
NewbieNewbie
6

    May 31, 2021#1

    Please suggest how to generate total page range count with a script file?

    Sample file:

    Code: Select all

    <book-part id="B9789004493414_s007" book-part-type="chapter">
    <book-part-meta>
    <book-part-id book-part-id-type="doi">10.1163/9789004493414_007</book-part-id>
    <title-group>
    <label>Chapter 5.</label>
    <title>The interaction of local adverb, verb and sentence particle</title>
    </title-group>
    <contrib-group>
    <contrib contrib-type="author">
    <name name-style="western"><surname>Tjerkstra</surname><given-names>F.A.</given-names></name>
    </contrib>
    </contrib-group>
    <fpage>1</fpage>
    <lpage>20</lpage>
    <self-uri content-type="pdf" xlink:href="9789004493414_webready_content_s007.pdf"/>
    <counts>
    <book-page-count count="@@"/>
    </counts>
    </book-part-meta>
    </book-part>
    
    For example:

    The values for the tags <fpage> and <lpage> are:

    Code: Select all

    <fpage>2</fpage>
    <lpage>20</lpage>
    Total page count: <book-page-count count="19"/>

    62
    Advanced UserAdvanced User
    62

      May 31, 2021#2

      Hi Debjit,

      Here is the script for the total page count:

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
          // Define environment for this script.
          UltraEdit.insertMode();
          if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
          else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      
          UltraEdit.perlReOn();
          UltraEdit.activeDocument.findReplace.mode=0;
          UltraEdit.activeDocument.findReplace.matchCase=false;
          UltraEdit.activeDocument.findReplace.matchWord=false;
          UltraEdit.activeDocument.findReplace.regExp=true;
          UltraEdit.activeDocument.findReplace.searchDown=true;
          UltraEdit.activeDocument.findReplace.searchInColumn=false;
      
          UltraEdit.activeDocument.top();
          while(UltraEdit.activeDocument.findReplace.find("(?<=<fpage>)([^<>\r\n]*?)(?=</fpage>)"))
          {
              var fPage = UltraEdit.activeDocument.selection.replace("(?<=<fpage>)([^<>\r\n]*?)(?=</fpage>)","$1");
              var fNumber = parseInt(fPage,10);
              fPage = fNumber.toString(10);
          }
          while(UltraEdit.activeDocument.findReplace.find("(?<=<lpage>)([^<>\r\n]*?)(?=</lpage>)"))
          {
              var lPage = UltraEdit.activeDocument.selection.replace("(?<=<lpage>)([^<>\r\n]*?)(?=</lpage>)","$1");
              var lNumber = parseInt(lPage,10);
              tPage = lNumber.toString(10) - fPage + 1;
          }
          UltraEdit.activeDocument.top();
          UltraEdit.activeDocument.findReplace.find("(<book-page-count count=\")@@");
          var sText = UltraEdit.activeDocument.selection.replace("@@",""+tPage);
          UltraEdit.activeDocument.write(sText);
      }
      

      6
      NewbieNewbie
      6

        Jun 01, 2021#3

        The script does not work for multiple tags and Roman page numbers.

        For example:

        Code: Select all

        <fapge>ii</fapge>
        <lpage>iv</lpage>
        <book-page-count count="3"/>
        <fapge>1</fapge>
        <lpage>5</lpage>
        <book-page-count count="5"/>
        <fapge>6</fapge>
        <lpage>10</lpage>
        <book-page-count count="5"/>
        

        6,680583
        Grand MasterGrand Master
        6,680583

          Jun 03, 2021#4

          I find the element name fapge instead of fpage curious, but the script below searches for the values of element fapge.

          There was nothing written about a requirement for supporting multiple occurrences of the elements fapge 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