@speed-highlight/core 1.1.7 → 1.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. package/README.md +56 -9
  2. package/doc/common.js.html +87 -0
  3. package/doc/css/jsdoc-template.css +1 -0
  4. package/doc/css/tomorrow.min.css +2 -0
  5. package/doc/detect%0ALanguage%20detectormodule_.html +309 -0
  6. package/doc/detect.js.html +113 -0
  7. package/doc/global.html +1826 -0
  8. package/doc/index%0ABase%20scriptmodule_.html +1179 -0
  9. package/doc/index.html +402 -0
  10. package/doc/index.js.html +209 -0
  11. package/doc/js/jsdoc-template.js +2 -0
  12. package/doc/js/jsdoc-template.js.map +1 -0
  13. package/doc/js/prettify.min.js +47 -0
  14. package/doc/term%0ATerminal%20adaptormodule_.html +596 -0
  15. package/doc/term.js.html +108 -0
  16. package/examples/{helper/data.js → data.js} +28 -16
  17. package/examples/deno.js +53 -7
  18. package/examples/index.html +10 -7
  19. package/examples/languages/test.c +1 -1
  20. package/examples/languages/test.go +4 -4
  21. package/examples/languages/test.java +3 -3
  22. package/examples/node.js +1 -1
  23. package/jsdoc.json +16 -0
  24. package/package.json +42 -38
  25. package/src/color.js +11 -0
  26. package/src/detect.js +21 -5
  27. package/src/index.js +44 -38
  28. package/src/languages/c.js +1 -1
  29. package/src/languages/go.js +1 -1
  30. package/src/languages/java.js +1 -1
  31. package/src/languages/js.js +1 -1
  32. package/src/languages/py.js +1 -1
  33. package/src/languages/rs.js +1 -1
  34. package/src/terminal.js +42 -0
  35. package/src/themes/atom-dark.js +18 -0
  36. package/src/themes/default.js +16 -24
  37. package/typedef.js +34 -0
  38. package/assets/screenshot.png +0 -0
  39. package/dist/common.js +0 -1
  40. package/dist/detect.js +0 -1
  41. package/dist/index.js +0 -3
  42. package/dist/languages/asm.js +0 -1
  43. package/dist/languages/bash.js +0 -1
  44. package/dist/languages/bf.js +0 -1
  45. package/dist/languages/c.js +0 -1
  46. package/dist/languages/css.js +0 -1
  47. package/dist/languages/csv.js +0 -1
  48. package/dist/languages/diff.js +0 -1
  49. package/dist/languages/docker.js +0 -1
  50. package/dist/languages/git.js +0 -1
  51. package/dist/languages/go.js +0 -1
  52. package/dist/languages/html.js +0 -1
  53. package/dist/languages/http.js +0 -1
  54. package/dist/languages/ini.js +0 -1
  55. package/dist/languages/java.js +0 -1
  56. package/dist/languages/js.js +0 -1
  57. package/dist/languages/js_template_literals.js +0 -1
  58. package/dist/languages/jsdoc.js +0 -1
  59. package/dist/languages/json.js +0 -1
  60. package/dist/languages/leanpub-md.js +0 -2
  61. package/dist/languages/log.js +0 -1
  62. package/dist/languages/lua.js +0 -1
  63. package/dist/languages/make.js +0 -1
  64. package/dist/languages/md.js +0 -2
  65. package/dist/languages/pl.js +0 -1
  66. package/dist/languages/plain.js +0 -1
  67. package/dist/languages/py.js +0 -1
  68. package/dist/languages/regex.js +0 -1
  69. package/dist/languages/rs.js +0 -1
  70. package/dist/languages/sql.js +0 -1
  71. package/dist/languages/todo.js +0 -1
  72. package/dist/languages/toml.js +0 -1
  73. package/dist/languages/ts.js +0 -1
  74. package/dist/languages/uri.js +0 -1
  75. package/dist/languages/xml.js +0 -1
  76. package/dist/languages/yaml.js +0 -1
  77. package/dist/node/common.js +0 -1
  78. package/dist/node/detect.js +0 -1
  79. package/dist/node/index.js +0 -3
  80. package/dist/node/languages/asm.js +0 -1
  81. package/dist/node/languages/bash.js +0 -1
  82. package/dist/node/languages/bf.js +0 -1
  83. package/dist/node/languages/c.js +0 -1
  84. package/dist/node/languages/css.js +0 -1
  85. package/dist/node/languages/csv.js +0 -1
  86. package/dist/node/languages/diff.js +0 -1
  87. package/dist/node/languages/docker.js +0 -1
  88. package/dist/node/languages/git.js +0 -1
  89. package/dist/node/languages/go.js +0 -1
  90. package/dist/node/languages/html.js +0 -1
  91. package/dist/node/languages/http.js +0 -1
  92. package/dist/node/languages/ini.js +0 -1
  93. package/dist/node/languages/java.js +0 -1
  94. package/dist/node/languages/js.js +0 -1
  95. package/dist/node/languages/js_template_literals.js +0 -1
  96. package/dist/node/languages/jsdoc.js +0 -1
  97. package/dist/node/languages/json.js +0 -1
  98. package/dist/node/languages/leanpub-md.js +0 -2
  99. package/dist/node/languages/log.js +0 -1
  100. package/dist/node/languages/lua.js +0 -1
  101. package/dist/node/languages/make.js +0 -1
  102. package/dist/node/languages/md.js +0 -2
  103. package/dist/node/languages/pl.js +0 -1
  104. package/dist/node/languages/plain.js +0 -1
  105. package/dist/node/languages/py.js +0 -1
  106. package/dist/node/languages/regex.js +0 -1
  107. package/dist/node/languages/rs.js +0 -1
  108. package/dist/node/languages/sql.js +0 -1
  109. package/dist/node/languages/todo.js +0 -1
  110. package/dist/node/languages/toml.js +0 -1
  111. package/dist/node/languages/ts.js +0 -1
  112. package/dist/node/languages/uri.js +0 -1
  113. package/dist/node/languages/xml.js +0 -1
  114. package/dist/node/languages/yaml.js +0 -1
  115. package/dist/node/term.js +0 -1
  116. package/dist/node/themes/default.js +0 -1
  117. package/dist/term.js +0 -1
  118. package/dist/themes/atom-dark.css +0 -1
  119. package/dist/themes/dark.css +0 -1
  120. package/dist/themes/default.css +0 -1
  121. package/dist/themes/default.js +0 -1
  122. package/dist/themes/github-dark.css +0 -1
  123. package/dist/themes/github-dim.css +0 -1
  124. package/dist/themes/github-light.css +0 -1
  125. package/dist/themes/visual-studio-dark.css +0 -1
  126. package/examples/dist.html +0 -26
  127. package/src/term.js +0 -19
