// 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.range; import std.d.lexer; void writeSpan(Sink)(ref Sink sink, string cssClass, string value) if(isOutputRange!(Sink, string)) { sink.put(``); sink.put(value.replace("&", "&").replace("<", "<")); sink.put(``); } void highlight(R)(TokenRange!R tokens, string fileName) { struct StdoutSink { void put(string data) { stdout.write(data); } } StdoutSink sink; highlight(tokens, sink, fileName); } // http://ethanschoonover.com/solarized void highlight(R, Sink)(TokenRange!R tokens, ref Sink sink, string fileName) if(isOutputRange!(Sink, string)) { sink.put(q"[ ]"); sink.put(""); sink.put(fileName); sink.put("\n"); sink.put(q"[
]");

	foreach (Token t; tokens)
	{
		if (isBasicType(t.type))
			writeSpan(sink, "type", t.value);
		else if (isKeyword(t.type))
			writeSpan(sink, "kwrd", t.value);
		else if (t.type == TokenType.comment)
			writeSpan(sink, "com", t.value);
		else if (isStringLiteral(t.type) || t.type == TokenType.characterLiteral)
			writeSpan(sink, "str", t.value);
		else if (isNumberLiteral(t.type))
			writeSpan(sink, "num", t.value);
		else if (isOperator(t.type))
			writeSpan(sink, "op", t.value);
		else
			sink.put(t.value.replace("<", "<"));
	}
	sink.put("
\n\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(); }+/