use std::collections::{BTreeMap, BTreeSet};
use panache_parser::syntax::{
AstNode, ChunkInfoItem, CodeBlock, SyntaxKind, SyntaxNode, YamlBlockMapKey, YamlFlowMapKey,
};
use crate::configuration::{ContentConfiguration, ExecutionConfigurationError, ExecutionMode};
use crate::diagnostics::{Diagnostic, DiagnosticCode, Severity};
use crate::execution::{EffectiveOption, ExecutionDefaults, OptionOrigin, PreparedCell};
use crate::ir::{Attributes, Block, CodeCell, Inline, SourceSpan};
use crate::validation::validate_document_execution;
use super::{AuthoredFormat, DocumentParse, ParseMode, parse_document_with_resources, span};
mod options;
mod scalars;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PreparedDocument {
pub parsed: DocumentParse,
pub preparation: Option<QmdPreparation>,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct QmdPreparation {
pub defaults: ExecutionDefaults,
pub page_veto: bool,
pub cells: Vec<PreparedCell>,
pub authored_anchors: BTreeSet<String>,
pub execution_eligible: bool,
}
pub fn prepare_collection_document(
source: &str,
collection: &ContentConfiguration,
) -> Result<PreparedDocument, ExecutionConfigurationError> {
prepare_collection_document_with_resources(source, collection, &|target, _| target.to_owned())
}
pub(crate) fn prepare_collection_document_with_resources(
source: &str,
collection: &ContentConfiguration,
resource_target: &dyn Fn(&str, SourceSpan) -> String,
) -> Result<PreparedDocument, ExecutionConfigurationError> {
collection.validate_execution()?;
let mut parsed = parse_document_with_resources(
source,
collection.format,
ParseMode::Collection,
resource_target,
);
if collection.format == AuthoredFormat::Documenter
&& collection.execution.mode == ExecutionMode::Execute
&& let Err(diagnostics) = super::documenter::examples(source)
{
parsed.diagnostics.extend(diagnostics);
}
if collection.format != AuthoredFormat::Qmd {
return Ok(PreparedDocument {
parsed,
preparation: None,
});
}
let authority = validate_document_execution(&parsed.document, collection)?;
parsed.diagnostics.retain(|diagnostic| {
diagnostic.code != DiagnosticCode::InvalidQmdMetadata
|| !authority.iter().any(|authority| {
std::iter::once(authority.span)
.flatten()
.chain(authority.related_spans.iter().copied())
.any(|range| diagnostic.span.is_some_and(|s| contains(range, s)))
})
});
parsed.diagnostics.extend(authority);
parsed
.diagnostics
.retain(|d| d.code != DiagnosticCode::AmbiguousCellOption);
let mut context = PreparationContext {
source,
diagnostics: &mut parsed.diagnostics,
cells: Vec::new(),
anchors: BTreeMap::new(),
};
let (defaults, page_veto) = options::document_defaults(
source,
parsed.document.frontmatter.as_ref(),
context.diagnostics,
);
context.blocks(&parsed.document.blocks, &defaults);
context.check_anchors();
let authored_anchors = context.anchors.into_keys().collect();
let cells = context.cells;
let execution_eligible = collection.execution.mode == ExecutionMode::Execute
&& !page_veto
&& cells.iter().any(|cell| cell.options.execution.eval.value);
parsed.diagnostics.sort();
let preparation = (!parsed
.diagnostics
.iter()
.any(|d| d.severity == Severity::Error))
.then_some(QmdPreparation {
defaults,
page_veto,
cells,
authored_anchors,
execution_eligible,
});
Ok(PreparedDocument {
parsed,
preparation,
})
}
struct PreparationContext<'a> {
source: &'a str,
diagnostics: &'a mut Vec<Diagnostic>,
cells: Vec<PreparedCell>,
anchors: BTreeMap<String, Vec<Anchor>>,
}
struct Anchor {
span: SourceSpan,
cell: bool,
}
impl PreparationContext<'_> {
fn blocks(&mut self, blocks: &[Block], defaults: &ExecutionDefaults) {
for block in blocks {
match block {
Block::CodeCell(cell) => self.cell(cell, defaults),
Block::BlockQuote { blocks, .. } | Block::Container { blocks, .. } => {
self.blocks(blocks, defaults)
}
Block::Callout {
attributes, blocks, ..
}
| Block::Component {
attributes, blocks, ..
}
| Block::Tabset {
attributes, blocks, ..
} => {
self.attributes(attributes);
self.blocks(blocks, defaults);
}
Block::List { items, .. } => {
for item in items {
self.blocks(&item.blocks, defaults);
}
}
Block::Table { caption, rows, .. } => {
self.inlines(caption);
for row in rows {
for cell in &row.cells {
self.blocks(&cell.blocks, defaults);
}
}
}
Block::Heading {
attributes,
inlines,
..
} => {
self.attributes(attributes);
self.inlines(inlines);
}
Block::Paragraph { inlines, .. } => self.inlines(inlines),
Block::ApiDocumentation { .. }
| Block::ThematicBreak { .. }
| Block::CodeBlock { .. }
| Block::Unsupported { .. }
| Block::DisplayMath { .. } => {}
}
}
}
fn inlines(&mut self, inlines: &[Inline]) {
for inline in inlines {
match inline {
Inline::Link {
attributes,
inlines,
..
} => {
self.attributes(attributes);
self.inlines(inlines);
}
Inline::Image {
attributes, alt, ..
} => {
self.attributes(attributes);
self.inlines(alt);
}
Inline::Emphasis { inlines, .. }
| Inline::Strong { inlines, .. }
| Inline::Styled { inlines, .. }
| Inline::Citation { inlines, .. }
| Inline::Strikeout { inlines, .. } => self.inlines(inlines),
_ => {}
}
}
}
fn attributes(&mut self, attributes: &Attributes) {
if let Some(id) = &attributes.identifier {
self.anchors
.entry(id.value.clone())
.or_default()
.push(Anchor {
span: id.span,
cell: false,
});
}
}
fn cell(&mut self, cell: &CodeCell, defaults: &ExecutionDefaults) {
let mut options = options::cell_options(self.source, cell, defaults, self.diagnostics);
for class in &cell.classes {
self.diagnostics.push(error(
DiagnosticCode::UnsupportedCellOption,
"Cell fence classes are unsupported.",
class.span,
));
}
if let Some(identifier) = &cell.identifier {
if !options::valid_label(&identifier.value) {
self.diagnostics.push(error(
DiagnosticCode::InvalidCellOption,
"Cell identifiers must match [A-Za-z][A-Za-z0-9_.:-]*.",
identifier.span,
));
}
if let Some(label) = &options.label.value {
if label != &identifier.value {
let mut diagnostic = error(
DiagnosticCode::InvalidCellOption,
"The cell label and fence identifier must agree.",
origin_span(options.label.origin).unwrap(),
);
diagnostic.related_spans.push(identifier.span);
self.diagnostics.push(diagnostic);
}
} else {
options.label = EffectiveOption {
value: Some(identifier.value.clone()),
origin: OptionOrigin::FenceIdentifier {
span: identifier.span,
},
};
}
}
if let Some(label) = &options.label.value {
self.anchors.entry(label.clone()).or_default().push(Anchor {
span: origin_span(options.label.origin).unwrap(),
cell: true,
});
}
self.cells.push(PreparedCell {
ordinal: self.cells.len(),
cell: cell.clone(),
options,
});
}
fn check_anchors(&mut self) {
for anchors in self.anchors.values_mut() {
if anchors.len() < 2 || !anchors.iter().any(|a| a.cell) {
continue;
}
anchors.sort_by_key(|a| (a.span.start, a.span.end));
let mut diagnostic = error(
DiagnosticCode::InvalidCellOption,
"A cell label conflicts with another authored anchor on this page.",
anchors[0].span,
);
diagnostic
.related_spans
.extend(anchors[1..].iter().map(|a| a.span));
self.diagnostics.push(diagnostic);
}
}
}
fn origin_span(origin: OptionOrigin) -> Option<SourceSpan> {
match origin {
OptionOrigin::Default => None,
OptionOrigin::Document { span }
| OptionOrigin::Inline { span }
| OptionOrigin::Hashpipe { span }
| OptionOrigin::FenceIdentifier { span } => Some(span),
}
}
fn error(code: DiagnosticCode, message: impl Into<String>, span: SourceSpan) -> Diagnostic {
Diagnostic {
code,
severity: Severity::Error,
message: message.into(),
span: Some(span),
related_spans: Vec::new(),
related_entity: None,
source: None,
}
}
fn contains(outer: SourceSpan, inner: SourceSpan) -> bool {
outer.start <= inner.start && inner.end <= outer.end
}
pub(super) fn syntax_diagnostics(root: &SyntaxNode) -> Vec<Diagnostic> {
let mut diagnostics = Vec::new();
for code in root.descendants().filter_map(CodeBlock::cast) {
if code.executable_cell().is_some()
&& let Some(info) = code.info()
{
let identifiers = info
.chunk_items()
.filter_map(|item| match item {
ChunkInfoItem::Id(id) => Some(span(id.range())),
_ => None,
})
.collect::<Vec<_>>();
if identifiers.len() > 1 {
let mut diagnostic = error(
DiagnosticCode::InvalidCellOption,
"A cell fence may declare only one identifier.",
identifiers[0],
);
diagnostic
.related_spans
.extend_from_slice(&identifiers[1..]);
diagnostics.push(diagnostic);
}
}
}
for element in root.descendants_with_tokens() {
if matches!(
element.kind(),
SyntaxKind::YAML_TAG | SyntaxKind::YAML_ANCHOR | SyntaxKind::YAML_ALIAS
) {
let parent = element.parent().expect("YAML token parent");
let code = yaml_diagnostic_code(&parent);
diagnostics.push(error(
code,
"YAML tags, anchors, and aliases are unsupported.",
span(element.text_range()),
));
}
if let Some(node) = element.as_node() {
let invalid_key = match node.kind() {
SyntaxKind::YAML_BLOCK_MAP_KEY => YamlBlockMapKey::cast(node.clone())
.unwrap()
.scalar()
.is_none(),
SyntaxKind::YAML_FLOW_MAP_KEY => YamlFlowMapKey::cast(node.clone())
.unwrap()
.scalar()
.is_none(),
_ => false,
};
if invalid_key {
diagnostics.push(error(
yaml_diagnostic_code(node),
"QMD option and metadata keys must be scalars.",
span(node.text_range()),
));
}
if node.kind() == SyntaxKind::CHUNK_LABEL {
diagnostics.push(error(
DiagnosticCode::UnsupportedCellOption,
"Bare cell labels are unsupported; use a fence identifier or the label option.",
span(node.text_range()),
));
}
}
}
diagnostics
}
fn yaml_diagnostic_code(node: &SyntaxNode) -> DiagnosticCode {
if node
.ancestors()
.any(|n| n.kind() == SyntaxKind::HASHPIPE_YAML_CONTENT)
{
DiagnosticCode::InvalidCellOption
} else {
DiagnosticCode::InvalidQmdMetadata
}
}