ikat 0.11.0

Weave Markdown into camera-ready LaTeX: mermaid-to-TikZ, pgfplots figures, publisher templates
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
//! Markdown block splitter (E.1 / M5.4; line numbers added F.1):
//! pulldown-cmark owns block BOUNDARIES; every content rule is
//! applied to raw source slices (byte offsets). Inline events are
//! IGNORED — the slice is the truth.
//!
//! Every block carries its 1-based ORIGINAL manuscript line
//! (`prepass` deletions shift stripped lines, so a line map rides
//! along). Fence/table inner lines resolve as `block.line + row`
//! (fence and table bodies are consecutive original lines).
//! Paragraph lines may be non-consecutive (a deleted quote between
//! them); para errors report the first line with the joined text
//! as echo.
//!
//! Mapping (each justified by the `md_spike` parity harness):
//! - `>` quote lines: pre-deleted (the hand scanner dropped them
//!   without even breaking paragraphs — deletion replicates that).
//! - `%% table`-before-table: pre-extracted to
//!   `(table_seq, attrs, orig_line)`; `Directive` blocks splice
//!   positionally before their table. A dangling `%% table` stays
//!   paragraph text, as before.
//! - Link reference definitions `[label]: ...` are a hard error
//!   naming the line (the stream would swallow them silently).
//! - ATX `#`/`##`/`###` (+ `## Appendix`/`## Abstract` prefixes):
//!   headings as before. Setext, H4+, `#NoSpace`: raw lines as
//!   paragraph text (closed `## X ##` keeps its closers).
//! - Backtick fences: raw info line + raw body lines (pulldown
//!   preserves both verbatim — probed). `~~~` and indented code:
//!   raw lines as paragraph text.
//! - Tables: raw slice rows (delimiter included). `|`-runs
//!   pulldown rejects are re-scanned out of paragraph slices.
//! - Paragraphs break ONLY on blank lines (stripped-index
//!   arithmetic). Titles never flush; last wins.

use pulldown_cmark::{Event, Options, Parser, Tag};

use crate::error::Error;

/// A block with its 1-based original manuscript line.
#[derive(Debug, PartialEq)]
pub struct MdBlock {
    pub line: usize,
    pub kind: MdKind,
}

/// Block stream `convert()` consumes.
#[derive(Debug, PartialEq)]
pub enum MdKind {
    Title(String),
    H2(String),
    H3(String),
    Appendix,
    Abstract,
    Fence { info: String, body: String },
    Table(Vec<String>),
    Directive(String),
    Para(Vec<String>),
    /// Display math: a paragraph that is exactly one `$$...$$`
    /// span (M7.8). The weaver emits `\[...\]`, widening to a
    /// full-width `strip` when the span exceeds the column.
    DisplayMath(String),
}

/// Fence-aware pre-pass: drop `>` quote lines, extract
/// `%% table`-before-table directives keyed by the table sequence
/// they precede, reject link reference definitions. Returns the
/// stripped lines, their 1-based original line numbers, and
/// `(table_seq, attrs, orig_line)` directives.
pub fn prepass(md: &str) -> Result<(Vec<String>, Vec<usize>, Vec<(usize, String, usize)>), Error> {
    let lines: Vec<&str> = md.lines().collect();
    let mut kept: Vec<bool> = vec![true; lines.len()];
    let mut directives: Vec<(usize, String, usize)> = Vec::new();
    let mut infence = false;
    for (n, l) in lines.iter().enumerate() {
        let s = l.trim();
        if s.starts_with("```") {
            infence = !infence;
            continue;
        }
        if infence {
            continue;
        }
        if s.starts_with('>') {
            kept[n] = false;
            continue;
        }
        if s.starts_with("%% table") {
            let mut j = n + 1;
            while j < lines.len() && lines[j].trim().is_empty() {
                j += 1;
            }
            if j < lines.len() && lines[j].trim().starts_with('|') {
                kept[n] = false;
                let mut runs = 0;
                let mut k = 0;
                while k < j {
                    if kept[k] && lines[k].trim().starts_with('|') {
                        runs += 1;
                        while k < j && kept[k] && lines[k].trim().starts_with('|') {
                            k += 1;
                        }
                    } else {
                        k += 1;
                    }
                }
                directives.push((runs, crate::doc::fence_attr_str(s).to_string(), n + 1));
            }
            continue;
        }
        // Link reference definition (`[label]: dest`, not `[^..]:`):
        // the event stream swallows these with no event — the text
        // would vanish. Error loudly instead, naming the line.
        if !s.starts_with("[^") && s.starts_with('[') {
            if let Some(col) = s.find("]:") {
                if !s[1..col].contains([' ', '\n', '[']) {
                    return Err(Error::new(
                        "reference definitions `[label]: ...` are not in the subset (write the text inline)",
                        n + 1,
                        s.to_string(),
                    ).with_col(Some(1)));
                }
            }
        }
    }
    let mut stripped = Vec::new();
    let mut orig = Vec::new();
    for (n, l) in lines.iter().enumerate() {
        if kept[n] {
            stripped.push(l.to_string());
            orig.push(n + 1);
        }
    }
    Ok((stripped, orig, directives))
}

