Text Literals

Text literals are character sequences stored in Text objects. All text literals in Nisus Writer macros can be treated as Text objects, even if the literal is constructed as a plain-text string with no formatting. All text literals can span multiple lines. 

There are a variety of ways to construct text literals:

Single Quoted Text

A text literal enclosed in single quotation marks, eg: 'hello'. This kind of literal preserves attributes applied to the text.

If you wish to include a single quote or backslash character inside the text, you must escape it by preceding it with a backslash, eg: 'don\'t' and 'and\\or'.

Double Quoted Text

An interpolated text literal enclosed in double quotes, eg: "hello $name". Interpolated means that the string is scanned for special character sequences which are replaced before the text is used (see the table below). This kind of literal preserves attributes applied to the text. Some examples:

# interpolating a variable's value into the text

$name = "Stark"

Prompt "Off with $name's head!"


# escaped dollar sign

$six = 6

Prompt "It costs \$$six dollars."


# newline characters using an escape

$text = "Three \n Lines \n Of Text"

The interpolations that occur are similar to those used by Perl and include the following:

Escape

Replaced by:

\n 

Newline character. 

\t 

Tab character. 

\\ 

Single backslash character (eg: "\"). 

\x 

The next two characters are treated as a Unicode code point (in hexadecimal). 

\u 

The next four characters are treated as a Unicode code point (in hexadecimal). 

\U 

The next eight characters are treated as a Unicode code point (in hexadecimal). 

\x{...}

The characters between the braces are treated as a single Unicode code point (in hexadecimal). The number of characters between the braces should be between 2-8. (added for v1.0.2)

$varName 

Current value of the named variable.

\$

Single dollar sign character.

$$

Single dollar sign character (added for v2.1)

@String Literal

A plain-text string literal defined with the @String construct (added for v2.1), which does not preserve attributes applied to the text. The first character just after the @String keyword can be any Unicode character that is not whitespace, A-Z, or 0-9. Whatever character is encountered will be used to determine the endpoint of the string. Some examples:

@String<She said, "How's trix?">

@StringNearly any Unicode character can be used!

@String#([0-9]+)\s+"(\w+)"#

The characters (, [, {, and < are special and will match ), ], }, and > as their endpoint respectively.

Variables inside the string are interpolated, eg:

$name = "Stark"

Prompt @String(He said, "off with $name's head!")

Except for variables and replacing $$ with a single dollar sign, no other interpolation occurs. Characters cannot be escaped using backslashes. 

Prompt @String{It costs $$7 dollars.}

The @String construct may also be abbreviated as @S, eg:

Replace All @S<\d>, @S<#>, 'E'

@Text Literal

A text literal defined with the @Text construct (added for v2.1). This construct is exactly like @String, allowing customization of the delimiting characters and providing variable interpolation, but also preserves attributes applied to the text, eg:

$comment = @Text<Such a pretty red apple.>

The @Text construct can also be abbreviated as just @T, eg:

$object = "car"

$comment = @T#Such a pretty red $object.#

Text Literals Summary

The following table gives a summary of the various text literal constructs and their capabilities:

Literal Construct

Example

Preserves
Formatting

Custom
Delimiters

Interpolates
Variables

Character
Escapes

Single Quoted Text

'Hi'

yes

no

no

\' and \\

Double Quoted Text

"Hi"

yes

no

yes

many, see table

@String

@String(Hi)

no

yes

yes

$$

@Text

@Text(Hi)

yes

yes

yes

$$


Previous Chapter
Literals
<<  index  >>
 
Next Chapter
Comments