// 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"[
]"); stdout.writeln("]"); 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("\n"); } void writeSpan(string cssClass, string value) { version (Windows) stdout.write(``, value.replace("&", "&").replace("<", "<").replace("\r", ""), ``); else stdout.write(``, value.replace("&", "&").replace("<", "<"), ``); }