@@ -1,51 +1,63 @@
1
+ import '../typedef.js'
2
+
3
+ /** @type {ShjLanguage[]} */
1
4
  export const languages = [
5
+ 'js',
6
+ 'py',
2
7
  'bash',
3
- 'bf',
8
+ 'ts',
9
+ 'c',
4
10
  'css',
11
+ 'asm',
5
12
  'csv',
6
13
  'diff',
14
+ 'docker',
7
15
  'git',
16
+ 'go',
8
17
  'html',
9
18
  'http',
10
19
  'ini',
11
- 'js',
20
+ 'java',
12
21
  'jsdoc',
13
22
  'json',
14
23
  'leanpub-md',
24
+ 'bf',
15
25
  'log',
16
26
  'lua',
17
27
  'make',
18
28
  'md',
19
29
  'pl',
20
30
  'plain',
21
- 'py',
22
31
  'regex',
32
+ 'rs',
23
33
  'sql',
24
34
  'todo',
25
35
  'toml',
26
- 'ts',
27
36
  'uri',
28
- 'yaml',
29
- 'docker',
30
37
  'xml',
31
- 'c',
32
- 'rs',
33
- 'go',
34
- 'java',
35
- 'asm'
36
- ].sort();
37
- export const themes = [
38
+ 'yaml'
39
+ ]
40
+
41
+ /** @type {ShjBrowserTheme[]} */
42
+ export const themesBrowser = [
43
+ 'default',
38
44
  'atom-dark',
39
45
  'github-dark',
40
46
  'github-dim',
41
47
  'dark',
42
- 'default',
43
48
  'github-light',
44
49
  'visual-studio-dark'
45
- ];
50
+ ]
51
+
52
+ /** @type {ShjTerminalTheme[]} */
53
+ export const themesTerminal = [
54
+ 'default',
55
+ 'atom-dark'
56
+ ]
57
+
46
58
  export const testFiles = [
47
59
  ...languages,
48
60
  'oneline.bash',
49
61
  'oneline.http',
50
62
  'screenshot.js'
51
- ];
63
+ ]
package/examples/deno.js CHANGED
@@ -1,14 +1,60 @@
1
- import { setTheme, printHighlight } from '../src/term.js';
1
+ import { languages, themesTerminal } from './data.js'
2
+ import { setTheme, printHighlight } from '../src/terminal.js';
2
3
  import { fromFileUrl } from 'https://deno.land/std/path/mod.ts';
