Skip to content

Commit

Permalink
feat(snippts.js): parse snippets with line selection (#83)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
c-forrest authored Dec 2, 2024
1 parent 4dc378b commit d6b8fe9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
21 changes: 15 additions & 6 deletions oi-wiki-export-typst/snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
})))
Expand Down
18 changes: 14 additions & 4 deletions oi-wiki-export/snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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");
}
Expand Down

0 comments on commit d6b8fe9

Please sign in to comment.