Skip to content

Commit 66f4521

Browse files
committed
Fix .nb parser to handle real Wolfram notebook files
The parser now skips comment headers/footers, handles notebook-level options after the cell list, unescapes \< \> string delimiters and \ + newline line continuations in multi-line text cells.
1 parent 859a68a commit 66f4521

3 files changed

Lines changed: 304 additions & 59 deletions

File tree

tests/notebooks/hello_world.nb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,28 @@
1010
NotebookFileLineBreakTest
1111
NotebookFileLineBreakTest
1212
NotebookDataPosition[ 154, 7]
13-
NotebookDataLength[ 1512, 51]
14-
NotebookOptionsPosition[ 1039, 34]
15-
NotebookOutlinePosition[ 1433, 50]
16-
CellTagsIndexPosition[ 1390, 47]
13+
NotebookDataLength[ 1641, 53]
14+
NotebookOptionsPosition[ 1168, 36]
15+
NotebookOutlinePosition[ 1562, 52]
16+
CellTagsIndexPosition[ 1519, 49]
1717
WindowFrame->Normal*)
1818

1919
(* Beginning of Notebook Content *)
2020
Notebook[{
2121

2222
Cell[CellGroupData[{
2323
Cell[BoxData[
24-
RowBox[{"1", " ", "+", " ", "2", " ", "+", " ", "3", " ", "+", " ",
25-
"4"}]], "Input",
24+
RowBox[{"StringJoin", "[",
25+
RowBox[{"\"\<Hello\>\"", ",", " ", "\"\< \>\"", ",",
26+
" ", "\"\<World!\>\""}], "]"}]], "Input",
2627
CellChangeTimes->{{3.979539634998089*^9, 3.979539634999322*^9}, {
27-
3.979539927309093*^9, 3.979539929150152*^9}},
28+
3.979539927309093*^9, 3.979539929150152*^9}, {3.98542855710907*^9,
29+
3.985428567921555*^9}},
2830
CellLabel->"In[1]:=",ExpressionUUID->"036b8a36-c26c-422e-8935-92fbb0c5b2d0"],
2931

30-
Cell[BoxData["10"], "Output",
31-
CellChangeTimes->{3.979616855730513*^9},
32-
CellLabel->"Out[1]=",ExpressionUUID->"2a8c9bb2-9b87-47c6-bcad-bef10b961f62"]
32+
Cell[BoxData["\<\"Hello World!\"\>"], "Output",
33+
CellChangeTimes->{3.979616855730513*^9, 3.985428570642437*^9},
34+
CellLabel->"Out[1]=",ExpressionUUID->"ebdac650-4101-40e4-a070-a9a564c141c4"]
3335
}, Open ]]
3436
},
3537
WindowSize->{808, 911},
@@ -50,8 +52,8 @@ CellTagsIndex->{}
5052
(*NotebookFileOutline
5153
Notebook[{
5254
Cell[CellGroupData[{
53-
Cell[576, 22, 295, 5, 29, "Input",ExpressionUUID->"036b8a36-c26c-422e-8935-92fbb0c5b2d0"],
54-
Cell[874, 29, 149, 2, 33, "Output",ExpressionUUID->"2a8c9bb2-9b87-47c6-bcad-bef10b961f62"]
55+
Cell[576, 22, 384, 7, 29, "Input",ExpressionUUID->"036b8a36-c26c-422e-8935-92fbb0c5b2d0"],
56+
Cell[963, 31, 189, 2, 33, "Output",ExpressionUUID->"ebdac650-4101-40e4-a070-a9a564c141c4"]
5557
}, Open ]]
5658
}
5759
]

tests/notebooks/syntax.nb

Lines changed: 121 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
NotebookFileLineBreakTest
1111
NotebookFileLineBreakTest
1212
NotebookDataPosition[ 154, 7]
13-
NotebookDataLength[ 3579, 106]
14-
NotebookOptionsPosition[ 2452, 77]
15-
NotebookOutlinePosition[ 2846, 93]
16-
CellTagsIndexPosition[ 2803, 90]
13+
NotebookDataLength[ 7147, 203]
14+
NotebookOptionsPosition[ 5142, 158]
15+
NotebookOutlinePosition[ 5536, 174]
16+
CellTagsIndexPosition[ 5493, 171]
1717
WindowFrame->Normal*)
1818

