| 22 января, пятница, 2021 | 16:38:40

Скопировано в буфер обмена!  Копировать Боковая панель Переносы строк Сырой код
1import org.graalvm.polyglot.Context;
2
3import java.io.File;
4import java.io.FileNotFoundException;
5
6import java.util.Scanner;
7
8public class Highlighter {
9 private abstract class Highlight {
10 protected final Context polyglot =
11 Context.newBuilder("js", "python", "ruby").allowAllAccess(true).allowIO(true)
12 .build();
13
14 protected abstract String language();
15 protected abstract String renderHtml(String language, String rawCode);
16
17 protected String execute(String sourceCode) {
18 try {
19 return polyglot.eval(language(), sourceCode).asString();
20 } catch (RuntimeException re) { re.printStackTrace(); }
21 return sourceCode;
22 }
23
24 protected void importValue(String name, String value) {
25 try {
26 polyglot.getBindings(language()).putMember(name, value);
27 } catch (RuntimeException re) { re.printStackTrace(); }
28 }
29 }
30
31 private class Hjs extends Highlight {
32 @Override
33 protected String language() { return "js"; }
34
35 @Override
36 public String renderHtml(String language, String rawCode) {
37 importValue("source", rawCode);
38
39 String hjs = "";
40 try {
41 hjs = new Scanner(new File("highlight.min.js")).useDelimiter("\\A").next();
42 } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); }
43
44 final String renderLanguageSnippet =
45 hjs + "\n" +
46 "hljs.highlight('" + language + "', String(source)).value";
47 return execute(renderLanguageSnippet);
48 }
49 }
50
51 private class Rouge extends Highlight {
52 @Override
53 protected String language() { return "ruby"; }
54
55 @Override
56 public String renderHtml(String language, String rawCode) {
57 importValue("$source", rawCode);
58 final String renderLanguageSnippet =
59 "require 'rouge'" + "\n" +
60
61 "formatter = Rouge::Formatters::HTML.new" + "\n" +
62 "lexer = Rouge::Lexer::find('" + language + "')" + "\n" +
63 "formatter.format(lexer.lex($source.to_str))";
64 return execute(renderLanguageSnippet);
65 }
66 }
67
68 private class Pygments extends Highlight {
69 @Override
70 protected String language() { return "python"; }
71
72 @Override
73 public String renderHtml(String language, String rawCode) {
74 importValue("source", rawCode);
75 final String renderLanguageSnippet =
76 "import site" + "\n" +
77 "from pygments import highlight" + "\n" +
78 "from pygments.lexers import get_lexer_by_name" + "\n" +
79 "from pygments.formatters import HtmlFormatter" + "\n" +
80
81 "formatter = HtmlFormatter(nowrap=True)" + "\n" +
82 "lexer = get_lexer_by_name('" + language + "')" + "\n" +
83 "highlight(source, lexer, formatter)";
84 return execute(renderLanguageSnippet);
85 }
86 }
87
88 public String highlight(String library, String language, String code) {
89 switch (library) {
90 default:
91 case "hjs": return new Hjs().renderHtml(language, code);
92 case "rouge": return new Rouge().renderHtml(language, code);
93 case "pygments": return new Pygments().renderHtml(language, code);
94 }
95 }
96
97 public static void main(String[] args) {
98 Scanner scanner = new Scanner(System.in).useDelimiter("\\A");
99 if (scanner.hasNext()) {
100 String code = scanner.next();
101 if (!code.isEmpty()) {
102 System.out.println(new Highlighter().highlight(args[0], args[1], code));
103 }
104 }
105 }
106}
```
Язык
Библиотека
VM
Скорость
JavaScript 20.3.0
Highlight.js 10.4.1
GraalVM
Быстро
Ruby 2.6.6
Rouge 3.26.0
GraalVM
Приемлемо
Время рендеринга подсветки синтаксиса на сервере: 1422ms.
```
Версия Code Polyglot: 1.0.0, 40b65db_190, 19-Jan-2021 15:55:44