use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use comrak::nodes::{AstNode, NodeValue};
use comrak::{Arena, parse_document};
use ignore::WalkBuilder;
use crate::document::{Document, Kind};
use crate::folder::Folder;
use crate::page::Rendered;
use crate::widgets::{Modules, Widgets};
use super::*;
#[derive(Debug, Clone, PartialEq)]
pub(super) struct Unit {
pub(super) file: PathBuf,
pub(super) kind: PageKind,
pub(super) trail: Vec<String>,
pub(super) document: Owner,
pub(super) address: String,
pub(super) name: Option<String>,
pub(super) nav: u64,
pub(super) shown_in: Option<Shown>,
}
#[derive(Debug, Clone, PartialEq)]
pub(super) struct Shown {
pub(super) file: PathBuf,
pub(super) widgets: Option<PathBuf>,
}
impl Shown {
fn of(document: &Document) -> Self {
Self {
file: document.path(),
widgets: document.widgets.clone(),
}
}
}
pub(super) type Documents = HashMap<PathBuf, Option<Arc<Document>>>;
#[cfg(test)]
thread_local! {
pub(super) static WALKED: std::cell::RefCell<Vec<PathBuf>> = Default::default();
}
fn markdown(file: &Path) -> Option<String> {
#[cfg(test)]
WALKED.with_borrow_mut(|walked| walked.push(file.to_path_buf()));
std::fs::read_to_string(file).ok()
}
pub(super) struct Published {
pub(super) units: BTreeMap<String, Unit>,
pub(super) documents: Documents,
footprints: HashMap<PathBuf, Footprint>,
}
#[derive(Debug, Default, PartialEq)]
struct Footprint {
topics: Option<Vec<crate::nav::Topic>>,
trail: Option<Vec<String>>,
search: Option<Option<bool>>,
title: Option<String>,
}
impl Footprint {
fn again(was: &Self, source: &str, file: &Path) -> Self {
Self {
topics: was.topics.as_ref().map(|_| crate::nav::topics(source)),
trail: was
.trail
.as_ref()
.map(|_| vec![crate::markup::title(source, file)]),
search: was
.search
.as_ref()
.map(|_| FrontMatter::read(source).search),
title: was
.title
.as_ref()
.map(|_| crate::markup::title(source, file)),
}
}
}
impl Published {
pub(super) fn find(pass: &mut Pass, entry: &Path) -> Self {
let (units, footprints) = scope(pass, entry);
Self {
units: units.into_iter().map(|unit| (unit.url(), unit)).collect(),
documents: HashMap::new(),
footprints,
}
}
pub(super) fn moved_by(&self, paths: &[PathBuf]) -> bool {
paths.iter().any(|path| {
if path.is_dir()
|| !path.exists()
|| path.file_name().is_some_and(|name| name == ".gitignore")
|| path
.extension()
.is_some_and(|ext| ext == "yml" || ext == "yaml")
{
return true;
}
if !path.extension().is_some_and(|ext| ext == "md") {
return false;
}
let Some(was) = self.footprints.get(path) else {
return true;
};
*was != Footprint::default()
&& markdown(path).is_none_or(|source| Footprint::again(was, &source, path) != *was)
})
}
}
pub(super) struct Pass<'a> {
pub(super) folder: &'a Folder,
documents: Documents,
widgets: HashMap<PathBuf, Option<Widgets>>,
modules: Modules,
}
impl<'a> Pass<'a> {
pub(super) fn new(folder: &'a Folder) -> Self {
Self::with(folder, HashMap::new())
}
pub(super) fn with(folder: &'a Folder, documents: Documents) -> Self {
Self {
folder,
documents,
widgets: HashMap::new(),
modules: Modules::default(),
}
}
pub(super) fn into_documents(self) -> Documents {
self.documents
}
pub(super) fn document(&mut self, file: &Path) -> Option<Arc<Document>> {
let file = if file.is_dir() {
file.join("devpage.yml")
} else {
file.to_path_buf()
};
let served = self.folder.served();
self.documents
.entry(file)
.or_insert_with_key(|file| Document::load(file, served).ok().map(Arc::new))
.clone()
}
fn shown(&mut self, file: &Path) -> Option<Shown> {
let by = crate::page::shown_by(file, self.folder);
self.document(&by).map(|document| Shown::of(&document))
}
pub(super) fn widgets(&mut self, shown: &Shown) -> Option<&Widgets> {
if !self.widgets.contains_key(&shown.file) {
let set = self
.document(&shown.file)
.map(|document| Widgets::of(&document, &self.modules));
self.widgets.insert(shown.file.clone(), set);
}
self.widgets[&shown.file].as_ref()
}
}
impl Unit {
pub(super) fn reached(&self, path: &Path) -> bool {
self.file.starts_with(path)
|| self
.shown_in
.as_ref()
.and_then(|shown| shown.widgets.as_ref())
.is_some_and(|widgets| path.starts_with(widgets) || widgets.starts_with(path))
}
pub(super) fn url(&self) -> String {
match &self.name {
Some(name) => format!("{}#{name}", self.address),
None => self.address.clone(),
}
}
}
#[derive(Debug, PartialEq, Default)]
pub(super) struct Section {
pub(super) heading: String,
pub(super) id: Option<String>,
pub(super) level: u8,
pub(super) prose: String,
pub(super) code: String,
}
pub(super) struct Read {
pub(super) sections: Vec<Section>,
pub(super) lede: String,
}
#[derive(Default)]
pub(super) struct FrontMatter {
pub(super) search: Option<bool>,
pub(super) meta: String,
}
impl FrontMatter {
pub(super) fn read(source: &str) -> Self {
let Some(yaml) = crate::page::markdown::front_matter(source) else {
return Self::default();
};
let Ok(serde_yaml::Value::Mapping(map)) = serde_yaml::from_str(yaml) else {
return Self::default();
};
let text = |key: &str| match map.get(key) {
Some(serde_yaml::Value::String(text)) => text.clone(),
Some(serde_yaml::Value::Sequence(items)) => items
.iter()
.filter_map(|item| item.as_str())
.collect::<Vec<_>>()
.join(" "),
_ => String::new(),
};
Self {
search: map.get("search").and_then(serde_yaml::Value::as_bool),
meta: squeeze(&format!("{} {}", text("description"), text("keywords"))),
}
}
}
fn scope(pass: &mut Pass, entry: &Path) -> (Vec<Unit>, HashMap<PathBuf, Footprint>) {
let folder = pass.folder;
let mut walk = Walk {
pass,
documents: BTreeSet::new(),
units: Vec::new(),
footprints: HashMap::new(),
};
walk.document(entry);
let mut footprints = walk.footprints;
let parts: BTreeSet<PathBuf> = walk
.units
.iter()
.filter(|unit| unit.kind != PageKind::Page)
.map(|unit| unit.file.clone())
.collect();
let mut seen = BTreeSet::new();
let mut units: Vec<Unit> = walk
.units
.into_iter()
.filter(|unit| unit.kind != PageKind::Page || !parts.contains(&unit.file))
.filter(|unit| unit.kind != PageKind::Page || seen.insert(unit.file.clone()))
.collect();
for file in markdown_files(folder) {
if seen.contains(&file) || parts.contains(&file) {
footprints.entry(file).or_default();
continue;
}
let Some(source) = markdown(&file) else {
continue;
};
let search = FrontMatter::read(&source).search;
let footprint = footprints.entry(file.clone()).or_default();
footprint.search = Some(search);
if search != Some(true) {
continue;
}
let Some(address) = folder.served().url(&file) else {
continue;
};
let site = folder.site(&file).and_then(|site| pass.document(&site));
let document = match site {
Some(site) => Owner {
title: site.title.clone(),
path: site.part_url(&site.path()),
},
None => {
let title = crate::markup::title(&source, &file);
footprint.title = Some(title.clone());
Owner {
title,
path: address.clone(),
}
}
};
units.push(Unit {
shown_in: pass.shown(&file),
file,
kind: PageKind::Page,
trail: Vec::new(),
document,
address,
name: None,
nav: 0,
});
}
for (nav, unit) in units.iter_mut().enumerate() {
unit.nav = nav as u64;
}
(units, footprints)
}
struct Walk<'p, 'a> {
pass: &'p mut Pass<'a>,
documents: BTreeSet<PathBuf>,
units: Vec<Unit>,
footprints: HashMap<PathBuf, Footprint>,
}
impl Walk<'_, '_> {
fn in_sight(&self, file: &Path) -> bool {
!self.pass.folder.ignored(file, false)
}
fn document(&mut self, file: &Path) {
if !self.documents.insert(file.to_path_buf()) || !self.in_sight(file) {
return;
}
let Some(document) = self.pass.document(file) else {
return;
};
let owner = Owner {
title: document.title.clone(),
path: document.part_url(&document.path()),
};
match &document.kind {
Kind::Site { root } => {
let (tree, topics) = crate::nav::tree_and_topics(&document);
for (page, topics) in topics {
self.footprints.entry(page).or_default().topics = Some(topics);
}
let trails = trails(&document, &tree);
let page = |walk: &mut Self, file: &Path| {
if walk.in_sight(file)
&& let Some(address) = document.url(file)
{
walk.units.push(Unit {
shown_in: walk.pass.shown(file),
file: file.to_path_buf(),
kind: PageKind::Page,
trail: trails.get(file).cloned().unwrap_or_default(),
document: owner.clone(),
address,
name: None,
nav: 0,
});
}
};
page(self, &document.base.join(root));
fn listed(groups: &[crate::nav::Group], out: &mut Vec<PathBuf>) {
for entry in groups.iter().flat_map(|group| &group.entries) {
out.push(entry.file.clone());
listed(&entry.children, out);
}
}
let mut files = Vec::new();
listed(&tree, &mut files);
for file in files {
if file.extension().is_some_and(|ext| ext == "md") {
page(self, &file);
} else {
self.document(&file);
}
}
for file in crate::page::navbar::reached(&document).0 {
if file.extension().is_some_and(|ext| ext == "md") {
if file.starts_with(&document.base) {
page(self, &file);
}
} else if crate::document::kind_of(&file).is_some() {
self.document(&file);
}
}
}
Kind::Doc { chapters, .. } => {
let scoped = chapters.len() > 1;
for chapter in chapters {
if !self.in_sight(chapter) {
continue;
}
let name = chapter
.file_stem()
.unwrap_or_default()
.to_string_lossy()
.into_owned();
let trail = if scoped {
let trail = markdown(chapter)
.map(|source| vec![crate::markup::title(&source, chapter)])
.unwrap_or_default();
self.footprints.entry(chapter.clone()).or_default().trail =
Some(trail.clone());
trail
} else {
Vec::new()
};
self.units.push(Unit {
file: chapter.clone(),
kind: PageKind::Chapter,
trail,
document: owner.clone(),
address: owner.path.clone(),
name: scoped.then_some(name),
nav: 0,
shown_in: Some(Shown::of(&document)),
});
}
}
Kind::Slides { slides, .. } => {
for (n, slide) in slides.iter().enumerate() {
if !self.in_sight(&slide.file) {
continue;
}
self.units.push(Unit {
file: slide.file.clone(),
kind: PageKind::Slide,
trail: Vec::new(),
document: owner.clone(),
address: owner.path.clone(),
name: Some((n + 1).to_string()),
nav: 0,
shown_in: Some(Shown::of(&document)),
});
}
}
Kind::Design => {}
}
}
}
pub(super) fn markdown_files(folder: &Folder) -> Vec<PathBuf> {
let walk = WalkBuilder::new(folder.served().path())
.hidden(false)
.parents(false)
.ignore(false)
.git_global(false)
.git_exclude(false)
.require_git(false)
.filter_entry(|entry| entry.file_name() != ".git" && entry.file_name() != ".devpage")
.build();
let mut files: Vec<PathBuf> = walk
.flatten()
.filter(|entry| entry.file_type().is_some_and(|kind| kind.is_file()))
.map(|entry| entry.into_path())
.filter(|path| path.extension().is_some_and(|ext| ext == "md"))
.filter(|path| !folder.ignored(path, false))
.collect();
files.sort();
files
}
pub(super) fn trails(
document: &Document,
tree: &[crate::nav::Group],
) -> BTreeMap<PathBuf, Vec<String>> {
pub(super) fn walk(
groups: &[crate::nav::Group],
above: &[String],
out: &mut BTreeMap<PathBuf, Vec<String>>,
) {
for entry in groups.iter().flat_map(|group| &group.entries) {
let mut trail = above.to_vec();
trail.push(entry.label.clone());
walk(&entry.children, &trail, out);
out.insert(entry.file.clone(), trail);
}
}
let mut out = BTreeMap::new();
if let Kind::Site { root } = &document.kind {
out.insert(document.base.join(root), Vec::new());
}
walk(tree, &[], &mut out);
out
}
pub(super) fn slide_title(source: &str, read: &Read, number: &str) -> String {
if let Some(section) = read.sections.iter().find(|s| s.level == 1) {
return section.heading.clone();
}
source
.split("title=\"")
.nth(1)
.and_then(|rest| rest.split('"').next())
.map(str::to_string)
.unwrap_or_else(|| format!("Slide {number}"))
}
pub(super) fn read(rendered: &Rendered) -> Read {
let source = rendered.expanded();
let arena = Arena::new();
let root = parse_document(&arena, source, &crate::page::markdown::options());
let lede = root
.children()
.find(|node| matches!(node.data().value, NodeValue::Paragraph))
.map(|node| squeeze(&node.collect_text()))
.unwrap_or_default();
let mut ids = rendered.heading_ids().into_iter();
let mut sections = vec![Section::default()];
let mut stack: Vec<&AstNode> = root.reverse_children().collect();
while let Some(node) = stack.pop() {
if let NodeValue::Heading(heading) = node.data().value {
let text = node.collect_text();
sections.push(Section {
heading: text.trim().to_string(),
id: ids.next(),
level: heading.level,
..Section::default()
});
continue;
}
let current = sections.last_mut().expect("there is always a section");
match &node.data().value {
NodeValue::Text(text) => current.prose.push_str(text),
NodeValue::Code(code) => {
current.prose.push_str(&code.literal);
current.code.push(' ');
current.code.push_str(&code.literal);
}
NodeValue::CodeBlock(block) => {
if block.info.split_whitespace().next() == Some("mermaid") {
current.prose.push(' ');
current.prose.push_str(&diagram_labels(&block.literal));
current.prose.push(' ');
} else {
current.code.push(' ');
current.code.push_str(&block.literal);
}
}
NodeValue::HtmlBlock(block) => {
current.prose.push(' ');
current.prose.push_str(&widget_text(&block.literal));
current.prose.push(' ');
}
NodeValue::SoftBreak | NodeValue::LineBreak => current.prose.push(' '),
NodeValue::HtmlInline(_) | NodeValue::FrontMatter(_) => {}
_ => {
if node.data().value.block() {
current.prose.push(' ');
}
stack.extend(node.reverse_children());
}
}
}
for section in &mut sections {
section.prose = squeeze(§ion.prose);
section.code = squeeze(§ion.code);
}
if sections[0].prose.is_empty() && sections[0].code.is_empty() {
sections.remove(0);
}
Read { sections, lede }
}
pub(super) fn squeeze(text: &str) -> String {
text.split_whitespace().collect::<Vec<_>>().join(" ")
}
pub(super) fn diagram_labels(source: &str) -> String {
const SKIPPED: &[&str] = &[
"click",
"classDef",
"class",
"style",
"linkStyle",
"flowchart",
"graph",
"sequenceDiagram",
"stateDiagram",
"stateDiagram-v2",
"classDiagram",
"erDiagram",
"direction",
"end",
"autonumber",
];
let mut labels = Vec::new();
for line in source.lines() {
let line = line.trim();
let first = line.split_whitespace().next().unwrap_or("");
if line.is_empty() || line.starts_with("%%") || SKIPPED.contains(&first) {
continue;
}
if matches!(first, "participant" | "actor") {
let name = line
.split_once(" as ")
.map_or(&line[first.len()..], |(_, as_)| as_);
labels.push(name.trim().to_string());
continue;
}
if (line.contains("->") || first == "Note" || first == "note")
&& let Some((_, text)) = line.split_once(':')
{
labels.push(text.trim().to_string());
continue;
}
let chars: Vec<char> = line.chars().collect();
let mut i = 0;
while i < chars.len() {
let close = match chars[i] {
'[' => ']',
'(' => ')',
'{' => '}',
'|' => '|',
'"' => '"',
_ => {
i += 1;
continue;
}
};
let open = chars[i];
let mut depth = 1;
let mut j = i + 1;
while j < chars.len() {
if chars[j] == close && (open == close || depth == 1) {
break;
}
if open != close && chars[j] == open {
depth += 1;
} else if open != close && chars[j] == close {
depth -= 1;
}
j += 1;
}
let label: String = chars[i + 1..j.min(chars.len())].iter().collect();
labels.push(label);
i = j + 1;
}
}
let text = labels
.iter()
.map(|label| {
strip_tags(label)
.trim_matches(|c: char| "\"'()[]{}/\\ ".contains(c))
.to_string()
})
.filter(|label| !label.is_empty())
.collect::<Vec<_>>()
.join(" ");
squeeze(&text)
}
pub(super) const NOT_TEXT: &[&str] = &[
"href",
"src",
"to",
"class",
"className",
"id",
"style",
"icon",
"logo",
"width",
"height",
"size",
"columns",
"kind",
"variant",
"type",
"lang",
"language",
"delivery",
"target",
"rel",
"data-widget",
"data-widget-source",
"data-widget-source-html",
];
const WIDGET_ERRORS: [(&str, &str); 2] = [
("<div class=\"widget-error\">", "</div>"),
("<span class=\"widget-error\">", "</span>"),
];
fn without_errors(html: &str) -> String {
let mut html = html.to_string();
for (open, close) in WIDGET_ERRORS {
while let Some(at) = html.find(open) {
let end = html[at..]
.find(close)
.map_or(html.len(), |end| at + end + close.len());
html.replace_range(at..end, " ");
}
}
html
}
fn props_text(props: &str) -> String {
let mut text = Vec::new();
let mut rest = props.trim().strip_prefix('{').unwrap_or_default();
loop {
rest = rest.trim_start_matches([' ', ',']);
let mut strings = serde_json::Deserializer::from_str(rest).into_iter::<String>();
let Some(Ok(key)) = strings.next() else {
break;
};
rest = rest[strings.byte_offset()..].trim_start();
let Some(value) = rest.strip_prefix(':') else {
break;
};
rest = value.trim_start();
if rest.starts_with('"') {
let mut strings = serde_json::Deserializer::from_str(rest).into_iter::<String>();
let Some(Ok(value)) = strings.next() else {
break;
};
rest = &rest[strings.byte_offset()..];
if !NOT_TEXT.contains(&key.as_str()) && !value.contains("://") {
text.push(value);
}
} else if rest.starts_with('(') {
rest = &rest[expression_end(rest)..];
} else {
rest = rest.trim_start_matches(|c: char| c.is_ascii_alphanumeric());
}
}
text.join(" ")
}
fn expression_end(text: &str) -> usize {
let mut depth = 0;
let mut string: Option<char> = None;
let mut escaped = false;
for (at, c) in text.char_indices() {
if let Some(quote) = string {
if escaped {
escaped = false;
} else if c == '\\' {
escaped = true;
} else if c == quote {
string = None;
}
continue;
}
match c {
'"' | '\'' | '`' => string = Some(c),
'(' => depth += 1,
')' => {
depth -= 1;
if depth == 0 {
return at + 1;
}
}
_ => {}
}
}
text.len()
}
pub(super) fn widget_text(html: &str) -> String {
let html = without_errors(html);
let chars: Vec<char> = html.chars().collect();
let mut text = String::new();
let mut i = 0;
let starts = |i: usize, s: &str| chars[i..].iter().take(s.len()).copied().eq(s.chars());
while i < chars.len() {
if starts(i, "<!--") {
i = (i..chars.len())
.find(|&j| starts(j, "-->"))
.map_or(chars.len(), |j| j + 3);
continue;
}
let tag = chars[i] == '<'
&& chars
.get(i + 1)
.is_some_and(|c| c.is_alphabetic() || *c == '/' || *c == '!');
if !tag {
text.push(chars[i]);
i += 1;
continue;
}
i += 1;
while i < chars.len() && !chars[i].is_whitespace() && chars[i] != '>' && chars[i] != '/' {
i += 1;
}
let mut name = String::new();
while i < chars.len() && chars[i] != '>' {
let c = chars[i];
if c.is_alphanumeric() || c == '-' || c == '_' {
name.push(c);
i += 1;
continue;
}
if c == '=' {
i += 1;
continue;
}
if c == '"' || c == '\'' {
let end = (i + 1..chars.len())
.find(|&j| chars[j] == c)
.unwrap_or(chars.len());
let value: String = chars[i + 1..end].iter().collect();
if name == "data-props" {
text.push(' ');
text.push_str(&props_text(&crate::html::unescape(&value)));
text.push(' ');
} else if !NOT_TEXT.contains(&name.as_str()) && !value.contains("://") {
text.push(' ');
text.push_str(&value);
text.push(' ');
}
name.clear();
i = end + 1;
continue;
}
if c == '{' {
let mut depth = 0;
while i < chars.len() {
match chars[i] {
'{' => depth += 1,
'}' => depth -= 1,
_ => {}
}
i += 1;
if depth == 0 {
break;
}
}
name.clear();
continue;
}
name.clear();
i += 1;
}
text.push(' ');
i += 1;
}
squeeze(&text)
}
pub(super) fn strip_tags(html: &str) -> String {
let mut text = String::new();
let mut in_tag = false;
for c in html.chars() {
match c {
'<' => in_tag = true,
'>' => {
in_tag = false;
text.push(' ');
}
c if !in_tag => text.push(c),
_ => {}
}
}
text
}