WYSIWYG String Syntax

A syntax for expressing strings in a "What You See Is What You Get" (wysiwyg) form is seems useful. In many cases the output of software is the text it is written in. As such, it would be expedient to express templates of such output in the source unmangled. Currently, doing so is hampered when newlines eliminating source indents is unnecessary.

Ideally, some literal string syntax that embraces the presence of source level newlines but also strips source code level indents and perhaps preceding and trailing newlines as follows (here l"..." is used as a new literal text annotizer):

format!(l"
    The prefixed newline is stripped
    The source code level indent is stripped
    This template was created written: {:?}
           Indents can still occur
    The suffixed newline is stripped
    ",
    std::time::SystemTime::now(),
);

I don't know that "literal" is a good term for something that removes indentation as it is no longer using the literal content (it is also confusing because any "foo" is a "string literal"). A builtin (magic?) macro like dedent! would be better I think.

As a prefix, how would this compose with raw string literals? Or is that not allowed?

FWIW, I've used concat! to handle cases like this myself, but it does have extra noise with the quotes and commas compared to this.

2 Likes

Aside: if you see {:?}, but you get a timestamp, it's not WYSIWYG. Compilation of formatting code is never WYSIWYG, that's the whole point of it.

1 Like

This is https://docs.rs/indoc.

use indoc::formatdoc;

fn main() {
    let s = formatdoc! {"
        The prefixed newline is stripped
        The source code level indent is stripped
        This template was created written: {:?}
               Indents can still occur
        The suffixed newline is stripped
        ",
        std::time::SystemTime::now(),
    };
    print!("{}", s);
}

or:

    printdoc! {"
        The prefixed newline is stripped
        The source code level indent is stripped
        This template was created written: {:?}
               Indents can still occur
        The suffixed newline is stripped
        ",
        std::time::SystemTime::now(),
    };
16 Likes

Nice. Was not aware of formatdoc! :smiley: