// 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 cssClassPrefix, string cssClass, string value) if(isOutputRange!(Sink, string)) { sink.put(``); sink.put(value.replace("&", "&").replace("<", "<")); sink.put(``); } private struct StdoutSink { void put(string data) { stdout.write(data); } } // http://ethanschoonover.com/solarized void highlight(R)(TokenRange!R tokens, string fileName) { StdoutSink sink; highlight(tokens, sink, fileName); } /// Outputs span-highlighted code only, no wrapper HTML void highlightBare(R)(TokenRange!R tokens, string cssClassPrefix=null) { StdoutSink sink; highlightBare(tokens, sink, cssClassPrefix); } void highlight(R, Sink)(TokenRange!R tokens, ref Sink sink, string fileName) if (isOutputRange!(Sink, string)) { highlightImpl(tokens, sink, fileName, false, null); } /// Outputs span-highlighted code only, no wrapper HTML void highlightBare(R, Sink)(TokenRange!R tokens, ref Sink sink, string cssClassPrefix=null) if (isOutputRange!(Sink, string)) { highlightImpl(tokens, sink, null, true, cssClassPrefix); } private void highlightImpl(R, Sink)(TokenRange!R tokens, ref Sink sink, string fileName, bool bare, string cssClassPrefix) if (isOutputRange!(Sink, string)) { if (!bare) { sink.put(q"[ ]"); sink.put(""); sink.put(fileName); sink.put("\n"); sink.put(q"[
]");
	}

	foreach (Token t; tokens)
	{
		if (isBasicType(t.type))
			writeSpan(sink, cssClassPrefix, "type", t.value);
		else if (isKeyword(t.type))
			writeSpan(sink, cssClassPrefix, "kwrd", t.value);
		else if (t.type == TokenType.comment)
			writeSpan(sink, cssClassPrefix, "com", t.value);
		else if (isStringLiteral(t.type) || t.type == TokenType.characterLiteral)
			writeSpan(sink, cssClassPrefix, "str", t.value);
		else if (isNumberLiteral(t.type))
			writeSpan(sink, cssClassPrefix, "num", t.value);
		else if (isOperator(t.type))
			writeSpan(sink, cssClassPrefix, "op", t.value);
		else
			sink.put(t.value.replace("<", "<"));
	}

	if (!bare)
		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(); }+/