Classes
LiteralIncludeDirective
Literal include directive for including code files as code blocks.
Syntax:
```{literalinclude}…
LiteralIncludeDirective
Literal include directive for including code files as code blocks.
Syntax:
```{literalinclude} path/to/file.py
```
Or with options:
```{literalinclude} path/to/file.py
:language: python
:start-line: 5
:end-line: 20
:emphasize-lines: 7,8,9
:linenos: true
```
Paths are resolved relative to:
- Current page's directory (if source_path available in state)
- Site root (if root_path available in state)
- Current working directory (fallback)
Security: Only allows paths within the site root to prevent path traversal.
DirectivePluginMethods 1
parse
Parse literalinclude directive.
parse
def parse(self, block: BlockParser, m: Match[str], state: BlockState) -> dict[str, Any]
Parse literalinclude directive.
Parameters 3
block |
BlockParser |
Block parser |
m |
Match[str] |
Regex match object |
state |
BlockState |
Parser state (may contain root_path, source_path) |
Returns
Token dict with type 'literalinclude'dict[str, Any]
—
Internal Methods 4
_detect_language
Detect language from file extension.
_detect_language
def _detect_language(self, path: str) -> str | None
Detect language from file extension.
Parameters 1
path |
str |
File path |
Returns
Language name or Nonestr | None
—
_resolve_path
Resolve file path relative to current page or site root.
Security:
- Rejec…
_resolve_path
def _resolve_path(self, path: str, state: BlockState) -> Path | None
Resolve file path relative to current page or site root.
Security:
- Rejects absolute paths
- Rejects paths outside site root
- Rejects symlinks (could escape containment)
Path Resolution:
- root_path MUST be provided via state (set by rendering pipeline)
- No fallback to Path.cwd() - eliminates CWD-dependent behavior
- See: plan/active/rfc-path-resolution-architecture.md
Parameters 2
path |
str |
Relative or absolute path to file |
state |
BlockState |
Parser state (must contain root_path, may contain source_path) |
Returns
Resolved Path object, or None if not found, outside site root,
or if root_path is not available in statePath | None
—
_load_file
Load file content, optionally with line range and emphasis.
Security: Enforces…
_load_file
def _load_file(self, file_path: Path, start_line: int | None, end_line: int | None, emphasize_lines: str | None) -> str | None
Load file content, optionally with line range and emphasis.
Security: Enforces file size limit to prevent memory exhaustion.
Parameters 4
file_path |
Path |
Path to file |
start_line |
int | None |
Optional start line (1-indexed) |
end_line |
int | None |
Optional end line (1-indexed) |
emphasize_lines |
str | None |
Optional comma-separated line numbers to emphasize |
Returns
File content as string, or None on errorstr | None
—
__call__
Register literalinclude directive.
__call__
def __call__(self, directive: Any, md: Any) -> None
Register literalinclude directive.
Parameters 2
directive |
Any |
|
md |
Any |
Functions
render_literalinclude
Render literalinclude directive as code block.
render_literalinclude
def render_literalinclude(renderer: Any, text: str, **attrs: Any) -> str
Render literalinclude directive as code block.
Parameters 2
| Name | Type | Default | Description |
|---|---|---|---|
renderer |
Any |
— | Mistune renderer |
text |
str |
— | Not used (content is in attrs['code']) **attrs: Directive attributes |
Returns
HTML stringstr
—