-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathjupyter-embed.ts
More file actions
833 lines (760 loc) · 22.9 KB
/
jupyter-embed.ts
File metadata and controls
833 lines (760 loc) · 22.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
/*
* jupyter-embed.ts
*
* Copyright (C) 2022 by Posit, PBC
*/
import { resourcePath } from "../resources.ts";
import { getNamedLifetime, ObjectWithLifetime } from "../lifetimes.ts";
import {
jupyterAssets,
jupyterFromFile,
jupyterToMarkdown,
kQuartoOutputDisplay,
kQuartoOutputOrder,
} from "../jupyter/jupyter.ts";
import {
kCellLabel,
kFigDpi,
kFigFormat,
kFigPos,
kKeepHidden,
kRenderFileLifetime,
} from "../../config/constants.ts";
import {
isHtmlCompatible,
isHtmlOutput,
isIpynbOutput,
isLatexOutput,
isMarkdownOutput,
isPresentationOutput,
} from "../../config/format.ts";
import { resolveParams } from "../../command/render/flags.ts";
import {
RenderContext,
RenderFlags,
RenderServices,
} from "../../command/render/types.ts";
import {
JupyterAssets,
JupyterCell,
JupyterCellOutput,
} from "../jupyter/types.ts";
import {
basename,
dirname,
extname,
isAbsolute,
join,
} from "../../deno_ral/path.ts";
import { languages } from "../handlers/base.ts";
import {
extractJupyterWidgetDependencies,
includesForJupyterWidgetDependencies,
} from "./widgets.ts";
import { globalTempContext } from "../temp.ts";
import { partitionMarkdown } from "../pandoc/pandoc-partition.ts";
import { dirAndStem, normalizePath, safeExistsSync } from "../path.ts";
import { InternalError } from "../lib/error.ts";
import { ipynbFormat } from "../../format/ipynb/format-ipynb.ts";
import {
kQmdIPynb,
NotebookMetadata,
} from "../../render/notebook/notebook-types.ts";
import { ProjectContext } from "../../project/types.ts";
import { logProgress } from "../log.ts";
import * as ld from "../../../src/core/lodash.ts";
import { texSafeFilename } from "../tex.ts";
export interface JupyterNotebookAddress {
path: string;
ids?: string[];
indexes?: number[];
}
export interface JupyterMarkdownOptions extends Record<string, unknown> {
echo?: boolean;
warning?: boolean;
asis?: boolean;
preserveCellMetadata?: boolean;
filter?: (cell: JupyterCell) => boolean;
}
interface JupyterNotebookOutputCache extends ObjectWithLifetime {
cache: Record<
string,
{ outputs: JupyterCellOutput[]; title?: string }
>;
}
const kEcho = "echo";
const kWarning = "warning";
const kOutput = "output";
const kHashRegex = /(.*?)#(.*)/;
const kIndexRegex = /(.*)\[([0-9,-]*)\]/;
const placeholderRegex = () => {
return /<!-- 12A0366C\|(.*)\|:(.*?) \| (.*?) \| (.*?) -->/g;
};
const kNotebookCache = "notebook-cache";
// Parses a notebook address string into a file path with
// an optional list of ids or list of cell indexes.
export function parseNotebookAddress(
path: string,
): JupyterNotebookAddress | undefined {
// This is a hash based path
const hashResult = path.match(kHashRegex);
if (hashResult) {
const path = hashResult[1];
if (isEmbeddable(path)) {
return {
path,
ids: resolveCellIds(hashResult[2]),
};
} else {
unsupportedEmbed(path);
}
}
// This is an index based path
const indexResult = path.match(kIndexRegex);
if (indexResult) {
const path = indexResult[1];
if (isEmbeddable(path)) {
return {
path,
indexes: resolveRange(indexResult[2]),
};
} else {
unsupportedEmbed(path);
}
}
// This is the path to a notebook
if (isEmbeddable(path)) {
return {
path,
};
} else {
unsupportedEmbed(path);
}
}
function unsupportedEmbed(path: string) {
if (extname(path) === "") {
throw new Error(
`Unable to embed content from ${path} - there is no extension on the file path.`,
);
} else {
throw new Error(
`Unable to embed content from ${path} - embedding content is not supported for this file type.`,
);
}
}
export async function ensureNotebookContext(
markdown: string,
services: RenderServices,
context: ProjectContext,
) {
const regex = placeholderRegex();
let match = regex.exec(markdown);
const nbsToRender: Array<{ path: string; name: string }> = [];
while (match) {
// Parse the address and if this is a notebook
// then proceed with the replacement
const inputPath = match[1];
const nbAddr = match[2];
const nbAddress = parseNotebookAddress(nbAddr);
if (!nbAddress) {
throw new InternalError(
"Unexpected - there must be a notebook address since we matched.",
);
}
const nbAbsPath = resolveNbPath(inputPath, nbAddress.path, context);
if (!isNotebook(nbAbsPath)) {
if (!services.notebook.get(nbAbsPath, context)?.[kQmdIPynb]) {
nbsToRender.push({ path: nbAbsPath, name: nbAddress.path });
}
}
match = regex.exec(markdown);
}
const uniqueRenders = ld.uniqBy(
nbsToRender,
(nb: { path: string }) => nb.path,
) as Array<{ path: string; name: string }>;
if (uniqueRenders.length) {
logProgress("Rendering qmd embeds");
}
let count = 0;
for (const nb of uniqueRenders) {
logProgress(`[${++count}/${uniqueRenders.length}] ${nb.name}`);
// See if we can get a nice title
const partitioned = partitionMarkdown(Deno.readTextFileSync(nb.path));
let notebookMeta: NotebookMetadata | undefined;
const filename = basename(nb.path);
if (partitioned.yaml && partitioned.yaml.title) {
notebookMeta = {
title: partitioned.yaml.title as string,
filename,
};
} else {
notebookMeta = {
title: filename,
filename: filename,
};
}
// Render the document
await services.notebook.render(
nb.path,
ipynbFormat(),
kQmdIPynb,
services,
notebookMeta,
context,
);
}
}
// Creates a placeholder that will later be replaced with
// rendered notebook markdown. Note that the placeholder
// must stipulate all the information required to generate the
// markdown (e.g. options, output indexes and so on)
export function notebookMarkdownPlaceholder(
input: string,
nbPath: string,
options: JupyterMarkdownOptions,
outputs?: string,
) {
return `<!-- 12A0366C|${input}|:${nbPath} | ${outputs || ""} | ${
optionsToPlaceholder(options)
} -->\n`;
}
// Replaces any notebook markdown placeholders with the
// rendered contents.
export async function replaceNotebookPlaceholders(
to: string,
context: RenderContext,
flags: RenderFlags,
markdown: string,
services: RenderServices,
) {
const assetCache: Record<string, JupyterAssets> = {};
const regex = placeholderRegex();
let match = regex.exec(markdown);
let includes;
const notebooks: string[] = [];
while (match) {
// Parse the address and if this is a notebook
// then proceed with the replacement
const inputPath = match[1];
const nbAddressStr = match[2];
const nbAddress = parseNotebookAddress(nbAddressStr);
if (!nbAddress) {
throw new InternalError(
"Expected to find a valid notebook address string for an embed.",
);
}
// This holds the notebook path that will end being used
// for reading the embed
const nbAbsPath = resolveNbPath(inputPath, nbAddress.path, context.project);
let assets = assetCache[inputPath];
if (!assets) {
assets = jupyterAssets(
inputPath,
to,
);
assetCache[inputPath] = assets;
}
if (nbAddress) {
// If a list of outputs are provided, resolve that range
const outputsStr = match[3];
const nbOutputs = outputsStr ? resolveRange(outputsStr) : undefined;
// If cell options are provided, resolve those
const placeholderStr = match[4];
const nbOptions = placeholderStr
? placeholderToOptions(placeholderStr)
: {};
// Compute appropriate includes based upon the note
// dependendencies
const notebookIncludes = () => {
if (safeExistsSync(nbAbsPath)) {
const notebook = jupyterFromNotebookOrQmd(
nbAbsPath,
services,
context.project,
);
const dependencies = isHtmlOutput(context.format.pandoc)
? extractJupyterWidgetDependencies(notebook)
: undefined;
if (dependencies) {
const tempDir = globalTempContext().createDir();
return includesForJupyterWidgetDependencies(
[dependencies],
tempDir,
);
} else {
return undefined;
}
} else {
throw new Error(
`Unable to embed content from notebook '${nbAddress.path}'\nThe file ${nbAbsPath} doesn't exist or cannot be read.`,
);
}
};
includes = notebookIncludes();
// Render the notebook markdown
const nbMarkdown = await notebookMarkdown(
inputPath,
nbAddress,
nbAbsPath,
assets,
context,
flags,
nbOptions,
nbOutputs,
);
if (nbMarkdown && !notebooks.includes(nbAddress.path)) {
notebooks.push(nbAddress.path);
}
// Replace the placeholders with the rendered markdown
markdown = markdown.replaceAll(match[0], nbMarkdown || "");
}
match = regex.exec(markdown);
}
regex.lastIndex = 0;
const supporting = Object.values(assetCache).map((assets) => {
return join(assets.base_dir, assets.supporting_dir);
});
return {
notebooks,
includes,
markdown,
supporting,
};
}
function resolveNbPath(input: string, path: string, context: ProjectContext) {
// If this is a project, absolute means project relative
if (!context.isSingleFile) {
const projectMatch = path.match(/^[\\/](.*)/);
if (projectMatch) {
return join(context.dir, projectMatch[1]);
}
}
if (isAbsolute(path)) {
return path;
} else {
if (isAbsolute(input)) {
return join(dirname(input), path);
} else {
const baseDir = context.isSingleFile ? Deno.cwd() : context.dir;
const result = join(baseDir, dirname(input), path);
return result;
}
}
}
// Gets the markdown for a specific notebook and set of options
export async function notebookMarkdown(
inputPath: string,
nbAddress: JupyterNotebookAddress,
nbAbsPath: string,
assets: JupyterAssets,
context: RenderContext,
flags: RenderFlags,
options?: JupyterMarkdownOptions,
outputs?: number[],
) {
// Get the cell outputs for this notebook
const notebookInfo = await getCachedNotebookInfo(
inputPath,
nbAddress,
assets,
context,
flags,
options,
outputs,
);
// This notebook is empty
if (notebookInfo.outputs.length === 0) {
return undefined;
}
// Wrap any injected cells with a div that includes a back link to
// the notebook that originated the cells
const notebookMarkdown = (
nbAbsPath: string,
cells: JupyterCellOutput[],
title?: string,
) => {
const cellId = cells.length > 0 ? cells[0].id || "" : "";
const markdown = [
"",
`:::{.quarto-embed-nb-cell notebook="${nbAbsPath}" ${
title ? `notebook-title="${title}"` : ""
} notebook-cellId="${cellId}"}`,
];
const cellOutput = cells.map((cell) => cell.markdown).join("");
markdown.push("");
markdown.push(cellOutput);
markdown.push("");
markdown.push(":::");
return markdown.join("\n");
};
if (nbAddress.ids) {
// If cellIds are present, filter the notebook to only include
// those cells (cellIds can eiher be an explicitly set cellId, a label in the
// cell metadata, or a tag on a cell that matches an id)
const theCells = nbAddress.ids.map((id) => {
const cell = cellForId(id, notebookInfo.outputs);
if (cell === undefined) {
throw new Error(
`The cell ${id} does not exist in notebook`,
);
} else if (cell.markdown.trim() === "") {
throw new Error(
`The notebook ${nbAddress.path} doesn't contain output to embed with the cell id, tag, or label '${id}'. Please be sure to have executed any cells that you are embedding.`,
);
} else {
return cell;
}
});
return notebookMarkdown(nbAbsPath, theCells, notebookInfo.title);
} else if (nbAddress.indexes) {
// Filter and sort based upon cell indexes
const theCells = nbAddress.indexes.map((idx) => {
if (idx < 1 || idx > notebookInfo.outputs.length) {
throw new Error(
`The cell index ${idx} isn't within the range of cells`,
);
} else {
const cell = notebookInfo.outputs[idx - 1];
if (cell.markdown.trim() === "") {
throw new Error(
`The notebook ${nbAddress.path} doesn't contain output to embed for the cell in position ${idx}. Please be sure to have executed any cells that you are embedding.`,
);
}
return cell;
}
});
return notebookMarkdown(nbAbsPath, theCells, notebookInfo.title);
} else {
// Return all the cell outputs as there is no addtional
// specification of cells
const notebookMd = notebookMarkdown(
nbAbsPath,
notebookInfo.outputs,
notebookInfo.title,
);
if (notebookMd.trim() === "") {
throw new Error(
`The notebook ${nbAddress.path} doesn't contain output to embed. Please be sure to have executed any cells that you are embedding.`,
);
} else {
return notebookMd;
}
}
}
const isNotebook = (path: string) => {
return extname(path) === ".ipynb";
};
const isEmbeddable = (path: string) => {
return isNotebook(path) || extname(path) === ".qmd";
};
// Caches the notebook info for a a particular notebook and
// set of options. Since the markdown is what is cached,
// the cache will include options that control markdown output
// when determining whether it can use cached contents.
async function getCachedNotebookInfo(
inputPath: string,
nbAddress: JupyterNotebookAddress,
assets: JupyterAssets,
context: RenderContext,
flags: RenderFlags,
options?: JupyterMarkdownOptions,
outputs?: number[],
) {
// We can cache outputs on a per rendered file basis to
// improve performance
const lifetime = getNamedLifetime(kRenderFileLifetime);
if (lifetime === undefined) {
throw new InternalError(`named lifetime ${kRenderFileLifetime} not found`);
}
const nbCache =
lifetime.get(kNotebookCache) as unknown as JupyterNotebookOutputCache ||
{
cache: {},
cleanup: () => {
},
};
// Compute a cache key
const cacheKey = notebookCacheKey(
inputPath,
nbAddress,
assets,
options,
outputs,
);
if (!nbCache.cache[cacheKey]) {
// Render the notebook and place it in the cache
// Read and filter notebook
const nbAbsPath = resolveNbPath(inputPath, nbAddress.path, context.project);
// See if we can resolve non-notebooks. Note that this
// requires that we have pre-rendered any notebooks that we discover
// along the embed pipeline
const notebook = jupyterFromNotebookOrQmd(
nbAbsPath,
context.options.services,
context.project,
);
if (options) {
notebook.cells = notebook.cells.map((cell) => {
if (options.echo !== undefined) {
cell.metadata[kEcho] = options.echo;
}
if (options.warning !== undefined) {
cell.metadata[kWarning] = options.warning;
}
if (options.asis !== undefined) {
cell.metadata[kOutput] = options.asis ? true : false;
}
// Filter outputs if so desired
if (outputs && cell.outputs) {
cell.outputs = cell.outputs.map((output, index) => {
const oneBasedIdx = index + 1;
output.metadata = output.metadata || {};
if (!outputs.includes(oneBasedIdx)) {
output.metadata[kQuartoOutputDisplay] = false;
} else {
const explicitOrder = outputs.indexOf(oneBasedIdx);
if (explicitOrder > -1) {
output.metadata[kQuartoOutputOrder] = explicitOrder;
}
}
return output;
});
}
return cell;
});
}
// Filter the cells, if applicable
if (options?.filter) {
notebook.cells = notebook.cells.filter((cell) => {
if (options?.filter) {
return options?.filter(cell);
} else {
return true;
}
});
}
// Get the markdown for the notebook
const format = context.format;
const executeOptions = {
target: context.target,
resourceDir: resourcePath(),
tempDir: context.options.services.temp.createDir(),
dependencies: true,
libDir: context.libDir,
format: context.format,
projectDir: context.project?.dir,
cwd: flags.executeDir ||
dirname(normalizePath(context.target.source)),
params: resolveParams(flags.params, flags.paramsFile),
quiet: flags.quiet,
previewServer: context.options.previewServer,
handledLanguages: languages(),
project: context.project,
};
const [dir, stem] = dirAndStem(nbAddress.path);
const outputPrefix = texSafeFilename(
`${dir !== "." ? dir + "-" : ""}${stem}`,
);
const result = await jupyterToMarkdown(
notebook,
{
executeOptions,
language: notebook.metadata.kernelspec.language.toLowerCase(),
assets,
execute: format.execute,
keepHidden: format.render[kKeepHidden],
preserveCellMetadata: options?.preserveCellMetadata,
toHtml: isHtmlCompatible(format),
toLatex: isLatexOutput(format.pandoc),
toMarkdown: isMarkdownOutput(format),
toIpynb: isIpynbOutput(format.pandoc),
toPresentation: isPresentationOutput(format.pandoc),
figFormat: format.execute[kFigFormat],
figDpi: format.execute[kFigDpi],
figPos: format.render[kFigPos],
fixups: "minimal",
outputPrefix,
},
);
// Compute the notebook title
const title = findTitle(result.cellOutputs);
// Place the outputs in the cache
nbCache.cache[cacheKey] = { outputs: result.cellOutputs, title };
lifetime.attach(nbCache, kNotebookCache);
}
return nbCache.cache[cacheKey];
}
// Tries to find a title within a Notebook
function findTitle(cells: JupyterCellOutput[]) {
for (const cell of cells) {
const partitioned = partitionMarkdown(cell.markdown);
if (partitioned.yaml?.title) {
return partitioned.yaml.title as string;
} else if (partitioned.headingText) {
return partitioned.headingText;
}
}
return undefined;
}
// Create a notebook hash key for the cache
// that incorporates options that affect markdown
// output
function notebookCacheKey(
inputPath: string,
nbAddress: JupyterNotebookAddress,
assets: JupyterAssets,
nbOptions?: JupyterMarkdownOptions,
nbOutputs?: number[],
) {
const optionsKey = nbOptions
? Object.keys(nbOptions).reduce((current, key) => {
if (
nbOptions[key] !== undefined && typeof (nbOptions[key] === "boolean")
) {
return current + `${key}:${String(nbOptions[key])}`;
} else {
return current;
}
}, "")
: "";
const coreKey = optionsKey
? `${inputPath}-${nbAddress.path}-${optionsKey}`
: `${inputPath}-${nbAddress.path}`;
const outputsKey = nbOutputs ? nbOutputs.join(",") : "";
return `${coreKey}:${outputsKey}:${assets.supporting_dir}`;
}
function optionsToPlaceholder(options: JupyterMarkdownOptions) {
return Object.keys(options).map((key) => {
return `${key}:${String(options[key])}`;
}).join(",");
}
function placeholderToOptions(placeholder: string) {
const parts = placeholder.split(",");
const options: JupyterMarkdownOptions = {};
for (const part of parts) {
const kv = part.split(":");
if (kv.length > 1) {
const key = part.split(":")[0];
const value = part.split(":").slice(1).join(":");
options[key] = value.toLowerCase() === "true";
} else {
throw new Error("Unexpected placeholder for notebook option: " + part);
}
}
return options;
}
// Finds a cell matching an id. It will first try an explicit
// matching cell Id, then a cell label (in the code chunk front matter),
// and otherwise look for a cell(s) with that tag
function cellForId(id: string, cells: JupyterCellOutput[]) {
for (const cell of cells) {
// cellId can either by a literal cell Id, or a tag with that value
const hasId = cell.id ? id === cell.id : false;
if (hasId) {
// It's an ID
return cell;
}
// Check label
const hasLabel = cell.options && cell.options[kCellLabel]
? id === cell.options[kCellLabel]
: false;
if (hasLabel) {
// It matches a label
return cell;
}
// Check tags
const hasTag = cell.metadata && cell.metadata.tags
? cell.metadata.tags.find((tag) => id === tag) !==
undefined
: false;
if (hasTag) {
return cell;
}
// Check contents of the cell itself
if (cell.markdown && cell.markdown.includes(id)) {
// Now look more carefully to see if id is indeed an attr
/*
{#fig-coo}
{#fig-coo-1}
{#fig-coo .bar}
{#fig-coo }
{#fig-coo .car class="cool"}
{#fig-coo .car class="cool" height="20 and I {hate you}"}
{#fig-coo .car .foo}
*/
if (cell.markdown.match(new RegExp(`.*{#${id}(\\s+[^}]*)*}$`, "gm"))) {
return cell;
}
}
}
}
// Parses a string into one or more cellids.
// Syntax like:
// notebook.ipynb#cellid1
// notebook.ipynb#cellid1,cellid2,cellid3
function resolveCellIds(hash?: string) {
if (hash && hash.indexOf(",") > 0) {
return hash.split(",");
} else if (hash) {
return [hash];
} else {
return undefined;
}
}
// Parses a string with one more numbers or ranges into
// a list of numbers, in order.
// Syntax like:
// notebook.ipynb[0]
// notebook.ipynb[0,1]
// notebook.ipynb[0-2]
// notebook.ipynb[2,0-1]
// notebook.ipynb[2,6-4]
function resolveRange(rangeRaw?: string) {
if (rangeRaw) {
const result: number[] = [];
const ranges = rangeRaw.split(",");
ranges.forEach((range) => {
if (range.indexOf("-") > -1) {
// This is range
const parts = range.split("-");
const start = parseInt(parts[0]);
const end = parseInt(parts[1]);
const step = start > end ? -1 : 1;
for (let i = start; step > 0 ? i <= end : i >= end; i = i + step) {
result.push(i);
}
} else {
// This is raw value
result.push(parseInt(range));
}
});
return result;
} else {
return undefined;
}
}
function jupyterFromNotebookOrQmd(
nbAbsPath: string,
services: RenderServices,
project: ProjectContext,
) {
// See if we can resolve non-notebooks. Note that this
// requires that we have pre-rendered any notebooks that we discover
// along the embed pipeline
let jupyterNotebookPath = nbAbsPath;
if (!isNotebook(jupyterNotebookPath)) {
const notebook = services.notebook.get(
nbAbsPath,
project,
);
if (notebook?.[kQmdIPynb] && notebook[kQmdIPynb]) {
jupyterNotebookPath = notebook[kQmdIPynb].path;
} else {
throw new InternalError(
`Expected an 'ipynb' file to be present for the 'qmd' file ${nbAbsPath}`,
);
}
}
return jupyterFromFile(jupyterNotebookPath);
}