Creating a book index

Creating a book index

6
NewbieNewbie
6

    Oct 15, 2011#1

    Commonly when working on ebook creation, I encounter a tedious time-consuming task, that of creating the index with page numbers for various subjects within the book. The html code I work with is such:

    Dimensions of Grand Cañon. <a href="../Text/Section0001.xhtml#Page_87">87</a>, <a href="../Text/Section0001.xhtml#Page_142">142</a>, <a href="../Text/Section0001.xhtml#Page_144">144</a>

    What I need to do is changing the Section0001.xhtml to the proper epub section number according to a table such as

    Code: Select all

    page no.     section no.
    9-25          0008
    26-50         0009
    51-60         0010
    etc.
    Is it possible to construct a script that will consult the page no./section no. in conjunction with regex to quickly do what I've been doing manually? If so, I will greatly appreciate help (I've never used a script in UE before.)
    Bob

    6,677581
    Grand MasterGrand Master
    6,677581

      Oct 16, 2011#2

      Here is a quickly written UltraEdit script for your task based on your data. It is not the best solution, but it should work. You have to open as first file the file with the index to update, as second file the file with the page numbers and section information, and as third file open this script and execute it with Scripting - Run Active Script.

      Please note that the second file should not contain anything else than the page numbers separated with a hyphen character and the section numbers, separated from the page numbers with spaces, tabs or a hyphen character. For more details read the comments.

      Code: Select all

      // At least two files must be opened when running this script.
      // The first (most left) file must be the file to update.
      // The second file must be the file with the section and page data.
      if (UltraEdit.document.length >= 2) {
      
         UltraEdit.insertMode();
         UltraEdit.columnModeOff();
      
         // Load all lines from second file into an array of strings each string
         // containing a number and discard the selection by moving caret to top
         // of second file. But before splitting the content replace all sequences
         // of white-spaces by hyphen character.
         UltraEdit.document[1].selectAll();
         var sSectionInfo = UltraEdit.document[1].selection.replace(/\s+/g,"-");
         var asData = sSectionInfo.split('-');
         UltraEdit.document[1].top();
         // If last line of second file has also a line termination, remove the
         // last string from the array which is in this case an empty string.
         if (asData[asData.length-1] == "") asData.pop();
      
         // Convert start and end page of every section to an integer stored in
         // separate arrays and copy the section number as string to a third array.
         var nDataCount = asData.length / 3;
         var anStartPage = new Array(nDataCount);
         var anEndPage   = new Array(nDataCount);
         var asSection   = new Array(nDataCount);
      
         for (var nIndex = 0; nIndex < nDataCount; nIndex++)
         {
            anStartPage[nIndex] = parseInt(asData[nIndex*3],10);
            anEndPage[nIndex]   = parseInt(asData[nIndex*3+1],10);
            asSection[nIndex]   = asData[nIndex*3+2];
         }
      
         // Now set caret to top of first file which should be updated.
         UltraEdit.document[0].top();
         // Define the UltraEdit regular expression engine as engine to use
         // for finding the strings to update with a regular expression search.
         UltraEdit.ueReOn();
         // Define the find parameters used for the regular expression find below.
         UltraEdit.document[0].findReplace.mode=0;
         UltraEdit.document[0].findReplace.matchCase=false;
         UltraEdit.document[0].findReplace.matchWord=false;
         UltraEdit.document[0].findReplace.regExp=true;
         UltraEdit.document[0].findReplace.searchDown=true;
         if (typeof(UltraEdit.document[0].findReplace.searchInColumn) == "boolean") {
            UltraEdit.document[0].findReplace.searchInColumn=false;
         }
      
         // Run the following loop from top to bottom until no section reference found anymore.
         while (UltraEdit.document[0].findReplace.find("Section[0-9]+.xhtml#Page_[0-9]+"))
         {
            // Store found string temporarily in a string variable.
            var sReference = UltraEdit.document[0].selection;
            // Remove from found string everything left to page number.
            var sPageNum = sReference.replace(/Section.*#Page_/,"");
            // Convert the page number string to an integer.
            var nPageNum = parseInt(sPageNum,10);
      
            // Find the section for this page.
            var bSectionFound = false;
            for (var nIndex = 0; nIndex < nDataCount; nIndex++)
            {
               if ((nPageNum >= anStartPage[nIndex]) && (nPageNum <= anEndPage[nIndex]))
               {
                  // Section found, build new reference string by replacing section number.
                  var sNewRef = sReference.replace(/Section[0-9]+/,"Section"+asSection[nIndex]);
                  // Overwrite still selected reference by updated section reference.
                  UltraEdit.document[0].write(sNewRef);
                  bSectionFound = true;
                  break;
               }
            }
            if (!bSectionFound)  // Output an error information for pages not found in section data.
            {
               if (UltraEdit.outputWindow.visible == false) UltraEdit.outputWindow.showWindow(true);
               UltraEdit.outputWindow.write("Can't find section for: " + sReference);
            }
         }
         UltraEdit.document[0].top();
         UltraEdit.document[0].setActive();
      }

      6
      NewbieNewbie
      6

        Oct 16, 2011#3

        Mofi,

        Thanks for the code/instructions. I'll be working on it for a while. Being a rank beginner in UltraEdit and a non-starter in Javascript.

        The script is working great, Mofi!

        Bob