Skip to content

Commit 159e9c9

Browse files
snprajwalWebFreak001
authored andcommitted
feat(highlight): support multiple themes
Signed-off-by: Prajwal S N <prajwalnadig21@gmail.com>
1 parent b43c8f4 commit 159e9c9

3 files changed

Lines changed: 62 additions & 15 deletions

File tree

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ To avoid these cases, it's possible to pass the "--skipTests" option.
202202
#### Configuration
203203
By default all checks are enabled. Individual checks can be enabled or disabled
204204
by using a configuration file. Such a file can be placed, for example, is the root directory of your project.
205-
Running ```dscanner --defaultConfig``` will generate a default configuration file and print the file's location.
205+
Running `dscanner --defaultConfig` will generate a default configuration file and print the file's location.
206206
You can also specify the path to a configuration file by using the "--config" option if
207207
you want to override the default or the local settings.
208208

@@ -306,8 +306,15 @@ and case tokens in the file.
306306

307307
### Syntax Highlighting
308308
The "--highlight" option prints the given source file as syntax-highlighted HTML
309-
to the standard output. The CSS styling is currently hard-coded to use the
310-
[Solarized](http://ethanschoonover.com/solarized) color scheme.
309+
to the standard output. The CSS styling uses the [Solarized](http://ethanschoonover.com/solarized)
310+
color scheme by default, but can be customised using the "--theme" option.
311+
312+
The following themes are available:
313+
314+
- `solarized`
315+
- `solarized-dark`
316+
- `gruvbox`
317+
- `gruvbox-dark`
311318

312319
No example. It would take up too much space
313320

src/dscanner/highlighter.d

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import std.array;
1010
import dparse.lexer;
1111

1212
// http://ethanschoonover.com/solarized
13-
void highlight(R)(ref R tokens, string fileName)
13+
void highlight(R)(ref R tokens, string fileName, string themeName)
1414
{
15+
immutable(Theme)* theme = getTheme(themeName);
16+
1517
stdout.writeln(q"[
1618
<!DOCTYPE html>
1719
<html>
@@ -20,17 +22,19 @@ void highlight(R)(ref R tokens, string fileName)
2022
stdout.writeln("<title>", fileName, "</title>");
2123
stdout.writeln(q"[</head>
2224
<body>
23-
<style type="text/css">
24-
html { background-color: #fdf6e3; color: #002b36; }
25-
.kwrd { color: #b58900; font-weight: bold; }
26-
.com { color: #93a1a1; font-style: italic; }
27-
.num { color: #dc322f; font-weight: bold; }
28-
.str { color: #2aa198; font-style: italic; }
29-
.op { color: #586e75; font-weight: bold; }
30-
.type { color: #268bd2; font-weight: bold; }
31-
.cons { color: #859900; font-weight: bold; }
25+
<style type="text/css">]");
26+
stdout.writefln("
27+
html { background-color: %s; color: %s; }
28+
.kwrd { color: %s; font-weight: bold; }
29+
.com { color: %s; font-style: italic; }
30+
.num { color: %s; font-weight: bold; }
31+
.str { color: %s; font-style: italic; }
32+
.op { color: %s; font-weight: bold; }
33+
.type { color: %s; font-weight: bold; }
34+
.cons { color: %s; font-weight: bold; }
3235
</style>
33-
<pre>]");
36+
<pre>", theme.bg, theme.fg, theme.kwrd, theme.com, theme.num, theme.str,
37+
theme.op, theme.type, theme.cons);
3438

3539
while (!tokens.empty)
3640
{
@@ -76,3 +80,37 @@ void writeSpan(string cssClass, string value)
7680
stdout.write(`<span class="`, cssClass, `">`, value.replace("&",
7781
"&amp;").replace("<", "&lt;"), `</span>`);
7882
}
83+
84+
struct Theme
85+
{
86+
string bg;
87+
string fg;
88+
string kwrd;
89+
string com;
90+
string num;
91+
string str;
92+
string op;
93+
string type;
94+
string cons;
95+
}
96+
97+
immutable(Theme)* getTheme(string themeName)
98+
{
99+
immutable Theme[string] themes = [
100+
"solarized": Theme("#fdf6e3", "#002b36", "#b58900", "#93a1a1", "#dc322f", "#2aa198", "#586e75",
101+
"#268bd2", "#859900"),
102+
"solarized-dark": Theme("#002b36", "#fdf6e3", "#b58900", "#586e75", "#dc322f", "#2aa198",
103+
"#93a1a1", "#268bd2", "#859900"),
104+
"gruvbox": Theme("#fbf1c7", "#282828", "#b57614", "#a89984", "#9d0006", "#427b58",
105+
"#504945", "#076678", "#79740e"),
106+
"gruvbox-dark": Theme("#282828", "#fbf1c7", "#d79921", "#7c6f64",
107+
"#cc241d", "#689d6a", "#a89984", "#458588", "#98971a")
108+
];
109+
110+
immutable(Theme)* theme = themeName in themes;
111+
// Default theme
112+
if (theme is null)
113+
theme = &themes["solarized"];
114+
115+
return theme;
116+
}

src/dscanner/main.d

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ else
6565
bool report;
6666
bool skipTests;
6767
bool applySingleFixes;
68+
string theme;
6869
string resolveMessage;
6970
string reportFormat;
7071
string reportFile;
@@ -85,6 +86,7 @@ else
8586
getopt(args, std.getopt.config.caseSensitive,
8687
"sloc|l", &sloc,
8788
"highlight", &highlight,
89+
"theme", &theme,
8890
"ctags|c", &ctags,
8991
"help|h", &help,
9092
"etags|e", &etags,
@@ -257,7 +259,7 @@ else
257259
if (highlight)
258260
{
259261
auto tokens = byToken(bytes, config, &cache);
260-
dscanner.highlighter.highlight(tokens, args.length == 1 ? "stdin" : args[1]);
262+
dscanner.highlighter.highlight(tokens, args.length == 1 ? "stdin" : args[1], theme);
261263
return 0;
262264
}
263265
else if (tokenDump)

0 commit comments

Comments
 (0)