use crate::ast::{self, Node, PathSegment};
use crate::ext::ParseOptions;
use crate::grammar::{self, AttributeType};
use crate::markdown_it::Token;
use crate::next::index::{default_tokenizer, parse_tokens};
use crate::next::parser::ParserArgs;
use crate::next::parser::positions::Positions;
use crate::next::tokenizer::TokenizerOptions;
use crate::positions::LineIndex;
use crate::types::{MarkdocError, Position, Result, Scalar, Span, js_insert};
use indexmap::IndexMap;
use std::ops::Range;
#[derive(Debug, Clone)]
pub struct Occurrence {
pub kind: OccurrenceKind,
pub span: Range<usize>,
pub location: Span,
pub in_fence: bool,
pub partner: Option<usize>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum OccurrenceKind {
Open {
name: String,
attributes: IndexMap<String, Scalar>,
},
SelfClosing {
name: String,
attributes: IndexMap<String, Scalar>,
},
Close { name: String },
Annotation {
attributes: IndexMap<String, Scalar>,
},
Interpolation { value: Scalar },
Error { message: String },
}
fn tokenize(source: &str, options: &ParseOptions) -> Result<(Vec<Token>, ParserArgs)> {
let args = options.parser_args().into_owned();
let text = if args.strip_bom {
source.strip_prefix('\u{FEFF}').unwrap_or(source)
} else {
source
};
let tokens = if options.tokenizer == TokenizerOptions::default() {
default_tokenizer().tokenize(text)
} else {
options.tokenizer().tokenize(text)
};
let tokens = tokens.map_err(|e| MarkdocError::ParseError(e.to_string()))?;
Ok((tokens, args))
}
pub fn occurrences(source: &str, options: &ParseOptions) -> Result<Vec<Occurrence>> {
let (tokens, args) = tokenize(source, options)?;
Ok(from_tokens(&tokens, source, &args))
}
pub fn parse_with_occurrences(
source: &str,
options: &ParseOptions,
) -> Result<(Node, Vec<Occurrence>)> {
let (tokens, args) = tokenize(source, options)?;
let node = parse_tokens(&tokens, source, &args)?;
Ok((node, from_tokens(&tokens, source, &args)))
}
struct Open {
type_name: String,
tag: Option<String>,
occurrence: Option<usize>,
}
struct Walk<'t> {
children: &'t [Token],
spans: Vec<Range<usize>>,
end: usize,
next: usize,
in_fence: bool,
}
struct Finder<'s> {
source: &'s str,
file: Option<String>,
index: LineIndex,
stack: Vec<Open>,
found: Vec<Occurrence>,
}
pub fn from_tokens(tokens: &[Token], source: &str, args: &ParserArgs) -> Vec<Occurrence> {
let mut finder = Finder {
source,
file: args.file.clone(),
index: LineIndex::new(source),
stack: vec![Open {
type_name: "document".into(),
tag: None,
occurrence: None,
}],
found: Vec::new(),
};
let mut positions = Positions::new(source);
for i in 0..tokens.len() {
let (span, children) = positions.token(tokens, i);
let children = children.map(|spans| spans.iter().map(|s| s.start..s.end).collect());
let mut open: Vec<Walk> = Vec::new();
open.extend(finder.visit(&tokens[i], span.start..span.end, children, false));
while let Some(walk) = open.last_mut() {
let Some(child) = walk.children.get(walk.next) else {
open.pop();
finder.stack.pop();
continue;
};
let span = walk
.spans
.get(walk.next)
.cloned()
.unwrap_or(walk.end..walk.end);
walk.next += 1;
let in_fence = walk.in_fence;
let grandchildren = child
.children
.as_ref()
.map(|c| vec![span.start..span.start; c.len()]);
open.extend(finder.visit(child, span, grandchildren, in_fence));
}
}
finder.found
}
fn same(open: &Open, type_name: &str, tag: Option<&str>) -> bool {
open.type_name == type_name && open.tag.as_deref() == tag
}
impl Finder<'_> {
fn location(&self, span: &Range<usize>) -> Span {
let edge = |offset: usize| {
let (line, character) = self.index.line_col(self.source, offset);
Position {
line,
character: Some(character),
offset,
}
};
Span {
file: self.file.clone(),
start: edge(span.start),
end: edge(span.end.max(span.start)),
}
}
fn visit<'t>(
&mut self,
token: &'t Token,
span: Range<usize>,
children: Option<Vec<Range<usize>>>,
in_fence: bool,
) -> Option<Walk<'t>> {
if token.kind == "frontmatter"
|| token.hidden
|| (token.kind == "text" && token.content.is_empty())
{
return None;
}
if token.kind == "fence" {
self.fence_tag(token, &span);
}
let occurrence = self.occurrence(token, &span, in_fence);
if token.kind == "annotation" {
return None;
}
let kind = token
.kind
.strip_suffix("_open")
.or_else(|| token.kind.strip_suffix("_close"))
.unwrap_or(&token.kind);
let type_name = match kind {
"ordered_list" | "bullet_list" => "list",
"code_inline" => "code",
"list_item" => "item",
"variable" => "text",
other => other,
};
let meta_tag = token
.meta
.as_ref()
.and_then(|m| m.get("tag"))
.and_then(|t| t.as_str());
if token.nesting < 0
&& let Some(top) = self.stack.last()
&& same(top, type_name, meta_tag)
{
if let (Some(open), Some(close)) = (top.occurrence, occurrence) {
self.found[open].partner = Some(close);
self.found[close].partner = Some(open);
}
self.stack.pop();
return None;
}
let node = || Open {
type_name: type_name.to_string(),
tag: meta_tag.filter(|t| !t.is_empty()).map(str::to_string),
occurrence,
};
if token.nesting > 0 {
self.stack.push(node());
}
let tokens = token.children.as_deref()?;
if type_name == "image" {
return None;
}
self.stack.push(node());
Some(Walk {
children: tokens,
spans: children.unwrap_or_default(),
end: span.end,
next: 0,
in_fence: in_fence || token.kind == "fence",
})
}
fn fence_tag(&mut self, token: &Token, span: &Range<usize>) {
if !token.info.contains("{%") {
return;
}
let kind = match &token.grammar_meta {
Some(meta) => OccurrenceKind::Annotation {
attributes: attributes(meta),
},
None => {
let Some(message) = token.errors.iter().flatten().find_map(|e| {
let message = e.get("message")?.as_str()?;
(e.get("id")?.as_str()? == "fence-tag-error").then_some(message)
}) else {
return;
};
OccurrenceKind::Error {
message: message
.strip_prefix("Syntax error in fence tag: ")
.unwrap_or(message)
.to_string(),
}
}
};
let line_end = self.source[span.start..]
.find(['\n', '\r'])
.map_or(self.source.len(), |i| span.start + i);
let line = &self.source[span.start..line_end];
let marker = token.markup.len().min(line.len());
let Some(open) = line.get(marker..).and_then(|rest| rest.find("{%")) else {
return;
};
let start = span.start + marker + open;
let units = crate::markdown_it::js::to_utf16(&self.source[start..line_end]);
let end = match crate::next::tokenizer::utils::find_tag_end(&units, 0) {
Some(close) => start + String::from_utf16_lossy(&units[..close + 2]).len(),
None => line_end,
};
let span = start..end.min(line_end);
self.found.push(Occurrence {
kind,
location: self.location(&span),
span,
in_fence: false,
partner: None,
});
}
fn occurrence(&mut self, token: &Token, span: &Range<usize>, in_fence: bool) -> Option<usize> {
let meta = token.grammar_meta.as_deref();
let name = || meta.and_then(|m| m.tag.clone()).unwrap_or_default();
let attributes = || meta.map(attributes).unwrap_or_default();
let kind = match token.kind.as_str() {
"tag_open" => OccurrenceKind::Open {
name: name(),
attributes: attributes(),
},
"tag" => OccurrenceKind::SelfClosing {
name: name(),
attributes: attributes(),
},
"tag_close" => OccurrenceKind::Close { name: name() },
"annotation" => OccurrenceKind::Annotation {
attributes: attributes(),
},
"variable" => OccurrenceKind::Interpolation {
value: meta
.and_then(|m| m.variable.as_ref())
.map_or(Scalar::Undefined, scalar),
},
"error" => OccurrenceKind::Error {
message: token
.meta
.as_ref()
.and_then(|m| m.get("error"))
.and_then(|e| e.get("message"))
.and_then(|m| m.as_str())
.unwrap_or_default()
.to_string(),
},
_ => return None,
};
let span = span.clone();
self.found.push(Occurrence {
kind,
location: self.location(&span),
span,
in_fence,
partner: None,
});
Some(self.found.len() - 1)
}
}
fn attributes(meta: &grammar::Meta) -> IndexMap<String, Scalar> {
let mut out = IndexMap::new();
for attribute in meta.attributes.iter().flatten().flatten() {
match attribute.kind {
AttributeType::Attribute => {
js_insert(&mut out, attribute.name.clone(), scalar(&attribute.value));
}
AttributeType::Class => match out.get_mut("class") {
Some(Scalar::Object(class)) => {
js_insert(class, attribute.name.clone(), scalar(&attribute.value));
}
Some(_) => {}
None => {
let mut class = IndexMap::new();
js_insert(&mut class, attribute.name.clone(), scalar(&attribute.value));
js_insert(&mut out, "class".to_string(), Scalar::Object(class));
}
},
}
}
out
}
fn scalar(value: &grammar::Value) -> Scalar {
let object = |o: &grammar::Object| {
let mut out = IndexMap::new();
for (k, v) in o.entries() {
js_insert(&mut out, k.to_string(), scalar(v));
}
out
};
match value {
grammar::Value::Null => Scalar::Null,
grammar::Value::Bool(b) => Scalar::Boolean(*b),
grammar::Value::Number(n) => Scalar::Number(*n),
grammar::Value::String(s) => Scalar::String(s.clone()),
grammar::Value::Array(items) => Scalar::Array(items.iter().map(scalar).collect()),
grammar::Value::Object(o) => Scalar::Object(object(o)),
grammar::Value::Variable(v) => Scalar::Variable(ast::Variable::new(
v.path
.iter()
.map(|step| match step {
grammar::Value::Number(n) => PathSegment::Number(*n),
grammar::Value::String(s) => PathSegment::String(s.clone()),
other => PathSegment::String(other.to_json_string()),
})
.collect(),
)),
grammar::Value::Function(f) => {
Scalar::Function(ast::Function::new(f.name.clone(), object(&f.parameters)))
}
}
}
#[cfg(test)]
mod tests;