How to change the case of words in title of page?

How to change the case of words in title of page?

62
Advanced UserAdvanced User
62

    9:19 - Jul 26#1

    Hi all!

    I try to select text for proper title casing by a script. The script is attached.

    I tried this example for testing the casing of page title.

    Code: Select all

    <title>FROM SAMPLE <italic>PLOTS</italic> IN A WOOD TO WOODS IN A LANDSCAPE U.S.A.: AN INTERDISCIPLINARY APPROACH (<xref ref-type="bib" rid="Ref4">REF. 4</xref>)</title>
    I selected the above text before running the script. The script execution result is:

    Code: Select all

    <title>from Sample <italic>Plots</italic> in a Wood to Woods in a Landscape U.s.a.: an Interdisciplinary Approach (<xref ref-type="bib" rid="Ref4">Ref. 4</xref>)</title>
    The proper casing should be:

    Code: Select all

    <title>From Sample <italic>Plots</italic> in a Wood to Woods in a Landscape U.S.A.: An Interdisciplinary Approach (<xref ref-type="bib" rid="Ref4">Ref. 4</xref>)</title>
    The phrase <title>from should be <title>From, the country abbreviation U.s.a. should be U.S.A. and the phrase  : an should be : An.

    Can anyone help me.

    6,680583
    Grand MasterGrand Master
    6,680583

      13:09 - Jul 26#2

      There is no script attached. I wrote therefore the script completely producing for the example the expected result.

      Code: Select all

      function CapitalizeChar (match, sBefore, sChar)
      {
         return sBefore + sChar.toUpperCase();
      }
      function CapitalizeWord (match, sWord)
      {
         return sWord.charAt(0).toUpperCase() + sWord.substr(1);
      }
      
      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         if (UltraEdit.activeDocument.isSel())  // Is text selected in active file?
         {
            // Define environment for this script.
            UltraEdit.insertMode();
            if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
            else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      
            // Split up the selected text into text parts using closing angle bracket
            // (end of a tag) as text delimiter. Each text part is either just an
            // tag or the value of an element with next tag or an empty string.
            var asTextParts = UltraEdit.activeDocument.selection.split(">");
            if (asTextParts)
            {
               var sCapitalized = "";
               for (var nPartIndex = 0; nPartIndex < asTextParts.length; ++nPartIndex)
               {
                  if (asTextParts[nPartIndex].length > 0)   // Is the string not empty?
                  {
                     // Split up the text part into the element value and the next tag. 
                     var asCurrentParts = asTextParts[nPartIndex].split("<");
                     if (asCurrentParts)
                     {
                        if (asCurrentParts[0].length > 0)   // Is there an element value?
                        {
                           // Convert the entire element value string to lower case.
                           var sCurrentText = asCurrentParts[0].toLowerCase();
                           // Capitalize the first character after a closing angle bracket
                           // (end of tag) or after a dot on being a word character.
                           sCurrentText = sCurrentText.replace(/(^|\.)(\w)/g, CapitalizeChar);
                           // Capitalize the first character after a colon
                           // and a normal space on being a word character.
                           sCurrentText = sCurrentText.replace(/(: )(\w)/g, CapitalizeChar);
                           // Capitalize the first character of a string beginning with
                           // a word character at the beginning of a word which has at
                           // least three more word characters or unusual also dots.
                           sCurrentText = sCurrentText.replace(/\b(\w[\w.]{3,})/g, CapitalizeWord);
                           // Append the reformatted text to the entire capitalized text.
                           sCapitalized += sCurrentText;
                        }
                        // Append the tag with the opening and closing angle bracket.
                        sCapitalized += "<" + asCurrentParts[1] + ">";
                     }
                  }
               }
               // Is the selected text in active file different to capitalized text?
               if (UltraEdit.activeDocument.selection != sCapitalized)
               {
                  // Replace the selected text by the capitalized text.
                  UltraEdit.activeDocument.write(sCapitalized);
               }
            }
         }
      }
      
      Best regards from an UC/UE/UES for Windows user from Austria

      62
      Advanced UserAdvanced User
      62

        5:29 - Jul 27#3

        Thanks, Mofi for your reply.

        First of all apologies for not attaching the script file. Now attached my script file.

        The purpose of the script is to make the selected text proper title case.

        Proper title case is a style of capitalization where the first letter of each major word is capitalized, and minor words are typically written in lowercase unless they are the first or last word of the title.

        There are some conditions while doing casing:
        1. In the context of title case, minor words are typically articles, conjunctions, and prepositions. Here's a comprehensive list of common minor words that are generally not capitalized in title case unless they are the first or last word in the title:   

          Code: Select all

          'a', 'about', 'above', 'across', 'after', 'against', 'along', 'amid', 'among', 'an', 'and', 'around', 'as', 'at', 'before', 'behind', 'below', 'beneath', 'beside', 'between', 'beyond', 'but', 'by', 'down', 'during', 'except', 'for', 'from', 'in', 'inside', 'into', 'like', 'nor', 'of', 'off', 'on', 'onto', 'or', 'outside', 'over', 'past', 'since', 'so', 'the', 'through', 'throughout', 'till', 'to', 'toward', 'under', 'underneath', 'until', 'up', 'upon', 'via', 'with', 'within', 'without', 'yet'
        2. The beginning word of the sentence is always capitalized it can be a minor word also.
        3. Any word capitalized after ([.!?:\(] ?) can also be a minor word.
        4. Any tags <...> and "&#x[0-9A-Fa-f]+;" will be ignored.
        Please find the attachment title_case.zip

        Sample Text for tasting:

        Code: Select all

        FROM SAMPLE <italic>PLOTS</italic> IN THE WOOD TO WOODS IN A LANDSCAPE U.S.A.: AN INTERDISCIPLINARY APPROACH (THE CONTEXT OF <xref ref-type="bib" rid="Ref4">REF. 4</xref>). (A SAMPLE FOR TEST).
        After run my script output like:

        Code: Select all

        From Sample <italic>Plots</italic> in the Wood to Woods in a Landscape U.s.a.: An Interdisciplinary Approach (the Context of <xref ref-type="bib" rid="Ref4">Ref. 4</xref>). (a Sample for Test).
        Actual Output should be like:

        Code: Select all

        From Sample <italic>Plots</italic> in the Wood to Woods in a Landscape U.S.A.: An Interdisciplinary Approach (The Context of <xref ref-type="bib" rid="Ref4">Ref. 4</xref>). (A Sample for Test).
        title_case.zip (1.15 KiB)   0

        6,680583
        Grand MasterGrand Master
        6,680583

          17:33 - Jul 27#4

          The part string U.S.A.: is after a space and is not a minor word. It is capitalized for that reason by your script code. There can be a simple condition added to exclude this country abbreviation for being modified at all. You could of course also add a rule like if the current part string begins with a word character and contains more than one dot than do not modify this part string. The simple condition is in the attached script in a commented line and the better rule is active using the regular expression pattern named ignorePattern.

          An extra condition is added to capitalize part strings beginning with an opening round bracket like (THE and (A by keeping the opening round bracket as is, convert the next character to uppercase and convert all other characters to lowercase for producing (The and (A.
          title_case_v2.zip (1.35 KiB)   0
          Second version of the script with two additional conditions
          Best regards from an UC/UE/UES for Windows user from Austria

          62
          Advanced UserAdvanced User
          62

            12:10 - Jul 30#5

            Thanks again, I really appreciate it.