Skip to content

Commit 39c1bb9

Browse files
committed
šŸ› FIX: Add lexer to source code
1 parent 48deccc commit 39c1bb9

24 files changed

Lines changed: 142 additions & 57 deletions
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
jupytext:
3+
text_representation:
4+
extension: .md
5+
format_name: myst
6+
format_version: '0.12'
7+
jupytext_version: 1.5.2
8+
kernelspec:
9+
display_name: Coconut
10+
language: coconut
11+
name: coconut
12+
---
13+
14+
# Notebooks in other languages
15+
16+
<http://coconut-lang.org/>
17+
18+
```{code-cell} coconut
19+
def factorial(n):
20+
"""Compute n! where n is an integer >= 0."""
21+
if n `isinstance` int and n >= 0:
22+
acc = 1
23+
for x in range(1, n+1):
24+
acc *= x
25+
return acc
26+
else:
27+
raise TypeError("the argument to factorial must be an integer >= 0")
28+
29+
3 |> factorial |> print
30+
```
31+
32+
```{code-cell} coconut
33+
def quick_sort(l):
34+
"""Sort the input iterator using the quick sort algorithm."""
35+
match [head] :: tail in l:
36+
tail = reiterable(tail)
37+
yield from quick_sort(left) :: [head] :: quick_sort(right) where:
38+
left = (x for x in tail if x < head)
39+
right = (x for x in tail if x >= head)
40+
# By yielding nothing if the match falls through, we implicitly return an empty iterator.
41+
42+
[3,0,4,2,1] |> quick_sort |> list |> print
43+
```
44+
45+
```{code-cell} coconut
46+
:tags: [raises-exception]
47+
x
48+
```

ā€Ždocs/examples/index.mdā€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ and reference.
55

66
```{toctree}
77
basic
8+
coconut-lang
89
interactive
910
jupyter_sphinx
1011
```

ā€Žmyst_nb/parser.pyā€Ž

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ def parse_block(src, start_line):
112112
block_tokens = []
113113
source_map = ntbk.metadata.get("source_map", None)
114114

115+
# get language lexer name
116+
langinfo = ntbk.metadata.get("language_info", {})
117+
lexer = langinfo.get("pygments_lexer", langinfo.get("name", None))
118+
# TODO log warning if lexer is still None
119+
115120
for cell_index, nb_cell in enumerate(ntbk.cells):
116121

117122
# if the the source_map has been stored (for text-based notebooks),
@@ -144,7 +149,7 @@ def parse_block(src, start_line):
144149
"nb_code_cell",
145150
"",
146151
0,
147-
meta={"cell": nb_cell},
152+
meta={"cell": nb_cell, "lexer": lexer},
148153
map=[start_line, start_line],
149154
)
150155
)
@@ -209,7 +214,9 @@ def render_nb_code_cell(self, token: Token):
209214
sphinx_cell += cell_input
210215

211216
# Input block
212-
code_block = nodes.literal_block(text=cell["source"])
217+
code_block = nodes.literal_block(
218+
text=cell["source"], language=token.meta["lexer"]
219+
)
213220
cell_input += code_block
214221

215222
# ==================

