-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
📑 Add
literalinclude
directive (#610)
Supports `literalinclude` directive and options across RST and Sphinx. - [RST documentation](https://docutils.sourceforge.io/docs/ref/rst/directives.html#including-an-external-document-fragment) - [Sphinx documentation](https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-literalinclude) See jupyter-book/jupyterlab-myst#189 and https://github.com/orgs/executablebooks/discussions/1026
- Loading branch information
Showing
17 changed files
with
669 additions
and
126 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'myst-directives': patch | ||
'myst-transforms': patch | ||
--- | ||
|
||
Move includeDirective transform to myst-transforms and make it generic for use in JupyterLab |
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,5 @@ | ||
--- | ||
'myst-spec-ext': patch | ||
--- | ||
|
||
Add `include` node, that implements the `literalinclude` directive |
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,5 @@ | ||
--- | ||
'myst-directives': patch | ||
--- | ||
|
||
Remove the codeBlockDirective, this is now the same as the `codeDirective`. |
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,6 @@ | ||
--- | ||
'myst-directives': patch | ||
'myst-cli': patch | ||
--- | ||
|
||
Add `literalinclude` directive |
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 |
---|---|---|
@@ -1,27 +1,29 @@ | ||
import path from 'node:path'; | ||
import fs from 'node:fs'; | ||
import type { GenericNode, GenericParent } from 'myst-common'; | ||
import type { GenericParent } from 'myst-common'; | ||
import { fileError } from 'myst-common'; | ||
import { parseMyst } from '../process/index.js'; | ||
import { selectAll } from 'unist-util-select'; | ||
import { join, dirname } from 'node:path'; | ||
import type { ISession } from '../session/types.js'; | ||
import type { VFile } from 'vfile'; | ||
import { includeDirectiveTransform } from 'myst-transforms'; | ||
|
||
/** | ||
* This is the {include} directive, that loads from disk. | ||
* | ||
* RST documentation: | ||
* - https://docutils.sourceforge.io/docs/ref/rst/directives.html#including-an-external-document-fragment | ||
*/ | ||
export function includeFilesDirective(session: ISession, filename: string, mdast: GenericParent) { | ||
const includeNodes = selectAll('include', mdast) as GenericNode[]; | ||
const dir = dirname(filename); | ||
includeNodes.forEach((node) => { | ||
const file = join(dir, node.file); | ||
if (!fs.existsSync(file)) { | ||
session.log.error(`Include Directive: Could not find "${file}" in "${filename}"`); | ||
export function includeFilesTransform( | ||
session: ISession, | ||
baseFile: string, | ||
tree: GenericParent, | ||
vfile: VFile, | ||
) { | ||
const dir = path.dirname(baseFile); | ||
const loadFile = (filename: string) => { | ||
const fullFile = path.join(dir, filename); | ||
if (!fs.existsSync(fullFile)) { | ||
fileError(vfile, `Include Directive: Could not find "${fullFile}" in "${baseFile}"`); | ||
return; | ||
} | ||
const content = fs.readFileSync(file).toString(); | ||
const children = parseMyst(session, content, filename).children as GenericNode[]; | ||
node.children = children; | ||
}); | ||
return fs.readFileSync(fullFile).toString(); | ||
}; | ||
const parseContent = (filename: string, content: string) => { | ||
return parseMyst(session, content, filename).children; | ||
}; | ||
includeDirectiveTransform(tree, vfile, { loadFile, parseContent }); | ||
} |
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,54 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { getCodeBlockOptions } from './code.js'; | ||
import { VFile } from 'vfile'; | ||
|
||
describe('Code block options', () => { | ||
test('default options', () => { | ||
const vfile = new VFile(); | ||
const opts = getCodeBlockOptions({}, vfile); | ||
expect(opts).toEqual({}); | ||
expect(vfile.messages.length).toEqual(0); | ||
}); | ||
test('number-lines', () => { | ||
const vfile = new VFile(); | ||
const opts = getCodeBlockOptions({ 'number-lines': 1 }, vfile); | ||
expect(opts).toEqual({ showLineNumbers: true }); | ||
expect(vfile.messages.length).toEqual(0); | ||
}); | ||
test('number-lines: 2', () => { | ||
const vfile = new VFile(); | ||
const opts = getCodeBlockOptions({ 'number-lines': 2 }, vfile); | ||
expect(opts).toEqual({ showLineNumbers: true, startingLineNumber: 2 }); | ||
expect(vfile.messages.length).toEqual(0); | ||
}); | ||
test('number-lines clashes with lineno-start', () => { | ||
const vfile = new VFile(); | ||
const opts = getCodeBlockOptions({ 'number-lines': 1, 'lineno-start': 2 }, vfile); | ||
expect(opts).toEqual({ showLineNumbers: true, startingLineNumber: 2 }); | ||
// Show warning! | ||
expect(vfile.messages.length).toEqual(1); | ||
}); | ||
test('lineno-start activates showLineNumbers', () => { | ||
const vfile = new VFile(); | ||
const opts = getCodeBlockOptions({ 'lineno-start': 1 }, vfile); | ||
expect(opts).toEqual({ showLineNumbers: true }); | ||
expect(vfile.messages.length).toEqual(0); | ||
}); | ||
test('emphasize-lines', () => { | ||
const vfile = new VFile(); | ||
const opts = getCodeBlockOptions({ 'emphasize-lines': '3,5' }, vfile); | ||
expect(opts).toEqual({ emphasizeLines: [3, 5] }); | ||
expect(vfile.messages.length).toEqual(0); | ||
}); | ||
// See https://github.com/executablebooks/jupyterlab-myst/issues/174 | ||
test(':lineno-start: 10, :emphasize-lines: 12,13', () => { | ||
const vfile = new VFile(); | ||
const opts = getCodeBlockOptions({ 'lineno-start': 10, 'emphasize-lines': '12,13' }, vfile); | ||
expect(opts).toEqual({ | ||
showLineNumbers: true, | ||
emphasizeLines: [12, 13], | ||
startingLineNumber: 10, | ||
}); | ||
expect(vfile.messages.length).toEqual(0); | ||
}); | ||
}); |
Oops, something went wrong.