use std::collections::HashMap;
use panache_parser::syntax::{
AlertKind as PanacheAlertKind, AstNode, AttributeEntry, AttributeNode, BlockNode,
CalloutKind as PanacheCalloutKind, CellOptionResolution as PanacheOptionResolution,
ChunkOptionSource, CodeBlock, InlineNode, ListKind, Table as PanacheTable,
TableAlignment as PanacheTableAlignment, TextRange, YamlBlockMap, YamlBlockMapEntry,
YamlBlockSequence, YamlFlowMap, YamlFlowMapEntry, YamlFlowSequence, YamlNode,
};
use panache_parser::{Flavor, ParserOptions, normalize_reference_label};
use serde::{Deserialize, Serialize};
use crate::configuration::{ContentConfiguration, ExecutionConfigurationError};
use crate::diagnostics::{
Diagnostic, DiagnosticCode, DiagnosticEntity, DiagnosticSource, Severity,
};
use crate::ir::{
AttributeKeyValue, Attributes, Block, CalloutKind, CellOption, CellOptionResolution, CodeCell,
Document, Inline, ListItem, MetadataEntry, MetadataValue, ResolvedCellOption, SourceSegment,
SourceSpan, SpannedString, TableAlignment, TableCell, TableRow,
};
pub(crate) mod documenter;
mod fragments;
pub(crate) mod includes;
mod layout;
mod preparation;
mod rmarkdown;
pub use fragments::{MarkdownFragmentOrigin, MarkdownFragmentParse, parse_markdown_fragment};
pub(crate) use layout::PageLayout;
pub(crate) use preparation::prepare_collection_document_with_resources;
pub use preparation::{PreparedDocument, QmdPreparation, prepare_collection_document};
pub use rmarkdown::{RmdPreparation, parse_evaluated_rmd, prepare_rmd_document};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum AuthoredFormat {
Gfm,
Qmd,
Rmd,
Documenter,
}
impl AuthoredFormat {
pub(crate) fn matches_path(self, path: &std::path::Path) -> bool {
matches!(
(
self,
path.extension().and_then(|extension| extension.to_str())
),
(Self::Gfm | Self::Documenter, Some("md"))
| (Self::Qmd, Some("qmd"))
| (Self::Rmd, Some("rmd" | "Rmd"))
)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DocumentParse {
pub document: Document,
pub diagnostics: Vec<Diagnostic>,
}
impl DocumentParse {
pub fn with_context(mut self, source: DiagnosticSource, entity: DiagnosticEntity) -> Self {
for diagnostic in &mut self.diagnostics {
diagnostic.source = Some(source.clone());
diagnostic.related_entity = Some(entity.clone());
}
self.diagnostics.sort();
self
}
}
pub fn parse_authored_document(source: &str, format: AuthoredFormat) -> DocumentParse {
parse_document(source, format, ParseMode::Authored)
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum ParseMode {
Authored,
Fragment,
Collection,
Evaluated,
}
fn parse_document(source: &str, format: AuthoredFormat, mode: ParseMode) -> DocumentParse {
parse_document_with_resources(source, format, mode, &|target, _| target.to_owned())
}
fn parse_document_with_resources(
source: &str,
format: AuthoredFormat,
mode: ParseMode,
resource_target: &dyn Fn(&str, SourceSpan) -> String,
) -> DocumentParse {
if format == AuthoredFormat::Documenter {
return crate::extractors::julia::docstrings::parse_documenter(source);
}
let fragment = mode == ParseMode::Fragment;
let flavor = match format {
AuthoredFormat::Gfm => Flavor::Gfm,
AuthoredFormat::Qmd => Flavor::Quarto,
AuthoredFormat::Rmd => Flavor::RMarkdown,
AuthoredFormat::Documenter => unreachable!(),
};
let mut options = ParserOptions::for_flavor(flavor);
options.preserve_unresolved_references = true;
if !fragment {
options.extensions.yaml_metadata_block = true;
options.extensions.fenced_divs = true;
options.extensions.header_attributes = true;
}
if mode == ParseMode::Evaluated {
options.extensions.executable_code = false;
options.extensions.rmarkdown_inline_code = false;
}
if fragment {
options.extensions.executable_code = false;
options.extensions.rmarkdown_inline_code = false;
options.extensions.quarto_inline_code = false;
options.extensions.auto_identifiers = false;
options.extensions.gfm_auto_identifiers = false;
options.extensions.implicit_header_references = false;
}
let parsed = panache_parser::parse_document(source, Some(options.clone()));
let fragment_sources = if fragment || mode == ParseMode::Evaluated {
fragments::display_sources(source, parsed.document(), options)
} else {
HashMap::new()
};
let references = parsed
.document()
.reference_definitions()
.filter_map(|definition| {
let url = definition.url()?;
Some((
normalize_reference_label(&definition.label()),
(
resource_target(&url, span(definition.syntax().text_range())),
definition.title(),
),
))
})
.collect();
let footnote_definitions = parsed
.document()
.syntax()
.descendants()
.filter_map(panache_parser::syntax::FootnoteDefinition::cast)
.map(|note| (note.id(), note.content()))
.collect();
let mut context = AdapterContext {
format,
resource_target,
fragment,
fragment_sources,
references,
evaluated: mode == ParseMode::Evaluated,
footnotes: Vec::new(),
footnote_definitions,
footnote_numbers: HashMap::new(),
math_delimiters: if format == AuthoredFormat::Rmd {
rmarkdown::math_delimiters(parsed.document().syntax())
} else {
Vec::new()
},
diagnostics: parsed
.errors()
.iter()
.map(|error| Diagnostic {
code: DiagnosticCode::InvalidEmbeddedYaml,
severity: Severity::Error,
message: error.message.clone(),
span: Some(span(error.range)),
related_spans: Vec::new(),
related_entity: None,
source: None,
})
.collect(),
};
let frontmatter = parsed
.document()
.frontmatter()
.and_then(|metadata| metadata.document())
.map(|document| {
document.as_node().map_or_else(
|| MetadataValue::Null {
span: span(document.syntax().text_range()),
},
|value| yaml_value(&value),
)
});
if !fragment && let Err(diagnostic) = PageLayout::from_metadata(frontmatter.as_ref()) {
context.diagnostics.push(*diagnostic);
}
let mut blocks: Vec<_> = parsed
.document()
.block_nodes()
.filter_map(|block| context.block(block))
.collect();
if !context.footnotes.is_empty() {
blocks.push(Block::Container {
kind: crate::ir::DocumentContainerKind::Footnotes,
identifier: Some("footnotes".into()),
blocks: std::mem::take(&mut context.footnotes),
span: span(parsed.document().syntax().text_range()),
});
}
if mode == ParseMode::Collection && format == AuthoredFormat::Qmd {
context
.diagnostics
.extend(preparation::syntax_diagnostics(parsed.document().syntax()));
}
context.diagnostics.sort();
DocumentParse {
document: Document {
span: span(parsed.document().syntax().text_range()),
frontmatter: if fragment { None } else { frontmatter },
blocks,
},
diagnostics: context.diagnostics,
}
}
pub fn parse_collection_document(
source: &str,
collection: &ContentConfiguration,
) -> Result<DocumentParse, ExecutionConfigurationError> {
Ok(prepare_collection_document(source, collection)?.parsed)
}
struct AdapterContext<'a> {
resource_target: &'a dyn Fn(&str, SourceSpan) -> String,
format: AuthoredFormat,
fragment: bool,
fragment_sources: HashMap<TextRange, Vec<SourceSegment>>,
references: HashMap<String, (String, Option<String>)>,
diagnostics: Vec<Diagnostic>,
evaluated: bool,
footnotes: Vec<Block>,
footnote_definitions: HashMap<String, String>,
footnote_numbers: HashMap<String, usize>,
math_delimiters: Vec<SourceSpan>,
}
impl AdapterContext<'_> {
fn block(&mut self, block: BlockNode) -> Option<Block> {
let fallback_kind = format!("{:?}", block.syntax_kind());
let fallback_raw = block.source_text();
let fallback_range = block.text_range();
match block {
BlockNode::YamlMetadata(metadata) if self.fragment => {
Some(self.unsupported_syntax(metadata.syntax()))
}
BlockNode::YamlMetadata(_)
| BlockNode::ReferenceDefinition(_)
| BlockNode::Trivia(_) => None,
BlockNode::Paragraph(paragraph) => {
if self.format == AuthoredFormat::Gfm
&& let Some(math) = paragraph
.syntax()
.children()
.find_map(panache_parser::syntax::DisplayMath::cast)
&& paragraph.syntax().text().to_string().trim()
== math.syntax().text().to_string().trim()
{
return Some(Block::DisplayMath {
latex: math.content(),
fallback: None,
span: span(math.syntax().text_range()),
});
}
Some(Block::Paragraph {
inlines: self.inlines(paragraph.inline_nodes()),
span: span(paragraph.syntax().text_range()),
})
}
BlockNode::Plain(plain) => Some(Block::Paragraph {
inlines: self.inlines(plain.inline_nodes()),
span: span(plain.syntax().text_range()),
}),
BlockNode::Heading(heading) => Some(Block::Heading {
level: heading.level(),
attributes: heading.attributes().map(attributes).unwrap_or_default(),
inlines: self.inlines(heading.inline_nodes()),
span: span(heading.syntax().text_range()),
}),
BlockNode::BlockQuote(quote) => {
if let Some(alert) = quote.alert() {
return Some(self.alert(alert));
}
Some(Block::BlockQuote {
blocks: quote
.block_nodes()
.filter_map(|block| self.block(block))
.collect(),
span: span(quote.syntax().text_range()),
})
}
BlockNode::Alert(alert) => Some(self.alert(alert)),
BlockNode::List(list) => {
let ordered = matches!(list.kind(), Some(ListKind::Ordered));
let list_span = span(list.syntax().text_range());
let items = list
.items()
.map(|item| ListItem {
checked: item.task_checked(),
blocks: item
.block_nodes()
.filter_map(|block| self.block(block))
.collect(),
span: span(item.syntax().text_range()),
})
.collect();
Some(Block::List {
ordered,
items,
span: list_span,
})
}
BlockNode::CodeBlock(code) => Some(self.code_block(code)),
BlockNode::FencedDiv(div) => {
if !self.evaluated
&& div
.attributes()
.is_some_and(|a| a.classes().iter().any(|c| c == "component"))
{
return Some(Block::Component {
attributes: div.attributes().map(attributes).unwrap_or_default(),
blocks: div.block_nodes().filter_map(|b| self.block(b)).collect(),
span: span(div.syntax().text_range()),
});
}
if div
.attributes()
.is_some_and(|a| a.classes().iter().any(|c| c == "panel-tabset"))
{
return Some(Block::Tabset {
attributes: div.attributes().map(attributes).unwrap_or_default(),
blocks: div.block_nodes().filter_map(|b| self.block(b)).collect(),
span: span(div.syntax().text_range()),
});
}
if self.evaluated {
let classes = div.attributes().map(|a| a.classes()).unwrap_or_default();
let kind = if classes.iter().any(|c| c == "diplodocus-rmd-output") {
Some(crate::ir::DocumentContainerKind::GeneratedOutput)
} else if classes.iter().any(|c| c == "diplodocus-rmd-chunk") {
Some(crate::ir::DocumentContainerKind::GeneratedChunk)
} else {
None
};
if let Some(kind) = kind {
return Some(Block::Container {
kind,
identifier: None,
blocks: div.block_nodes().filter_map(|b| self.block(b)).collect(),
span: span(div.syntax().text_range()),
});
}
}
let Some(callout) = div.quarto_callout() else {
return Some(self.unsupported(
format!("{:?}", div.syntax().kind()),
div.syntax().text().to_string(),
div.syntax().text_range(),
));
};
Some(Block::Callout {
kind: callout_kind(callout.kind()),
attributes: callout.attributes().map(attributes).unwrap_or_default(),
blocks: callout
.block_nodes()
.filter_map(|block| self.block(block))
.collect(),
span: span(callout.syntax().text_range()),
})
}
BlockNode::Table(table) => Some(self.table(table)),
BlockNode::ThematicBreak(rule) => Some(Block::ThematicBreak {
span: span(rule.text_range()),
}),
BlockNode::Figure(figure) => match figure.image() {
Some(image) if self.format == AuthoredFormat::Rmd => {
let image = self.image(image);
let caption = match &image {
Inline::Image { alt, .. } => alt.clone(),
_ => Vec::new(),
};
let range = span(figure.syntax().text_range());
Some(Block::Container {
kind: crate::ir::DocumentContainerKind::Figure,
identifier: None,
blocks: vec![
Block::Paragraph {
inlines: vec![image],
span: range,
},
Block::Paragraph {
inlines: caption,
span: range,
},
],
span: range,
})
}
Some(image) => Some(Block::Paragraph {
inlines: vec![self.image(image)],
span: span(figure.syntax().text_range()),
}),
None => Some(self.unsupported(
format!("{:?}", figure.syntax().kind()),
figure.syntax().text().to_string(),
figure.syntax().text_range(),
)),
},
BlockNode::PandocTitleBlock(node) => Some(self.unsupported_syntax(node.syntax())),
BlockNode::MmdTitleBlock(node) => Some(self.unsupported_syntax(node.syntax())),
BlockNode::DefinitionList(node) if self.format == AuthoredFormat::Rmd => {
let mut blocks = Vec::new();
for item in node.items() {
for child in item.syntax().children() {
let range = span(child.text_range());
let (kind, content) = if child.kind()
== panache_parser::syntax::SyntaxKind::TERM
{
(
crate::ir::DocumentContainerKind::DefinitionTerm,
vec![Block::Paragraph {
inlines: self.inlines(
child.children_with_tokens().map(InlineNode::cast),
),
span: range,
}],
)
} else if child.kind() == panache_parser::syntax::SyntaxKind::DEFINITION {
(
crate::ir::DocumentContainerKind::Definition,
child
.children()
.map(|n| BlockNode::cast(n.into()))
.filter_map(|b| self.block(b))
.collect(),
)
} else {
continue;
};
blocks.push(Block::Container {
kind,
identifier: None,
blocks: content,
span: range,
});
}
}
Some(Block::Container {
kind: crate::ir::DocumentContainerKind::DefinitionList,
identifier: None,
blocks,
span: span(node.syntax().text_range()),
})
}
BlockNode::DefinitionList(node) => Some(self.unsupported_syntax(node.syntax())),
BlockNode::LineBlock(node) => Some(self.unsupported_syntax(node.syntax())),
BlockNode::FootnoteDefinition(_) if self.format == AuthoredFormat::Rmd => None,
BlockNode::FootnoteDefinition(node) => Some(self.unsupported_syntax(node.syntax())),
BlockNode::DisplayMath(node)
if matches!(self.format, AuthoredFormat::Gfm | AuthoredFormat::Rmd) =>
{
Some(Block::DisplayMath {
latex: node.content(),
fallback: None,
span: span(node.syntax().text_range()),
})
}
BlockNode::DisplayMath(node) => Some(self.unsupported_syntax(node.syntax())),
BlockNode::TexBlock(node)
if self.format == AuthoredFormat::Rmd
&& rmarkdown::math_environment(&node.text()) =>
{
Some(Block::DisplayMath {
latex: node.text(),
fallback: None,
span: span(node.syntax().text_range()),
})
}
BlockNode::TexBlock(node) => Some(self.unsupported_syntax(node.syntax())),
BlockNode::MystDirective(node) => Some(self.unsupported_syntax(node.syntax())),
BlockNode::Unknown(node) => {
Some(self.unsupported(node.syntax_name(), node.source_text(), node.text_range()))
}
_ => Some(self.unsupported(fallback_kind, fallback_raw, fallback_range)),
}
}
fn alert(&mut self, alert: panache_parser::syntax::Alert) -> Block {
let Some(kind) = alert.kind().map(alert_kind) else {
return self.unsupported(
format!("{:?}", alert.syntax().kind()),
alert.syntax().text().to_string(),
alert.syntax().text_range(),
);
};
Block::Callout {
kind,
attributes: Attributes::default(),
blocks: alert
.block_nodes()
.filter_map(|block| self.block(block))
.collect(),
span: span(alert.syntax().text_range()),
}
}
fn code_block(&mut self, code: CodeBlock) -> Block {
if self.fragment || self.evaluated {
return fragments::display_code(&code, &mut self.fragment_sources)
.unwrap_or_else(|| self.unsupported_syntax(code.syntax()));
}
if matches!(self.format, AuthoredFormat::Qmd | AuthoredFormat::Rmd)
&& let Some(cell) = code.executable_cell()
{
let declarations = cell.option_declarations();
let options = declarations
.iter()
.map(|option| CellOption {
key: option.key().map(ToOwned::to_owned),
canonical_key: option.canonical_key(),
raw_value: option.raw_value().map(ToOwned::to_owned),
cooked_value: option.cooked_value().map(ToOwned::to_owned),
value: option.yaml_value().map(yaml_value),
source: match option.source() {
ChunkOptionSource::InlineInfo => crate::ir::CellOptionSource::InlineInfo,
ChunkOptionSource::HashpipeYaml => {
crate::ir::CellOptionSource::HashpipeYaml
}
},
key_span: option.key_range().map(span),
value_span: option.value_range().map(span),
span: span(option.declaration_range()),
})
.collect::<Vec<_>>();
let resolved_options = cell
.resolved_options()
.into_iter()
.map(|resolved| {
let resolution = match resolved.resolution() {
PanacheOptionResolution::Resolved(winner) => {
let declaration = declaration_index(&declarations, winner);
CellOptionResolution::Resolved { declaration }
}
PanacheOptionResolution::Ambiguous(winners) => {
let declarations = winners
.iter()
.map(|winner| declaration_index(&declarations, winner))
.collect::<Vec<_>>();
let first = declarations
.first()
.and_then(|index| options.get(*index))
.map(|option| option.span);
self.diagnostics.push(Diagnostic {
code: DiagnosticCode::AmbiguousCellOption,
severity: Severity::Warning,
message: format!(
"cell option `{}` has multiple declarations at the winning precedence",
resolved.key()
),
span: first,
related_spans: Vec::new(),
related_entity: None,
source: None,
});
CellOptionResolution::Ambiguous { declarations }
}
};
ResolvedCellOption {
key: resolved.key().to_string(),
resolution,
}
})
.collect();
return Block::CodeCell(CodeCell {
language: cell.language(),
identifier: cell.identifier().map(|(value, range)| SpannedString {
value,
span: span(range),
}),
classes: cell
.classes()
.into_iter()
.map(|(value, range)| SpannedString {
value,
span: span(range),
})
.collect(),
labels: cell
.labels()
.into_iter()
.map(|label| SpannedString {
value: label.value().to_string(),
span: span(label.value_range()),
})
.collect(),
source: cell.code_source(),
source_segments: source_segments(cell.code_source_segments()),
options,
resolved_options,
outputs: Vec::new(),
code_span: cell.code_range().map(span),
span: span(cell.text_range()),
});
}
Block::CodeBlock {
language: code.language(),
source: code.code_source(),
source_segments: source_segments(code.code_source_segments()),
span: span(code.syntax().text_range()),
}
}
fn table(&mut self, table: PanacheTable) -> Block {
if self.format == AuthoredFormat::Rmd && !matches!(table, PanacheTable::Pipe(_)) {
return self.rmd_table(table);
}
let PanacheTable::Pipe(table) = table else {
return self.unsupported_syntax(table.syntax());
};
let caption = table
.caption()
.map(|caption| self.inlines(caption.inline_nodes()))
.unwrap_or_default();
let alignments = table
.alignments()
.into_iter()
.map(table_alignment)
.collect();
let rows = table
.all_rows()
.map(|row| TableRow {
header: row.is_header(),
cells: row
.cells()
.map(|cell| {
let cell_span = span(cell.syntax().text_range());
let typed_blocks = cell.block_nodes().collect::<Vec<_>>();
let has_semantic_blocks = typed_blocks.iter().any(|block| {
matches!(
block,
BlockNode::Paragraph(_)
| BlockNode::Plain(_)
| BlockNode::BlockQuote(_)
| BlockNode::List(_)
| BlockNode::CodeBlock(_)
)
});
let blocks = if has_semantic_blocks {
typed_blocks
.into_iter()
.filter_map(|block| self.block(block))
.collect()
} else {
vec![Block::Paragraph {
inlines: self.inlines(cell.inline_nodes()),
span: cell_span,
}]
};
TableCell {
blocks,
span: cell_span,
}
})
.collect(),
span: span(row.syntax().text_range()),
})
.collect();
Block::Table {
caption,
alignments,
rows,
span: span(table.syntax().text_range()),
}
}
fn inlines(&mut self, nodes: impl Iterator<Item = InlineNode>) -> Vec<Inline> {
nodes
.filter(|node| node.syntax_kind() != panache_parser::syntax::SyntaxKind::LINE_PREFIX)
.map(|node| self.inline(node))
.collect()
}
fn inline(&mut self, inline: InlineNode) -> Inline {
let fallback_kind = format!("{:?}", inline.syntax_kind());
let fallback_raw = inline.source_text();
let fallback_range = inline.text_range();
match inline {
InlineNode::Text(text) => Inline::Text {
value: if self.math_delimiters.iter().any(|marker| {
marker.start < span(text.text_range()).end
&& marker.end > span(text.text_range()).start
}) {
text.decoded().replace('$', "")
} else {
text.decoded()
},
span: span(text.text_range()),
},
InlineNode::Space(text) => Inline::Space {
span: span(text.text_range()),
},
InlineNode::SoftBreak(text) => Inline::SoftBreak {
span: span(text.text_range()),
},
InlineNode::HardBreak(text) => Inline::HardBreak {
span: span(text.text_range()),
},
InlineNode::NonbreakingSpace(text) => Inline::NonbreakingSpace {
span: span(text.text_range()),
},
InlineNode::Emphasis(container) => Inline::Emphasis {
inlines: self.inlines(container.inline_nodes()),
span: span(container.syntax().text_range()),
},
InlineNode::Strong(container) => Inline::Strong {
inlines: self.inlines(container.inline_nodes()),
span: span(container.syntax().text_range()),
},
InlineNode::Strikeout(container) => Inline::Strikeout {
inlines: self.inlines(container.inline_nodes()),
span: span(container.syntax().text_range()),
},
InlineNode::Code(code) => Inline::Code {
value: code.content(),
span: span(code.syntax().text_range()),
},
InlineNode::Link(link) => self.link(link),
InlineNode::Image(image) => self.image(image),
InlineNode::AutoLink(link) => Inline::AutoLink {
target: link.target(),
span: span(link.syntax().text_range()),
},
InlineNode::UnresolvedReference(reference) => {
let mut children = reference.inline_nodes().collect::<Vec<_>>();
if children.len() == 1
&& let InlineNode::Code(code) = children.remove(0)
{
return Inline::SemanticReference {
target: code.content(),
span: span(reference.syntax().text_range()),
target_span: span(
code.content_range()
.unwrap_or_else(|| code.syntax().text_range()),
),
};
}
Inline::Text {
value: reference.syntax().text().to_string(),
span: span(reference.syntax().text_range()),
}
}
InlineNode::Superscript(container) if self.format == AuthoredFormat::Rmd => {
Inline::Styled {
style: crate::ir::InlineStyle::Superscript,
inlines: self.inlines(container.inline_nodes()),
span: span(container.syntax().text_range()),
}
}
InlineNode::Subscript(container) if self.format == AuthoredFormat::Rmd => {
Inline::Styled {
style: crate::ir::InlineStyle::Subscript,
inlines: self.inlines(container.inline_nodes()),
span: span(container.syntax().text_range()),
}
}
InlineNode::Mark(container)
| InlineNode::Superscript(container)
| InlineNode::Subscript(container) => self.unsupported_inline(
format!("{:?}", container.syntax().kind()),
container.syntax().text().to_string(),
container.syntax().text_range(),
),
InlineNode::Math(math)
if matches!(self.format, AuthoredFormat::Gfm | AuthoredFormat::Rmd) =>
{
Inline::Math {
latex: math.content(),
fallback: None,
span: span(math.syntax().text_range()),
}
}
InlineNode::Executable(code) if self.format == AuthoredFormat::Rmd => Inline::Code {
value: code.syntax().text().to_string(),
span: span(code.syntax().text_range()),
},
InlineNode::Math(math) => self.unsupported_inline(
format!("{:?}", math.syntax().kind()),
math.syntax().text().to_string(),
math.syntax().text_range(),
),
InlineNode::Unknown(node) => {
if self.format == AuthoredFormat::Gfm
&& let Some(math) = node
.syntax_element()
.as_node()
.and_then(|node| panache_parser::syntax::DisplayMath::cast(node.clone()))
{
return Inline::Math {
latex: math.content(),
fallback: None,
span: span(math.syntax().text_range()),
};
}
if self.format == AuthoredFormat::Rmd
&& let Some(inline) = self.rmd_unknown(&node)
{
return inline;
}
self.unsupported_inline(node.syntax_name(), node.source_text(), node.text_range())
}
_ => self.unsupported_inline(fallback_kind, fallback_raw, fallback_range),
}
}
fn link(&mut self, link: panache_parser::syntax::Link) -> Inline {
let (target, title) = self.link_target(
link.dest().map(|destination| {
(
(self.resource_target)(
&destination.url_content(),
span(destination.syntax().text_range()),
),
destination.title(),
)
}),
link.reference().map(|reference| reference.label()),
link.text().map(|text| text.raw_label()),
);
Inline::Link {
inlines: self.inlines(link.inline_nodes()),
target,
title,
attributes: link.attributes().map(attributes).unwrap_or_default(),
span: span(link.syntax().text_range()),
}
}
fn image(&mut self, image: panache_parser::syntax::ImageLink) -> Inline {
let (target, title) = self.link_target(
image.dest().map(|destination| {
(
(self.resource_target)(
&destination.url_content(),
span(destination.syntax().text_range()),
),
destination.title(),
)
}),
image.reference().map(|reference| reference.label()),
image.alt().map(|alt| alt.syntax().text().to_string()),
);
Inline::Image {
alt: self.inlines(image.inline_nodes()),
target,
title,
attributes: image.attributes().map(attributes).unwrap_or_default(),
span: span(image.syntax().text_range()),
}
}
fn link_target(
&self,
inline: Option<(String, Option<String>)>,
reference: Option<String>,
fallback_label: Option<String>,
) -> (String, Option<String>) {
if let Some(target) = inline {
return target;
}
let label = reference
.filter(|label| !label.is_empty())
.or(fallback_label)
.unwrap_or_default();
self.references
.get(&normalize_reference_label(&label))
.cloned()
.unwrap_or((String::new(), None))
}
fn unsupported_syntax(&mut self, syntax: &panache_parser::syntax::SyntaxNode) -> Block {
self.unsupported(
format!("{:?}", syntax.kind()),
syntax.text().to_string(),
syntax.text_range(),
)
}
fn unsupported(&mut self, source_kind: String, raw: String, range: TextRange) -> Block {
let source_span = span(range);
self.diagnostics.push(Diagnostic {
code: DiagnosticCode::UnsupportedAuthoredSyntax,
severity: Severity::Warning,
message: format!("unsupported authored syntax: {source_kind}"),
span: Some(source_span),
related_spans: Vec::new(),
related_entity: None,
source: None,
});
Block::Unsupported {
source_kind,
raw,
span: source_span,
}
}
fn unsupported_inline(&mut self, source_kind: String, raw: String, range: TextRange) -> Inline {
let source_span = span(range);
self.diagnostics.push(Diagnostic {
code: DiagnosticCode::UnsupportedAuthoredSyntax,
severity: Severity::Warning,
message: format!("unsupported authored syntax: {source_kind}"),
span: Some(source_span),
related_spans: Vec::new(),
related_entity: None,
source: None,
});
Inline::Unsupported {
source_kind,
raw,
span: source_span,
}
}
}
fn attributes(node: AttributeNode) -> Attributes {
let mut attributes = Attributes::default();
for entry in node.entries() {
match entry {
AttributeEntry::Identifier(value) => {
attributes.identifier = Some(SpannedString {
value: value.value().to_string(),
span: span(value.text_range()),
});
}
AttributeEntry::Class(value) => attributes.classes.push(SpannedString {
value: value.value().to_string(),
span: span(value.text_range()),
}),
AttributeEntry::KeyValue { key, value } => {
attributes.key_values.push(AttributeKeyValue {
key: SpannedString {
value: key.value().to_string(),
span: span(key.text_range()),
},
value: SpannedString {
value: value.value().to_string(),
span: span(value.text_range()),
},
});
}
}
}
attributes
}
fn yaml_value(value: &YamlNode) -> MetadataValue {
match value {
YamlNode::Scalar(scalar) => MetadataValue::Scalar {
raw: scalar.raw(),
value: scalar.value(),
span: span(scalar.text_range()),
},
YamlNode::BlockMap(map) => yaml_block_map(map),
YamlNode::FlowMap(map) => yaml_flow_map(map),
YamlNode::BlockSequence(sequence) => yaml_block_sequence(sequence),
YamlNode::FlowSequence(sequence) => yaml_flow_sequence(sequence),
}
}
fn yaml_block_map(map: &YamlBlockMap) -> MetadataValue {
MetadataValue::Mapping {
entries: map.entries().map(yaml_block_entry).collect(),
span: span(map.syntax().text_range()),
}
}
fn yaml_block_entry(entry: YamlBlockMapEntry) -> MetadataEntry {
let key_scalar = entry.key().and_then(|key| key.scalar());
let key_span = key_scalar.as_ref().map_or_else(
|| span(entry.syntax().text_range()),
|key| span(key.text_range()),
);
let value = entry.value();
MetadataEntry {
key: SpannedString {
value: entry.key_text().unwrap_or_default(),
span: key_span,
},
value: value
.as_ref()
.and_then(|value| value.as_node())
.as_ref()
.map_or_else(
|| MetadataValue::Null {
span: value
.as_ref()
.map_or(key_span, |value| span(value.syntax().text_range())),
},
yaml_value,
),
span: span(entry.syntax().text_range()),
}
}
fn yaml_flow_map(map: &YamlFlowMap) -> MetadataValue {
MetadataValue::Mapping {
entries: map.entries().map(yaml_flow_entry).collect(),
span: span(map.syntax().text_range()),
}
}
fn yaml_flow_entry(entry: YamlFlowMapEntry) -> MetadataEntry {
let key_scalar = entry.key().and_then(|key| key.scalar());
let key_span = key_scalar.as_ref().map_or_else(
|| span(entry.syntax().text_range()),
|key| span(key.text_range()),
);
let value = entry.value();
MetadataEntry {
key: SpannedString {
value: entry.key_text().unwrap_or_default(),
span: key_span,
},
value: value
.as_ref()
.and_then(|value| value.as_node())
.as_ref()
.map_or_else(
|| MetadataValue::Null {
span: value
.as_ref()
.map_or(key_span, |value| span(value.syntax().text_range())),
},
yaml_value,
),
span: span(entry.syntax().text_range()),
}
}
fn yaml_block_sequence(sequence: &YamlBlockSequence) -> MetadataValue {
MetadataValue::Sequence {
items: sequence
.items()
.map(|item| {
item.as_node().as_ref().map_or_else(
|| MetadataValue::Null {
span: span(item.syntax().text_range()),
},
yaml_value,
)
})
.collect(),
span: span(sequence.syntax().text_range()),
}
}
fn yaml_flow_sequence(sequence: &YamlFlowSequence) -> MetadataValue {
MetadataValue::Sequence {
items: sequence
.items()
.map(|item| {
item.as_node().as_ref().map_or_else(
|| MetadataValue::Null {
span: span(item.syntax().text_range()),
},
yaml_value,
)
})
.collect(),
span: span(sequence.syntax().text_range()),
}
}
fn source_segments(segments: Vec<panache_parser::syntax::CodeSourceSegment>) -> Vec<SourceSegment> {
segments
.into_iter()
.map(|segment| SourceSegment {
text: segment.text().to_string(),
span: span(segment.text_range()),
})
.collect()
}
fn declaration_index(
declarations: &[panache_parser::syntax::CellOptionDeclaration],
needle: &panache_parser::syntax::CellOptionDeclaration,
) -> usize {
declarations
.iter()
.position(|candidate| candidate.declaration_range() == needle.declaration_range())
.expect("resolved declaration belongs to the cell")
}
fn alert_kind(kind: PanacheAlertKind) -> CalloutKind {
match kind {
PanacheAlertKind::Note => CalloutKind::Note,
PanacheAlertKind::Tip => CalloutKind::Tip,
PanacheAlertKind::Important => CalloutKind::Important,
PanacheAlertKind::Warning => CalloutKind::Warning,
PanacheAlertKind::Caution => CalloutKind::Caution,
}
}
fn callout_kind(kind: PanacheCalloutKind) -> CalloutKind {
match kind {
PanacheCalloutKind::Note => CalloutKind::Note,
PanacheCalloutKind::Tip => CalloutKind::Tip,
PanacheCalloutKind::Important => CalloutKind::Important,
PanacheCalloutKind::Warning => CalloutKind::Warning,
PanacheCalloutKind::Caution => CalloutKind::Caution,
}
}
fn table_alignment(alignment: PanacheTableAlignment) -> TableAlignment {
match alignment {
PanacheTableAlignment::Default => TableAlignment::Default,
PanacheTableAlignment::Left => TableAlignment::Left,
PanacheTableAlignment::Center => TableAlignment::Center,
PanacheTableAlignment::Right => TableAlignment::Right,
}
}
fn span(range: TextRange) -> SourceSpan {
SourceSpan {
start: range.start().into(),
end: range.end().into(),
}
}
pub(crate) fn inline_text(inlines: &[Inline]) -> String {
inlines
.iter()
.map(|inline| match inline {
Inline::Text { value, .. } | Inline::Code { value, .. } => value.clone(),
Inline::Space { .. }
| Inline::SoftBreak { .. }
| Inline::HardBreak { .. }
| Inline::NonbreakingSpace { .. } => " ".into(),
Inline::Emphasis { inlines, .. }
| Inline::Strong { inlines, .. }
| Inline::Styled { inlines, .. }
| Inline::Citation { inlines, .. }
| Inline::Strikeout { inlines, .. }
| Inline::Link { inlines, .. } => inline_text(inlines),
Inline::Image { alt, .. } => inline_text(alt),
Inline::AutoLink { target, .. } | Inline::SemanticReference { target, .. } => {
target.clone()
}
Inline::Unsupported { raw, .. } => raw.clone(),
Inline::Math {
latex, fallback, ..
} => fallback.as_ref().unwrap_or(latex).clone(),
})
.collect()
}