s = encode(ʃencoding_type,ʅ text);
The
encode function escapes special characters to facilitate writing to
HTML,
Markdown, or CSV files, or creates properly encoded strings for a variety of formats.
The optional argument encoding_type specifies the format for encoding. The text argument is a string expression that specifies the text to encode. If an encoding type is provided with no text, then the default encoding type for the application is modified. An application starts with the default encoding type set to HTML.
The encoding types are:
| Encoding Type | Description |
| HTML | Encodes the characters < > & and newlines for writing to HTML files. |
| Markdown | Encodes special characters such as * for writing to Markdown files. |
| CSV | Encodes the characters , " and newlines for writing to comma-separated values files. |
| URI | Encodes a group of characters for writing uniform resource identifiers (URIs). |
| URIComponent | Encodes a group of characters for writing the components of URIs. |
| PercentEncoding | Encodes a group of characters for writing to formats such as connection strings. |
| JsonString | Encodes text for writing as a valid JSON string. |
| Slashes | Adds backslashes to escape quote and backslash characters. |
If you are encoding many strings, you might consider using
StringWriter, an object that supports the automatic encoding of text.
The function returns a string with the appropriate characters encoded. If the default encoding type is changed, the function returns a blank string.
// write to a HTML file
File html_file;
html_file.write("<p>Enumerator name: %s</p>", encode(strip(ENUMERATOR_NAME)));
string house_image_filename = pathconcat("../House Images", key(HOUSING_DICT) + ".jpg");
html_file.write("<p><img src='file:///%s' alt='House' /></p>", encode(URI, house_image_filename));
// write to a CSV file
File csv_file;
encode(CSV);
csv_file.write("Address,Head of Household");
csv_file.write("%s,%s", encode(strip(ADDRESS)), encode(strip(HEAD_NAME)));