ā€Žsetup.pyā€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"pandas",
6767
],
6868
"rtd": [
69+
"coconut~=1.4.3",
6970
"sphinxcontrib-bibtex",
7071
"ipywidgets",
7172
"pandas",

ā€Žtests/nb_fixtures/basic.txtā€Ž

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,36 @@ cells:
2828
<docinfo>
2929
<CellNode cell_type="code" classes="cell">
3030
<CellInputNode classes="cell_input">
31-
<literal_block xml:space="preserve">
31+
<literal_block language="True" xml:space="preserve">
3232
a=1
3333
print(a)
3434
.
3535

36+
Code Cell (with lexer):
37+
.
38+
metadata:
39+
language_info:
40+
pygments_lexer: mylexer
41+
cells:
42+
- cell_type: code
43+
metadata: {}
44+
execution_count: null
45+
source: a=1
46+
outputs: []
47+
.
48+
<document source="notset">
49+
<docinfo>
50+
<field>
51+
<field_name>
52+
language_info
53+
<field_body>
54+
{"pygments_lexer": "mylexer"}
55+
<CellNode cell_type="code" classes="cell">
56+
<CellInputNode classes="cell_input">
57+
<literal_block language="mylexer" xml:space="preserve">
58+
a=1
59+
.
60+
3661
Code Cell (simple output):
3762
.
3863
cells:
@@ -51,7 +76,7 @@ cells:
5176
<docinfo>
5277
<CellNode cell_type="code" classes="cell">
5378
<CellInputNode classes="cell_input">
54-
<literal_block xml:space="preserve">
79+
<literal_block language="True" xml:space="preserve">
5580
a=1
5681
print(a)
5782
<CellOutputNode classes="cell_output">
@@ -84,7 +109,7 @@ cells:
84109
A Title
85110
<CellNode cell_type="code" classes="cell">
86111
<CellInputNode classes="cell_input">
87-
<literal_block xml:space="preserve">
112+
<literal_block language="True" xml:space="preserve">
88113
a=1
89114
print(a)
90115
<paragraph>

ā€Žtests/test_execute/test_basic_failing.xmlā€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
some text
77
<CellNode cell_type="code" classes="cell">
88
<CellInputNode classes="cell_input">
9-
<literal_block xml:space="preserve">
9+
<literal_block language="ipython3" xml:space="preserve">
1010
raise Exception('oopsie!')

ā€Žtests/test_execute/test_basic_unrun.xmlā€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
some text
77
<CellNode cell_type="code" classes="cell">
88
<CellInputNode classes="cell_input">
9-
<literal_block xml:space="preserve">
9+
<literal_block language="ipython3" xml:space="preserve">
1010
a=1
1111
print(a)
1212
<CellOutputNode classes="cell_output">

ā€Žtests/test_execute/test_basic_unrun_nbclient.xmlā€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
some text
77
<CellNode cell_type="code" classes="cell">
88
<CellInputNode classes="cell_input">
9-
<literal_block xml:space="preserve">
9+
<literal_block language="ipython3" xml:space="preserve">
1010
a=1
1111
print(a)
1212
<CellOutputNode classes="cell_output">

ā€Žtests/test_execute/test_complex_outputs_unrun.xmlā€Ž

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<document source="complex_outputs_unrun">
22
<CellNode cell_type="code" classes="cell">
33
<CellInputNode classes="cell_input">
4-
<literal_block xml:space="preserve">
4+
<literal_block language="ipython3" xml:space="preserve">
55
import matplotlib.pyplot as plt
66
import pandas as pd
77
pd.set_option('display.latex.repr', True)
@@ -89,7 +89,7 @@
8989
Text Output
9090
<CellNode cell_type="code" classes="cell">
9191
<CellInputNode classes="cell_input">
92-
<literal_block xml:space="preserve">
92+
<literal_block language="ipython3" xml:space="preserve">
9393
print("""
9494
This is some printed text,
9595
with a nicely formatted output.
@@ -113,7 +113,7 @@
113113
The plotting code for a pandas Dataframe table (\cref{tbl:example}).
114114
<CellNode cell_type="code" classes="cell">
115115
<CellInputNode classes="cell_input">
116-
<literal_block xml:space="preserve">
116+
<literal_block language="ipython3" xml:space="preserve">
117117
np.random.seed(0)
118118
df = pd.DataFrame(np.random.rand(3,4),columns=['a','b','c','d'])
119119
df.a = ['$\delta$','x','y']
@@ -127,15 +127,15 @@
127127
Equations (with ipython or sympy)
128128
<CellNode cell_type="code" classes="cell">
129129
<CellInputNode classes="cell_input">
130-
<literal_block xml:space="preserve">
130+
<literal_block language="ipython3" xml:space="preserve">
131131
Latex('$$ a = b+c $$')
132132
<CellOutputNode classes="cell_output">
133133
<CellOutputBundleNode output_count="1">
134134
<paragraph>
135135
The plotting code for a sympy equation (=@eqn:example_sympy).
136136
<CellNode cell_type="code" classes="cell">
137137
<CellInputNode classes="cell_input">
138-
<literal_block xml:space="preserve">
138+
<literal_block language="ipython3" xml:space="preserve">
139139
y = sym.Function('y')
140140
n = sym.symbols(r'\alpha')
141141
f = y(n)-2*y(n-1/sym.pi)-5*y(n-2)
@@ -150,7 +150,7 @@
150150
ipywidgets
151151
<CellNode cell_type="code" classes="cell">
152152
<CellInputNode classes="cell_input">
153-
<literal_block xml:space="preserve">
153+
<literal_block language="ipython3" xml:space="preserve">
154154
import ipywidgets as widgets
155155
widgets.Layout(model_id="1337h4x0R")
156156
<CellOutputNode classes="cell_output">

ā€Žtests/test_execute/test_complex_outputs_unrun_nbclient.xmlā€Ž

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<document source="complex_outputs_unrun">
22
<CellNode cell_type="code" classes="cell">
33
<CellInputNode classes="cell_input">
4-
<literal_block xml:space="preserve">
4+
<literal_block language="ipython3" xml:space="preserve">
55
import matplotlib.pyplot as plt
66
import pandas as pd
77
pd.set_option('display.latex.repr', True)
@@ -89,7 +89,7 @@
8989
Text Output
9090
<CellNode cell_type="code" classes="cell">
9191
<CellInputNode classes="cell_input">
92-
<literal_block xml:space="preserve">
92+
<literal_block language="ipython3" xml:space="preserve">
9393
print("""
9494
This is some printed text,
9595
with a nicely formatted output.
@@ -113,7 +113,7 @@
113113
The plotting code for a pandas Dataframe table (\cref{tbl:example}).
114114
<CellNode cell_type="code" classes="cell">
115115
<CellInputNode classes="cell_input">
116-
<literal_block xml:space="preserve">
116+
<literal_block language="ipython3" xml:space="preserve">
117117
np.random.seed(0)
118118
df = pd.DataFrame(np.random.rand(3,4),columns=['a','b','c','d'])
119119
df.a = ['$\delta$','x','y']
@@ -127,15 +127,15 @@
127127
Equations (with ipython or sympy)
128128
<CellNode cell_type="code" classes="cell">
129129
<CellInputNode classes="cell_input">
130-
<literal_block xml:space="preserve">
130+
<literal_block language="ipython3" xml:space="preserve">
131131
Latex('$$ a = b+c $$')
132132
<CellOutputNode classes="cell_output">
133133
<CellOutputBundleNode output_count="1">
134134
<paragraph>
135135
The plotting code for a sympy equation (=@eqn:example_sympy).
136136
<CellNode cell_type="code" classes="cell">
137137
<CellInputNode classes="cell_input">
138-
<literal_block xml:space="preserve">
138+
<literal_block language="ipython3" xml:space="preserve">
139139
y = sym.Function('y')
140140
n = sym.symbols(r'\alpha')
141141
f = y(n)-2*y(n-1/sym.pi)-5*y(n-2)
@@ -150,7 +150,7 @@
150150
ipywidgets
151151
<CellNode cell_type="code" classes="cell">
152152
<CellInputNode classes="cell_input">
153-
<literal_block xml:space="preserve">
153+
<literal_block language="ipython3" xml:space="preserve">
154154
import ipywidgets as widgets
155155
widgets.Layout(model_id="1337h4x0R")
156156
<CellOutputNode classes="cell_output">

0 commit comments

Comments
Ā (0)
⚔