XMLWriter.writeComment

Writes a comment to the output range.

  1. void writeComment(R text, Newline newline, InsertIndent insertIndent)
    struct XMLWriter(OR)
    void
    writeComment
    (
    R
    )
    if (
    isForwardRange!R &&
    isSomeChar!(ElementType!R)
    )
    if (
    isOutputRange!(OR, char)
    )
  2. void writeComment(R text, InsertIndent insertIndent, Newline newline)

Parameters

text R

The text of the comment.

newline Newline

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

insertIndent InsertIndent

Whether an indent will be inserted after each newline within the text.

Throws

XMLWritingException if the given text contains "--" or ends with "-".

Examples

1 import std.array : appender;
2 import std.exception : assertThrown;
3 
4 auto writer = xmlWriter(appender!string());
5 
6 writer.writeComment(" And so it begins... ", Newline.no);
7 writer.writeStartTag("root");
8 writer.writeComment("A comment");
9 writer.writeComment("Another comment");
10 writer.writeComment("No preceding newline", Newline.no);
11 writer.writeComment("A comment\nwith a newline");
12 writer.writeComment("Another newline\nbut no indent",
13                     InsertIndent.no);
14 writer.writeStartTag("tag");
15 writer.writeComment("Deeper comment");
16 writer.writeEndTag("tag");
17 writer.writeEndTag("root");
18 writer.writeComment(" And so it ends... ");
19 
20 assert(writer.output.data ==
21        "<!-- And so it begins... -->\n" ~
22        "<root>\n" ~
23        "    <!--A comment-->\n" ~
24        "    <!--Another comment--><!--No preceding newline-->\n" ~
25        "    <!--A comment\n" ~
26        "        with a newline-->\n" ~
27        "    <!--Another newline\n" ~
28        "but no indent-->\n" ~
29        "    <tag>\n" ~
30        "        <!--Deeper comment-->\n" ~
31        "    </tag>\n" ~
32        "</root>\n" ~
33        "<!-- And so it ends... -->");
34 
35 // -- is not legal in an XML comment.
36 assertThrown!XMLWritingException(writer.writeComment("foo--bar"));
37 
38 // - is not legal at the end of an XML comment.
39 assertThrown!XMLWritingException(writer.writeComment("foo-"));
40 
41 // Unchanged after an XMLWritingException is thrown.
42 assert(writer.output.data ==
43        "<!-- And so it begins... -->\n" ~
44        "<root>\n" ~
45        "    <!--A comment-->\n" ~
46        "    <!--Another comment--><!--No preceding newline-->\n" ~
47        "    <!--A comment\n" ~
48        "        with a newline-->\n" ~
49        "    <!--Another newline\n" ~
50        "but no indent-->\n" ~
51        "    <tag>\n" ~
52        "        <!--Deeper comment-->\n" ~
53        "    </tag>\n" ~
54        "</root>\n" ~
55        "<!-- And so it ends... -->");

See Also

Meta