-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhtml.delete
More file actions
98 lines (86 loc) · 2.54 KB
/
Copy pathhtml.delete
File metadata and controls
98 lines (86 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
HTML character entity escaping.
TODO: Make things @safe once Appender is.
Copyright: © 2012-2014 RejectedSoftware e.K.
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
Authors: Sönke Ludwig
*/
module dmarkdown.html;
import std.array;
import std.conv;
import std.range;
package:
/** Writes the HTML escaped version of a given string to an output range.
*/
void filterHTMLEscape(R, S)(ref R dst, S str, HTMLEscapeFlags flags = HTMLEscapeFlags.escapeNewline)
if (isOutputRange!(R, dchar) && isInputRange!S)
{
for (;!str.empty;str.popFront())
filterHTMLEscape(dst, str.front, flags);
}
/** Writes the HTML escaped version of a given string to an output range (also escapes double quotes).
*/
void filterHTMLAttribEscape(R, S)(ref R dst, S str)
if (isOutputRange!(R, dchar) && isInputRange!S)
{
for (; !str.empty; str.popFront())
filterHTMLEscape(dst, str.front, HTMLEscapeFlags.escapeNewline|HTMLEscapeFlags.escapeQuotes);
}
/** Writes the HTML escaped version of a given string to an output range (escapes every character).
*/
void filterHTMLAllEscape(R, S)(ref R dst, S str)
if (isOutputRange!(R, dchar) && isInputRange!S)
{
for (; !str.empty; str.popFront()) {
dst.put("&#");
dst.put(to!string(cast(uint)str.front));
dst.put(';');
}
}
/**
Writes the HTML escaped version of a character to an output range.
*/
void filterHTMLEscape(R)(ref R dst, dchar ch, HTMLEscapeFlags flags = HTMLEscapeFlags.escapeNewline )
{
switch (ch) {
default:
if (flags & HTMLEscapeFlags.escapeUnknown) {
dst.put("&#");
dst.put(to!string(cast(uint)ch));
dst.put(';');
} else dst.put(ch);
break;
case '"':
if (flags & HTMLEscapeFlags.escapeQuotes) dst.put(""");
else dst.put('"');
break;
case '\'':
if (flags & HTMLEscapeFlags.escapeQuotes) dst.put("'");
else dst.put('\'');
break;
case '\r', '\n':
if (flags & HTMLEscapeFlags.escapeNewline) {
dst.put("&#");
dst.put(to!string(cast(uint)ch));
dst.put(';');
} else dst.put(ch);
break;
case 'a': .. case 'z': goto case;
case 'A': .. case 'Z': goto case;
case '0': .. case '9': goto case;
case ' ', '\t', '-', '_', '.', ':', ',', ';',
'#', '+', '*', '?', '=', '(', ')', '/', '!',
'%' , '{', '}', '[', ']', '`', '´', '$', '^', '~':
dst.put(cast(char)ch);
break;
case '<': dst.put("<"); break;
case '>': dst.put(">"); break;
case '&': dst.put("&"); break;
}
}
enum HTMLEscapeFlags {
escapeMinimal = 0,
escapeQuotes = 1<<0,
escapeNewline = 1<<1,
escapeUnknown = 1<<2
}