MCPcopy Index your code
hub / github.com/RustPython/RustPython / iomdParser

Function iomdParser

wasm/notebook/src/parse.js:19–86  ·  view source on GitHub ↗
(fullIomd)

Source from the content-addressed store, hash-verified

17}
18
19export function iomdParser(fullIomd) {
20 const iomdLines = fullIomd.split('\n');
21 const chunks = [];
22 let currentChunkLines = [];
23 let currentEvalType = '';
24 let evalFlags = [];
25 let currentChunkStartLine = 1;
26
27 const newChunkId = (str) => {
28 const hash = hashCode(str);
29 let hashNum = '0';
30 for (const chunk of chunks) {
31 const [prevHash, prevHashNum] = chunk.chunkId.split('_');
32 if (hash === prevHash) {
33 hashNum = (parseInt(prevHashNum, 10) + 1).toString();
34 }
35 }
36 return `${hash}_${hashNum}`;
37 };
38
39 const pushChunk = (endLine) => {
40 const chunkContent = currentChunkLines.join('\n');
41 chunks.push({
42 chunkContent,
43 chunkType: currentEvalType,
44 chunkId: newChunkId(chunkContent),
45 evalFlags,
46 startLine: currentChunkStartLine,
47 endLine,
48 });
49 };
50
51 for (const [i, line] of iomdLines.entries()) {
52 const lineNum = i + 1; // uses 1-based indexing
53 if (line.slice(0, 2) === '%%') {
54 // if line start with '%%', a new chunk has started
55 // push the current chunk (unless it's on line 1), then reset
56 if (lineNum !== 1) {
57 // DON'T push a chunk if we're only on line 1
58 pushChunk(lineNum - 1);
59 }
60 // reset the currentChunk state
61 currentChunkStartLine = lineNum;
62 currentChunkLines = [];
63 evalFlags = [];
64 // find the first char on this line that isn't '%'
65 let lineColNum = 0;
66 while (line[lineColNum] === '%') {
67 lineColNum += 1;
68 }
69 const chunkFlags = line
70 .slice(lineColNum)
71 .split(/[ \t]+/)
72 .filter((s) => s !== '');
73 if (chunkFlags.length > 0) {
74 // if there is a captured group, update the eval type
75 [currentEvalType, ...evalFlags] = chunkFlags;
76 }

Callers 1

executeNotebookFunction · 0.90

Calls 5

pushChunkFunction · 0.85
splitMethod · 0.45
sliceMethod · 0.45
filterMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected