Bold, Italicize, and Underline Parts of an Answer

From HotDocs Wiki

Jump to: navigation, search
NOTE:

HotDocs 2006 and later users: You can use dot codes to format text computation results. See the HotDocs Help for a complete description.


CAUTION: The scripting described below works only in word processor templates. It is also fairly complex and should be implemented by advanced users only. In addition, the computations use the WHILE instruction, which works only in HotDocs 6.2 and higher.

In HotDocs, when a variable or computation is merged in a template, you can format the variable in any way you want. You simply select the variable field, including the chevrons, and apply the desired formatting, such as bold-face or italics:

Template: Hobble Creek Publishing will publish «Novel Title», by «Author Name».
Answers: Novel Title: Secrets of HotDocs
Author Name: Bonnie Wright
Assembled Document: Hobble Creek Publishing will publish Secrets of HotDocs, by Bonnie Wright.

Unfortunately, HotDocs does not have the ability to format individual parts of an answer differently. For example, let's say a template includes a variable named Optional Text. If the user enters the same text about a novel to be published by Hobble Creek Publishing, there would be no way to indicate that the title should be italicized. The entire answer must have the same formatting:

Template: «Optional Text»
Answers: Optional Text: Hobble Creek Publishing will publish Secrets of HotDocs, by Bonnie Wright.
Assembled Document: Hobble Creek Publishing will publish Secrets of HotDocs, by Bonnie Wright.

As you can see, the simplest method of formatting individual parts of an answer differently is to create separate variables for each part. However, sometimes this isn't practical and the template would function best if it were possible to format individual words or sections of an answer. The following computations are a work-around that allow parts of a single answer or computation result to be formatted differently.

To make parts of an answer bold, italic, or underlined, you must complete the following five parts:

  • Adopt a series of format codes for indicating which parts of an answer to format each way.
  • Add format codes to the answer.
  • Create a Computation variable that parses (breaks up) the answer to separate text from the format codes. Each section of text or formatting code is saved in a separate repetition of a temporary repeated dialog.
  • Create a Computation variable for each formatting style. For example, if you want both regular and bold text, you must create two computations. (If you want to allow three styles (regular, bold, and underline), you would need four computations: regular, bold, underline, and bold/ underline. Likewise, four styles (regular, bold, underline, and italic) would require eight computations: regular, bold, underline, italic, bold/underline, bold/italic, underline/italic, and bold/italic/underline.)
  • Add a REPEAT instruction and Computation variable fields to the template.

Contents

Part 1: To adopt a series of format codes

The first part is to determine which formats you would like to apply to different parts of the answer. This example uses HTML codes for bold text, where <b> indicates the beginning of bold text and </b> indicates the end. The following table shows other HTML codes you may use, or you may choose to adopt your own series of codes:

HTML Code Output
<B>Bold Text</B> Bold Text
<I>Italicized Text</I> Italicized Text
<U>Underlined Text</U> Underlined Text

Part 2: To add format codes to the text

The next part is to add format codes to the answer. If you want to format parts of a single Text variable's answer, users can include these codes in their answers. For example, the user could enter something like this to indicate that part of the answer should be italicized:

 I am the author of <I>Secrets of HotDocs</I>.

You can also insert these format codes in the result of a computation. For example, if you have a list of names in a repeated dialog and you want to list them in the document with the last name bold, you could write a script like this, where Names List is a repeated dialog that contains the First Name and Last Name variables:

""
REPEAT Names List
  FORMAT "a, b, and c"
  RESULT + First Name + "<B>" + Last Name + "</B>"
END REPEAT

Part 3: To create a Computation variable that parses the answer

Once you have adopted a series of format codes and added the format codes to the text, you can create a Computation variable (Parser Computation) that parses the text and format codes. At the beginning of the script, you must first set the result to an answer that contains format codes. You can either use the name of a single Text variable, or you can combine multiple variables like the script in part 2 above. The following example creates a list of books by repeating a dialog (Book List) that contains two variables (Book Title and Author Name). Formatting codes are added to indicate that the titles should be underlined:

//Parser Computation
""
REPEAT Book List
RESULT + "<U>" + Book Title + "</U>, by " + Author Name + "
"
END REPEAT

