blob: 22b8b3d246648f79030c47dcc31eaba3d6e40e26 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
importScripts('/_static/pyodide/pyodide.js');
async function loadPyodideAndPygments() {
self.pyodide = await loadPyodide();
await self.pyodide.loadPackage(["Pygments"]);
const styles = self.pyodide.runPython(`
from pygments.formatters.html import HtmlFormatter
from pygments.styles import STYLE_MAP
{s: HtmlFormatter(style=s).get_style_defs('.demo-highlight') for s in STYLE_MAP}
`).toJs();
self.postMessage({loaded: {styles}})
}
let pyodideReadyPromise = loadPyodideAndPygments();
self.onmessage = async (event) => {
// Make sure loading is done.
await pyodideReadyPromise;
if (event.data.highlight) {
self.pyodide.globals.set('code', event.data.highlight.code);
self.pyodide.globals.set('lexer_name', event.data.highlight.lexer);
self.pyodide.runPython(`
import pygments.lexers
lexer = pygments.lexers.get_lexer_by_name(lexer_name)
if type(code) == memoryview:
code = bytes(code)
tokens = lexer.get_tokens(code)
`);
const formatter = event.data.highlight.formatter;
if (formatter == 'html') {
const html = self.pyodide.runPython(`
import io
from pygments.formatters.html import HtmlFormatter
fmter = HtmlFormatter(cssclass='demo-highlight')
buf = io.StringIO()
fmter.format(tokens, buf)
buf.getvalue()
`);
self.postMessage({html});
} else if (formatter == 'tokens') {
const tokens = self.pyodide.runPython('list(tokens)').toJs();
self.postMessage({tokens});
} else {
console.warn('unknown formatter:', formatter);
}
} else if (event.data.guess_lexer) {
self.pyodide.globals.set('code', event.data.guess_lexer.code);
self.pyodide.globals.set('filename', event.data.guess_lexer.filename);
const lexer = self.pyodide.runPython(`
import sys
sys.setrecursionlimit(1000)
# TODO: remove after upgrading to Pyodide 0.19
import pygments.lexers
import pygments.util
if type(code) == memoryview:
code = bytes(code)
if filename:
lexer = pygments.lexers.guess_lexer_for_filename(filename, code)
else:
lexer = pygments.lexers.guess_lexer(code)
lexer.aliases[0]
`);
self.postMessage({lexer});
} else {
console.warn('unknown command: expected highlight or guess_lexer but received ', event.data);
}
}
|