4
+ import { parse } from "https://deno.land/std/flags/mod.ts"
3
5
 
4
- const lang = Deno.args[0] ?? 'js';
6
+ let args = parse(Deno.args)
5
7
 
6
- const path = fromFileUrl(import.meta.url.replace(/[^\\\/]+$/, `languages/test.${lang}`))
8
+ if (args.help)
9
+ {
10
+ console.log(`Usage deno run --allow-read deno.js [OPTION]... [TEST_FILE]`)
11
+ console.log(``)
12
+ console.log(` --help display this help and exit`)
13
+ console.log(` --theme=[-]THEME change the used THEME`)
14
+ console.log(` --lang=[-]LANGUAGE change the used LANGUAGE`)
15
+ console.log(` --stdin highlight code given from the stdin`)
16
+ console.log(``)
17
+ console.log(`THEME may be:`)
18
+ console.log(`${themesTerminal.join(', ')}`)
19
+ console.log(`LANGUAGE may be:`)
20
+ console.log(`${languages.join(', ')}`)
21
+ Deno.exit(0)
22
+ }
23
+
24
+ if (args.theme)
25
+ {
26
+ if (!themesTerminal.includes(args.theme))
27
+ {
28
+ console.error(`'${args.theme}' is not a supported try on of the following theme:`)
29
+ console.log(`${themesTerminal.join(', ')}`)
30
+ Deno.exit(1)
31
+ }
32
+ await setTheme(args.theme)
33
+ }
34
+
35
+ if (args.lang && !languages.includes(args.lang))
36
+ {
37
+ console.error(`'${args.lang}' is not a supported try on of the following languages:`)
38
+ console.log(`${languages.join(', ')}`)
39
+ Deno.exit(1)
40
+ }
41
+
42
+ if (args.stdin)
43
+ {
44
+ const buf = new Uint8Array(1024)
45
+ let n;
46
+ do {
47
+ n = await Deno.stdin.read(buf)
48
+ const code = new TextDecoder().decode(buf.subarray(0, n))
49
+ await printHighlight(code, args.lang ?? 'js');
50
+ } while (n)
51
+ Deno.exit(0)
52
+ }
53
+
54
+ const absolutePath = args._[0] ?? fromFileUrl(import.meta.url.replace(/[^\\\/]+$/, './languages/test.js'))
7
55
  .replace(Deno.cwd(), '')
8
56
  .slice(1)
9
57
 
10
- const code = await Deno.readTextFile(path);
11
-
12
- await setTheme('default');
58
+ const code = await Deno.readTextFile(absolutePath);
13
59
 
