cellix-doc 0.6.7

Documents for Cellix: typeset math, HTML export, LaTeX structure and PDF pages
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
//! Suggested edits kept in the text itself, so they survive any tool and travel with the file.
//! Markdown uses CriticMarkup and a paper uses the `changes` package.

/// How a document writes its changes.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Syntax {
    Critic,
    Latex,
}

impl Syntax {
    /// What opens and closes an insertion.
    pub fn insertion(self) -> (&'static str, &'static str) {
        match self {
            Self::Critic => ("{++", "++}"),
            Self::Latex => ("\\added{", "}"),
        }
    }

    /// What opens and closes a deletion.
    pub fn deletion(self) -> (&'static str, &'static str) {
        match self {
            Self::Critic => ("{--", "--}"),
            Self::Latex => ("\\deleted{", "}"),
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Kind {
    Insert,
    Delete,
    Replace,
}

/// One change, as a range of chars in the document.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Change {
    pub start: usize,
    pub end: usize,
    pub kind: Kind,
    pub old: String,
    pub new: String,
}

fn starts(chars: &[char], at: usize, text: &str) -> bool {
    text.chars()
        .enumerate()
        .all(|(index, wanted)| chars.get(at + index) == Some(&wanted))
}

fn find_text(chars: &[char], from: usize, text: &str) -> Option<usize> {
    (from..chars.len()).find(|at| starts(chars, *at, text))
}

fn string(chars: &[char]) -> String {
    chars.iter().collect()
}

fn critic(chars: &[char]) -> Vec<Change> {
    let mut found = Vec::new();
    let mut at = 0;
    while at + 2 < chars.len() {
        // A code span's text is code, marks and all.
        if chars[at] == '`'
            && let Some(close) = chars[at + 1..].iter().position(|c| *c == '`')
        {
            at += close + 2;
            continue;
        }
        let change = if starts(chars, at, "{++") {
            find_text(chars, at + 3, "++}").map(|close| Change {
                start: at,
                end: close + 3,
                kind: Kind::Insert,
                old: String::new(),
                new: string(&chars[at + 3..close]),
            })
        } else if starts(chars, at, "{--") {
            find_text(chars, at + 3, "--}").map(|close| Change {
                start: at,
                end: close + 3,
                kind: Kind::Delete,
                old: string(&chars[at + 3..close]),
                new: String::new(),
            })
        } else if starts(chars, at, "{~~") {
            find_text(chars, at + 3, "~>").and_then(|arrow| {
                let close = find_text(chars, arrow, "~~}")?;
                Some(Change {
                    start: at,
                    end: close + 3,
                    kind: Kind::Replace,
                    old: string(&chars[at + 3..arrow]),
                    new: string(&chars[arrow + 2..close]),
                })
            })
        } else {
            None
        };
        match change {
            Some(change) => {
                at = change.end;
                found.push(change);
            }
            None => at += 1,
        }
    }
    found
}

/// Where the brace opened at `open` closes.
fn brace_end(chars: &[char], open: usize) -> Option<usize> {
    let mut depth = 0;
    for (index, c) in chars.iter().enumerate().skip(open) {
        match c {
            '{' => depth += 1,
            '}' => {
                depth -= 1;
                if depth == 0 {
                    return Some(index);
                }
            }
            _ => {}
        }
    }
    None
}

/// A brace argument after a command, stepping over any `[options]`.
fn argument(chars: &[char], from: usize) -> Option<(usize, usize)> {
    let mut open = from;
    if chars.get(open) == Some(&'[') {
        open += chars[open..].iter().position(|c| *c == ']')? + 1;
    }
    (chars.get(open) == Some(&'{')).then_some(())?;
    Some((open, brace_end(chars, open)?))
}

fn latex(chars: &[char]) -> Vec<Change> {
    let mut found = Vec::new();
    let mut at = 0;
    while at < chars.len() {
        let change = if starts(chars, at, "\\added") {
            argument(chars, at + 6).map(|(open, close)| Change {
                start: at,
                end: close + 1,
                kind: Kind::Insert,
                old: String::new(),
                new: string(&chars[open + 1..close]),
            })
        } else if starts(chars, at, "\\deleted") {
            argument(chars, at + 8).map(|(open, close)| Change {
                start: at,
                end: close + 1,
                kind: Kind::Delete,
                old: string(&chars[open + 1..close]),
                new: String::new(),
            })
        } else if starts(chars, at, "\\replaced") {
            argument(chars, at + 9).and_then(|(open, close)| {
                let (second, end) = argument(chars, close + 1)?;
                Some(Change {
                    start: at,
                    end: end + 1,
                    kind: Kind::Replace,
                    old: string(&chars[second + 1..end]),
                    new: string(&chars[open + 1..close]),
                })
            })
        } else {
            None
        };
        match change {
            Some(change) => {
                at = change.end;
                found.push(change);
            }
            None => at += 1,
        }
    }
    found
}

pub fn find(text: &str, syntax: Syntax) -> Vec<Change> {
    let chars: Vec<char> = text.chars().collect();
    match syntax {
        Syntax::Critic => critic(&chars),
        Syntax::Latex => latex(&chars),
    }
}

/// The document with every change kept or dropped.
/// The change from `old` to `new` as suggestions in `old`, word by word, so whoever reviews it
/// accepts or rejects each one. Accepting all of them gives `new` back.
pub fn suggest(old: &str, new: &str, syntax: Syntax) -> String {
    let diff = similar::TextDiff::from_words(old, new);
    let mut out = String::with_capacity(new.len() + new.len() / 4);
    let (mut gone, mut added) = (String::new(), String::new());
    let flush = |out: &mut String, gone: &mut String, added: &mut String| {
        // Space at a change's edges stays plain, so the marks sit on words.
        let lead = common_lead(gone, added).len();
        let trail = common_trail(&gone[lead..], &added[lead..]).len();
        out.push_str(&gone[..lead]);
        let (old, new) = (
            &gone[lead..gone.len() - trail],
            &added[lead..added.len() - trail],
        );
        out.push_str(&mark(old, new, syntax));
        out.push_str(&gone[gone.len() - trail..]);
        gone.clear();
        added.clear();
    };
    for change in diff.iter_all_changes() {
        match change.tag() {
            similar::ChangeTag::Equal => {
                if !gone.is_empty() || !added.is_empty() {
                    flush(&mut out, &mut gone, &mut added);
                }
                out.push_str(change.value());
            }
            similar::ChangeTag::Delete => gone.push_str(change.value()),
            similar::ChangeTag::Insert => added.push_str(change.value()),
        }
    }
    if !gone.is_empty() || !added.is_empty() {
        flush(&mut out, &mut gone, &mut added);
    }
    out
}

/// The change from `old` to `new` for reading, line by line: a line that went is struck, a
/// line that came is marked new, and a line changed in place shows its words. No mark spans a
/// line, so every one renders. Accepting it all doesn't give `new` back exactly; it's a view.
pub fn compare(old: &str, new: &str) -> String {
    let diff = similar::TextDiff::from_lines(old, new);
    let mut out = String::new();
    for op in diff.ops() {
        let (gone, came) = (op.old_range(), op.new_range());
        let old_lines: Vec<&str> = old.lines().collect();
        let new_lines: Vec<&str> = new.lines().collect();
        match op.tag() {
            similar::DiffTag::Equal => {
                for line in &new_lines[came] {
                    out.push_str(line);
                    out.push('\n');
                }
            }
            _ => {
                let (gone, came) = (&old_lines[gone], &new_lines[came]);
                // Pairs read as edits in place, and what's over as lines that went or came.
                let paired = gone.len().min(came.len());
                for (a, b) in gone.iter().zip(came.iter()).take(paired) {
                    out.push_str(&in_line(a, b));
                    out.push('\n');
                }
                for line in &gone[paired..] {
                    out.push_str(&whole(line, "{--", "--}"));
                    out.push('\n');
                }
                for line in &came[paired..] {
                    out.push_str(&whole(line, "{++", "++}"));
                    out.push('\n');
                }
            }
        }
    }
    out
}

/// A line wholly marked, keeping a list item's or heading's marker outside, so it still reads
/// as one.
fn whole(line: &str, open: &str, close: &str) -> String {
    if line.trim().is_empty() {
        return line.to_string();
    }
    let lead = line.len() - line.trim_start().len();
    let body = &line[lead..];
    let marker = ["- ", "* ", "+ ", "#"]
        .iter()
        .find(|m| body.starts_with(**m))
        .map(|m| {
            if *m == "#" {
                body.len() - body.trim_start_matches('#').trim_start().len()
            } else {
                m.len()
            }
        })
        .or_else(|| {
            let digits = body.chars().take_while(char::is_ascii_digit).count();
            (digits > 0 && body[digits..].starts_with(". ")).then_some(digits + 2)
        })
        .unwrap_or(0);
    let (keep, rest) = body.split_at(marker);
    format!("{}{keep}{open}{rest}{close}", &line[..lead])
}

/// One line changed in place, as word suggestions.
fn in_line(old: &str, new: &str) -> String {
    suggest(old, new, Syntax::Critic)
}

/// One suggestion in the document's own syntax.
pub fn mark(old: &str, new: &str, syntax: Syntax) -> String {
    match (syntax, old.is_empty(), new.is_empty()) {
        (_, true, true) => String::new(),
        (Syntax::Critic, true, false) => format!("{{++{new}++}}"),
        (Syntax::Critic, false, true) => format!("{{--{old}--}}"),
        (Syntax::Critic, false, false) => format!("{{~~{old}~>{new}~~}}"),
        (Syntax::Latex, true, false) => format!("\\added{{{new}}}"),
        (Syntax::Latex, false, true) => format!("\\deleted{{{old}}}"),
        (Syntax::Latex, false, false) => format!("\\replaced{{{new}}}{{{old}}}"),
    }
}

/// The whitespace both start with.
fn common_lead<'a>(a: &'a str, b: &str) -> &'a str {
    let n = a
        .chars()
        .zip(b.chars())
        .take_while(|(x, y)| x == y && x.is_whitespace())
        .map(|(x, _)| x.len_utf8())
        .sum();
    &a[..n]
}

