use std::collections::HashMap;
use panache_parser::ParserOptions;
use panache_parser::syntax::{AstNode, CodeBlock, Document, TextRange};
use serde::{Deserialize, Serialize};
use crate::diagnostics::{Diagnostic, DiagnosticSource};
use crate::ir::{
Block, CellOutput, CellOutputKind, OutputRepresentation, Provenance, ProvenanceActivity,
SourceLocation, SourceSegment,
};
use crate::provenance::builtin_tools;
use super::{AuthoredFormat, parse_document, source_segments, span};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MarkdownFragmentOrigin {
pub collection: String,
pub cell: usize,
pub output: usize,
pub source: SourceLocation,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MarkdownFragmentParse {
pub representation: OutputRepresentation,
pub provenance: Provenance,
pub diagnostics: Vec<Diagnostic>,
}
impl MarkdownFragmentParse {
pub fn into_cell_output(self, kind: CellOutputKind) -> (CellOutput, Vec<Diagnostic>) {
(
CellOutput {
kind,
representations: vec![self.representation],
provenance: vec![self.provenance],
},
self.diagnostics,
)
}
}
pub fn parse_markdown_fragment(
source: &str,
origin: MarkdownFragmentOrigin,
) -> MarkdownFragmentParse {
let parsed = parse_document(source, AuthoredFormat::Gfm, super::ParseMode::Fragment);
MarkdownFragmentParse {
representation: OutputRepresentation::MarkdownBlocks {
media_type: "text/markdown".into(),
blocks: parsed.document.blocks,
},
provenance: Provenance {
activity: ProvenanceActivity::GeneratedMarkdown {
collection: origin.collection,
cell: origin.cell,
output: origin.output,
},
source: Some(DiagnosticSource::Repository {
repository: origin.source.repository,
path: origin.source.path,
}),
span: origin.source.span,
tools: builtin_tools(),
},
diagnostics: parsed.diagnostics,
}
}
pub(super) fn display_sources(
source: &str,
document: &Document,
options: ParserOptions,
) -> HashMap<TextRange, Vec<SourceSegment>> {
let mut display_source = source.to_owned();
let mut changed = false;
for code in document.syntax().descendants().filter_map(CodeBlock::cast) {
if code.hashpipe_yaml_preamble().is_some()
&& let Some(info) = code.info()
{
let range = span(info.syntax().text_range());
display_source
.replace_range(range.start..range.end, &" ".repeat(range.end - range.start));
changed = true;
}
}
if !changed {
return HashMap::new();
}
let parsed = panache_parser::parse_document(&display_source, Some(options));
parsed
.document()
.syntax()
.descendants()
.filter_map(CodeBlock::cast)
.map(|code| {
(
code.syntax().text_range(),
source_segments(code.code_source_segments()),
)
})
.collect()
}
pub(super) fn display_code(
code: &CodeBlock,
display_sources: &mut HashMap<TextRange, Vec<SourceSegment>>,
) -> Option<Block> {
let source_segments = if code.hashpipe_yaml_preamble().is_some() {
display_sources.remove(&code.syntax().text_range())?
} else {
source_segments(code.code_source_segments())
};
Some(Block::CodeBlock {
language: code.language(),
source: source_segments
.iter()
.map(|segment| segment.text.as_str())
.collect(),
source_segments,
span: span(code.syntax().text_range()),
})
}