writeTaggedText

Helper function for writing _text which has a start tag and end tag on each side and no attributes so that it can be done with one function call instead of three.

writeTaggedText is essentially equivalent to calling

writer.writeStartTag(name, newline);
writer.writeText(text, insertIndent, Newline.no);
writer.writeEndTag(Newline.no);

with the difference being that both the name and text are validated before any data is written. So, if the text is invalid XML, then nothing will have been written to the output range when the exception is thrown (whereas if each function were called individually, then the start tag would have been written before the exception was thrown from writeText).

If more control is needed over the formatting, or if attributes are needed on the start tag, then the functions will have to be called separately instead of calling writeTaggedText.

  1. void writeTaggedText(XW writer, string name, R text, Newline newline, InsertIndent insertIndent)
    void
    writeTaggedText
    (
    XW
    R
    )
    if (
    isInstanceOf!(XMLWriter, XW) &&
    isForwardRange!R
    &&
    isSomeChar!(ElementType!R)
    )
  2. void writeTaggedText(XW writer, string name, R text, InsertIndent insertIndent, Newline newline)

Parameters

writer XW

The XMLWriter to write to.

name string

The _name of the start tag.

text R

The _text to write between the start and end tags.

newline Newline

Whether a _newline followed by an indent will be written to the output range before the start tag.

insertIndent InsertIndent

Whether an indent will be inserted after each _newline within the _text.

Throws

XMLWritingException if the given _name is an invalid XML tag _name or if the given _text contains any characters or sequence of characters which are not legal in the _text portion of an XML document. dxml.util.encodeText can be used to encode any characters that are not legal in their literal form in the _text but are legal as entity references.

Examples

import std.array : appender;

{
    auto writer = xmlWriter(appender!string());
    writer.writeStartTag("root", Newline.no);
    writer.writeTaggedText("foo", "Some text between foos");
    writer.writeEndTag("root");

    assert(writer.output.data ==
           "<root>\n" ~
           "    <foo>Some text between foos</foo>\n" ~
           "</root>");
}

// With Newline.no
{
    auto writer = xmlWriter(appender!string());
    writer.writeStartTag("root", Newline.no);
    writer.writeTaggedText("foo", "Some text between foos", Newline.no);
    writer.writeEndTag("root");

    assert(writer.output.data ==
           "<root><foo>Some text between foos</foo>\n" ~
           "</root>");
}

// With InsertIndent.yes
{
    auto writer = xmlWriter(appender!string());
    writer.writeStartTag("root", Newline.no);
    writer.writeTaggedText("foo", "Some text\nNext line");
    writer.writeEndTag("root");

    assert(writer.output.data ==
           "<root>\n" ~
           "    <foo>Some text\n" ~
           "        Next line</foo>\n" ~
           "</root>");
}

// With InsertIndent.no
{
    auto writer = xmlWriter(appender!string());
    writer.writeStartTag("root", Newline.no);
    writer.writeTaggedText("foo", "Some text\nNext line", InsertIndent.no);
    writer.writeEndTag("root");

    assert(writer.output.data ==
           "<root>\n" ~
           "    <foo>Some text\n" ~
           "Next line</foo>\n" ~
           "</root>");
}

See Also

Meta