/// The whitespace both end with.
fn common_trail<'a>(a: &'a str, b: &str) -> &'a str {
    let n: usize = a
        .chars()
        .rev()
        .zip(b.chars().rev())
        .take_while(|(x, y)| x == y && x.is_whitespace())
        .map(|(x, _)| x.len_utf8())
        .sum();
    &a[a.len() - n..]
}

/// A paper with the `changes` package its marks print with, just before its document begins.
/// None when it loads it already, or has no preamble to put it in. Comments don't count.
pub fn with_package(text: &str) -> Option<String> {
    let mut offset = 0;
    let mut begin = None;
    for line in text.split_inclusive('\n') {
        let code = crate::paper::uncommented(line);
        if loads_changes(code) {
            return None;
        }
        if begin.is_none()
            && let Some(at) = code.find("\\begin{document}")
        {
            begin = Some(offset + at);
        }
        offset += line.len();
    }
    let at = begin?;
    Some(format!(
        "{}\\usepackage{{changes}}\n{}",
        &text[..at],
        &text[at..]
    ))
}

/// Whether a line, without its comment, loads `changes` with `\usepackage[...]{...}`.
fn loads_changes(code: &str) -> bool {
    let mut rest = code;
    while let Some(at) = rest.find("\\usepackage") {
        let mut after = rest[at + "\\usepackage".len()..].trim_start();
        if after.starts_with('[') {
            after = after
                .find(']')
                .map_or("", |end| after[end + 1..].trim_start());
        }
        if let Some(inner) = after.strip_prefix('{')
            && let Some((names, _)) = inner.split_once('}')
            && names.split(',').any(|name| name.trim() == "changes")
        {
            return true;
        }
        rest = &rest[at + 1..];
    }
    false
}

