Unilight is a unicode string highlighter. It uses unicode variations
to highlight raw strings for almost any programming language. The output can be used anywhere that accepts unicode chars. Unilight has no dependencies, e.g.
/**
* ๐๐๐๐ค ๐๐ค ๐ ๐ก๐ฆ๐ฃ๐ ๐ค๐ฅ๐ฃ๐๐๐ ๐ ๐ฆ๐ฅ๐ก๐ฆ๐ฅ, ๐ช๐ ๐ฆ ๐๐๐ ๐ก๐๐ค๐ฅ๐ ๐๐ฅ ๐๐๐ช๐จ๐๐๐ฃ๐
* ๐ฅ๐๐๐ฅ ๐ค๐ฆ๐ก๐ก๐ ๐ฃ๐ฅ๐ค ๐ฆ๐๐๐๐ ๐๐ ๐๐๐ ๐ฅ๐๐ ๐๐๐๐๐๐๐๐๐ฅ๐ค ๐จ๐๐๐ ๐๐ ๐ก๐ฃ๐๐ค๐๐ฃ๐ง๐๐
**/
๐ณ๐๐ป๐ฐ๐๐ถ๐ผ๐ป example() {
๐ฐ๐ผ๐ป๐๐ a = "fizz";
๐ฐ๐ผ๐ป๐๐ b = "buzz";
// ๐๐๐๐ค ๐๐ค ๐๐ ๐๐๐๐๐๐ ๐๐ ๐๐๐๐๐ฅ
๐ฟ๐ฒ๐๐๐ฟ๐ป `${a}${b}`;
}
If you want to simply highlight a code string to use it anywhere, you can use the GUI tool available in this link: https://felippe-regazio.github.io/unilight/
If you want to highlight strings programatically, create your own highlighter, parse a source code string and modify its tokens, derive a tool from this library or anything else, keep reading this documentation.
First install the unilight as a dependency
npm install unilight
Then require or import unilight
(or just the highlight
method). Call the highlight
method passing a collection of keywords to be highlighted. Styles for comments, strings and ponctuation will be automatically inferred:
const { highlight } = require('unilight');
const code = `
// This is an example
function example() {
const a = "fizz";
const b = "buzz";
return a+b;
}
`;
const uTextHighlighted = highlight(code, [
'function',
'const',
'return',
'var',
'let',
// ...
]);
The output will be:
// ๐๐๐๐ค ๐๐ค ๐๐ ๐๐ฉ๐๐๐ก๐๐
๐ณ๐๐ป๐ฐ๐๐ถ๐ผ๐ป example() {
๐ฐ๐ผ๐ป๐๐ a = "fizz";
๐ฐ๐ผ๐ป๐๐ b = "buzz";
๐ฟ๐ฒ๐๐๐ฟ๐ป a+b;
}
Unilight is structured in 3 different modules: the parser
, the formatter
and the highlighter
:
The parser can parse almost any source code in any language. It receives a string input, a keyword list and some formatting callbacks. It will parse the source code string and run a callback for every diffent kind of token found. For each token type there will be a formatting callback
which you can use to modify the token output. Unilight will identify 5 different token types:
- Neutral (no formatting)
- Keyword
- Ponctuation
- String
- Comment
You can import the parser from unilight and use like this:
const { parse } = require('unilight');
const code = `
// THIS IS AN EXAMPLE
function example() {
const a = "fizz";
const b = "buzz";
return a+b;
}
`;
parse(code, {
keywords: [ 'function', 'const', 'return' ],
formatKeyword(t) {
// this callback will be called for all the keywords
// defined in { keywords }. you can return a modified
// version of the string found to modify the output.
// this is the general rule for all the other callbacks.
// for example, lets output all keywords in uppercase:
return t.toUpperCase();
},
formatPonctuation(t) {
// no formatting
return t;
},
formatString(t) {
// no formatting
return t;
},
formatComment(t) {
// comments will be lowercased
return t.toLowerCase();
}
});
The output would be:
// this is an example
FUNCTION example() {
CONST a = "fizz";
CONST b = "buzz";
RETURN a+b;
}
The parser was orinally developed by Helios (ASVD) for microlight
lib and slightly modified to be used on this library.
The formatter is a module that can convert a normal unicode string to some variation like bold
, italic
, outlined
and others. It was originally developed by David Konrad
as toUnicodeVariant
lib. Unilight uses a modified version on this library. You can check the original source of the formatter and its usage here: https://github.com/davidkonrad/toUnicodeVariant
To use toUnicodeVariant
on unilight
you must do:
const { format } = require('unilight');
const result = format('This is an example', 'd');
console.log(result);
The output will be:
๐๐๐๐ค ๐๐ค ๐๐ ๐๐ฉ๐๐๐ก๐๐
On the code above, d
tells the formatter which variation to use while formating the string. The available variations are:
Variant | Alias | Description | Example |
---|---|---|---|
monospace | m | Monospace | ๐๐๐๐๐๐๐๐๐ |
bold | b | Bold text | ๐๐จ๐ฅ๐ |
italic | i | Italic text | ๐๐ก๐๐๐๐ |
bold italic | bi | bold+italic text | ๐๐๐๐ ๐๐๐๐๐๐ |
script | c | Handwriting style | ๐๐ธ๐๐พ๐ ๐ |
bold script | bc | Bolder handwriting | ๐ซ๐ธ๐ต๐ญ ๐ผ๐ฌ๐ป๐ฒ๐น๐ฝ |
gothic | g | Gothic (fraktur) | ๐ค๐ฌ๐ฑ๐ฅ๐ฆ๐ |
gothic bold | gb | Gothic in bold | ๐๐๐๐๐๐ ๐๐๐๐ |
doublestruck | d | Outlined text | ๐๐ ๐ฆ๐๐๐๐ค๐ฅ๐ฃ๐ฆ๐๐ |
๐๐บ๐๐ | s | Sans-serif style | ๐๐บ๐๐ |
bold ๐๐บ๐๐ | bs | Bold sans-serif | ๐ฏ๐ผ๐น๐ฑ ๐๐ฎ๐ป๐ |
italic ๐๐บ๐๐ | is | Italic sans-serif | ๐ช๐ต๐ข๐ญ๐ช๐ค ๐ด๐ข๐ฏ๐ด |
bold italic sans | bis | Bold italic sans-serif | ๐๐ค๐ก๐ ๐๐ฉ๐๐ก๐๐ ๐จ๐๐ฃ๐จ |
circled | o | Letters within circles | โโโกโโโโ |
circled negative | on | -- negative | ๐ ๐ ๐ ก๐ ๐ ๐ ๐ |
squared | q | Letters within squares | ๐ ๐ ๐ ๐ฐ๐ ๐ด๐ณ |
squared negative | qn | -- negative | ๐๐๐๐ ฐ๐๐ ด๐ ณ |
paranthesis | p | Letters within paranthesis | โซโโญโ โฉโฏโฃโ โฎโค |
fullwidth | w | Wider monospace font | ๏ฝ๏ฝ๏ฝ๏ฝ๏ฝ๏ฝ๏ฝ๏ฝ๏ฝ |
The unilight highlighter is just a pre-configured parser
and formatter
that you can use to highlight raw strings. You can use the highlighter by import the highlight
function from unilight
.
const { highlight } = require('unilight');
const output = highlight(code, [ 'function', 'const', 'var', /** goes on... **/ ]);
THe unicode formatting for keywords and comments will be automatically applied. You can also create your own highlighter.
Use the snippet below to create your own Highlighter. You dont have to rely on unicode variations, you can modify the tokens as you prefer. This would be a Rich Code Highlighter
to be used on an HTML page for example:
const { parse, format } = require('unilight');
function customHighlighter(text) {
return parse(text, {
keywords: [ 'function', 'return' ],
formatKeyword(t) {
return `<span class="highlight-keyword">${t}</span>`;
},
formatPonctuation(t) {
return `<span class="highlight-ponctuation">${t}</span>`;
},
formatString(t) {
return `<span class="highlight-string">${t}</span>`;
},
formatComment(t) {
return `<span class="highlight-comment">${t}</span>`;
}
});
}
// usage
const output = customHighlighter(`
function example() {
console.log("This is just an example");
return true;
}
`);
The code above will find tokens and surround them with a span
identified by a custom class depending on the token type. You can use this class to style the output on an HTML page.
Unilight is a colletion of modules that builds to an UMD under the global name unilight
using parcel bundler.
Before start, install dev dependencies
npm install
npm run build
npm run test
npm run start
This file is used by the GUI Editor (index.html) to serve different collections of keywords for different programming languages.
Just open the keywords.js
file on the project root, add a new property with the language name you want to add, and an array of strings containing each keyword you want to be highlighted.
This is a toy project and it was made for aesthetics only. Some of the caveats of a unicode highlighter are:
- Environments that don't support unicode can show a broken text
- Since unicode variations are basically hidden chars, the resulting string may be longer than it looks
- Interpreters won't run the code because the keywords will not be recognized
- The unicode characters may vary from one system to another
Unilight was written by Felippe Regazio.
Special thanks to David Konrad, which created the module Unicode Variations
that can modify a unicode string and apply new variations. This is used by Unilight to highlight the string.
Special thanks to Helios (ASVD) that created Microlight Highlighter. His parsing techiniques
were a strong inspiration and direct reference to Unilight parser, which uses part of his code.