chrisjsewell / ipypublish
1 3
import io
2 3
import json
3 3
import panflute as pf
4 3
from jsonextended import edict
5 3
from six import u
6

7 3
from panflute import Element, Doc  # noqa: F401
8 3
from types import FunctionType  # noqa: F401
9

10 3
from ipypublish.filters_pandoc.prepare_labels import main
11

12

13 3
def apply_to_json(in_json, filter_func):
14
    # type: (dict, FunctionType) -> dict
15 3
    f = io.StringIO(u(json.dumps(in_json)))
16 3
    doc = pf.load(f)
17 3
    new_doc = filter_func(doc)  # type: Doc
18 3
    return new_doc.to_json()
19

20

21 3
def test_basic():
22
    """
23
    $a=1$ {#a b=$2$}
24

25
    ![a](a/b.jpg){b .x a=$1$ b=2}
26
    """
27 3
    in_json = {
28
        "meta": {},
29
        "pandoc-api-version": [1, 17, 5, 1],
30
        "blocks": [
31
            {
32
                "t": "Para",
33
                "c": [
34
                    {"t": "Math", "c": [{"t": "InlineMath"}, "a=1"]},
35
                    {"t": "Space"},
36
                    {"t": "Str", "c": "{#a"},
37
                    {"t": "Space"},
38
                    {"t": "Str", "c": ".a"},
39
                    {"t": "Space"},
40
                    {"t": "Str", "c": "b="},
41
                    {"t": "Math", "c": [{"t": "InlineMath"}, "2"]},
42
                    {"t": "Str", "c": "}"},
43
                ],
44
            },
45
            {
46
                "t": "Para",
47
                "c": [
48
                    {
49
                        "t": "Image",
50
                        "c": [
51
                            ["b", ["x"], [["a", "$1$"], ["b", "2"]]],
52
                            [{"t": "Str", "c": "a"}],
53
                            ["a/b.jpg", "fig:"],
54
                        ],
55
                    }
56
                ],
57
            },
58
        ],
59
    }
60

61 3
    out_json = apply_to_json(in_json, main)
62

63 3
    assert (
64
        edict.diff(
65
            out_json,
66
            {
67
                "pandoc-api-version": [1, 17, 5, 1],
68
                "meta": {
69
                    "$$references": {
70
                        "t": "MetaMap",
71
                        "c": {
72
                            "a": {
73
                                "t": "MetaMap",
74
                                "c": {
75
                                    "type": {"t": "MetaString", "c": "Math"},
76
                                    "number": {"t": "MetaString", "c": "1"},
77
                                },
78
                            },
79
                            "b": {
80
                                "t": "MetaMap",
81
                                "c": {
82
                                    "type": {"t": "MetaString", "c": "Image"},
83
                                    "number": {"t": "MetaString", "c": "1"},
84
                                },
85
                            },
86
                        },
87
                    }
88
                },
89
                "blocks": [
90
                    {
91
                        "t": "Para",
92
                        "c": [
93
                            {
94
                                "t": "Span",
95
                                "c": [
96
                                    ["a", ["labelled-Math", "a"], [["b", "2"]]],
97
                                    [{"t": "Math", "c": [{"t": "InlineMath"}, "a=1"]}],
98
                                ],
99
                            }
100
                        ],
101
                    },
102
                    {
103
                        "t": "Para",
104
                        "c": [
105
                            {
106
                                "t": "Image",
107
                                "c": [
108
                                    ["b", ["x"], [["a", "$1$"], ["b", "2"]]],
109
                                    [{"t": "Str", "c": "a"}],
110
                                    ["a/b.jpg", "fig:"],
111
                                ],
112
                            }
113
                        ],
114
                    },
115
                ],
116
            },
117
        )
118
        == {}
119
    )
120

121

122 3
def test_multiple_on_line():
123
    """"
124
    $a=1$ {#a b=$2$} $g=3$ {#gid}
125

126
    ![a](a/b.jpg)
127
    """
