use serde::{Deserialize, Serialize};
use super::{CoreFields, core_fields};
use crate::{
Body, Finding, RepoPath, Segments, Source, Spanned,
markdown::{self, BlockKind},
source, yaml,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ParseMode {
Document,
Plain,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Parsed {
pub frontmatter: Option<Spanned>,
pub core: CoreFields,
pub body: Body,
pub segments: Segments,
pub body_line: usize,
pub frontmatter_found: bool,
}
pub fn parse_source(source: &Source, mode: ParseMode) -> Parsed {
let (frontmatter, text, body_line) = match mode {
ParseMode::Document => {
let split = source::split(source);
(split.frontmatter, split.body, split.body_line)
}
ParseMode::Plain => (None, source.text.as_str(), 1),
};
let yaml = frontmatter.map(|(text, line)| yaml::parse(text, line));
let core = match mode {
ParseMode::Document => core_fields(yaml.as_ref().map(|r| r.as_ref()), &source.path),
ParseMode::Plain => CoreFields::default(),
};
let segments = markdown::segments(text, body_line);
let body = markdown::parse(text, body_line, &segments);
Parsed {
frontmatter: yaml.and_then(Result::ok),
core,
body,
segments,
body_line,
frontmatter_found: frontmatter.is_some(),
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Document {
pub path: RepoPath,
pub source: Source,
pub frontmatter: Option<Spanned>,
pub core: CoreFields,
pub body: Body,
pub segments: Segments,
pub body_line: usize,
pub lines: usize,
pub frontmatter_found: bool,
}
#[derive(Serialize)]
struct Visible<'a> {
id: Option<&'a str>,
type_: Option<&'a str>,
title: Option<&'a str>,
description: Option<&'a str>,
status: Option<super::Status>,
resource: Option<&'a super::Resource>,
headings: Vec<(u8, &'a str, &'a str)>,
anchors: Vec<&'a str>,
frontmatter_found: bool,
}
impl Document {
pub fn new(source: Source, parsed: Parsed) -> Self {
Document {
path: source.path.clone(),
lines: source.line_count(),
source,
frontmatter: parsed.frontmatter,
core: parsed.core,
body: parsed.body,
segments: parsed.segments,
body_line: parsed.body_line,
frontmatter_found: parsed.frontmatter_found,
}
}
pub fn visible_hash(&self) -> u64 {
let headings = crate::markdown::link_headings(&self.body);
let visible = Visible {
id: self.core.id.as_deref(),
type_: self.core.type_.as_deref(),
title: self.core.title.as_deref(),
description: self.core.description.as_deref(),
status: self.core.status,
resource: self.core.resource.as_ref(),
headings: headings
.iter()
.map(|h| (h.level, h.text.as_str(), h.slug.as_str()))
.collect(),
anchors: self.body.anchors.iter().map(|a| a.id.as_str()).collect(),
frontmatter_found: self.frontmatter_found,
};
source::fnv1a64(&serde_json::to_vec(&visible).unwrap_or_default())
}
pub fn line_count(&self) -> usize {
self.lines
}
pub fn body_text(&self) -> &str {
source::split(&self.source).body
}
pub fn id(&self) -> Option<&str> {
self.core.id.as_deref()
}
pub fn type_name(&self) -> Option<&str> {
self.core.type_.as_deref()
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct SchemaDocument {
pub doc: Document,
pub schema_yaml: Option<Spanned>,
pub schema_problem: Option<Finding>,
pub globs: Vec<String>,
pub asset_globs: Vec<String>,
pub type_const: Option<String>,
pub uses: Vec<String>,
pub archived: super::Archived,
pub loaded: bool,
}
impl SchemaDocument {
pub fn finding(&self, criterion: &'static str, message: impl Into<String>) -> Finding {
Finding::error(
self.doc.path.clone(),
Some(self.doc.body_line),
criterion,
message,
)
}
pub fn placeholder(path: RepoPath, meta: super::SchemaMeta) -> Self {
let source = Source::from_text(path, "");
let mut doc = Document::new(source.clone(), parse_source(&source, ParseMode::Document));
doc.core.id = meta.id;
doc.core.type_ = Some("Schema".to_string());
doc.body_line = meta.body_line;
SchemaDocument {
doc,
schema_yaml: None,
schema_problem: None,
globs: meta.globs,
asset_globs: meta.asset_globs,
type_const: meta.type_const,
uses: meta.uses,
archived: meta.archived,
loaded: false,
}
}
pub fn new(doc: Document) -> Self {
let mut schema_yaml = None;
let mut schema_problem = None;
let fence = doc.body.blocks.iter().find(|b| b.kind == BlockKind::Code);
match fence {
Some(block) => {
let text = block.units.first().map(|u| u.text.as_str()).unwrap_or("");
match yaml::parse(text, block.line + 1) {
Ok(y) => schema_yaml = Some(y),
Err(e) => {
schema_problem = Some(Finding::error(
doc.path.clone(),
Some(e.line()),
"SOKF_SCHEMA_2",
format!("schema: {e}"),
));
}
}
}
None => {
schema_problem = Some(Finding::error(
doc.path.clone(),
Some(doc.body_line),
"SOKF_SCHEMA_1",
"the Schema document has no schema because it has no YAML fence",
));
}
}
let list = |key: &str| {
schema_yaml
.as_ref()
.and_then(|c| c.get(key))
.and_then(|a| a.as_str_or_list())
.unwrap_or_default()
};
let (globs, asset_globs) = (list("additional-files"), list("asset-files"));
let type_const = schema_yaml
.as_ref()
.and_then(|c| c.get("frontmatter"))
.and_then(|f| f.get("type"))
.and_then(|t| t.get("const"))
.and_then(|c| c.scalar_text());
let uses = crate::grammar::schema_uses(&doc.source.text);
let archived = schema_yaml
.as_ref()
.map(super::Archived::of_schema)
.unwrap_or_default();
SchemaDocument {
doc,
schema_yaml,
schema_problem,
globs,
asset_globs,
type_const,
uses,
archived,
loaded: true,
}
}
pub fn id(&self) -> String {
self.doc.id().map(str::to_string).unwrap_or_else(|| {
self.doc
.path
.file_name()
.trim_end_matches(".md")
.to_string()
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct SchemaRef {
pub knowledge: usize,
pub index: usize,
}
#[derive(Debug, Clone, PartialEq)]
pub struct AdditionalFile {
pub path: RepoPath,
pub source: Source,
pub body: Body,
pub segments: Segments,
pub schema: Option<SchemaRef>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Asset {
pub path: RepoPath,
pub schema: SchemaRef,
pub sidecar: Option<usize>,
pub has_sidecar: bool,
}
impl Asset {
pub fn sidecar_path(&self) -> RepoPath {
sidecar_path(&self.path)
}
}
pub fn sidecar_path(path: &RepoPath) -> RepoPath {
RepoPath::parse(&format!("{path}.md")).expect("a path plus `.md` is a path")
}
#[derive(Debug, Clone, PartialEq)]
pub struct IndexFile {
pub path: RepoPath,
pub source: Source,
}
#[cfg(test)]
mod tests {
use super::*;
fn src(text: &str) -> Source {
Source::from_text(RepoPath::parse("/k/schemas/s.md").unwrap(), text)
}
#[test]
fn parses_documents_and_plain_files() {
let s = src("---\ntype: A\nid: a\n---\n\n# T\n\n<!-- sokf:links -->\n");
let p = parse_source(&s, ParseMode::Document);
assert_eq!(p.core.type_.as_deref(), Some("A"));
assert_eq!(p.body.headings[0].line, 6);
assert_eq!(p.body_line, 5);
assert!(p.segments.definition.is_some());
let d = Document::new(s.clone(), p);
assert_eq!(d.line_count(), 8);
assert_eq!(d.body_text(), "\n# T\n\n<!-- sokf:links -->\n");
assert_eq!(d.id(), Some("a"));
assert_eq!(d.type_name(), Some("A"));
let plain = parse_source(&src("---\nnot: fm\n---\n# H\n"), ParseMode::Plain);
assert!(plain.frontmatter.is_none());
assert_eq!(
plain
.body
.headings
.iter()
.find(|h| h.text == "H")
.unwrap()
.line,
4
);
assert!(!plain.core.present);
let json = serde_json::to_string(&plain).unwrap();
let back: Parsed = serde_json::from_str(&json).unwrap();
assert_eq!(back, plain);
}
#[test]
fn a_line_shift_is_not_visible_and_a_title_is() {
let text = "---\ntype: A\nid: a\ntitle: T\n---\n\n# T\n\nSee [b][sokf:b].\n";
let doc = |t: &str| {
let s = Source::from_text(RepoPath::parse("/k/a.md").unwrap(), t);
Document::new(s.clone(), parse_source(&s, ParseMode::Document))
};
let full = doc(text);
let shifted = doc(&text.replacen("# T\n", "\n\n# T\n", 1));
assert_ne!(shifted.body.headings[0].line, full.body.headings[0].line);
assert_eq!(shifted.visible_hash(), full.visible_hash());
let renamed = doc(&text.replacen("title: T", "title: U", 1));
assert_ne!(renamed.visible_hash(), full.visible_hash());
let reheaded = doc(&text.replacen("# T\n", "# U\n", 1));
assert_ne!(reheaded.visible_hash(), full.visible_hash());
}
#[test]
fn schemas_extract_their_schema() {
let s = src(
"---\ntype: Schema\nid: schema-x\n---\n\n````yaml\nadditional-files: [/README.md]\nasset-files: '/k/media/**/*.png'\nfrontmatter:\n type:\n const: X\n````\n",
);
let schema = SchemaDocument::new(Document::new(
s.clone(),
parse_source(&s, ParseMode::Document),
));
assert_eq!(schema.globs, vec!["/README.md"]);
assert_eq!(schema.asset_globs, vec!["/k/media/**/*.png"]);
assert_eq!(schema.type_const.as_deref(), Some("X"));
assert_eq!(
sidecar_path(&RepoPath::parse("/k/media/a.png").unwrap()).as_str(),
"/k/media/a.png.md"
);
assert_eq!(
schema
.schema_yaml
.as_ref()
.unwrap()
.get("additional-files")
.unwrap()
.line,
7
);
assert_eq!(schema.id(), "schema-x");
assert!(schema.schema_problem.is_none());
let none = src("---\ntype: Schema\n---\n\nno fence\n");
let schema = SchemaDocument::new(Document::new(
none.clone(),
parse_source(&none, ParseMode::Document),
));
assert!(schema.schema_yaml.is_none());
assert!(
schema
.schema_problem
.as_ref()
.unwrap()
.message
.contains("no YAML fence")
);
assert_eq!(schema.id(), "s");
let bad = src("---\ntype: Schema\n---\n\n````yaml\na: &x 1\n````\n");
let schema = SchemaDocument::new(Document::new(
bad.clone(),
parse_source(&bad, ParseMode::Document),
));
let problem = schema.schema_problem.unwrap();
assert_eq!(problem.line, Some(6));
assert!(problem.message.contains("anchor"));
}
}