1919
(* Beginning of Notebook Content *)
@@ -26,12 +26,13 @@ Cell[BoxData[
2626
CellChangeTimes->{{3.979539634998089*^9, 3.979539634999322*^9}, {
2727
3.979539927309093*^9, 3.979539929150152*^9}, {3.979620208134152*^9,
2828
3.979620230723179*^9}},
29-
CellLabel->"In[2]:=",ExpressionUUID->"036b8a36-c26c-422e-8935-92fbb0c5b2d0"],
29+
CellLabel->"In[21]:=",ExpressionUUID->"036b8a36-c26c-422e-8935-92fbb0c5b2d0"],
3030

3131
Cell[BoxData["11"], "Output",
3232
CellChangeTimes->{
33-
3.979616855730513*^9, {3.9796202171427593`*^9, 3.979620233683016*^9}},
34-
CellLabel->"Out[2]=",ExpressionUUID->"17b38f69-43fc-4fca-8a80-c7b6f48490d2"]
33+
3.979616855730513*^9, {3.9796202171427593`*^9, 3.979620233683016*^9}, {
34+
3.985428739037912*^9, 3.985428763844301*^9}},
35+
CellLabel->"Out[21]=",ExpressionUUID->"48e3fc63-4e67-4e1d-a235-e8d92d817137"]
3536
}, Open ]],
3637

3738
Cell[CellGroupData[{
@@ -40,11 +41,12 @@ Cell[BoxData[
4041
RowBox[{"Sin", "[",
4142
RowBox[{"Pi", "/", "2"}], "]"}]], "Input",
4243
CellChangeTimes->{{3.979620235793231*^9, 3.979620239156399*^9}},
43-
CellLabel->"In[3]:=",ExpressionUUID->"0e8c7e58-8c49-44af-87a7-bb1d1851f02d"],
44+
CellLabel->"In[22]:=",ExpressionUUID->"0e8c7e58-8c49-44af-87a7-bb1d1851f02d"],
4445

4546
Cell[BoxData["1"], "Output",
46-
CellChangeTimes->{3.979620239895835*^9},
47-
CellLabel->"Out[3]=",ExpressionUUID->"5fa08545-aaf2-4473-b0f4-2336feb73e31"]
47+
CellChangeTimes->{
48+
3.979620239895835*^9, {3.985428739071267*^9, 3.985428763879084*^9}},
49+
CellLabel->"Out[22]=",ExpressionUUID->"7762ccd7-a118-419d-a7f2-7bbfa97184cf"]
4850
}, Open ]],
4951

5052
Cell[CellGroupData[{
@@ -54,11 +56,12 @@ Cell[BoxData[
5456
RowBox[{"(",
5557
RowBox[{"Pi", "/", "2"}], ")"}]}]], "Input",
5658
CellChangeTimes->{{3.979620249688081*^9, 3.9796202522971697`*^9}},
57-
CellLabel->"In[4]:=",ExpressionUUID->"bbf0d4cb-3fa5-4e25-801d-2da4070ef869"],
59+
CellLabel->"In[23]:=",ExpressionUUID->"bbf0d4cb-3fa5-4e25-801d-2da4070ef869"],
5860

5961
Cell[BoxData["1"], "Output",
60-
CellChangeTimes->{3.979620257950946*^9},
61-
CellLabel->"Out[4]=",ExpressionUUID->"58d5610f-1e49-45e1-a79f-291eb80045d8"]
62+
CellChangeTimes->{
63+
3.979620257950946*^9, {3.985428739073399*^9, 3.985428763882876*^9}},
64+
CellLabel->"Out[23]=",ExpressionUUID->"ba62f39b-04c7-4f15-9e96-c85ed02a1f23"]
6265
}, Open ]],
6366

6467
Cell[CellGroupData[{
@@ -68,11 +71,89 @@ Cell[BoxData[
6871
RowBox[{"(",
6972
RowBox[{"Pi", "/", "2"}], ")"}], " ", "//", " ", "Sin"}]], "Input",
7073
CellChangeTimes->{{3.979620263540757*^9, 3.9796202731768093`*^9}},
71-
CellLabel->"In[6]:=",ExpressionUUID->"6510beb1-df11-4010-a812-59c24e7ef796"],
74+
CellLabel->"In[24]:=",ExpressionUUID->"6510beb1-df11-4010-a812-59c24e7ef796"],
7275

