Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/execute/ojs/annotate-source.ts
6458 views
1
/*
2
* annotate-source.ts
3
*
4
* Copyright (C) 2021-2022 Posit Software, PBC
5
*
6
*/
7
8
import { RenderContext } from "../../command/render/types.ts";
9
10
import { lines } from "../../core/text.ts";
11
12
import { isJupyterNotebook } from "../../core/jupyter/jupyter.ts";
13
14
interface OJSLineNumbersAnnotation {
15
ojsBlockLineNumbers: number[];
16
}
17
18
export function annotateOjsLineNumbers(
19
context: RenderContext,
20
): OJSLineNumbersAnnotation {
21
const canPatch = !isJupyterNotebook(context.target.source);
22
23
if (canPatch) {
24
const source = lines(Deno.readTextFileSync(context.target.source));
25
26
const ojsBlockLineNumbers: number[] = [];
27
let lineNumber = 1;
28
29
// we're using a regexp based on knitr, tweaked to what we actually need here:
30
// https://github.com/yihui/knitr/blob/3237add034368a3018ff26fa9f4d0ca89a4afd78/R/pattern.R#L32
31
const chunkBegin = /^[\t ]*```+\s*\{(ojs( *[ ,].*)?)\}\s*$/;
32
33
source.forEach((line) => {
34
lineNumber++;
35
if (line.match(chunkBegin)) {
36
ojsBlockLineNumbers.push(lineNumber);
37
}
38
});
39
40
return {
41
ojsBlockLineNumbers,
42
};
43
} else {
44
return {
45
ojsBlockLineNumbers: [],
46
};
47
}
48
}
49
50