mirror of
https://github.com/dlang-community/D-Scanner.git
synced 2025-04-26 05:10:03 +03:00
feat(highlight): support multiple themes
Signed-off-by: Prajwal S N <prajwalnadig21@gmail.com>
This commit is contained in:
parent
b43c8f45cf
commit
159e9c9eec
3 changed files with 62 additions and 15 deletions
13
README.md
13
README.md
|
@ -202,7 +202,7 @@ To avoid these cases, it's possible to pass the "--skipTests" option.
|
||||||
#### Configuration
|
#### Configuration
|
||||||
By default all checks are enabled. Individual checks can be enabled or disabled
|
By default all checks are enabled. Individual checks can be enabled or disabled
|
||||||
by using a configuration file. Such a file can be placed, for example, is the root directory of your project.
|
by using a configuration file. Such a file can be placed, for example, is the root directory of your project.
|
||||||
Running ```dscanner --defaultConfig``` will generate a default configuration file and print the file's location.
|
Running `dscanner --defaultConfig` will generate a default configuration file and print the file's location.
|
||||||
You can also specify the path to a configuration file by using the "--config" option if
|
You can also specify the path to a configuration file by using the "--config" option if
|
||||||
you want to override the default or the local settings.
|
you want to override the default or the local settings.
|
||||||
|
|
||||||
|
@ -306,8 +306,15 @@ and case tokens in the file.
|
||||||
|
|
||||||
### Syntax Highlighting
|
### Syntax Highlighting
|
||||||
The "--highlight" option prints the given source file as syntax-highlighted HTML
|
The "--highlight" option prints the given source file as syntax-highlighted HTML
|
||||||
to the standard output. The CSS styling is currently hard-coded to use the
|
to the standard output. The CSS styling uses the [Solarized](http://ethanschoonover.com/solarized)
|
||||||
[Solarized](http://ethanschoonover.com/solarized) color scheme.
|
color scheme by default, but can be customised using the "--theme" option.
|
||||||
|
|
||||||
|
The following themes are available:
|
||||||
|
|
||||||
|
- `solarized`
|
||||||
|
- `solarized-dark`
|
||||||
|
- `gruvbox`
|
||||||
|
- `gruvbox-dark`
|
||||||
|
|
||||||
No example. It would take up too much space
|
No example. It would take up too much space
|
||||||
|
|
||||||
|
|
|
@ -10,8 +10,10 @@ import std.array;
|
||||||
import dparse.lexer;
|
import dparse.lexer;
|
||||||
|
|
||||||
// http://ethanschoonover.com/solarized
|
// http://ethanschoonover.com/solarized
|
||||||
void highlight(R)(ref R tokens, string fileName)
|
void highlight(R)(ref R tokens, string fileName, string themeName)
|
||||||
{
|
{
|
||||||
|
immutable(Theme)* theme = getTheme(themeName);
|
||||||
|
|
||||||
stdout.writeln(q"[
|
stdout.writeln(q"[
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
|
@ -20,17 +22,19 @@ void highlight(R)(ref R tokens, string fileName)
|
||||||
stdout.writeln("<title>", fileName, "</title>");
|
stdout.writeln("<title>", fileName, "</title>");
|
||||||
stdout.writeln(q"[</head>
|
stdout.writeln(q"[</head>
|
||||||
<body>
|
<body>
|
||||||
<style type="text/css">
|
<style type="text/css">]");
|
||||||
html { background-color: #fdf6e3; color: #002b36; }
|
stdout.writefln("
|
||||||
.kwrd { color: #b58900; font-weight: bold; }
|
html { background-color: %s; color: %s; }
|
||||||
.com { color: #93a1a1; font-style: italic; }
|
.kwrd { color: %s; font-weight: bold; }
|
||||||
.num { color: #dc322f; font-weight: bold; }
|
.com { color: %s; font-style: italic; }
|
||||||
.str { color: #2aa198; font-style: italic; }
|
.num { color: %s; font-weight: bold; }
|
||||||
.op { color: #586e75; font-weight: bold; }
|
.str { color: %s; font-style: italic; }
|
||||||
.type { color: #268bd2; font-weight: bold; }
|
.op { color: %s; font-weight: bold; }
|
||||||
.cons { color: #859900; font-weight: bold; }
|
.type { color: %s; font-weight: bold; }
|
||||||
|
.cons { color: %s; font-weight: bold; }
|
||||||
</style>
|
</style>
|
||||||
<pre>]");
|
<pre>", theme.bg, theme.fg, theme.kwrd, theme.com, theme.num, theme.str,
|
||||||
|
theme.op, theme.type, theme.cons);
|
||||||
|
|
||||||
while (!tokens.empty)
|
while (!tokens.empty)
|
||||||
{
|
{
|
||||||
|
@ -76,3 +80,37 @@ void writeSpan(string cssClass, string value)
|
||||||
stdout.write(`<span class="`, cssClass, `">`, value.replace("&",
|
stdout.write(`<span class="`, cssClass, `">`, value.replace("&",
|
||||||
"&").replace("<", "<"), `</span>`);
|
"&").replace("<", "<"), `</span>`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Theme
|
||||||
|
{
|
||||||
|
string bg;
|
||||||
|
string fg;
|
||||||
|
string kwrd;
|
||||||
|
string com;
|
||||||
|
string num;
|
||||||
|
string str;
|
||||||
|
string op;
|
||||||
|
string type;
|
||||||
|
string cons;
|
||||||
|
}
|
||||||
|
|
||||||
|
immutable(Theme)* getTheme(string themeName)
|
||||||
|
{
|
||||||
|
immutable Theme[string] themes = [
|
||||||
|
"solarized": Theme("#fdf6e3", "#002b36", "#b58900", "#93a1a1", "#dc322f", "#2aa198", "#586e75",
|
||||||
|
"#268bd2", "#859900"),
|
||||||
|
"solarized-dark": Theme("#002b36", "#fdf6e3", "#b58900", "#586e75", "#dc322f", "#2aa198",
|
||||||
|
"#93a1a1", "#268bd2", "#859900"),
|
||||||
|
"gruvbox": Theme("#fbf1c7", "#282828", "#b57614", "#a89984", "#9d0006", "#427b58",
|
||||||
|
"#504945", "#076678", "#79740e"),
|
||||||
|
"gruvbox-dark": Theme("#282828", "#fbf1c7", "#d79921", "#7c6f64",
|
||||||
|
"#cc241d", "#689d6a", "#a89984", "#458588", "#98971a")
|
||||||
|
];
|
||||||
|
|
||||||
|
immutable(Theme)* theme = themeName in themes;
|
||||||
|
// Default theme
|
||||||
|
if (theme is null)
|
||||||
|
theme = &themes["solarized"];
|
||||||
|
|
||||||
|
return theme;
|
||||||
|
}
|
||||||
|
|
|
@ -65,6 +65,7 @@ else
|
||||||
bool report;
|
bool report;
|
||||||
bool skipTests;
|
bool skipTests;
|
||||||
bool applySingleFixes;
|
bool applySingleFixes;
|
||||||
|
string theme;
|
||||||
string resolveMessage;
|
string resolveMessage;
|
||||||
string reportFormat;
|
string reportFormat;
|
||||||
string reportFile;
|
string reportFile;
|
||||||
|
@ -85,6 +86,7 @@ else
|
||||||
getopt(args, std.getopt.config.caseSensitive,
|
getopt(args, std.getopt.config.caseSensitive,
|
||||||
"sloc|l", &sloc,
|
"sloc|l", &sloc,
|
||||||
"highlight", &highlight,
|
"highlight", &highlight,
|
||||||
|
"theme", &theme,
|
||||||
"ctags|c", &ctags,
|
"ctags|c", &ctags,
|
||||||
"help|h", &help,
|
"help|h", &help,
|
||||||
"etags|e", &etags,
|
"etags|e", &etags,
|
||||||
|
@ -257,7 +259,7 @@ else
|
||||||
if (highlight)
|
if (highlight)
|
||||||
{
|
{
|
||||||
auto tokens = byToken(bytes, config, &cache);
|
auto tokens = byToken(bytes, config, &cache);
|
||||||
dscanner.highlighter.highlight(tokens, args.length == 1 ? "stdin" : args[1]);
|
dscanner.highlighter.highlight(tokens, args.length == 1 ? "stdin" : args[1], theme);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else if (tokenDump)
|
else if (tokenDump)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue