This repository has been archived on 2024-06-20. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
coffee.pygments/doc/_static/demo-worker.js
amitkummer cbcbffea1b
Overhaul demo and support running it locally (#2141)
* Add Make target for building Pyodide with currently checked out Pygments

Add a target to the documentation's Makefile to make Pyodide with
Pygments from the current checkout.

Additionally, use this target when building the documentation with
the demo page.

The new target works by building Pyodide on a Docker container, and
then exporting the build artifacts back to the host.

The Dockerfile used by the new target is based on the Dockerfile which
was used for building Pyodide on the CI:
https://github.com/pygments/pyodide-artifacts/blob/master/container/Dockerfile

The main difference between the two is that the new Dockerfile uses the
newest Pyodide base image, which is built by a repository which is actively
maintained (the Iodide project is no longer maintained).

The purpose of this change is to allow running the demo locally,
which was not possible previously, as Pyodide was only built by the
CI when deploying to Github Pages.

* Add instructions on how to run the demo locally to the README

* Update demo to work with Pyodide v0.20

Remove usage of Pyodide functionality which was deprecated in Pyodide v0.17:

- Loading Pyodide using `languagePluginURL` and `languagePluginLoader`.
- Access to globals via `pyodide.globals.x` has changed to `pyodide.globals.get("x")`.

Source:
https://pyodide.org/en/stable/project/release-notes/v0.17.0.html?highlight=languageplugin#api-changes

* Fix #2137

* Fix use of `styles` variable before it's defined

The `styles` variable was being used before it's value was set (which
is done when Pyodide finishes loading).

* Remove GitHub action for building Pyodide

This action is obsolete, as building Pyodide is now done using `make pyodide`.

* Upgrade Pages deployment action version

* Add to .dockerignore all files in .gitignore

* Change `pyodide` target to be a phony target
2022-05-21 17:56:08 +02:00

74 lines
2.6 KiB
JavaScript

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);
}
}