128 3
    in_json = {
129
        "pandoc-api-version": [1, 17, 5, 1],
130
        "meta": {},
131
        "blocks": [
132
            {
133
                "t": "Para",
134
                "c": [
135
                    {"t": "Math", "c": [{"t": "InlineMath"}, "a=1"]},
136
                    {"t": "Space"},
137
                    {"t": "Str", "c": "{#a"},
138
                    {"t": "Space"},
139
                    {"t": "Str", "c": "b="},
140
                    {"t": "Math", "c": [{"t": "InlineMath"}, "2"]},
141
                    {"t": "Str", "c": "}"},
142
                    {"t": "Space"},
143
                    {"t": "Math", "c": [{"t": "InlineMath"}, "g=3"]},
144
                    {"t": "Space"},
145
                    {"t": "Str", "c": "{#gid}"},
146
                ],
147
            },
148
            {
149
                "t": "Para",
150
                "c": [
151
                    {
152
                        "t": "Image",
153
                        "c": [
154
                            ["", [], []],
155
                            [{"t": "Str", "c": "a"}],
156
                            ["a/b.jpg", "fig:"],
157
                        ],
158
                    }
159
                ],
160
            },
161
        ],
162
    }
163

164 3
    out_json = apply_to_json(in_json, main)
165

166 3
    assert (
167
        edict.diff(
168
            out_json,
169
            {
170
                "pandoc-api-version": [1, 17, 5, 1],
171
                "meta": {
172
                    "$$references": {
173
                        "t": "MetaMap",
174
                        "c": {
175
                            "a": {
176
                                "t": "MetaMap",
177
                                "c": {
178
                                    "type": {"t": "MetaString", "c": "Math"},
179
                                    "number": {"t": "MetaString", "c": "1"},
180
                                },
181
                            },
182
                            "gid": {
183
                                "t": "MetaMap",
184
                                "c": {
185
                                    "type": {"t": "MetaString", "c": "Math"},
186
                                    "number": {"t": "MetaString", "c": "2"},
187
                                },
188
                            },
189
                        },
190
                    }
191
                },
192
                "blocks": [
193
                    {
194
                        "t": "Para",
195
                        "c": [
196
                            {
197
                                "t": "Span",
198
                                "c": [
199
                                    ["a", ["labelled-Math"], [["b", "2"]]],
200
                                    [{"t": "Math", "c": [{"t": "InlineMath"}, "a=1"]}],
201
                                ],
202
                            },
203
                            {"t": "Space"},
204
                            {
205
                                "t": "Span",
206
                                "c": [
207
                                    ["gid", ["labelled-Math"], []],
208
                                    [{"t": "Math", "c": [{"t": "InlineMath"}, "g=3"]}],
209
                                ],
210
                            },
211
                        ],
212
                    },
213
                    {
214
                        "t": "Para",
215
                        "c": [
216
                            {
217
                                "t": "Image",
218
                                "c": [
219
                                    ["", [], []],
220
                                    [{"t": "Str", "c": "a"}],
221
                                    ["a/b.jpg", "fig:"],
222
                                ],
223
                            }
224
                        ],
225
                    },
226
                ],
227
            },
228
        )
229
        == {}
230
    )
231

232

233 3
def test_with_tables():
234
    """
235
    Some text
236

237
    a b c
238
    - - -
239
    1 2 3
240
    4 5 6
241

242
    Table: Caption. {#tbl:id}
243
    """
244 3
    in_json = {
245
        "pandoc-api-version": [1, 17, 5, 1],
246
        "meta": {},
247
        "blocks": [
248
            {
249
                "t": "Para",
250
                "c": [
251
                    {"t": "Str", "c": "Some"},
252
                    {"t": "Space"},
253
                    {"t": "Str", "c": "text"},
254
                ],
255
            },
256
            {
257
                "t": "Table",
258
                "c": [
259
                    [
260
                        {"t": "Str", "c": "Caption."},
261
                        {"t": "Space"},
262
                        {"t": "Str", "c": "{#tbl:id}"},
263
                    ],
264
                    [
265
                        {"t": "AlignDefault"},
266
                        {"t": "AlignDefault"},
267
                        {"t": "AlignDefault"},
268
                    ],
269
                    [0, 0, 0],
270
                    [
271
                        [{"t": "Plain", "c": [{"t": "Str", "c": "a"}]}],
272
                        [{"t": "Plain", "c": [{"t": "Str", "c": "b"}]}],
273
                        [{"t": "Plain", "c": [{"t": "Str", "c": "c"}]}],
274
                    ],
275
                    [
276
                        [
277
                            [{"t": "Plain", "c": [{"t": "Str", "c": "1"}]}],
278
                            [{"t": "Plain", "c": [{"t": "Str", "c": "2"}]}],
279
                            [{"t": "Plain", "c": [{"t": "Str", "c": "3"}]}],
280
                        ],
281
                        [
282
                            [{"t": "Plain", "c": [{"t": "Str", "c": "4"}]}],
283
                            [{"t": "Plain", "c": [{"t": "Str", "c": "5"}]}],
284
                            [{"t": "Plain", "c": [{"t": "Str", "c": "6"}]}],
285
                        ],
286
                    ],
287
                ],
288
            },
289
        ],
290
    }
