-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighlighter.d
More file actions
78 lines (70 loc) · 2.28 KB
/
Copy pathhighlighter.d
File metadata and controls
78 lines (70 loc) · 2.28 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
// Copyright Brian Schott (Sir Alaran) 2012.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
module highlighter;
import std.stdio;
import std.array;
import std.d.lexer;
// http://ethanschoonover.com/solarized
void highlight(R)(ref R tokens, string fileName)
{
stdout.writeln(q"[
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>]");
stdout.writeln("<title>", fileName, "</title>");
stdout.writeln(q"[</head>
<body>
<style type="text/css">
html { background-color: #fdf6e3; color: #002b36; }
.kwrd { color: #b58900; font-weight: bold; }
.com { color: #93a1a1; font-style: italic; }
.num { color: #dc322f; font-weigth: bold; }
.str { color: #2aa198; font-style: italic; }
.op { color: #586e75; font-weight: bold; }
.type { color: #268bd2; font-weight: bold; }
.cons { color: #859900; font-weight: bold; }
</style>
<pre>]");
while (!tokens.empty)
{
auto t = tokens.front;
tokens.popFront();
if (isBasicType(t.type))
writeSpan("type", str(t.type));
else if (isKeyword(t.type))
writeSpan("kwrd", str(t.type));
else if (t.type == tok!"comment")
writeSpan("com", t.text);
else if (isStringLiteral(t.type) || t.type == tok!"characterLiteral")
writeSpan("str", t.text);
else if (isNumberLiteral(t.type))
writeSpan("num", t.text);
else if (isOperator(t.type))
writeSpan("op", str(t.type));
else if (t.type == tok!"specialTokenSequence" || t.type == tok!"scriptLine")
writeSpan("cons", t.text.replace("<", "<"));
else
{
version(Windows)
{
// Stupid Windows automatically does a LF → CRLF, so
// CRLF → CRCRLF, which is obviously wrong.
// Strip out the CR characters here to avoid this.
stdout.write(t.text.replace("<", "<").replace("\r", ""));
}
else
stdout.write(t.text.replace("<", "<"));
}
}
stdout.writeln("</pre>\n</body></html>");
}
void writeSpan(string cssClass, string value)
{
version(Windows)
stdout.write(`<span class="`, cssClass, `">`, value.replace("&", "&").replace("<", "<").replace("\r", ""), `</span>`);
else
stdout.write(`<span class="`, cssClass, `">`, value.replace("&", "&").replace("<", "<"), `</span>`);
}