Skip to content

Commit

Permalink
Experimental: set the language lexer only
Browse files Browse the repository at this point in the history
if the language_info is preserved in the filtered metadata
  • Loading branch information
mwouts committed Feb 16, 2025
1 parent 9aba222 commit d3a5eb3
Show file tree
Hide file tree
Showing 58 changed files with 460 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Jupytext ChangeLog

**Changed**
- We have updated the JupyterLab extension dependencies ([#1300](https://github.com/mwouts/jupytext/pull/1300)). Thanks to [Mahendra Paipuri](https://github.com/mahendrapaipuri) for this PR!
- The language lexer (e.g. `ipython3`) for `md:myst` files is now only included when `language_info` is added to `notebook_metadata_filter`.


1.16.7 (2025-02-09)
Expand Down
24 changes: 13 additions & 11 deletions src/jupytext/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,14 @@ def test_round_trip_conversion(


# The functions below are used in the Jupytext text collection
def create_mirror_file_if_missing(mirror_file, notebook, fmt):
def create_mirror_file_if_missing(mirror_file, notebook, fmt, config):
if not os.path.isfile(mirror_file):
write(notebook, mirror_file, fmt=fmt)
write(notebook, mirror_file, fmt=fmt, config=config)


def assert_conversion_same_as_mirror(nb_file, fmt, mirror_name, compare_notebook=False):
def assert_conversion_same_as_mirror(
nb_file, fmt, mirror_name, compare_notebook=False, config=None
):
"""This function is used in the tests"""
dirname, basename = os.path.split(nb_file)
file_name, org_ext = os.path.splitext(basename)
Expand All @@ -414,25 +416,25 @@ def assert_conversion_same_as_mirror(nb_file, fmt, mirror_name, compare_notebook
# it's better not to have Jupytext metadata in test notebooks:
if fmt == "ipynb" and "jupytext" in notebook.metadata: # pragma: no cover
notebook.metadata.pop("jupytext")
write(nb_file, fmt=fmt)
write(nb_file, fmt=fmt, config=config)

create_mirror_file_if_missing(mirror_file, notebook, fmt)
create_mirror_file_if_missing(mirror_file, notebook, fmt, config=config)

# Compare the text representation of the two notebooks
if compare_notebook:
# Read and convert the mirror file to the latest nbformat version if necessary
nb_mirror = read(mirror_file, as_version=notebook.nbformat)
nb_mirror = read(mirror_file, as_version=notebook.nbformat, config=config)
nb_mirror.nbformat_minor = notebook.nbformat_minor
compare_notebooks(nb_mirror, notebook)
return
elif ext == ".ipynb":
notebook = read(mirror_file)
notebook = read(mirror_file, config=config)
fmt.update({"extension": org_ext})
actual = writes(notebook, fmt)
actual = writes(notebook, fmt, config=config)
with open(nb_file, encoding="utf-8") as fp:
expected = fp.read()
else:
actual = writes(notebook, fmt)
actual = writes(notebook, fmt, config=config)
with open(mirror_file, encoding="utf-8") as fp:
expected = fp.read()

Expand All @@ -442,8 +444,8 @@ def assert_conversion_same_as_mirror(nb_file, fmt, mirror_name, compare_notebook

# Compare the two notebooks
if ext != ".ipynb":
notebook = read(nb_file)
nb_mirror = read(mirror_file, fmt=fmt)
notebook = read(nb_file, config=config)
nb_mirror = read(mirror_file, fmt=fmt, config=config)

if fmt.get("format_name") == "sphinx":
nb_mirror.cells = nb_mirror.cells[1:]
Expand Down
4 changes: 0 additions & 4 deletions src/jupytext/jupytext.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,8 @@ def writes(self, nb, metadata=None, **kwargs):
if self.fmt.get(
"format_name"
) == MYST_FORMAT_NAME or self.ext in myst_extensions(no_md=True):
pygments_lexer = metadata.get("language_info", {}).get(
"pygments_lexer", None
)
return notebook_to_myst(
self.filter_notebook(nb, metadata),
default_lexer=pygments_lexer,
)

# Copy the notebook, in order to be sure we do not modify the original notebook
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ kernelspec:
display_name: Python 3
language: python
name: python3
language_info:
codemirror_mode:
name: ipython
version: 3
file_extension: .py
mimetype: text/x-python
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.7.4
---

This cell uses no particular cell marker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ kernelspec:
display_name: Python 3
language: python
name: python3
language_info:
codemirror_mode:
name: ipython
version: 3
file_extension: .py
mimetype: text/x-python
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.6.6
---

```{code-cell} ipython3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ kernelspec:
display_name: Python 3
language: python
name: python3
language_info:
codemirror_mode:
name: ipython
version: 3
file_extension: .py
mimetype: text/x-python
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.6.6
---

```{code-cell} ipython3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ kernelspec:
display_name: Python 3
language: python
name: python3
language_info:
codemirror_mode:
name: ipython
version: 3
file_extension: .py
mimetype: text/x-python
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.6.6
---

##################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ kernelspec:
display_name: Python 3
language: python
name: python3
language_info:
codemirror_mode:
name: ipython
version: 3
file_extension: .py
mimetype: text/x-python
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.6.6
---

# Part one - various cells
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ kernelspec:
display_name: Python 2
language: python
name: python2
language_info:
codemirror_mode:
name: ipython
version: 2
file_extension: .py
mimetype: text/x-python
name: python
nbconvert_exporter: python
pygments_lexer: ipython2
version: 2.7.11
---

# A notebook with R cells
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ kernelspec:
display_name: Python 3
language: python
name: python3
language_info:
codemirror_mode:
name: ipython
version: 3
file_extension: .py
mimetype: text/x-python
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.6.6
---

```{code-cell} ipython3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ kernelspec:
display_name: R
language: R
name: ir
language_info:
codemirror_mode: r
file_extension: .r
mimetype: text/x-r-source
name: R
pygments_lexer: r
version: 3.5.1
---

This notebook was created with IRKernel 0.8.12, and is not completely valid, as the code cell below contains an unexpected 'source' entry. This did cause https://github.com/mwouts/jupytext/issues/234. Note that the problem is solved when one upgrades to IRKernel 1.0.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ kernelspec:
display_name: Calysto Scheme (Python)
language: scheme
name: calysto_scheme
language_info:
codemirror_mode:
name: scheme
mimetype: text/x-scheme
name: scheme
pygments_lexer: scheme
---

<img src="images/logo-64x64.png"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ kernelspec:
display_name: Python 3
language: python
name: python3
language_info:
codemirror_mode:
name: ipython
version: 3
file_extension: .py
mimetype: text/x-python
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.7.3
---

```{raw-cell}
Expand Down
10 changes: 10 additions & 0 deletions tests/data/notebooks/outputs/ipynb_to_myst/cat_variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ kernelspec:
display_name: Python 3
language: python
name: python3
language_info:
codemirror_mode:
name: ipython
version: 3
file_extension: .py
mimetype: text/x-python
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.6.4
---

```{code-cell} ipython3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ kernelspec:
display_name: Coconut
language: coconut
name: coconut
language_info:
codemirror_mode:
name: python
version: 3
file_extension: .coco
mimetype: text/x-python3
name: coconut
pygments_lexer: coconut
version: 1.4.3-post_dev25
---

Taken from [coconut-lang.org](coconut-lang.org)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ kernelspec:
display_name: Python 3
language: python
name: python3
language_info:
codemirror_mode:
name: ipython
version: 3
file_extension: .py
mimetype: text/x-python
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.5.3
---

```{code-cell} ipython3
Expand Down
6 changes: 6 additions & 0 deletions tests/data/notebooks/outputs/ipynb_to_myst/csharp.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ kernelspec:
display_name: .NET (C#)
language: C#
name: .net-csharp
language_info:
file_extension: .cs
mimetype: text/x-csharp
name: C#
pygments_lexer: csharp
version: '8.0'
---

We start with...
Expand Down
5 changes: 5 additions & 0 deletions tests/data/notebooks/outputs/ipynb_to_myst/demo_gdl_fbp.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ kernelspec:
display_name: IDL [conda env:gdl] *
language: IDL
name: conda-env-gdl-idl
language_info:
codemirror_mode: idl
file_extension: .pro
mimetype: text/x-idl
name: idl
---

## GDL demo notebook
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ kernelspec:
display_name: Rust
language: rust
name: rust
language_info:
codemirror_mode: rust
file_extension: .rs
mimetype: text/rust
name: Rust
pygment_lexer: rust
version: ''
---

# Tour of the EvCxR Jupyter Kernel
Expand Down
10 changes: 10 additions & 0 deletions tests/data/notebooks/outputs/ipynb_to_myst/frozen_cell.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ kernelspec:
display_name: Python 3
language: python
name: python3
language_info:
codemirror_mode:
name: ipython
version: 3
file_extension: .py
mimetype: text/x-python
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.7.0
---

```{code-cell} ipython3
Expand Down
6 changes: 6 additions & 0 deletions tests/data/notebooks/outputs/ipynb_to_myst/fsharp.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ kernelspec:
display_name: .NET (F#)
language: F#
name: .net-fsharp
language_info:
file_extension: .fs
mimetype: text/x-fsharp
name: C#
pygments_lexer: fsharp
version: '4.5'
---

This notebook was inspired by [Plottting with XPlot](https://github.com/dotnet/interactive/blob/master/NotebookExamples/fsharp/Docs/Plotting%20with%20Xplot.ipynb).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ kernelspec:
display_name: gnuplot
language: gnuplot
name: gnuplot
language_info:
codemirror_mode: Octave
file_extension: .gp
help_links:
- text: MetaKernel Magics
url: https://metakernel.readthedocs.io/en/latest/source/README.html
mimetype: text/x-gnuplot
name: gnuplot
---

# Sample gnuplot notebook
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ kernelspec:
display_name: Haskell
language: haskell
name: haskell
language_info:
codemirror_mode: ihaskell
file_extension: .hs
mimetype: text/x-haskell
name: haskell
pygments_lexer: Haskell
version: 8.10.7
---

# Example Haskell Notebook
Expand Down
Loading

0 comments on commit d3a5eb3

Please sign in to comment.