7376
Cell[BoxData["1"], "Output",
74-
CellChangeTimes->{{3.979620265067712*^9, 3.979620275618889*^9}},
75-
CellLabel->"Out[6]=",ExpressionUUID->"b54a5ad4-fbeb-4f9b-a94d-c1a938cd4424"]
77+
CellChangeTimes->{
78+
3.9854286189981613`*^9, {3.985428739075499*^9, 3.985428763884972*^9}},
79+
CellLabel->"Out[24]=",ExpressionUUID->"86298760-ab9a-4e1a-a6c7-f1a4a69f2d34"]
80+
}, Open ]],
81+
82+
Cell[CellGroupData[{
83+
84+
Cell[BoxData[
85+
RowBox[{"Sin", "[",
86+
RowBox[{"{",
87+
RowBox[{
88+
RowBox[{"Pi", "/", "2"}], ",", " ", "Pi", ",", " ",
89+
RowBox[{"2", "Pi"}]}], "}"}], "]"}]], "Input",
90+
CellChangeTimes->{{3.985428613903851*^9, 3.985428652566546*^9}},
91+
CellLabel->"In[25]:=",ExpressionUUID->"b54a5ad4-fbeb-4f9b-a94d-c1a938cd4424"],
92+
93+
Cell[BoxData[
94+
RowBox[{"{",
95+
RowBox[{"1", ",", "0", ",", "0"}], "}"}]], "Output",
96+
CellChangeTimes->{{3.985428615320787*^9, 3.985428663521401*^9}, {
97+
3.985428739087479*^9, 3.9854287638950872`*^9}},
98+
CellLabel->"Out[25]=",ExpressionUUID->"6a75e702-05c8-4f24-8747-5401adb53a46"]
99+
}, Open ]],
100+
101+
Cell[CellGroupData[{
102+
103+
Cell[BoxData[
104+
RowBox[{
105+
RowBox[{"{",
106+
RowBox[{
107+
RowBox[{"Pi", "/", "2"}], ",", " ", "Pi", ",", " ",
108+
RowBox[{"2", "Pi"}]}], "}"}], " ", "//", " ", "Sin"}]], "Input",
109+
CellChangeTimes->{{3.985428672061173*^9, 3.985428675089314*^9}},
110+
CellLabel->"In[26]:=",ExpressionUUID->"afb67bb6-a1c0-4013-8d8e-9949f05d78ea"],
111+
112+
Cell[BoxData[
113+
RowBox[{"{",
114+
RowBox[{"1", ",", "0", ",", "0"}], "}"}]], "Output",
115+
CellChangeTimes->{
116+
3.985428676889802*^9, {3.985428739089514*^9, 3.985428763898735*^9}},
117+
CellLabel->"Out[26]=",ExpressionUUID->"8d54d858-7da1-4616-954d-779c127e5171"]
118+
}, Open ]],
119+
120+
Cell[CellGroupData[{
121+
122+
Cell[BoxData[
123+
RowBox[{
124+
RowBox[{"{",
125+
RowBox[{
126+
RowBox[{"Pi", "/", "2"}], ",", " ", "Pi", ",", " ",
127+
RowBox[{"2", "Pi"}]}], "}"}], " ", "//", " ",
128+
RowBox[{"Map", "[", "Sin", "]"}]}]], "Input",
129+
CellChangeTimes->{{3.985428684916731*^9, 3.985428697804187*^9}},
130+
CellLabel->"In[27]:=",ExpressionUUID->"6b0d6960-349b-4431-ab6d-f5a6d5731f85"],
131+
132+
Cell[BoxData[
133+
RowBox[{"{",
134+
RowBox[{"1", ",", "0", ",", "0"}], "}"}]], "Output",
135+
CellChangeTimes->{
136+
3.98542869058921*^9, {3.98542873909144*^9, 3.985428763907219*^9}},
137+
CellLabel->"Out[27]=",ExpressionUUID->"01e3a9c9-de78-4073-8260-460b45d1225a"]
138+
}, Open ]],
139+
140+
Cell[CellGroupData[{
141+
142+
Cell[BoxData[
143+
RowBox[{"Sin", " ", "@", " ",
144+
RowBox[{"{",
145+
RowBox[{
146+
RowBox[{"Pi", "/", "2"}], ",", " ", "Pi", ",", " ",
147+
RowBox[{"2", "Pi"}]}], "}"}]}]], "Input",
148+
CellChangeTimes->{{3.985428692373439*^9, 3.985428699899634*^9}},
149+
CellLabel->"In[28]:=",ExpressionUUID->"669d75a2-9301-4d6f-95ba-c8af11bd8bf6"],
150+
151+
Cell[BoxData[
152+
RowBox[{"{",
153+
RowBox[{"1", ",", "0", ",", "0"}], "}"}]], "Output",
154+
CellChangeTimes->{
155+
3.985428700831993*^9, {3.985428739093215*^9, 3.985428763908971*^9}},
156+
CellLabel->"Out[28]=",ExpressionUUID->"3ab1261d-632a-486a-80e6-45e4e9e2ec6a"]
76157
}, Open ]]
77158
},
78159
WindowSize->{808, 911},
@@ -93,20 +174,36 @@ CellTagsIndex->{}
93174
(*NotebookFileOutline
94175
Notebook[{
95176
Cell[CellGroupData[{
96-
Cell[576, 22, 334, 6, 29, "Input",ExpressionUUID->"036b8a36-c26c-422e-8935-92fbb0c5b2d0"],
97-
Cell[913, 30, 200, 3, 33, "Output",ExpressionUUID->"17b38f69-43fc-4fca-8a80-c7b6f48490d2"]
177+
Cell[576, 22, 335, 6, 29, "Input",ExpressionUUID->"036b8a36-c26c-422e-8935-92fbb0c5b2d0"],
178+
Cell[914, 30, 251, 4, 33, "Output",ExpressionUUID->"48e3fc63-4e67-4e1d-a235-e8d92d817137"]
179+
}, Open ]],
180+
Cell[CellGroupData[{
181+
Cell[1202, 39, 225, 4, 29, "Input",ExpressionUUID->"0e8c7e58-8c49-44af-87a7-bb1d1851f02d"],
182+
Cell[1430, 45, 198, 3, 33, "Output",ExpressionUUID->"7762ccd7-a118-419d-a7f2-7bbfa97184cf"]
183+
}, Open ]],
184+
Cell[CellGroupData[{
185+
Cell[1665, 53, 256, 5, 29, "Input",ExpressionUUID->"bbf0d4cb-3fa5-4e25-801d-2da4070ef869"],
186+
Cell[1924, 60, 198, 3, 33, "Output",ExpressionUUID->"ba62f39b-04c7-4f15-9e96-c85ed02a1f23"]
187+
}, Open ]],
188+
Cell[CellGroupData[{
189+
Cell[2159, 68, 257, 5, 29, "Input",ExpressionUUID->"6510beb1-df11-4010-a812-59c24e7ef796"],
190+
Cell[2419, 75, 200, 3, 33, "Output",ExpressionUUID->"86298760-ab9a-4e1a-a6c7-f1a4a69f2d34"]
191+
}, Open ]],
192+
Cell[CellGroupData[{
193+
Cell[2656, 83, 316, 7, 29, "Input",ExpressionUUID->"b54a5ad4-fbeb-4f9b-a94d-c1a938cd4424"],
194+
Cell[2975, 92, 279, 5, 33, "Output",ExpressionUUID->"6a75e702-05c8-4f24-8747-5401adb53a46"]
98195
}, Open ]],
99196
Cell[CellGroupData[{
100-
Cell[1150, 38, 224, 4, 29, "Input",ExpressionUUID->"0e8c7e58-8c49-44af-87a7-bb1d1851f02d"],
101-
Cell[1377, 44, 148, 2, 33, "Output",ExpressionUUID->"5fa08545-aaf2-4473-b0f4-2336feb73e31"]
197+
Cell[3291, 102, 322, 7, 29, "Input",ExpressionUUID->"afb67bb6-a1c0-4013-8d8e-9949f05d78ea"],
198+
Cell[3616, 111, 253, 5, 33, "Output",ExpressionUUID->"8d54d858-7da1-4616-954d-779c127e5171"]
102199
}, Open ]],
103200
Cell[CellGroupData[{
104-
Cell[1562, 51, 255, 5, 29, "Input",ExpressionUUID->"bbf0d4cb-3fa5-4e25-801d-2da4070ef869"],
105-
Cell[1820, 58, 148, 2, 33, "Output",ExpressionUUID->"58d5610f-1e49-45e1-a79f-291eb80045d8"]
201+
Cell[3906, 121, 352, 8, 29, "Input",ExpressionUUID->"6b0d6960-349b-4431-ab6d-f5a6d5731f85"],
202+
Cell[4261, 131, 251, 5, 33, "Output",ExpressionUUID->"01e3a9c9-de78-4073-8260-460b45d1225a"]
106203
}, Open ]],
107204
Cell[CellGroupData[{
108-
Cell[2005, 65, 256, 5, 29, "Input",ExpressionUUID->"6510beb1-df11-4010-a812-59c24e7ef796"],
109-
Cell[2264, 72, 172, 2, 33, "Output",ExpressionUUID->"b54a5ad4-fbeb-4f9b-a94d-c1a938cd4424"]
205+
Cell[4549, 141, 321, 7, 29, "Input",ExpressionUUID->"669d75a2-9301-4d6f-95ba-c8af11bd8bf6"],
206+
Cell[4873, 150, 253, 5, 33, "Output",ExpressionUUID->"3ab1261d-632a-486a-80e6-45e4e9e2ec6a"]
110207
}, Open ]]
111208
}
112209
]

0 commit comments

Comments
 (0)