Skip to content

Make highlighting more useful as a lib #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 65 additions & 15 deletions highlighter.d
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,70 @@ module highlighter;

import std.stdio;
import std.array;
import std.range;
import std.d.lexer;

void writeSpan(string cssClass, string value)
void writeSpan(Sink)(ref Sink sink, string cssClassPrefix, string cssClass, string value)
if(isOutputRange!(Sink, string))
{
stdout.write(`<span class="`, cssClass, `">`, value.replace("&", "&amp;").replace("<", "&lt;"), `</span>`);
sink.put(`<span class="`);
sink.put(cssClassPrefix);
sink.put(cssClass);
sink.put(`">`);
sink.put(value.replace("&", "&amp;").replace("<", "&lt;"));
sink.put(`</span>`);
}

private struct StdoutSink
{
void put(string data)
{
stdout.write(data);
}
}

// http://ethanschoonover.com/solarized
void highlight(R)(TokenRange!R tokens, string fileName)
{
stdout.writeln(q"[
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"[
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>]");
stdout.writeln("<title>", fileName, "</title>");
stdout.writeln(q"[</head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
]");
sink.put("<title>");
sink.put(fileName);
sink.put("</title>\n");
sink.put(q"[</head>
<body>
<style type="text/css">
html { background-color: #fdf6e3; color: #002b36; }
Expand All @@ -37,26 +83,30 @@ html { background-color: #fdf6e3; color: #002b36; }
.type { color: #268bd2; font-weight: bold; }
.cons { color: #859900; font-weight: bold; }
</style>
<pre>]");
<pre>
]");
}

foreach (Token t; tokens)
{
if (isBasicType(t.type))
writeSpan("type", t.value);
writeSpan(sink, cssClassPrefix, "type", t.value);
else if (isKeyword(t.type))
writeSpan("kwrd", t.value);
writeSpan(sink, cssClassPrefix, "kwrd", t.value);
else if (t.type == TokenType.comment)
writeSpan("com", t.value);
writeSpan(sink, cssClassPrefix, "com", t.value);
else if (isStringLiteral(t.type) || t.type == TokenType.characterLiteral)
writeSpan("str", t.value);
writeSpan(sink, cssClassPrefix, "str", t.value);
else if (isNumberLiteral(t.type))
writeSpan("num", t.value);
writeSpan(sink, cssClassPrefix, "num", t.value);
else if (isOperator(t.type))
writeSpan("op", t.value);
writeSpan(sink, cssClassPrefix, "op", t.value);
else
stdout.write(t.value.replace("<", "&lt;"));
sink.put(t.value.replace("<", "&lt;"));
}
stdout.writeln("</pre>\n</body></html>");

if (!bare)
sink.put("</pre>\n</body></html>\n");
}

/+void main(string[] args)
Expand Down