From c51111914f8e4a2c6ab3d5deb01e2b28f8910b8d Mon Sep 17 00:00:00 2001 From: Etienne Dechamps Date: Sun, 11 Dec 2022 17:30:55 +0000 Subject: [PATCH] Add support for Wolfram Language (#1014) --- docs/languages.md | 2 + jupytext/cell_metadata.py | 4 + jupytext/languages.py | 9 ++ tests/notebooks/ipynb_wolfram/wolfram.ipynb | 122 ++++++++++++++++++ .../notebooks/mirror/ipynb_to_Rmd/wolfram.Rmd | 28 ++++ .../mirror/ipynb_to_hydrogen/wolfram.wolfram | 28 ++++ tests/notebooks/mirror/ipynb_to_md/wolfram.md | 28 ++++ .../notebooks/mirror/ipynb_to_myst/wolfram.md | 28 ++++ .../mirror/ipynb_to_percent/wolfram.wolfram | 28 ++++ .../mirror/ipynb_to_script/wolfram.wolfram | 21 +++ 10 files changed, 298 insertions(+) create mode 100644 tests/notebooks/ipynb_wolfram/wolfram.ipynb create mode 100644 tests/notebooks/mirror/ipynb_to_Rmd/wolfram.Rmd create mode 100644 tests/notebooks/mirror/ipynb_to_hydrogen/wolfram.wolfram create mode 100644 tests/notebooks/mirror/ipynb_to_md/wolfram.md create mode 100644 tests/notebooks/mirror/ipynb_to_myst/wolfram.md create mode 100644 tests/notebooks/mirror/ipynb_to_percent/wolfram.wolfram create mode 100644 tests/notebooks/mirror/ipynb_to_script/wolfram.wolfram diff --git a/docs/languages.md b/docs/languages.md index d8031f7a8..4d11a3a26 100644 --- a/docs/languages.md +++ b/docs/languages.md @@ -30,5 +30,7 @@ Jupytext works with notebooks in any of the following languages: - TypeScript - Haskell - Tcl +- Wolfram Language + - Note that Jupytext uses the non-standard `.wolfram` file extension for Wolfram Language files to avoid conflicts with Matlab. Extending Jupytext to more languages should be easy, see the sections on [contributing to](contributing.md) and [developing](developing.md) Jupytext. diff --git a/jupytext/cell_metadata.py b/jupytext/cell_metadata.py index 30547d3cc..5356a8cd2 100644 --- a/jupytext/cell_metadata.py +++ b/jupytext/cell_metadata.py @@ -256,6 +256,10 @@ def parse_rmd_options(line): def rmd_options_to_metadata(options, use_runtools=False): """Parse rmd options and return a metadata dictionary""" options = re.split(r"\s|,", options, 1) + # Special case Wolfram Language, which sadly has a space in the language + # name. + if options[0:2] == ["wolfram", "language"]: + options[0:2] = ["wolfram language"] if len(options) == 1: language = options[0] chunk_options = [] diff --git a/jupytext/languages.py b/jupytext/languages.py index 44bccc34e..2aee7d52c 100644 --- a/jupytext/languages.py +++ b/jupytext/languages.py @@ -29,6 +29,7 @@ "haskell", "tcl", "gnuplot", + "wolfram language", ] # Supported file extensions (and languages) @@ -47,6 +48,14 @@ ".ps1": {"language": "powershell", "comment": "#"}, ".q": {"language": "q", "comment": "/"}, ".m": {"language": "matlab", "comment": "%"}, + # Unfortunately, Wolfram Mathematica also uses the .m extension which + # conflicts with Matlab. To work around this problem we arbitrarily use a + # made-up .wolfram extension. + ".wolfram": { + "language": "wolfram language", + "comment": "(*", + "comment_suffix": "*)", + }, ".pro": {"language": "idl", "comment": ";"}, ".js": {"language": "javascript", "comment": "//"}, ".ts": {"language": "typescript", "comment": "//"}, diff --git a/tests/notebooks/ipynb_wolfram/wolfram.ipynb b/tests/notebooks/ipynb_wolfram/wolfram.ipynb new file mode 100644 index 000000000..20dbf6624 --- /dev/null +++ b/tests/notebooks/ipynb_wolfram/wolfram.ipynb @@ -0,0 +1,122 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "f48fe6fa", + "metadata": {}, + "source": [ + "**Note:** The `language_info` `file_extension` in this notebook should be `.m`, but it was deliberately changed to `.wolfram` to avoid conflicts with Matlab which is using the same extension." + ] + }, + { + "cell_type": "markdown", + "id": "19ffc137", + "metadata": {}, + "source": [ + "We start with..." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "17c5b20c", + "metadata": {}, + "outputs": [], + "source": [ + "Print[\"Hello, World!\"];" + ] + }, + { + "cell_type": "markdown", + "id": "2b9e8c46", + "metadata": {}, + "source": [ + "Then we draw the first example plot from the [ListPlot](https://reference.wolfram.com/language/ref/ListPlot.html) reference:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "7c4dfdc2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\"Output\"
" + ], + "text/plain": [ + "-Graphics-" + ] + }, + "execution_count": 2, + "metadata": { + "text/html": [], + "text/plain": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "ListPlot[Prime[Range[25]]]" + ] + }, + { + "cell_type": "markdown", + "id": "0e549b21", + "metadata": {}, + "source": [ + "We also test the math outputs as in the [Simplify](https://reference.wolfram.com/language/ref/Simplify.html) example:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "5c2010aa", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\"Output\"
" + ], + "text/plain": [ + " 1 -1 + 2 x 2\n", + "--------- - -------------- + -------------------\n", + "3 (1 + x) 2 2\n", + " 6 (1 - x + x ) (-1 + 2 x)\n", + " 3 (1 + -----------)\n", + " 3" + ] + }, + "execution_count": 3, + "metadata": { + "text/html": [], + "text/plain": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "D[Integrate[1/(x^3 + 1), x], x]" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Wolfram Language 13.1", + "language": "Wolfram Language", + "name": "wolframlanguage13.1" + }, + "language_info": { + "codemirror_mode": "mathematica", + "file_extension": ".wolfram", + "mimetype": "application/vnd.wolfram.m", + "name": "Wolfram Language", + "pygments_lexer": "mathematica", + "version": "12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/tests/notebooks/mirror/ipynb_to_Rmd/wolfram.Rmd b/tests/notebooks/mirror/ipynb_to_Rmd/wolfram.Rmd new file mode 100644 index 000000000..f1fa74df8 --- /dev/null +++ b/tests/notebooks/mirror/ipynb_to_Rmd/wolfram.Rmd @@ -0,0 +1,28 @@ +--- +jupyter: + kernelspec: + display_name: Wolfram Language 13.1 + language: Wolfram Language + name: wolframlanguage13.1 +--- + +**Note:** The `language_info` `file_extension` in this notebook should be `.m`, but it was deliberately changed to `.wolfram` to avoid conflicts with Matlab which is using the same extension. + + +We start with... + +```{wolfram language} +Print["Hello, World!"]; +``` + +Then we draw the first example plot from the [ListPlot](https://reference.wolfram.com/language/ref/ListPlot.html) reference: + +```{wolfram language} +ListPlot[Prime[Range[25]]] +``` + +We also test the math outputs as in the [Simplify](https://reference.wolfram.com/language/ref/Simplify.html) example: + +```{wolfram language} +D[Integrate[1/(x^3 + 1), x], x] +``` diff --git a/tests/notebooks/mirror/ipynb_to_hydrogen/wolfram.wolfram b/tests/notebooks/mirror/ipynb_to_hydrogen/wolfram.wolfram new file mode 100644 index 000000000..104743658 --- /dev/null +++ b/tests/notebooks/mirror/ipynb_to_hydrogen/wolfram.wolfram @@ -0,0 +1,28 @@ +(* --- *) +(* jupyter: *) +(* kernelspec: *) +(* display_name: Wolfram Language 13.1 *) +(* language: Wolfram Language *) +(* name: wolframlanguage13.1 *) +(* --- *) + +(* %% [markdown] *) +(* **Note:** The `language_info` `file_extension` in this notebook should be `.m`, but it was deliberately changed to `.wolfram` to avoid conflicts with Matlab which is using the same extension. *) + +(* %% [markdown] *) +(* We start with... *) + +(* %% *) +Print["Hello, World!"]; + +(* %% [markdown] *) +(* Then we draw the first example plot from the [ListPlot](https://reference.wolfram.com/language/ref/ListPlot.html) reference: *) + +(* %% *) +ListPlot[Prime[Range[25]]] + +(* %% [markdown] *) +(* We also test the math outputs as in the [Simplify](https://reference.wolfram.com/language/ref/Simplify.html) example: *) + +(* %% *) +D[Integrate[1/(x^3 + 1), x], x] diff --git a/tests/notebooks/mirror/ipynb_to_md/wolfram.md b/tests/notebooks/mirror/ipynb_to_md/wolfram.md new file mode 100644 index 000000000..698916026 --- /dev/null +++ b/tests/notebooks/mirror/ipynb_to_md/wolfram.md @@ -0,0 +1,28 @@ +--- +jupyter: + kernelspec: + display_name: Wolfram Language 13.1 + language: Wolfram Language + name: wolframlanguage13.1 +--- + +**Note:** The `language_info` `file_extension` in this notebook should be `.m`, but it was deliberately changed to `.wolfram` to avoid conflicts with Matlab which is using the same extension. + + +We start with... + +```wolfram language +Print["Hello, World!"]; +``` + +Then we draw the first example plot from the [ListPlot](https://reference.wolfram.com/language/ref/ListPlot.html) reference: + +```wolfram language +ListPlot[Prime[Range[25]]] +``` + +We also test the math outputs as in the [Simplify](https://reference.wolfram.com/language/ref/Simplify.html) example: + +```wolfram language +D[Integrate[1/(x^3 + 1), x], x] +``` diff --git a/tests/notebooks/mirror/ipynb_to_myst/wolfram.md b/tests/notebooks/mirror/ipynb_to_myst/wolfram.md new file mode 100644 index 000000000..1da4bda67 --- /dev/null +++ b/tests/notebooks/mirror/ipynb_to_myst/wolfram.md @@ -0,0 +1,28 @@ +--- +kernelspec: + display_name: Wolfram Language 13.1 + language: Wolfram Language + name: wolframlanguage13.1 +--- + +**Note:** The `language_info` `file_extension` in this notebook should be `.m`, but it was deliberately changed to `.wolfram` to avoid conflicts with Matlab which is using the same extension. + ++++ + +We start with... + +```{code-cell} mathematica +Print["Hello, World!"]; +``` + +Then we draw the first example plot from the [ListPlot](https://reference.wolfram.com/language/ref/ListPlot.html) reference: + +```{code-cell} mathematica +ListPlot[Prime[Range[25]]] +``` + +We also test the math outputs as in the [Simplify](https://reference.wolfram.com/language/ref/Simplify.html) example: + +```{code-cell} mathematica +D[Integrate[1/(x^3 + 1), x], x] +``` diff --git a/tests/notebooks/mirror/ipynb_to_percent/wolfram.wolfram b/tests/notebooks/mirror/ipynb_to_percent/wolfram.wolfram new file mode 100644 index 000000000..104743658 --- /dev/null +++ b/tests/notebooks/mirror/ipynb_to_percent/wolfram.wolfram @@ -0,0 +1,28 @@ +(* --- *) +(* jupyter: *) +(* kernelspec: *) +(* display_name: Wolfram Language 13.1 *) +(* language: Wolfram Language *) +(* name: wolframlanguage13.1 *) +(* --- *) + +(* %% [markdown] *) +(* **Note:** The `language_info` `file_extension` in this notebook should be `.m`, but it was deliberately changed to `.wolfram` to avoid conflicts with Matlab which is using the same extension. *) + +(* %% [markdown] *) +(* We start with... *) + +(* %% *) +Print["Hello, World!"]; + +(* %% [markdown] *) +(* Then we draw the first example plot from the [ListPlot](https://reference.wolfram.com/language/ref/ListPlot.html) reference: *) + +(* %% *) +ListPlot[Prime[Range[25]]] + +(* %% [markdown] *) +(* We also test the math outputs as in the [Simplify](https://reference.wolfram.com/language/ref/Simplify.html) example: *) + +(* %% *) +D[Integrate[1/(x^3 + 1), x], x] diff --git a/tests/notebooks/mirror/ipynb_to_script/wolfram.wolfram b/tests/notebooks/mirror/ipynb_to_script/wolfram.wolfram new file mode 100644 index 000000000..260cd8b66 --- /dev/null +++ b/tests/notebooks/mirror/ipynb_to_script/wolfram.wolfram @@ -0,0 +1,21 @@ +(* --- *) +(* jupyter: *) +(* kernelspec: *) +(* display_name: Wolfram Language 13.1 *) +(* language: Wolfram Language *) +(* name: wolframlanguage13.1 *) +(* --- *) + +(* **Note:** The `language_info` `file_extension` in this notebook should be `.m`, but it was deliberately changed to `.wolfram` to avoid conflicts with Matlab which is using the same extension. *) + +(* We start with... *) + +Print["Hello, World!"]; + +(* Then we draw the first example plot from the [ListPlot](https://reference.wolfram.com/language/ref/ListPlot.html) reference: *) + +ListPlot[Prime[Range[25]]] + +(* We also test the math outputs as in the [Simplify](https://reference.wolfram.com/language/ref/Simplify.html) example: *) + +D[Integrate[1/(x^3 + 1), x], x]