devpage-core 0.3.3

devpage's core: documents, rendering, link checking, comments and designs, with no I/O runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! Extension contracts and captured state; the executable installs the native loader.
mod browser;
use crate::folder::ServedFolder;
use anyhow::{Result, anyhow, bail};
use browser::Browser;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use sha2::{Digest, Sha256};
use std::{
    collections::BTreeMap,
    path::{Path, PathBuf},
    sync::{Arc, Mutex, OnceLock},
};
/// An extension's package, loaded: what devpage asks, in JSON, to read its
/// input and show its files.
pub trait Package: Send + Sync {
    /// The package's reply to `request`, a JSON object whose `op` names what is
    /// asked: `create`, `prepare`, `view`, `augment`, `embed`, `targets` or
    /// `route`.
    fn invoke(&self, request: Value) -> Result<Value>;
}
/// What loads an extension's package from its bundle; the executable installs
/// the native one.
pub trait Loader: Send + Sync {
    /// The package bundled at `path`.
    fn load(&self, path: &Path) -> Result<Arc<dyn Package>>;
}
/// The loader the executable installed; until it does, no extension can be
/// configured.
static LOADER: OnceLock<Arc<dyn Loader>> = OnceLock::new();
/// Installs `loader` as the one every package is loaded with; a second call is
/// ignored.
pub fn install_loader(loader: Arc<dyn Loader>) {
    let _ = LOADER.set(loader);
}
/// Every configured document's extensions, by the document's file.
static INSTANCES: OnceLock<Mutex<BTreeMap<PathBuf, Vec<Binding>>>> = OnceLock::new();
/// Every configured document's extensions, by the document's file — empty at
/// first.
fn instances() -> &'static Mutex<BTreeMap<PathBuf, Vec<Binding>>> {
    INSTANCES.get_or_init(Default::default)
}
/// An extension as a document's `extensions:` names it: its package and what it
/// is configured with.
#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
    /// The package's bundle; left out only for a system extension such as
    /// [`code`](crate::code).
    pub package: Option<String>,
    /// What the package is created with, as written under `config:`; null when
    /// left out.
    #[serde(default)]
    pub config: serde_yaml::Value,
}
/// An extension bound to a document: its package, the input it reads and what
/// it last prepared from it. Clones share one state; a
/// [`snapshot`](Self::snapshot) does not.
#[derive(Clone)]
pub struct Binding(Arc<Mutex<State>>);
impl std::fmt::Debug for Binding {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("ExtensionBinding")
    }
}
/// What a binding holds: its package, how it was configured and what it last
/// prepared.
#[derive(Clone)]
struct State {
    /// The extension's name under the document's `extensions:`.
    id: String,
    /// The document's whole `extensions:`, as JSON — what tells a changed
    /// configuration from the bound one.
    configuration: String,
    /// The instance's id: the first 16 hex digits of the SHA-256 of the
    /// document's path, `id` and `configuration`.
    owner: String,
    /// The package, loaded by the installed [`Loader`]; each request names
    /// this binding by `owner`.
    package: Arc<dyn Package>,
    /// The served folder its input and the files it shows must lie in.
    scope: ServedFolder,
    /// The file or folder it reads, as the package's `create` reply names it.
    input: PathBuf,
    /// The package's reply to `create`: the suffixes it owns, its input and the
    /// suffixes read there, its grammar, widgets and styles.
    descriptor: Value,
    /// The modules, widgets and stylesheets the package ships for the browser,
    /// compiled.
    browser: Arc<Browser>,
    /// What the package named its last preparation — empty before the first.
    revision: String,
    /// Why the last preparation failed — `None` when it didn't, or when the
    /// reply only lacked a revision. While set, every call fails.
    error: Option<String>,
    /// The input last prepared from: each file's contents, by its name in the
    /// served folder.
    sources: BTreeMap<String, String>,
}
impl Binding {
    /// The address the binding of `s` answers under:
    /// `<mount>/__devpage/extensions/<owner>/<id>/`.
    fn base(s: &State) -> String {
        format!(
            "{}/__devpage/extensions/{}/{}/",
            s.scope.mount(),
            s.owner,
            s.id
        )
    }
    /// The address of each of the package's widgets' modules, by widget name.
    pub fn widget_urls(&self) -> BTreeMap<String, String> {
        let s = self.0.lock().unwrap();
        s.browser.widgets(&Self::base(&s))
    }
    /// The addresses of the package's stylesheets.
    pub fn style_urls(&self) -> Vec<String> {
        let s = self.0.lock().unwrap();
        s.browser.styles(&Self::base(&s))
    }
    /// The package's view of `file`, a file it owns: its `layout`, `title` and
    /// `content`.
    pub fn view(&self, file: &Path) -> Result<Value> {
        let s = self.0.lock().unwrap();
        let path = s
            .scope
            .name(file)
            .ok_or_else(|| anyhow!("View outside served folder"))?;
        drop(s);
        self.call("view", json!({"path":path}))
    }