14
- printHighlight(code, lang);
60
+ printHighlight(code, args.lang ?? args._[0]?.split?.('.')?.[1] ?? 'js');
@@ -7,11 +7,14 @@
7
7
  <title>Languages | Speed-highlight JS</title>
8
8
  <style>
9
9
  @import url('https://fonts.googleapis.com/css2?family=Fira+Code:wght@400&display=swap');
10
+ main {
11
+ max-width: 1000px;
12
+ margin: 0 auto
13
+ }
10
14
  .fira {font: 18px 'Fira Code', monospace !important}
11
15
 
12
16
  body {margin: 0}
13
17
  html {background: #001220}
14
- main {margin: 10px}
15
18
  h1 {
16
19
  text-align: center;
17
20
  font: bold 60px Arial;
@@ -56,7 +59,7 @@
56
59
  <script type="module">
57
60
  import { highlightElement } from '../src/index.js'
58
61
  import { detectLanguage } from '../src/detect.js'
59
- import { themes, testFiles } from './helper/data.js'
62
+ import { themesBrowser, testFiles } from './data.js'
60
63
 
61
64
  const main = document.querySelector('main')
62
65
  const elm = (type, opt) => Object.assign(document.createElement(type), opt)
@@ -86,8 +89,8 @@
86
89
 
87
90
  async function highlight(language) {
88
91
  let path = language.includes('.') ? language : `test.${language}`,
89
- txt = code.textContent = await (await fetch(`./languages/${path}`)).text()
90
- let start = performance.now()
92
+ txt = code.textContent = await (await fetch(`./languages/${path}`)).text(),
93
+ start = performance.now()
91
94
  await highlightElement(code, detect ? detectLanguage(txt) : path.split('.')[1], undefined, { hideLineNumbers })
92
95
  chrono.textContent = `${(performance.now() - start).toFixed(3)}ms (counting loading time)`
93
96
  chrono.animate([ { background: '#25f3' }, { background: 'unset' }],{ duration: 150 })
@@ -103,13 +106,13 @@
103
106
 
104
107
  document.head.append(stylesheet)
105
108
 
106
- new Radio('theme', theme => stylesheet.href = `../src/themes/${theme}.css`, themes)
109
+ new Radio('theme', theme => stylesheet.href = `../src/themes/${theme}.css`, themesBrowser)
107
110
  new Checkbox('detect language', on => { detect = on; highlight(lang.selected) }, container)
108
111
  new Checkbox('header mode', mode => code.classList.toggle('shj-mode-header', mode), container)
109
112
  new Checkbox('use fira font', use => code.classList.toggle('fira', use), container).on = true
110
- new Checkbox('hide line numbering', ln => { hideLineNumbers = ln; highlight(lang.selected) }, container)
113
+ new Checkbox('hide line numbering', ln => { hideLineNumbers = ln; highlight(lang.selected) }, container).on = true
111
114
 
112
115
  main.append(container, chrono, code)
113
116
  </script>
114
117
  </body>
115
- </html>
118
+ </html>
@@ -30,7 +30,7 @@ int main(int argc, char **argv)
30
30
  {
31
31
  printf("Hello World!\n");
32
32
 
33
- Point p;
33
+ Point pTest;
34
34
 
35
35
  asm("movl $1, %0" : "=r"(p.x));
36
36
  asm {
@@ -7,15 +7,15 @@ import "fmt"
7
7
  // main is the entry point for the program
8
8
  func main() {
9
9
  // declare a variable named num and assign it the value of 10
10
- num := 10
10
+ numTest := 10
11
11
 
12
12
  // sanity check
13
- fmt.Println("num is", num)
13
+ fmt.Println("num is", numTest)
14
14
 
15
15
  //scan from the console and assign the value to the variable num
16
- fmt.Scanln(&num)
16
+ fmt.Scanln(&numTest)
17
17
 
18
18
 
19
19
  // print the value of num
20
- fmt.Println(num)
20
+ fmt.Println(numTest)
21
21
  }
@@ -17,7 +17,7 @@ class Token {
17
17
 
18
18
  //lexer function that take a string and return a list of tokens
19
19
  List<Token> lexer(String s) {
20
- List<Token> tokens = new ArrayList<Token>();
20
+ List<Token> tokensList = new ArrayList<Token>();
21
21
  int line = 1;
22
22
  int column = 0;
23
23
  int i = 0;
@@ -47,6 +47,6 @@ void run(List<Token>) {
47
47
  //main function
48
48
  public static void main(String[] args) {
49
49
  String s = "1+2+3";
50
- List<Token> tokens = lexer(s);
51
- run(tokens);
50
+ List<Token> tokensList = lexer(s);
51
+ run(tokensList);
52
52
  }
package/examples/node.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const fs = require('fs'),
2
2
  path = require('path'),
3
- { setTheme, printHighlight } = require('../dist/node/term.js');
3
+ { setTheme, printHighlight } = require('@speed-highlight/core/terminal');
4
4
 
5
5
  const lang = process.argv[2] ?? 'js';
6
6
 
package/jsdoc.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "plugins": [],
3
+ "recurseDepth": 10,
4
+ "source": {
5
+ "includePattern": ".+\\.js$"
6
+ },
7
+ "sourceType": "module",
8
+ "tags": {
9
+ "allowUnknownTags": true,
10
+ "dictionaries": ["jsdoc", "closure"]
11
+ },
12
+ "templates": {
13
+ "cleverLinks": false,
14
+ "monospaceLinks": false
15
+ }
16
+ }
package/package.json CHANGED
@@ -1,40 +1,44 @@
1
1
  {
2
- "name": "@speed-highlight/core",
3
- "version": "1.1.7",
4
- "description": "🌈 Light, fast, and easy to use, dependencies free javascript syntax highlighter, with automatic language detection",
5
- "main": "dist/node/index.js",
6
- "directories": {
7
- "example": "examples"
8
- },
9
- "scripts": {
10
- "test": "echo \"Error: no test specified\" && exit 1"
11
- },
12
- "repository": {
13
- "type": "git",
14
- "url": "git://github.com/speed-highlight/core.git"
15
- },
16
- "keywords": [
17
- "javascript",
18
- "syntax-highlighting",
19
- "language",
20
- "fast",
21
- "js",
22
- "simple",
23
- "highlighter",
24
- "regex",
25
- "highlighting",
26
- "highlightjs",
27
- "small",
28
- "deno"
29
- ],
30
- "author": "matubu",
31
- "license": "CC0-1.0",
32
- "bugs": {
33
- "url": "https://github.com/speed-highlight/core/issues"
34
- },
35
- "homepage": "https://github.com/speed-highlight/core#readme",
36
- "publishConfig": {
37
- "registry": "https://npm.pkg.github.com/",
38
- "access": "public"
39
- }
2
+ "name": "@speed-highlight/core",
3
+ "version": "1.1.8",
4
+ "description": "🌈 Light, fast, and easy to use, dependencies free javascript syntax highlighter, with automatic language detection",
5
+ "main": "dist/node/index.js",
6
+ "exports": {
7
+ "./terminal": "./dist/node/terminal.js",
8
+ "./terminal.js": "./dist/node/terminal.js"
9
+ },
10
+ "directories": {
11
+ "example": "examples"
12
+ },
13
+ "scripts": {
14
+ "build": "./.github/workflows/minify.sh"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git://github.com/speed-highlight/core.git"
19
+ },
20
+ "keywords": [
21
+ "javascript",
22
+ "syntax-highlighting",
23
+ "language",
24
+ "fast",
25
+ "js",
26
+ "simple",
27
+ "highlighter",
28
+ "regex",
29
+ "highlighting",
30
+ "highlightjs",
31
+ "small",
32
+ "deno"
33
+ ],
34
+ "author": "matubu",
35
+ "license": "CC0-1.0",
36
+ "bugs": {
37
+ "url": "https://github.com/speed-highlight/core/issues"
38
+ },
39
+ "homepage": "https://github.com/speed-highlight/core#readme",
40
+ "publishConfig": {
41
+ "registry": "https://npm.pkg.github.com/",
42
+ "access": "public"
43
+ }
40
44
  }
package/src/color.js ADDED
@@ -0,0 +1,11 @@
1
+ export default {
2
+ black: '\x1b[30m',
3
+ red: '\x1b[31m',
4
+ green: '\x1b[32m',
5
+ gray: '\x1b[90m',
6
+ yellow: '\x1b[33m',
7
+ blue: '\x1b[34m',
8
+ magenta: '\x1b[35m',
9
+ cyan: '\x1b[36m',
10
+ white: '\x1b[37m'
11
+ }
package/src/detect.js CHANGED
@@ -1,3 +1,13 @@
1
+ /**
2
+ * @module detect
3
+ * (Language detector)
4
+ */
5
+
6
+ import '../typedef.js'
7
+
8
+ /**
9
+ * @type {Array.<[ShjLanguage, ...[RegExp, Number][]]>}
10
+ */
1
11
  const languages = [
2
12
  ['bash', [/#!(\/usr)?\/bin\/bash/g, 500], [/\b(if|elif|then|fi|echo)\b|\$/g, 10]],
3
13
  ['html', [/<\/?[a-z-]+[^\n>]*>/g, 10], [/^\s+<!DOCTYPE\s+html/g, 500]],
@@ -22,12 +32,18 @@ const languages = [
22
32
  ['asm', [/^(section|global main|extern|\t(call|mov|ret))/gm, 100]],
23
33
  ]
24
34
 
35
+ /**
36
+ * @function detectLanguage
37
+ * Try to find the language the given code belong to
38
+ * @param {String} code The code
39
+ * @returns {ShjLanguage} The language of the code
40
+ */
25
41
  export const detectLanguage = code => {
26
42
  return (languages
27
43
  .map(([lang, ...features]) => [
28
- features.reduce((acc, [match, score]) => acc + [...code.matchAll(match)].length * score, 0),
29
- lang
44
+ lang,
45
+ features.reduce((acc, [match, score]) => acc + [...code.matchAll(match)].length * score, 0)
30
46
  ])
31
- .filter(([score, lang]) => score > 10)
32
- .sort((a, b) => b[0] - a[0])[0]?.[1] || 'plain');
33
- }
47
+ .filter(([lang, score]) => score > 20)
48
+ .sort((a, b) => b[1] - a[1])[0]?.[0] || 'plain');
49
+ }
package/src/index.js CHANGED
@@ -1,28 +1,34 @@
1
1
  /**
2
- * Base script
2
+ * @module index
3
+ * (Base script)
3
4
  */
4
5
 
6
+ import '../typedef.js'
7
+
5
8
  import expandData from './common.js';
6
9
 
7
10
  const langs = {},
8
11
  sanitize = (str = '') =>
9
12
  str.replaceAll('&', '&#38;').replaceAll?.('<', '&lt;').replaceAll?.('>', '&gt;'),
10
13
  /**
11
- * A function that turn text in HTML to apply a class to it
12
- * @param {String} str need to be sanitize to be safe
13
- * @param {String} [token] token type
14
- * @returns A string with HTML semantics
14
+ * @function
15
+ * @ignore
16
+ * Create a HTML element with the right token styling
17
+ * @param {String} str The content (need to be sanitized)
18
+ * @param {ShjToken} [token] The type of token
19
+ * @returns A HMTL string
15
20
  */
16
21
  toSpan = (str, token) => token ? `<span class="shj-syn-${token}">${str}</span>` : str;
17
22
 
18
23
  /**
19
- * A
20
- * @param {String} src
21
- * @param {String|Object} lang
22
- * @param {Function} token the callback that will be call for every token found.
23
- * the arguments pass will be:
24
- * * the token as a string (can be an empty string)
25
- * * the token type also as a string like 'err', 'cmnt', ...
24
+ * @function tokenize
25
+ * Find the tokens in the given code and call the callback
26
+ * @param {String} src The code
27
+ * @param {ShjLanguage|Array} lang The language of the code
28
+ * @param {function(String, ShjToken=):void} token The callback function
29
+ * this function will be given
30
+ * * the text of the token
31
+ * * the type of the token
26
32
  */
27
33
  export async function tokenize(src, lang, token) {
28
34
  try {
@@ -79,20 +85,16 @@ export async function tokenize(src, lang, token) {
79
85
  }
80
86
 
81
87
  /**
82
- * @typedef {Object} HighlightOptions
83
- * @property {boolean} [hideLineNumbers=false] - Indicates whether to hide line numbers.
84
- */
85
-
86
- /**
88
+ * @function highlightText
87
89
  * @async
88
- * @function highlightText A function that highlight a string text and return it
90
+ * Highlight a string passed as argument and return it
89
91
  * @example
90
92
  * elm.innerHTML = await highlightText(code, 'js');
91
- * @param {String} src the text content to be highlighted
92
- * @param {String} lang the lang name ex: 'js'
93
- * @param {Boolean} [multiline=true] inline mode
94
- * @param {HighlightOptions} [opt={}] other options
95
- * @returns {String} the highlighted as String text
93
+ * @param {String} src The code
94
+ * @param {ShjLanguage} lang The language of the code
95
+ * @param {Boolean} [multiline=true] If it is multiline, it will add a wrapper for the line numbering and header
96
+ * @param {ShjOptions} [opt={}] Customization options
97
+ * @returns {Promise<String>} The highlighted string
96
98
  */
97
99
  export async function highlightText(src, lang, multiline = true, opt = {}) {
98
100
  let tmp = ''
@@ -104,12 +106,13 @@ export async function highlightText(src, lang, multiline = true, opt = {}) {
104
106
  }
105
107
 
106
108
  /**
109
+ * @function highlightElement
107
110
  * @async
108
- * @function highlightElement highlight a element code with a 'pre' parent
109
- * @param {HTMLElement} elm the code elm
110
- * @param {String} [lang] the lang used for syntax highlighting by default is found in the className of the parent or the elm it self
111
- * @param {String} [mode] can be set to inline multiline or oneline by default it's auto detected: inline if `code` tag else depends of the number of lines
112
- * @param {HighlightOptions} [opt={}] other options
111
+ * Highlight a DOM element by getting the new innerHTML with highlightText
112
+ * @param {Element} elm The DOM element
113
+ * @param {ShjLanguage} [lang] The language of the code (seaching by default on `elm` for a 'shj-lang-' class)
114
+ * @param {ShjDisplayMode} [mode] The display mode (guessed by default)
115
+ * @param {ShjOptions} [opt={}] Customization options
113
116
  */
114
117
  export async function highlightElement(elm, lang = elm.className.match(/shj-lang-([\w-]+)/)?.[1], mode, opt) {
115
118
  let txt = elm.textContent;
@@ -120,19 +123,22 @@ export async function highlightElement(elm, lang = elm.className.match(/shj-lang
120
123
  }
121
124
 
122
125
  /**
123
- * @async
124
126
  * @function highlightAll
125
- * for all element that have a class name starting with "shj-lang-"
126
- * this function will call highlightElement with the html element as argument
127
- * The function will select those scheme for example:
128
- * ```html
129
- * <div class='shj-lang-[code-language]'>[code]</div>
130
- * or
131
- * <code class='shj-lang-[code-language]'>[inline code]</code>
132
- * ```
133
- * @param {HighlightOptions} [opt={}] other options
127
+ * @async
128
+ * Call highlightElement on element with a css class starting with `shj-lang-`
129
+ * @param {ShjOptions} [opt={}] Customization options
134
130
  */
135
131
  export let highlightAll = async (opt) =>
136
132
  document
137
133
  .querySelectorAll('[class*="shj-lang-"]')
138
134
  .forEach(elm => highlightElement(elm, undefined, undefined, opt))
135
+
136
+ /**
137
+ * @function loadLanguage
138
+ * Load a language and add it to the langs object
139
+ * @param {String} languageName The name of the language
140
+ * @param {ShjLanguage} language The language
141
+ */
142
+ export let loadLanguage = (languageName, language) => {
143
+ langs[languageName] = language;
144
+ }
@@ -47,6 +47,6 @@ export default [
47
47
  },
48
48
  {
49
49
  type: 'class',
50
- match: /[A-Z][\w_]*/g
50
+ match: /\b[A-Z][\w_]*\b/g
51
51
  }
52
52
  ]
@@ -19,7 +19,7 @@ export default [
19
19
  },
20
20
  {
21
21
  type: 'class',
22
- match: /[A-Z][\w_]*/g
22
+ match: /\b[A-Z][\w_]*\b/g
23
23
  },
24
24
  {
25
25
  type: 'oper',
@@ -23,6 +23,6 @@ export default [
23
23
  },
24
24
  {
25
25
  type: 'class',
26
- match: /[A-Z][\w_]*/g
26
+ match: /\b[A-Z][\w_]*\b/g
27
27
  }
28
28
  ]
@@ -39,7 +39,7 @@ export default [
39
39
  },
40
40
  {
41
41
  type: 'class',
42
- match: /\b[A-Z]\w*\b/g
42
+ match: /\b[A-Z][\w_]*\b/g
43
43
  },
44
44
  {
45
45
  type: 'func',
@@ -47,6 +47,6 @@ export default [
47
47
  },
48
48
  {
49
49
  type: 'class',
50
- match: /\b[A-Z]\w*\b/g
50
+ match: /\b[A-Z][\w_]*\b/g
51
51
  }
52
52
  ]
@@ -19,7 +19,7 @@ export default [
19
19
  },
20
20
  {
21
21
  type: 'class',
22
- match: /\b[A-Z]\w*\b/g
22
+ match: /\b[A-Z][\w_]*\b/g
23
23
  },
24
24
  {
25
25
  type: 'func',
@@ -0,0 +1,42 @@
1
+ /**
2
+ * @module terminal
3
+ * (Terminal adaptor)
4
+ */
5
+
6
+ import '../typedef.js'
7
+
8
+ import { tokenize } from './index.js';
9
+
10
+ let theme = import('./themes/default.js');
11
+
12
+ /**
13
+ * @function highlightText
14
+ * @async
15
+ * Highlight a string passed as argument and return a string that can directly be printed
16
+ * @param {String} src The code
17
+ * @param {ShjLanguage} lang The language of the code
18
+ * @returns {Promise<String>} The highlighted string
19
+ */
20
+ export const highlightText = async (src, lang) => {
21
+ let res = '', themeMap = (await theme).default;
22
+
23
+ await tokenize(src, lang, (str, token) => res += token ? `${themeMap[token] ?? ''}${str}\x1b[0m` : str);
24
+
25
+ return res;
26
+ };
27
+
28
+ /**
29
+ * @function printHighlight
30
+ * @async
31
+ * Highlight and print a given string
32
+ * @param {String} src The code
33
+ * @param {ShjLanguage} lang The language of the code
34
+ */
35
+ export const printHighlight = async (src, lang) => console.log(await highlightText(src, lang));
36
+
37
+ /**
38
+ * @function setTheme
39
+ * Change the current used theme for highlighting
40
+ * @param {ShjTerminalTheme} name The name of the theme
41
+ */
42
+ export const setTheme = async name => theme = import(`./themes/${name}.js`);
@@ -0,0 +1,18 @@
1
+ import col from '../color.js'
2
+
3
+ export default {
4
+ deleted: col.red,
5
+ var: col.red,
6
+ err: col.red,
7
+ kwd: col.magenta,
8
+ num: col.yellow,
9
+ class: col.yellow,
10
+ cmnt: col.gray,
11
+ insert: col.green,
12
+ str: col.green,
13
+ bool: col.cyan,
14
+ type: col.blue,
15
+ oper: col.blue,
16
+ section: col.magenta,
17
+ func: col.blue
18
+ }