From d6b8fe96e224f47f20dee2b19cf8dd6aa35c80fb Mon Sep 17 00:00:00 2001 From: Claudy Forrest <41589268+c-forrest@users.noreply.github.com> Date: Sun, 1 Dec 2024 21:54:27 -0600 Subject: [PATCH] feat(snippts.js): parse snippets with line selection (#83) * feat(snippts.js): parse snippets with line selection * fix: discard code formatting * fix: add end-of-file marker * fix: remove regex * fix: remove format changes * apply suggestions --- oi-wiki-export-typst/snippet.js | 21 +++++++++++++++------ oi-wiki-export/snippet.js | 18 ++++++++++++++---- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/oi-wiki-export-typst/snippet.js b/oi-wiki-export-typst/snippet.js index 638a76e..f85bd04 100644 --- a/oi-wiki-export-typst/snippet.js +++ b/oi-wiki-export-typst/snippet.js @@ -5,15 +5,24 @@ const SNIPPET_TOKEN = '--8<-- ' let oi_wiki_root = '.' -function resolvePath (snip, spacesAtStart) { - let str = snip.substring(SNIPPET_TOKEN.length + spacesAtStart) +function resolvePath(snip, spacesAtStart) { + const str = snip.substring(SNIPPET_TOKEN.length + spacesAtStart) + let res = { "path": str, "beg_line": undefined, "end_line": undefined } if ((str.startsWith('"') && str.endsWith('"')) || (str.startsWith("'") && str.endsWith("'"))) { - str = str.substring(1, str.length - 1) + const strs = str.substring(1, str.length - 1).split(":") + res.path = strs[0] + if (strs.length == 3) { + res.beg_line = Number(strs[1]) - 1 + res.end_line = Number(strs[2]) + } else if (strs.length != 1) { + console.error('cannot parse snippet:', snip) + } } else { console.error('cannot parse snippet:', snip) } - return resolve(oi_wiki_root, str) + res.path = resolve(oi_wiki_root, res.path) + return res } async function process_snippet (file) { @@ -25,8 +34,8 @@ async function process_snippet (file) { const spaceString = ' '.repeat(spacesAtStart) if (line.trim().startsWith(SNIPPET_TOKEN)) { const res = resolvePath(line, spacesAtStart) - line = await fs.readFile(res, 'utf8') - line = line.split('\n').map(l => spaceString + l).join('\n') + line = await fs.readFile(res.path, 'utf8') + line = line.split('\n').slice(res.beg_line, res.end_line).map(l => spaceString + l).join('\n') } return line }))) diff --git a/oi-wiki-export/snippet.js b/oi-wiki-export/snippet.js index eefc0ca..a333cd6 100644 --- a/oi-wiki-export/snippet.js +++ b/oi-wiki-export/snippet.js @@ -6,16 +6,25 @@ const SNIPPET_TOKEN = "--8<-- "; let oi_wiki_root = "."; function resolvePath(snip, spacesAtStart) { - let str = snip.substring(SNIPPET_TOKEN.length + spacesAtStart); + const str = snip.substring(SNIPPET_TOKEN.length + spacesAtStart); + let res = { "path": str, "beg_line": undefined, "end_line": undefined }; if ( (str.startsWith('"') && str.endsWith('"')) || (str.startsWith("'") && str.endsWith("'")) ) { - str = str.substring(1, str.length - 1); + const strs = str.substring(1, str.length - 1).split(":"); + res.path = strs[0]; + if (strs.length == 3) { + res.beg_line = Number(strs[1]) - 1; + res.end_line = Number(strs[2]); + } else if (strs.length != 1) { + console.error("cannot parse snippet:", snip); + } } else { console.error("cannot parse snippet:", snip); } - return resolve(oi_wiki_root, str); + res.path = resolve(oi_wiki_root, res.path); + return res; } async function process_snippet(file) { @@ -27,9 +36,10 @@ async function process_snippet(file) { const spaceString = " ".repeat(spacesAtStart); if (line.trim().startsWith(SNIPPET_TOKEN)) { const res = resolvePath(line, spacesAtStart); - line = await fs.readFile(res, "utf8"); + line = await fs.readFile(res.path, "utf8"); line = line .split("\n") + .slice(res.beg_line, res.end_line) .map((l) => spaceString + l) .join("\n"); }