    /// Pin this render to its prepared revision while the watcher prepares the next.
    pub fn snapshot(&self) -> Self {
        Self(Arc::new(Mutex::new(self.0.lock().unwrap().clone())))
    }
    /// The package's reply to `op`, asked with `extra` and the instance, its
    /// base address and revision. An error while the last preparation failed.
    fn call(&self, op: &str, extra: Value) -> Result<Value> {
        let s = self.0.lock().unwrap();
        if let Some(error) = &s.error {
            bail!("Extension preparation failed: {error}");
        }
        let mut request = extra;
        request["op"] = op.into();
        request["instance"] = s.owner.clone().into();
        request["base"] = format!(
            "{}/__devpage/extensions/{}/{}/",
            s.scope.mount(),
            s.owner,
            s.id
        )
        .into();
        request["revision"] = s.revision.clone().into();
        s.package.invoke(request)
    }
    /// The file or folder the extension reads.
    pub fn input(&self) -> PathBuf {
        self.0.lock().unwrap().input.clone()
    }
    /// Whether `file` is one the extension shows: its suffix is one the package
    /// declared and, when `file` is absolute, it lies in the input.
    pub fn owns(&self, file: &Path) -> bool {
        let s = self.0.lock().unwrap();
        if file.is_absolute() && !file.starts_with(&s.input) {
            return false;
        }
        let ext = file.extension().and_then(|e| e.to_str()).unwrap_or("");
        s.descriptor["file_extensions"]
            .as_array()
            .is_some_and(|a| a.iter().any(|v| v.as_str() == Some(ext)))
    }
    /// Whether a new revision was prepared: the input is read again, and the
    /// package prepares from it unless it is unchanged, the last preparation
    /// succeeded and a revision is held. A failure to read or prepare is kept,
    /// and fails every call until a preparation succeeds; a reply without a
    /// revision fails only this preparation.
    pub fn prepare(&self) -> Result<bool> {
        let mut s = self.0.lock().unwrap();
        let mut sources = BTreeMap::new();
        fn read(
            scope: &ServedFolder,
            p: &Path,
            files: &mut BTreeMap<String, String>,
            extensions: &[Value],
        ) -> Result<()> {
            if std::fs::symlink_metadata(p)?.file_type().is_symlink() {
                bail!("Symlinked extension input {}", p.display());
            }
            let canonical = p.canonicalize()?;
            if !canonical.starts_with(scope.path()) {
                bail!("Extension input outside served folder");
            }
            if p.is_dir() {
                for entry in std::fs::read_dir(p)? {
                    let entry = entry?;
                    if !entry.file_name().to_string_lossy().starts_with('.') {
                        read(scope, &entry.path(), files, extensions)?;
                    }
                }
            } else if p
                .extension()
                .and_then(|e| e.to_str())
                .is_some_and(|ext| extensions.iter().any(|v| v.as_str() == Some(ext)))
            {
                files.insert(
                    scope
                        .name(p)
                        .ok_or_else(|| anyhow!("Input outside served folder"))?,
                    std::fs::read_to_string(p)?,
                );
            }
            Ok(())
        }
        let extensions = s.descriptor["input_extensions"]
            .as_array()
            .ok_or_else(|| anyhow!("Extension must declare input_extensions"))?;
        if let Err(error) = read(&s.scope, &s.input, &mut sources, extensions) {
            s.error = Some(error.to_string());
            return Err(error);
        }
        if s.error.is_none() && s.sources == sources && !s.revision.is_empty() {
            return Ok(false);
        }
        let reply = match s
            .package
            .invoke(json!({"op":"prepare","instance":s.owner,"sources":sources}))
        {
            Ok(reply) => reply,
            Err(error) => {
                s.error = Some(format!("{error:#}"));
                return Err(error);
            }
        };
        s.revision = reply["revision"]
            .as_str()
            .ok_or_else(|| anyhow!("Extension preparation omitted revision"))?
            .into();
        s.sources = sources;
        s.error = None;
        Ok(true)
    }
    /// The syntax definition the package highlights its language with — `None`
    /// when it brings none.
    pub fn grammar(&self) -> Option<String> {
        self.0.lock().unwrap().descriptor["grammar"]
            .as_str()
            .map(String::from)
    }
    /// `source`, the page at `file`, as the package augments it — unchanged
    /// when the reply has no `source`.
    pub fn transform(&self, file: &Path, source: &str) -> Result<String> {
        let s = self.0.lock().unwrap();
        let path = s
            .scope
            .name(file)
            .ok_or_else(|| anyhow!("Page outside served folder"))?;
        drop(s);
        let reply = self.call("augment", json!({"path":path,"source":source}))?;
        Ok(reply["source"].as_str().unwrap_or(source).into())
    }
    /// The HTML the package shows `target` as in the page at `file`, with
    /// `label`; `identity` is its place among the page's `src` targets.
    pub fn embed(&self, file: &Path, target: &str, label: &str, identity: usize) -> Result<String> {
        let s = self.0.lock().unwrap();
        let path = s
            .scope
            .name(file)
            .ok_or_else(|| anyhow!("Page outside served folder"))?;
        drop(s);
        Ok(self.call(
            "embed",
            json!({"page":path,"target":target,"label":label,"identity":identity}),
        )?["html"]
            .as_str()
            .ok_or_else(|| anyhow!("Missing embed HTML"))?
            .into())
    }
    /// The ids a link may point at inside `file`, a file the extension owns.
    pub fn ids(&self, file: &Path) -> Result<Vec<String>> {
        let s = self.0.lock().unwrap();
        let path = s
            .scope
            .name(file)
            .ok_or_else(|| anyhow!("Target outside served folder"))?;
        drop(s);
        Ok(self.call("targets", json!({"path":path}))?["ids"]
            .as_array()
            .into_iter()
            .flatten()
            .filter_map(|v| v.as_str().map(String::from))
            .collect())
    }
}
/// Binds the extensions `configs` to the document at `doc`, each package loaded
/// from its bundle under `base`, and prepares each; no `configs` unbinds it. A
/// configuration already bound in `scope` is kept as it is.
pub fn configure(
    doc: &Path,
    base: &Path,
    scope: &ServedFolder,
    configs: BTreeMap<String, Config>,
) -> Result<()> {
    let mut guard = instances().lock().unwrap();
    if configs.is_empty() {
        guard.remove(doc);
        return Ok(());
    }
    let configuration = serde_json::to_string(&configs)?;
    if guard.get(doc).is_some_and(|bindings| {
        bindings.iter().all(|b| {
            let s = b.0.lock().unwrap();
            s.configuration == configuration && s.scope == *scope
        })
    }) {
        return Ok(());
    }
    let loader = LOADER
        .get()
        .ok_or_else(|| anyhow!("Native extension loader unavailable"))?;
    let mut bindings = vec![];
    let mut claims: std::collections::BTreeSet<String> = ["md", "yml", "yaml", "design"]
        .into_iter()
        .map(String::from)
        .collect();
    for (id, config) in configs {
        let Some(path) = &config.package else {
            bail!("extensions: {id}: package is required — {id} is not a system extension");
        };
        let package = loader
            .load(&base.join(path))
            .map_err(|e| anyhow!("Extension {id}: {e:#}"))?;
        let owner = format!(
            "{:x}",
            Sha256::digest(format!("{}:{id}:{configuration}", doc.display()))
        )[..16]
            .to_string();
        let descriptor = package
            .invoke(json!({"op":"create","instance":owner,"id":id,"config":config.config}))?;
        for suffix in descriptor["file_extensions"]
            .as_array()
            .ok_or_else(|| anyhow!("Extension {id} file_extensions missing"))?
        {
            let suffix = suffix
                .as_str()
                .filter(|s| !s.is_empty() && s.bytes().all(|b| b.is_ascii_alphanumeric()))
                .ok_or_else(|| anyhow!("Extension {id} invalid file suffix"))?;
            if !claims.insert(suffix.into()) {
                bail!("Extension {id} file format {suffix} is already claimed");
            }
        }
        if let Some(grammar) = descriptor["grammar"].as_str() {
            let syntax = syntect::parsing::SyntaxDefinition::load_from_str(grammar, true, None)
                .map_err(|e| anyhow!("Extension {id} grammar: {e}"))?;
            if syntax
                .file_extensions
                .iter()
                .any(|s| crate::highlight::builtin_language(s))
            {
                bail!("Extension {id} cannot replace a built-in language");
            }
        }
        let input = base.join(
            descriptor["input"]
                .as_str()
                .ok_or_else(|| anyhow!("Extension input missing"))?,
        );
        if !input.canonicalize()?.starts_with(scope.path()) {
            bail!("Extension {id} input outside served folder");
        }
        let browser = Browser::new(
            &descriptor,
            &format!("{}/__devpage/extensions/{owner}/{id}/", scope.mount()),
        )?;
        let binding = Binding(Arc::new(Mutex::new(State {
            id,
            configuration: configuration.clone(),
            owner,
            package,
            scope: scope.clone(),
            input,
            descriptor,
            browser: Arc::new(browser),
            revision: String::new(),
            error: None,
            sources: BTreeMap::new(),
        })));
        binding.prepare()?;
        bindings.push(binding);
    }
    guard.insert(doc.to_path_buf(), bindings);
    Ok(())
}
/// The extensions bound to the document at `doc` — none when it has none.
pub fn for_document(doc: &Path) -> Vec<Binding> {
    instances()
        .lock()
        .unwrap()
        .get(doc)
        .cloned()
        .unwrap_or_default()
}
/// The input of every extension bound in `scope`, for the watcher to watch.
pub fn input_roots(scope: &ServedFolder) -> Vec<PathBuf> {
    instances()
        .lock()
        .unwrap()
        .values()
        .flatten()
        .filter(|b| b.0.lock().unwrap().scope == *scope)
        .map(Binding::input)
        .collect()
}
/// Whether an extension in `scope` prepared anew after `files` changed: each
/// whose input holds one of them, or lies in one, prepares again. A failed
/// preparation counts as a change.
pub fn changed(scope: &ServedFolder, files: &[PathBuf]) -> bool {
    let all: Vec<_> = instances()
        .lock()
        .unwrap()
        .values()
        .flatten()
        .filter(|b| b.0.lock().unwrap().scope == *scope)
        .cloned()
        .collect();
    let mut changed = false;
    for b in all {
        let input = b.input();
        if files
            .iter()
            .any(|p| p.starts_with(&input) || input.starts_with(p))
        {
            changed |= b.prepare().unwrap_or(true);
        }
    }
    changed
}
/// The extensions of the document that owns `file` in `folder` — the entry
/// document's when none does.
pub fn for_file(file: &Path, folder: &crate::folder::Folder) -> Vec<Binding> {
    let path = folder
        .owner(file)
        .and_then(|o| o.document.ok())
        .map(|d| d.path())
        .unwrap_or_else(|| folder.entry().path());
    for_document(&path)
}
/// The reply to `path` when it is an extension's address in `scope`: a browser
/// file the package ships, or else what the package routes it to. `None` when
/// it is no extension's.
pub fn route(path: &str, scope: &ServedFolder) -> Option<Result<Value>> {
    let suffix = path
        .trim_start_matches('/')
        .strip_prefix("__devpage/extensions/")?;
    let (owner, tail) = suffix.split_once('/')?;
    let (id, tail) = tail.split_once('/')?;
    let all: Vec<_> = instances()
        .lock()
        .unwrap()
        .values()
        .flatten()
        .cloned()
        .collect();
    all.into_iter()
        .find(|b| {
            let s = b.0.lock().unwrap();
            s.owner == owner && s.id == id && s.scope == *scope
        })
        .map(|b| {
            let response = {
                let s = b.0.lock().unwrap();
                s.browser.response(tail)
            };
            response.map_or_else(|| b.call("route", json!({"path":tail})), Ok)
        })
}

