User to user discussion and support for UltraEdit, UEStudio, UltraCompare, and other IDM applications.

Help with writing and running scripts
2 posts Page 1 of 1
How to create auto IDs sequence of chapter-title, H1, H2, etc.?
ID structure like <chapter-title id="ch1">, <h1 id="h1_1">, <h2 id="h2_1">

Input

Code: Select all
<html>
<head>
<title>xxxxxxxxxxxxxx</title>
</head>
<body>
<p class="ch-title">Was sind Existenzanalyse und Logotherapie?</p>
<p class="h1">Die Wurzeln der Existenzanalyse und Logotherapie</p>
<p class="h2">Die Wurzeln der Existenzanalyse und Logotherapie</p>
</body>
</html>

Output file should be

Code: Select all
<html>
<head>
<title>xxxxxxxxxxxxxx</title>
</head>
<body>
<p class="ch-title" id="ch1">Was sind Existenzanalyse und Logotherapie?</p>
<p class="h1" id="h1_1">Die Wurzeln der Existenzanalyse und Logotherapie</p>
<p class="h2" id="h2_1">Die Wurzeln der Existenzanalyse und Logotherapie</p>
</body>
</html>

Apply for all "ch-title", "h1" and "h2".
Kindly help me.
I do not really understand why using a standard paragraph with a class h1 and h2 instead of using HTML element H1 and H2, but here is the commented UltraEdit script which adds identifiers or updates existing identifiers if not having already the right number.

Code: Select all
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 search string is
   // used to find and update the chapter and heading identifiers.
   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;
   }

   var sNumber;
   var nChapter = 0;
   var nHeading1 = 0;
   var nHeading2 = 0;

   // Find from top to bottom of file in a loop class ch-title or h1 or h2
   // with or without an id attribute as next attribute after class attribute.
   while(UltraEdit.activeDocument.findReplace.find('(?<=class=")(?:ch-title|h1|h2)"(?: id="[^"]+)?'))
   {
      // Get class ch or h1 or h2 from begin of found string.
      var sClass = UltraEdit.activeDocument.selection.substr(0,2);
      if (sClass == "ch")  // Is this a chapter?
      {
         // Increase chapter number and convert it to a string.
         nChapter++;
         sNumber = nChapter.toString(10);
         // Is an id attribute already present?
         if (UltraEdit.activeDocument.selection.length > 16)
         {
            // Has the chapter not the right number, overwrite it.
            if (UltraEdit.activeDocument.selection.substr(16) != sNumber)
            {
               UltraEdit.activeDocument.write('ch-title" id="ch' + sNumber);
            }
            else  // Chapter has already the right number, cancel selection.
            {
               UltraEdit.activeDocument.key("RIGHT ARROW");
            }
         }
         else     // Chapter has no identifier, add it.
         {
            UltraEdit.activeDocument.write('ch-title" id="ch' + sNumber + '"');
         }
      }
      else if (sClass == "h1")   // Is this a heading level 1?
      {
         // Increase heading level 1 number and convert it to a string.
         nHeading1++;
         sNumber = nHeading1.toString(10);
         // Is an id attribute already present?
         if (UltraEdit.activeDocument.selection.length > 11)
         {
            // Has the heading 1 not the right number, overwrite it.
            if (UltraEdit.activeDocument.selection.substr(11) != sNumber)
            {
               UltraEdit.activeDocument.write('h1" id="h1_' + sNumber);
            }
            else  // Heading 1 has already the right number, cancel selection.
            {
               UltraEdit.activeDocument.key("RIGHT ARROW");
            }
         }
         else     // Heading 1 has no identifier, add it.
         {
            UltraEdit.activeDocument.write('h1" id="h1_' + sNumber + '"');
         }
      }
      else  // This a heading level 2.
      {
         // Increase heading level 2 number and convert it to a string.
         nHeading2++;
         sNumber = nHeading2.toString(10);
         // Is an id attribute already present?
         if (UltraEdit.activeDocument.selection.length > 11)
         {
            // Has the heading 2 not the right number, overwrite it.
            if (UltraEdit.activeDocument.selection.substr(11) != sNumber)
            {
               UltraEdit.activeDocument.write('h2" id="h2_' + sNumber);
            }
            else  // Heading 2 has already the right number, cancel selection.
            {
               UltraEdit.activeDocument.key("RIGHT ARROW");
            }
         }
         else     // Heading 2 has no identifier, add it.
         {
            UltraEdit.activeDocument.write('h2" id="h2_' + sNumber + '"');
         }
      }
   }
}
Best regards from Austria
2 posts Page 1 of 1
cron