String data: formatting

You will encounter string-like data in many places in an OmniMark program:

You can output a string value directly. The following program outputs the value of a string shelf, the value of a literal string, the value of a pattern variable, another literal string, and a string source:

  find letter+ => file-name
     local string first-name initial { "The file" }
  
     output first-name
         || " named "
         || file-name
         || " contains%n"
         || file file-name

You can format string data using the g format command in tandem with the format operator. By itself, the g format command does not change the original format of the string. However, you can use g in a format item to output the contents of a string shelf item within a string expression:

  process
     local string first-name initial { "John" }
  
     output "Hello %g(first-name)."

To change the format of a string, you can add a format modifier to the g format command. For example, you can use the l and u format modifiers to force the string to lowercase or uppercase, respectively. The following program outputs johnJOHN:

  process
     local string first-name initial { "John" }
  
     output "Hello "
         || "lg" % first-name
         || "ug" % first-name

You can use the f format modifier to pad the string to a specified width. Spaces are added on the right to make up the width. If the string is longer than the specified width, the full string is output. The following program outputs [John ]:

  process
     local string first-name initial { "John" }
  
     output "[" || "8fg" % first-name || "]"

You can use the k format modifier with the f modifier to pad the string to the left instead of the right. The following program outputs [ John]:

  process
     local string first-name initial { "John" }
  
     output "[" || "8fkg" % first-name || "]"

You can use any reasonable combination of these modifiers together.

Format items can be used only with shelf items. However, you can use the format operator with any type of expression. You can use the format command g with the format operator to format the result of any type of string expression. For instance:

  element "codeblock"
     output "lg" % attribute "language"

In earlier versions of OmniMark, the g format command could not be used to format pattern variables. You had to use the x format item instead. The use of the x format command is now deprecated.

There are also format commands that can be used only in format items and are used to access and format various artifacts of markup parsing. These include,

  • %c—the parse continuation operator,
  • %q—which deals with element names, and
  • %v—which deals with attributes and external data entities.