devpage_core/page/
content.rs1use std::collections::BTreeSet;
8use std::path::{Path, PathBuf};
9
10use crate::document::{Document, Kind};
11use crate::folder::Folder;
12use crate::html;
13use crate::widgets::{Modules, Widgets};
14
15use super::frame::chapter_names;
16use super::markdown::markdown;
17
18pub struct Rendered {
20 file: PathBuf,
21 source: String,
22 expanded: String,
23 html: String,
24 links: Vec<Link>,
25 embeds: Vec<String>,
26 tags: Vec<String>,
27}
28
29#[derive(Debug, Clone, PartialEq)]
33pub struct Link {
34 pub file: PathBuf,
35 pub line: Option<usize>,
36 pub target: String,
37}
38
39impl Rendered {
40 pub fn file(&self) -> &Path {
42 &self.file
43 }
44
45 pub fn source(&self) -> &str {
47 &self.source
48 }
49
50 pub fn expanded(&self) -> &str {
54 &self.expanded
55 }
56
57 pub fn html(&self) -> &str {
59 &self.html
60 }
61
62 pub fn ids(&self) -> BTreeSet<String> {
65 html::values(&self.html, "id").into_iter().collect()
66 }
67
68 pub fn heading_ids(&self) -> Vec<String> {
72 super::markdown::heading_ids(&self.html)
73 }
74
75 pub fn links(&self) -> &[Link] {
77 &self.links
78 }
79
80 pub fn embeds(&self) -> &[String] {
84 &self.embeds
85 }
86
87 pub(crate) fn tag_problems(&self) -> &[String] {
90 &self.tags
91 }
92}
93
94pub fn content(widgets: &Widgets, file: &Path, source: String) -> Rendered {
96 let _render = tracing::info_span!("render").entered();
97 let mut tags = Vec::new();
98 let expanded = crate::widgets::expand(&source, widgets.names(), &mut tags);
99 let html = markdown(&expanded);
100 let mut targets = html::values(&html, "href");
101 let mut embeds = html::values(&html, "src");
102 targets.extend(embeds.iter().cloned());
103 for design in embedded_designs(&source) {
106 if !embeds.contains(&design) {
107 embeds.push(design.clone());
108 }
109 if !targets.contains(&design) {
110 targets.push(design);
111 }
112 }
113 let links = targets
114 .into_iter()
115 .map(|target| Link {
116 file: file.to_path_buf(),
117 line: source
118 .find(&target)
119 .map(|at| source[..at].lines().count().max(1)),
120 target,
121 })
122 .collect();
123 Rendered {
124 file: file.to_path_buf(),
125 source,
126 expanded,
127 html,
128 links,
129 embeds,
130 tags,
131 }
132}
133
134pub fn read(widgets: &Widgets, file: &Path) -> Rendered {
137 content(
138 widgets,
139 file,
140 std::fs::read_to_string(file).unwrap_or_default(),
141 )
142}
143
144pub fn shown_in(file: &Path, folder: &Folder) -> Option<Document> {
148 Document::load(&shown_by(file, folder), folder.served()).ok()
149}
150
151pub fn shown_by(file: &Path, folder: &Folder) -> PathBuf {
155 folder.site(file).unwrap_or_else(|| file.to_path_buf())
156}
157
158pub fn ids(file: &Path, folder: &Folder, modules: &Modules) -> BTreeSet<String> {
166 if file.extension().is_some_and(|ext| ext == "md") {
167 return shown_in(file, folder)
168 .map(|document| read(&Widgets::of(&document, modules), file).ids())
169 .unwrap_or_default();
170 }
171 let served = folder.served();
172 let Ok(document) = Document::load(file, served) else {
173 return BTreeSet::new();
174 };
175 let Kind::Doc { chapters, .. } = &document.kind else {
176 return BTreeSet::new();
177 };
178 let widgets = Widgets::of(&document, modules);
179 let own_ids = |chapter: &Path| read(&widgets, chapter).ids();
180 if chapters.len() == 1 {
181 return own_ids(&chapters[0]);
182 }
183 let names = chapter_names(chapters, served);
184 let mut ids = BTreeSet::new();
185 for (chapter, (_, name)) in chapters.iter().zip(&names) {
186 ids.extend(
187 own_ids(chapter)
188 .into_iter()
189 .map(|id| format!("{name}--{id}")),
190 );
191 ids.insert(name.clone());
192 }
193 ids
194}
195
196fn embedded_designs(source: &str) -> Vec<String> {
199 use comrak::nodes::NodeValue;
200 let arena = comrak::Arena::new();
201 let root = comrak::parse_document(&arena, source, &comrak::Options::default());
202 root.descendants()
203 .filter_map(|node| match &node.data.borrow().value {
204 NodeValue::Image(link) | NodeValue::Link(link) => Some(link.url.clone()),
205 _ => None,
206 })
207 .filter(|url| {
208 url.split('#')
209 .next()
210 .is_some_and(|path| path.ends_with(".design"))
211 })
212 .collect()
213}