// 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 stdx.d.lexer; // http://ethanschoonover.com/solarized void highlight(R)(TokenRange!R tokens, string fileName) { stdout.writeln(q"[
]"); stdout.writeln("]"); foreach (Token t; tokens) { if (isBasicType(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 { 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.value.replace("<", "<").replace("\r", "")); } else stdout.write(t.value.replace("<", "<")); } } stdout.writeln("\n"); } void writeSpan(string cssClass, string value) { version(Windows) stdout.write(``, value.replace("&", "&").replace("<", "<").replace("\r", ""), ``); else stdout.write(``, value.replace("&", "&").replace("<", "<"), ``); } /+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(); }+/