highlightBare supports optional CSS class prefix for better CSS hygiene.

This commit is contained in:
Nick Sabalausky 2013-08-01 23:26:55 -04:00
parent 901c777862
commit f290e05724
1 changed files with 14 additions and 13 deletions

View File

@ -11,10 +11,11 @@ import std.array;
import std.range; import std.range;
import std.d.lexer; import std.d.lexer;
void writeSpan(Sink)(ref Sink sink, string cssClass, string value) void writeSpan(Sink)(ref Sink sink, string cssClassPrefix, string cssClass, string value)
if(isOutputRange!(Sink, string)) if(isOutputRange!(Sink, string))
{ {
sink.put(`<span class="`); sink.put(`<span class="`);
sink.put(cssClassPrefix);
sink.put(cssClass); sink.put(cssClass);
sink.put(`">`); sink.put(`">`);
sink.put(value.replace("&", "&amp;").replace("<", "&lt;")); sink.put(value.replace("&", "&amp;").replace("<", "&lt;"));
@ -37,26 +38,26 @@ void highlight(R)(TokenRange!R tokens, string fileName)
} }
/// Outputs span-highlighted code only, no wrapper HTML /// Outputs span-highlighted code only, no wrapper HTML
void highlightBare(R)(TokenRange!R tokens) void highlightBare(R)(TokenRange!R tokens, string cssClassPrefix=null)
{ {
StdoutSink sink; StdoutSink sink;
highlightBare(tokens, sink); highlightBare(tokens, sink, cssClassPrefix);
} }
void highlight(R, Sink)(TokenRange!R tokens, ref Sink sink, string fileName) void highlight(R, Sink)(TokenRange!R tokens, ref Sink sink, string fileName)
if (isOutputRange!(Sink, string)) if (isOutputRange!(Sink, string))
{ {
highlightImpl(tokens, sink, fileName, false); highlightImpl(tokens, sink, fileName, false, null);
} }
/// Outputs span-highlighted code only, no wrapper HTML /// Outputs span-highlighted code only, no wrapper HTML
void highlightBare(R, Sink)(TokenRange!R tokens, ref Sink sink) void highlightBare(R, Sink)(TokenRange!R tokens, ref Sink sink, string cssClassPrefix=null)
if (isOutputRange!(Sink, string)) if (isOutputRange!(Sink, string))
{ {
highlightImpl(tokens, sink, null, true); highlightImpl(tokens, sink, null, true, cssClassPrefix);
} }
private void highlightImpl(R, Sink)(TokenRange!R tokens, ref Sink sink, string fileName, bool bare) private void highlightImpl(R, Sink)(TokenRange!R tokens, ref Sink sink, string fileName, bool bare, string cssClassPrefix)
if (isOutputRange!(Sink, string)) if (isOutputRange!(Sink, string))
{ {
if (!bare) if (!bare)
@ -89,17 +90,17 @@ html { background-color: #fdf6e3; color: #002b36; }
foreach (Token t; tokens) foreach (Token t; tokens)
{ {
if (isBasicType(t.type)) if (isBasicType(t.type))
writeSpan(sink, "type", t.value); writeSpan(sink, cssClassPrefix, "type", t.value);
else if (isKeyword(t.type)) else if (isKeyword(t.type))
writeSpan(sink, "kwrd", t.value); writeSpan(sink, cssClassPrefix, "kwrd", t.value);
else if (t.type == TokenType.comment) else if (t.type == TokenType.comment)
writeSpan(sink, "com", t.value); writeSpan(sink, cssClassPrefix, "com", t.value);
else if (isStringLiteral(t.type) || t.type == TokenType.characterLiteral) else if (isStringLiteral(t.type) || t.type == TokenType.characterLiteral)
writeSpan(sink, "str", t.value); writeSpan(sink, cssClassPrefix, "str", t.value);
else if (isNumberLiteral(t.type)) else if (isNumberLiteral(t.type))
writeSpan(sink, "num", t.value); writeSpan(sink, cssClassPrefix, "num", t.value);
else if (isOperator(t.type)) else if (isOperator(t.type))
writeSpan(sink, "op", t.value); writeSpan(sink, cssClassPrefix, "op", t.value);
else else
sink.put(t.value.replace("<", "&lt;")); sink.put(t.value.replace("<", "&lt;"));
} }