/// Split `md` into blocks per the mapping above.
pub fn blocks(md: &str) -> Result<Vec<MdBlock>, Error> {
    let (stripped, orig, directives) = prepass(md)?;
    let text = stripped.join("\n");
    // Byte offset of each stripped line's start.
    let mut line_off: Vec<usize> = Vec::with_capacity(stripped.len());
    let mut o = 0;
    for l in &stripped {
        line_off.push(o);
        o += l.len() + 1;
    }
    let line_of = |off: usize| -> usize {
        match line_off.binary_search(&off) {
            Ok(n) => n,
            Err(n) => n.saturating_sub(1),
        }
    };
    let is_blank = |k: usize| -> bool {
        stripped.get(k).map(|l| l.trim().is_empty()).unwrap_or(false)
    };

    let mut out: Vec<MdBlock> = Vec::new();
    let mut para: Vec<String> = Vec::new();
    // Stripped index of the last line that fed the pending para.
    let mut para_tail: usize = 0;
    // Original line of the pending para's first fed line.
    let mut para_line: usize = 0;

    let flush = |out: &mut Vec<MdBlock>, para: &mut Vec<String>, pline: &mut usize| {
        if !para.is_empty() {
            out.push(MdBlock { line: *pline, kind: MdKind::Para(std::mem::take(para)) });
        }
    };
    // Feed `(stripped index, text)` lines into the pending para,
    // flushing first when a truly blank stripped line lies between
    // the last fed line and this run's first line.
    let feed = |out: &mut Vec<MdBlock>,
                para: &mut Vec<String>,
                tail: &mut usize,
                pline: &mut usize,
                run: &[(usize, String)]| {
        if run.is_empty() {
            return;
        }
        if !para.is_empty() && (tail.saturating_add(1)..run[0].0).any(|k| is_blank(k)) {
            out.push(MdBlock { line: *pline, kind: MdKind::Para(std::mem::take(para)) });
        }
        for (k, l) in run {
            let t = l.trim();
            if !t.is_empty() {
                if para.is_empty() {
                    *pline = orig.get(*k).copied().unwrap_or(1);
                }
                para.push(t.to_string());
                *tail = *k;
            }
        }
    };

    enum Kind {
        Para,
        Heading,
        Code,
        Table,
        Other,
    }
    let mut stack: Vec<(Kind, usize)> = Vec::new();
    let mut spans: Vec<(Kind, usize, usize)> = Vec::new();
    for (ev, range) in Parser::new_ext(&text, Options::ENABLE_TABLES).into_offset_iter() {
        match ev {
            Event::Start(tag) => {
                let k = match tag {
                    Tag::Paragraph => Kind::Para,
                    Tag::Heading { .. } => Kind::Heading,
                    Tag::CodeBlock(_) => Kind::Code,
                    Tag::Table(_) => Kind::Table,
                    _ => Kind::Other,
                };
                stack.push((k, range.start));
            }
            Event::End(_) => {
                if let Some((k, s)) = stack.pop() {
                    if stack.is_empty() {
                        spans.push((k, s, range.end));
                    }
                }
            }
            _ => {}
        }
    }

    for (kind, s, e) in spans {
        // The span covers contiguous stripped lines.
        let base = line_of(s);
        let cover: Vec<(usize, String)> = text[s..e]
            .split('\n')
            .enumerate()
            .map(|(k, l)| (base + k, l.to_string()))
            .collect();
        // 1-based original line of the span's first line.
        let ln = orig.get(base).copied().unwrap_or(1);
        match kind {
            Kind::Other => {
                feed(&mut out, &mut para, &mut para_tail, &mut para_line, &cover);
            }
            Kind::Para => {
                // Re-scan for `|`-runs the hand scanner would table.
                // Segments carry their stripped indices so para
                // lines keep exact originals.
                let mut seg: Vec<(usize, String)> = Vec::new();
                let mut run: Vec<(usize, String)> = Vec::new();
                let mut runs: Vec<(Vec<(usize, String)>, Vec<(usize, String)>)> = Vec::new();
                for (k, l) in cover {
                    if l.trim().starts_with('|') {
                        if !seg.is_empty() {
                            runs.push((std::mem::take(&mut seg), Vec::new()));
                        }
                        run.push((k, l.trim().to_string()));
                    } else {
                        if !run.is_empty() {
                            runs.push((Vec::new(), std::mem::take(&mut run)));
                        }
                        seg.push((k, l));
                    }
                }
                if !run.is_empty() {
                    runs.push((Vec::new(), run));
                }
                if !seg.is_empty() {
                    runs.push((seg, Vec::new()));
                }
                for (textseg, tablerun) in runs {
                    if !tablerun.is_empty() {
                        flush(&mut out, &mut para, &mut para_line);
                        // Table line: original of its first row.
                        let tline = orig.get(tablerun[0].0).copied().unwrap_or(ln);
                        let rows = tablerun.into_iter().map(|(_, r)| r).collect();
                        out.push(MdBlock { line: tline, kind: MdKind::Table(rows) });
                    } else {
                        feed(&mut out, &mut para, &mut para_tail, &mut para_line, &textseg);
                    }
                }
            }
            Kind::Heading => {
                let first = cover[0].1.trim().to_string();
                if let Some(rest) = first.strip_prefix("### ") {
                    flush(&mut out, &mut para, &mut para_line);
                    out.push(MdBlock { line: ln, kind: MdKind::H3(rest.to_string()) });
                } else if let Some(rest) = first.strip_prefix("## ") {
                    flush(&mut out, &mut para, &mut para_line);
                    if rest.strip_prefix("Appendix").is_some() {
                        out.push(MdBlock { line: ln, kind: MdKind::Appendix });
                    } else if rest.starts_with("Abstract") {
                        out.push(MdBlock { line: ln, kind: MdKind::Abstract });
                    } else {
                        out.push(MdBlock { line: ln, kind: MdKind::H2(rest.to_string()) });
                    }
                } else if let Some(rest) = first.strip_prefix("# ") {
                    if let Some(MdBlock { kind: MdKind::Title(_), .. }) = out.last() {
                        out.pop();
                    }
                    out.push(MdBlock { line: ln, kind: MdKind::Title(rest.to_string()) });
                } else {
                    feed(&mut out, &mut para, &mut para_tail, &mut para_line, &cover);
                }
            }
            Kind::Code => {
                let first = cover[0].1.trim().to_string();
                if first.starts_with("```") {
                    flush(&mut out, &mut para, &mut para_line);
                    let mut body = Vec::new();
                    for (_, l) in cover.iter().skip(1) {
                        let t = l.trim();
                        if !t.is_empty() && t.chars().all(|c| c == '`') {
                            break;
                        }
                        body.push(l.to_string());
                    }
                    out.push(MdBlock {
                        line: ln,
                        kind: MdKind::Fence { info: first, body: body.join("\n") },
                    });
                } else {
                    feed(&mut out, &mut para, &mut para_tail, &mut para_line, &cover);
                }
            }
            Kind::Table => {
                flush(&mut out, &mut para, &mut para_line);
                let mut rows: Vec<String> =
                    cover.iter().map(|(_, l)| l.trim().to_string()).collect();
                while rows.last().map(|l| l.is_empty()).unwrap_or(false) {
                    rows.pop();
                }
                out.push(MdBlock { line: ln, kind: MdKind::Table(rows) });
            }
        }
    }
    flush(&mut out, &mut para, &mut para_line);

    let mut sorted = directives;
    sorted.sort_by_key(|(seq, _, _)| *seq);
    let mut diri = 0usize;
    let mut table_idx = 0usize;
    let mut merged: Vec<MdBlock> = Vec::with_capacity(out.len() + sorted.len());
    for b in out {
        if matches!(b.kind, MdKind::Table(_)) {
            while diri < sorted.len() && sorted[diri].0 == table_idx {
                merged.push(MdBlock {
                    line: sorted[diri].2,
                    kind: MdKind::Directive(sorted[diri].1.clone()),
                });
                diri += 1;
            }
            table_idx += 1;
        }
        merged.push(b);
    }
    // Display math: a paragraph that is exactly one `$$...$$` span
    // becomes its own block; anything else holding `$$` is a hard
    // error naming the subset (inline `$$` would silently misrender).
    let mut final_blocks: Vec<MdBlock> = Vec::with_capacity(merged.len());
    for b in merged {
        match b.kind {
            MdKind::Para(ls) => {
                let joined = ls.join(" ");
                let t = joined.trim();
                if t.starts_with("$$") && t.ends_with("$$") && t.len() >= 4 { // `$$$$` reports empty
                    let inner = t[2..t.len() - 2].trim();
                    if inner.is_empty() {
                        return Err(Error::data(
                            format!("line {}: empty display math `$$$$`", b.line),
                            String::new(),
                        ));
                    }
                    if inner.contains("$$") {
                        return Err(Error::data(
                            format!(
                                "line {}: display math holds a second `$$`; one span per paragraph",
                                b.line
                            ),
                            String::new(),
                        ));
                    }
                    final_blocks.push(MdBlock { line: b.line, kind: MdKind::DisplayMath(inner.to_string()) });
                } else {
                    if t.contains("$$") {
                        return Err(Error::data(
                            format!(
                                "line {}: `$$` outside a whole-paragraph span is not in the subset; display math must fill its own paragraph",
                                b.line
                            ),
                            String::new(),
                        ));
                    }
                    final_blocks.push(MdBlock { line: b.line, kind: MdKind::Para(ls) });
                }
            }
            _ => final_blocks.push(b),
        }
    }
    Ok(final_blocks)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn display_math_whole_paragraph() {
        let b = blocks("# T\n\n$$x^2$$\n").unwrap();
        assert!(matches!(b[1].kind, MdKind::DisplayMath(_)));
    }

    #[test]
    fn display_math_stray_dollars_error() {
        let err = blocks("# T\n\nsee $$x$$ here\n").unwrap_err();
        assert!(err.msg.contains("own paragraph"), "got {}", err.msg);
    }

    #[test]
    fn display_math_empty_and_double_error() {
        let err = blocks("# T\n\n$$$$\n").unwrap_err();
        assert!(err.msg.contains("empty"), "got {}", err.msg);
        let err = blocks("# T\n\n$$a$$b$$\n").unwrap_err();
        assert!(err.msg.contains("second"), "got {}", err.msg);
    }

    #[test]
    fn error_refdef_names_line() {
        let e = blocks("# T\n\n[lab]: /url\n").unwrap_err();
        assert!(e.msg.contains("reference definitions"), "got: {e}");
        assert_eq!(e.line, 3, "got: {e}");
        assert!(e.echo.contains("[lab]: /url"), "got: {e}");
    }

    #[test]
    fn blocks_carry_original_lines() {
        // Quote + directive lines vanish; the survivors keep
        // original numbers (title 1, section 5, table 7).
        let md = "# T\n\n> dropped\n\n## S\n\n%% table {pos=bottom}\n\n| A |\n|---|\n";
        let bs = blocks(md).unwrap();
        let lines: Vec<usize> = bs.iter().map(|b| b.line).collect();
        assert!(lines.contains(&1), "title: {lines:?}");
        assert!(lines.contains(&5), "section: {lines:?}");
        assert!(lines.contains(&7), "directive orig line: {lines:?}");
        assert!(lines.contains(&9), "table orig line: {lines:?}");
    }
}