-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7760 from quarto-dev/bugfix/1237
resolve include shortcodes in non-executable code blocks
- Loading branch information
Showing
6 changed files
with
155 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* include-standalone.ts | ||
* | ||
* Copyright (C) 2023 Posit Software, PBC | ||
*/ | ||
|
||
import { LanguageCellHandlerContext } from "./types.ts"; | ||
|
||
import { | ||
asMappedString, | ||
EitherString, | ||
mappedConcat, | ||
MappedString, | ||
mappedString, | ||
} from "../lib/mapped-text.ts"; | ||
|
||
import { rangedLines } from "../lib/ranged-text.ts"; | ||
import { isBlockShortcode } from "../lib/parse-shortcode.ts"; | ||
|
||
export const standaloneInclude = async ( | ||
handlerContext: LanguageCellHandlerContext, | ||
filename: string, | ||
): Promise<MappedString> => { | ||
const source = handlerContext.options.context.target.source; | ||
const retrievedFiles: string[] = [source]; | ||
|
||
const textFragments: EitherString[] = []; | ||
|
||
const retrieveInclude = async (filename: string) => { | ||
const path = handlerContext.resolvePath(filename); | ||
|
||
if (retrievedFiles.indexOf(path) !== -1) { | ||
throw new Error( | ||
`Include directive found circular include of file ${filename}.`, | ||
); | ||
} | ||
|
||
let includeSrc; | ||
try { | ||
includeSrc = asMappedString( | ||
Deno.readTextFileSync(path), | ||
path, | ||
); | ||
} catch (_e) { | ||
const errMsg: string[] = [`Include directive failed.`]; | ||
errMsg.push(...retrievedFiles.map((s) => ` in file ${s}, `)); | ||
errMsg.push( | ||
` could not find file ${path | ||
// relative(handlerContext.options.context.target.source, path) | ||
}.`, | ||
); | ||
throw new Error(errMsg.join("\n")); | ||
} | ||
|
||
retrievedFiles.push(filename); | ||
|
||
let rangeStart = 0; | ||
for (const { substring, range } of rangedLines(includeSrc.value)) { | ||
const m = isBlockShortcode(substring); | ||
if (m && m.name.toLocaleLowerCase() === "include") { | ||
textFragments.push( | ||
mappedString(includeSrc, [{ | ||
start: rangeStart, | ||
end: range.start, | ||
}]), | ||
); | ||
rangeStart = range.end; | ||
const params = m.params; | ||
if (params.length === 0) { | ||
throw new Error("Include directive needs file parameter"); | ||
} | ||
|
||
await retrieveInclude(params[0]); | ||
} | ||
} | ||
if (rangeStart !== includeSrc.value.length) { | ||
textFragments.push( | ||
mappedString(includeSrc, [{ | ||
start: rangeStart, | ||
end: includeSrc.value.length, | ||
}]), | ||
); | ||
} | ||
textFragments.push(includeSrc.value.endsWith("\n") ? "\n" : "\n\n"); | ||
|
||
retrievedFiles.pop(); | ||
}; | ||
|
||
await retrieveInclude(filename); | ||
|
||
return Promise.resolve(mappedConcat(textFragments)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
format: html | ||
_quarto: | ||
tests: | ||
html: | ||
ensureFileRegexMatches: | ||
- ["Hello"] | ||
- [] | ||
--- | ||
|
||
```.r | ||
{{< include file.r >}} | ||
``` | ||
|
||
|
||
```{r} | ||
# Don't do anything here. | ||
cat("world.") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
cat("Hello.") |