pub fn settle_all(text: &str, syntax: Syntax, accept: bool) -> String {
    let chars: Vec<char> = text.chars().collect();
    let mut out = String::with_capacity(text.len());
    let mut from = 0;
    for change in find(text, syntax) {
        out.extend(&chars[from..change.start]);
        out.push_str(if accept { &change.new } else { &change.old });
        from = change.end;
    }
    out.extend(&chars[from..]);
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn inline_code_keeps_its_marks_as_code() {
        let text = "Use `a {++b++} c` and {++d++}.";
        assert_eq!(
            settle_all(text, Syntax::Critic, true),
            "Use `a {++b++} c` and d."
        );
    }

    #[test]
    fn a_plan_compare_never_marks_across_a_line() {
        let old = "# Plan\n\n1. Add a clock.\n2. Make it injectable so tests pass.\n3. Drop the windows.\n";
        let new = "# Plan\n\n1. Add a clock.\n2. Make the clock injectable.\n";
        let shown = compare(old, new);
        for line in shown.lines() {
            let opens = line.matches("{~~").count()
                + line.matches("{++").count()
                + line.matches("{--").count();
            let closes = line.matches("~~}").count()
                + line.matches("++}").count()
                + line.matches("--}").count();
            assert_eq!(opens, closes, "a mark spans a line: {line:?}\n{shown}");
        }
        assert!(shown.contains("3. {--Drop the windows.--}"), "{shown}");
        assert!(shown.contains("1. Add a clock."), "{shown}");
    }

    #[test]
    fn an_edit_becomes_suggestions_that_accept_back_to_it() {
        let old = "The cache goes first, then the index.\n";
        let new = "The cache goes last, then the new index.\n";
        let marked = suggest(old, new, Syntax::Critic);
        assert_eq!(
            marked,
            "The cache goes {~~first,~>last,~~} then the {++new ++}index.\n"
        );
        assert_eq!(settle_all(&marked, Syntax::Critic, true), new);
        assert_eq!(settle_all(&marked, Syntax::Critic, false), old);
        let tex = suggest("A b c.\n", "A x c.\n", Syntax::Latex);
        assert_eq!(tex, "A \\replaced{x}{b} c.\n");
    }

    #[test]
    fn markdown_writes_its_changes_in_three_ways() {
        let text = "a {++new++} b {--old--} c {~~was~>now~~} d";
        let found = find(text, Syntax::Critic);
        assert_eq!(found.len(), 3);
        assert_eq!(found[0].kind, Kind::Insert);
        assert_eq!(found[0].new, "new");
        assert_eq!(found[1].old, "old");
        assert_eq!(
            (found[2].old.as_str(), found[2].new.as_str()),
            ("was", "now")
        );
        assert_eq!(settle_all(text, Syntax::Critic, true), "a new b  c now d");
        assert_eq!(settle_all(text, Syntax::Critic, false), "a  b old c was d");
    }

    #[test]
    fn a_paper_uses_the_changes_package() {
        let text = "x \\added{new} y \\deleted[id=a]{old} z \\replaced{now}{was {nested}}";
        let found = find(text, Syntax::Latex);
        assert_eq!(found.len(), 3);
        assert_eq!(found[1].old, "old");
        assert_eq!(found[2].old, "was {nested}");
        assert_eq!(settle_all(text, Syntax::Latex, true), "x new y  z now");
        assert_eq!(
            settle_all(text, Syntax::Latex, false),
            "x  y old z was {nested}"
        );
    }

    #[test]
    fn the_changes_package_goes_in_unless_it_is_really_loaded() {
        let paper = |preamble: &str| {
            format!(
                "\\documentclass{{article}}\n{preamble}\\begin{{document}}\nx\n\\end{{document}}\n"
            )
        };
        for loaded in [
            "\\usepackage{changes}\n",
            "\\usepackage[final]{changes}\n",
            "\\usepackage [markup=underlined] {xcolor, changes}\n",
        ] {
            assert_eq!(with_package(&paper(loaded)), None, "{loaded}");
        }
        for not_loaded in [
            "% \\usepackage{changes}\n",
            "\\label{changes}\n",
            "\\usepackage{changesets}\n",
        ] {
            let with = with_package(&paper(not_loaded)).expect(not_loaded);
            assert!(
                with.contains("\\usepackage{changes}\n\\begin{document}"),
                "{with}"
            );
        }
        let commented =
            "\\documentclass{article}\n% \\begin{document} comes later\n\\begin{document}\nx\n";
        assert_eq!(
            with_package(commented).unwrap(),
            "\\documentclass{article}\n% \\begin{document} comes later\n\\usepackage{changes}\n\\begin{document}\nx\n",
            "after the comment, before the real one"
        );
        assert_eq!(
            with_package("% \\begin{document}\nx\n"),
            None,
            "no preamble"
        );
    }

    #[test]
    fn places_count_chars_not_bytes() {
        let text = "é {++ü++}";
        let change = &find(text, Syntax::Critic)[0];
        assert_eq!((change.start, change.end), (2, 9));
    }

    #[test]
    fn an_unclosed_mark_is_just_text() {
        assert!(find("a {++b", Syntax::Critic).is_empty());
        assert_eq!(settle_all("a {++b", Syntax::Critic, true), "a {++b");
    }
}