Dynamic say phrases

If you have a text like “It was something nasty.” and you want to print it, but with the angle brackets removed and the word “nasty” in italics, how can you do it in Inform 7?

I tried using “replace”:

[code]to say reformat (atext - an indexed text):
replace the text “” in atext with “[italic type]”;
replace the text “
” in atext with “[roman type]”;
say “[atext]”;

when play begins:
say reformat “It was something nasty.”;
[/code]
that cuts the angle brackets but “nasty” is not printed in italics. (In the application the matching text and the replacement text are regular expressions - bonus points if you can think of solution which works for regular expressions).

Do you have a really strong reason for wanting to keep the angle brackets?

I think it’d be easier to play more Inform’s way by just replacing them with square brackets. Then the following will work:

[code]“replacement” by Wade Clarke

test is a room.

To say i:
say italic type.

To say /i:
say roman type.

when play begins:
say “It was something nasty.”[/code]

So what I’ve done is made a system where you can use square bracketed i and /i throughout your source to turn italics on and off, instead of angle bracketed i and /i. This also skips the need for indexed text or extra voluntary steps in the code. If you’re bringing in text from some other source where it has angled bracketed i’s, you just have to do a replace all with [ for < and ] for >.

  • Wade

The tags happen to be longer than the original texts. There are a few pages so it would be more convenient to use a phrase than hunt through re-typing (it’s not easy to search-and-replace the format).
Is it a bug that a say phrase does not work if it is replaced into a text?

No. Indexed text can’t store style declarations.

I agree with Wade that it’d be better to not do the transformation on the fly. You can run the program once so that it prints the correct I7 code formatting and then replace the original text with the correct one:

[code]To say reformat (atext - an indexed text):
replace the text “” in atext with “[bracket]italic type[close bracket]”;
replace the text “
” in atext with “[bracket]roman type[close bracket]”;
say “[atext]”;

when play begins:
say reformat “It was something nasty.”;[/code]

…then copy-paste the resulting text back into the source code and drop the reformatting function.

I’d been typing a new post but Juhana has already said it better and also came up with a solution I hadn’t thought of.

  • Wade