291

292 3
    out_json = apply_to_json(in_json, main)
293

294 3
    assert (
295
        edict.diff(
296
            out_json,
297
            {
298
                "pandoc-api-version": [1, 17, 5, 1],
299
                "meta": {
300
                    "$$references": {
301
                        "t": "MetaMap",
302
                        "c": {
303
                            "tbl:id": {
304
                                "t": "MetaMap",
305
                                "c": {
306
                                    "type": {"t": "MetaString", "c": "Table"},
307
                                    "number": {"t": "MetaString", "c": "1"},
308
                                },
309
                            }
310
                        },
311
                    }
312
                },
313
                "blocks": [
314
                    {
315
                        "t": "Para",
316
                        "c": [
317
                            {"t": "Str", "c": "Some"},
318
                            {"t": "Space"},
319
                            {"t": "Str", "c": "text"},
320
                        ],
321
                    },
322
                    {
323
                        "t": "Div",
324
                        "c": [
325
                            ["tbl:id", ["labelled-Table"], []],
326
                            [
327
                                {
328
                                    "t": "Table",
329
                                    "c": [
330
                                        [{"t": "Str", "c": "Caption."}, {"t": "Space"}],
331
                                        [
332
                                            {"t": "AlignDefault"},
333
                                            {"t": "AlignDefault"},
334
                                            {"t": "AlignDefault"},
335
                                        ],
336
                                        [0, 0, 0],
337
                                        [
338
                                            [
339
                                                {
340
                                                    "t": "Plain",
341
                                                    "c": [{"t": "Str", "c": "a"}],
342
                                                }
343
                                            ],
344
                                            [
345
                                                {
346
                                                    "t": "Plain",
347
                                                    "c": [{"t": "Str", "c": "b"}],
348
                                                }
349
                                            ],
350
                                            [
351
                                                {
352
                                                    "t": "Plain",
353
                                                    "c": [{"t": "Str", "c": "c"}],
354
                                                }
355
                                            ],
356
                                        ],
357
                                        [
358
                                            [
359
                                                [
360
                                                    {
361
                                                        "t": "Plain",
362
                                                        "c": [{"t": "Str", "c": "1"}],
363
                                                    }
364
                                                ],
365
                                                [
366
                                                    {
367
                                                        "t": "Plain",
368
                                                        "c": [{"t": "Str", "c": "2"}],
369
                                                    }
370
                                                ],
371
                                                [
372
                                                    {
373
                                                        "t": "Plain",
374
                                                        "c": [{"t": "Str", "c": "3"}],
375
                                                    }
376
                                                ],
377
                                            ],
378
                                            [
379
                                                [
380
                                                    {
381
                                                        "t": "Plain",
382
                                                        "c": [{"t": "Str", "c": "4"}],
383
                                                    }
384
                                                ],
385
                                                [
386
                                                    {
387
                                                        "t": "Plain",
388
                                                        "c": [{"t": "Str", "c": "5"}],
389
                                                    }
390
                                                ],
391
                                                [
392
                                                    {
393
                                                        "t": "Plain",
394
                                                        "c": [{"t": "Str", "c": "6"}],
395
                                                    }
396
                                                ],
397
                                            ],
398
                                        ],
399
                                    ],
400
                                }
401
                            ],
402
                        ],
403
                    },
404
                ],
405
            },
406
        )
407
        == {}
408
    )

Read our documentation on viewing source code .

Loading