Path: blob/main/src/execute/ojs/annotate-source.ts
6458 views
/*1* annotate-source.ts2*3* Copyright (C) 2021-2022 Posit Software, PBC4*5*/67import { RenderContext } from "../../command/render/types.ts";89import { lines } from "../../core/text.ts";1011import { isJupyterNotebook } from "../../core/jupyter/jupyter.ts";1213interface OJSLineNumbersAnnotation {14ojsBlockLineNumbers: number[];15}1617export function annotateOjsLineNumbers(18context: RenderContext,19): OJSLineNumbersAnnotation {20const canPatch = !isJupyterNotebook(context.target.source);2122if (canPatch) {23const source = lines(Deno.readTextFileSync(context.target.source));2425const ojsBlockLineNumbers: number[] = [];26let lineNumber = 1;2728// we're using a regexp based on knitr, tweaked to what we actually need here:29// https://github.com/yihui/knitr/blob/3237add034368a3018ff26fa9f4d0ca89a4afd78/R/pattern.R#L3230const chunkBegin = /^[\t ]*```+\s*\{(ojs( *[ ,].*)?)\}\s*$/;3132source.forEach((line) => {33lineNumber++;34if (line.match(chunkBegin)) {35ojsBlockLineNumbers.push(lineNumber);36}37});3839return {40ojsBlockLineNumbers,41};42} else {43return {44ojsBlockLineNumbers: [],45};46}47}484950