#[cfg(test)]
mod tests {
    use super::*;
    struct Toy;
    impl Loader for Toy {
        fn load(&self, _: &Path) -> Result<Arc<dyn Package>> {
            Ok(Arc::new(Toy))
        }
    }
    impl Package for Toy {
        fn invoke(&self, r: Value) -> Result<Value> {
            Ok(match r["op"].as_str().unwrap() {
                "create" => {
                    json!({"input":r["config"]["input"],"input_extensions":["toy","md"],"file_extensions":["toy"],
                    "grammar":"%YAML 1.2\n---\nname: Toy\nfile_extensions: [toy]\nscope: source.toy\ncontexts:\n  main:\n    - match: magic\n      scope: keyword.control.toy\n"})
                }
                "prepare" => {
                    json!({"revision":format!("{:x}",Sha256::digest(r["sources"].to_string()))})
                }
                "augment" => {
                    json!({"source":format!("{}\n\n## Extension content\n",r["source"].as_str().unwrap())})
                }
                "embed" => {
                    json!({"html":"<svg role=\"img\"><text>Prepared toy diagram</text></svg>"})
                }
                "view" => {
                    json!({"layout":"workspace","title":"Toy","content":format!("<h1>Toy viewer</h1><p>{}</p>",r["revision"].as_str().unwrap())})
                }
                "targets" => json!({"ids":["main"]}),
                "route" => json!({"body":r["revision"],"mime":"text/plain","status":200}),
                _ => bail!("Unknown toy operation"),
            })
        }
    }

