// 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; void writeSpan(string cssClass, string value) { stdout.write(``, value.replace("&", "&").replace("<", "<"), ``); } // http://ethanschoonover.com/solarized void highlight(R)(TokenRange!R tokens, string fileName) { stdout.writeln(q"[
]"); stdout.writeln("]");
foreach (Token t; tokens)
{
if (isType(t.type))
writeSpan("type", t.value);
else if (isKeyword(t.type))
writeSpan("kwrd", t.value);
else if (t.type == TokenType.comment)
writeSpan("com", t.value);
else if (isStringLiteral(t.type) || t.type == TokenType.characterLiteral)
writeSpan("str", t.value);
else if (isNumberLiteral(t.type))
writeSpan("num", t.value);
else if (isOperator(t.type))
writeSpan("op", t.value);
else
stdout.write(t.value.replace("<", "<"));
}
stdout.writeln("\n");
}
/+void main(string[] args)
{
LexerConfig config;
config.tokenStyle = TokenStyle.source;
config.iterStyle = IterationStyle.everything;
config.fileName = args[1];
auto f = File(args[1]);
(cast(ubyte[]) f.byLine(KeepTerminator.yes).join()).byToken(config).highlight();
}+/