The next part of the script sets a temporary counter variable (Index Var) to an initial value of 0. It also sets the initial value of the Underline True/False variable to FALSE. (If you want to allow more than just underlining, you must add a similar SET instruction for each formatting style.)

SET Index Var TO 0
SET Underline TO FALSE

Next, a WHILE loop is created that examines the computation result to determine which parts are text and which parts are formatting codes. A second WHILE loop then makes sure that the dialog containing a list of all format codes and text (Format Dialog) does not have more answers than it should:

WHILE LENGTH( RESULT ) > 0
  INCREMENT Index Var
  IF RESULT CONTAINS "<" AND RESULT CONTAINS ">"
    SET Text Var[Index Var] TO FIRST( RESULT, POSITION( RESULT, "<" ) - 1 )
    LAST( RESULT, LENGTH( RESULT ) - POSITION( RESULT, "<" ) + 1 )
    INCREMENT Index Var
    SET Text Var[Index Var] TO FIRST( RESULT, POSITION( RESULT, ">" ) )
    LAST( RESULT, LENGTH( RESULT ) - POSITION( RESULT, ">" ) )
  ELSE
    SET Text Var[Index Var] TO RESULT
    ""
  END IF
END WHILE
 
WHILE Index Var < COUNT( FormatDialog) 
  SET Text Var[COUNT( Format Dialog )] TO UNANSWERED
END WHILE

Parser Computation A computation that separates the text and format codes.
Book List A repeated dialog that contains a list of books and authors.
Book Title A Text variable in the Book List dialog.
Author Name A Text variable in the Book List dialog.
Index Var A temporary Number variable used as an index in Parser Computation. (See Create a Temporary Variable.)
Underline A True/False variable used to indicate if text should be underlined.
Text Var A temporary Text variable used in Format Dialog to save each string of characters or format code after the text is parsed in Parser Computation.
Format Dialog A temporary repeated dialog that contains the Text Var variable, which is a list of each code or section of text parsed in Parser Computation.

Part 4: To create a Computation variable for each formatting style

The first style computation you must create is the Regular Style computation. This computation analyzes each answer in the Format Dialog (see part 3 above) to see if it contains a format code or text. If it finds a format code, the corresponding True/False variable for that format is set to TRUE (if it is a beginning format code) or FALSE (if it is an ending format code). The following script only handles underlining, but you can adapt it to handle other styles as needed:

//Regular Style computation
""
IF Text Var = "<U>"
  SET Underline TO TRUE
ELSE IF Text Var = "</U>"
  SET Underline TO FALSE
ELSE IF !Underline
  Text Var
END IF

Next, you must create a separate Computation variable for each formatting style or combination of formatting styles. The following script for the Underline Style computation determines if the answer is text (not a format code), and if it is and the Underline True/False variable is TRUE, the answer is returned as the result of the computation:

//Underline Style computation
""
IF FIRST( Text Var, 1 ) != "<" AND Underline
 Text Var
END IF

Text Var A temporary Text variable used in Format Dialog to save each string of characters or format code after the text is parsed in Parser Computation.
Underline A True/False variable used to indicate if text should be underlined.

Part 5: To add the computations to the template

The final step is to add the Computation variables and a REPEAT instruction to the template. Here is an example of how your template should look, where Parser Computation is the computation that breaks up the answer and places the codes and text in Format Dialog, and Regular Style and Underline Style are the style computations:

«Parser Computation»«REPEAT Format Dialog»«Regular Style»«Underline Style»«END REPEAT»

As the dialog is repeated, the Regular Style and Underline Style computations determine if the text is a formatting code. If it is, corresponding True/False variables are set to indicate the current text format. Otherwise, the text is inserted by either the Regular Style or Underline Style computation, depending on what format it should be. To add additional formatting, such as bold or italics, additional computations must be added to the template:

«Parser Computation»«REPEAT Format Dialog»«Regular Style»«Bold Style»«Italic Style»«Underline Style»«Bold and Italic Style»«Bold and Underline Style»«Italic and Underline Style»«Bold Italic and Underline Style»«END REPEAT»