    #[test]
    fn another_language_uses_the_same_capture_render_targets_and_watch_contract() {
        install_loader(Arc::new(Toy));
        let dir =
            std::env::temp_dir().join(format!("devpage-extension-contract-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(dir.join("model/nested")).unwrap();
        std::fs::write(dir.join("model/nested/a.toy"), "first").unwrap();
        std::fs::write(dir.join("model/ignored.rs"), "not an input").unwrap();
        std::fs::write(
            dir.join("page.md"),
            "# Page\n\n```toy\nmagic\n```\n\n![Toy](model/nested/a.toy#main)",
        )
        .unwrap();
        std::fs::write(dir.join("devpage.yml"),"kind: site\ntitle: Toy\nroot: page.md\nwatch: [page.md]\nextensions:\n  toy:\n    package: ./toy-package\n    config:\n      input: model\n").unwrap();
        let dir = dir.canonicalize().unwrap();
        let scope = ServedFolder::new(&dir).unwrap();
        let doc = crate::document::Document::load(&dir.join("devpage.yml"), &scope).unwrap();
        let bindings = for_document(&doc.path());
        assert_eq!(bindings.len(), 1);
        assert!(!bindings[0].prepare().unwrap());
        let first = bindings[0].view(&dir.join("model/nested/a.toy")).unwrap();
        let rendered = crate::page::content(
            &crate::widgets::Widgets::of(&doc, &crate::widgets::Modules::default()),
            &dir.join("page.md"),
            std::fs::read_to_string(dir.join("page.md")).unwrap(),
        );
        assert!(rendered.html().contains("hl-keyword"));
        assert!(rendered.html().contains("Prepared toy diagram"));
        assert!(rendered.ids().contains("extension-content"));
        assert!(
            rendered
                .embeds()
                .iter()
                .any(|t| t == "model/nested/a.toy#main")
        );
        assert_eq!(
            bindings[0].ids(&dir.join("model/nested/a.toy")).unwrap(),
            vec!["main"]
        );
        let folder = crate::folder::Folder::new(Arc::new(doc));
        assert!(folder.matters(&dir.join("model/nested/new.toy"), false));
        std::fs::write(dir.join("model/nested/a.toy"), "second").unwrap();
        assert!(changed(&scope, &[dir.join("model/nested/a.toy")]));
        assert_ne!(
            first,
            bindings[0].view(&dir.join("model/nested/a.toy")).unwrap()
        );
        // Failure is visible; stale model data is not presented as current.
        std::fs::remove_dir_all(dir.join("model")).unwrap();
        assert!(bindings[0].prepare().is_err());
        assert!(bindings[0].view(&dir.join("model/nested/a.toy")).is_err());
        std::fs::create_dir_all(dir.join("model")).unwrap();
        assert!(bindings[0].prepare().unwrap());
        assert!(bindings[0].view(&dir.join("model/a.toy")).is_ok());
        instances().lock().unwrap().remove(&dir.join("devpage.yml"));
        std::fs::remove_dir_all(dir).unwrap();
    }
}