univert 0.4.0

Universal file converter (library and CLI)
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
//! Translate HTML to Typst markup. Used by `pdf_out::html_to_pdf` to drive a
//! pure-Rust HTML → PDF pipeline through the `typst` crate.
//!
//! HTML is first normalised to Markdown via `html2md` (which walks the HTML
//! DOM), then the Markdown event stream from `pulldown-cmark` is translated
//! to Typst markup. This trades CSS fidelity for a dependency-free, in-process
//! conversion that handles headings, paragraphs, emphasis, lists, tables,
//! links, inline/fenced code, blockquotes, and horizontal rules.

use pulldown_cmark::{
  CodeBlockKind, Event, HeadingLevel, Options, Parser, Tag, TagEnd,
};

pub fn html_to_typst(html: &str) -> String {
  let cleaned = strip_non_content(html);
  let md = html2md::parse_html(&cleaned);
  md_to_typst(&md)
}

/// Remove `<head>`, `<script>`, and `<style>` sections wholesale so their raw
/// contents don't leak into the Markdown/Typst output. Matching is case-
/// insensitive and tolerant of attributes on the opening tag. The prefix
/// must be followed by a tag terminator (`>`, whitespace, or `/`) so that
/// `<header>` isn't misidentified as `<head>`.
fn strip_non_content(html: &str) -> String {
  let lower = html.to_ascii_lowercase();
  let mut out = String::with_capacity(html.len());
  let mut i = 0;
  while i < html.len() {
    let rest_lower = &lower[i..];
    let skip = ["head", "script", "style"]
      .iter()
      .copied()
      .find(|name| matches_open_tag(rest_lower, name));
    if let Some(name) = skip {
      let close = format!("</{name}>");
      if let Some(end_rel) = lower[i..].find(&close) {
        i += end_rel + close.len();
        continue;
      } else {
        // No closing tag — drop the rest of the document.
        break;
      }
    }
    // Copy the next UTF-8 character intact.
    let next = html[i..].chars().next().unwrap();
    let len = next.len_utf8();
    out.push_str(&html[i..i + len]);
    i += len;
  }
  out
}

/// Whether `rest` (already lowercased) begins with `<name` followed by a
/// character that cannot extend an HTML tag name.
fn matches_open_tag(rest: &str, name: &str) -> bool {
  let Some(after_lt) = rest.strip_prefix('<') else {
    return false;
  };
  let Some(after_name) = after_lt.strip_prefix(name) else {
    return false;
  };
  matches!(
    after_name.as_bytes().first(),
    Some(b'>' | b'/' | b' ' | b'\t' | b'\n' | b'\r') | None
  )
}

fn md_to_typst(md: &str) -> String {
  let mut opts = Options::empty();
  opts.insert(Options::ENABLE_TABLES);
  opts.insert(Options::ENABLE_STRIKETHROUGH);
  opts.insert(Options::ENABLE_TASKLISTS);
  let parser = Parser::new_ext(md, opts);

  let mut buffers: Vec<String> = vec![String::new()];
  let mut list_stack: Vec<Option<u64>> = Vec::new();
  let mut link_stack: Vec<String> = Vec::new();
  let mut image_stack: Vec<String> = Vec::new();
  let mut table_cols: usize = 0;
  let mut table_cells: Vec<String> = Vec::new();
  let mut in_code_block = false;

  for ev in parser {
    match ev {
      Event::Start(Tag::Heading { level, .. }) => {
        let depth = heading_depth(level);
        let top = buffers.last_mut().unwrap();
        ensure_blank_line(top);
        for _ in 0..depth {
          top.push('=');
        }
        top.push(' ');
      }
      Event::End(TagEnd::Heading(_)) => {
        buffers.last_mut().unwrap().push('\n');
      }

      Event::Start(Tag::Paragraph) => {
        ensure_blank_line(buffers.last_mut().unwrap());
      }
      Event::End(TagEnd::Paragraph) => {
        buffers.last_mut().unwrap().push('\n');
      }

      Event::Start(Tag::BlockQuote(_)) => {
        let top = buffers.last_mut().unwrap();
        ensure_blank_line(top);
        top.push_str("#quote(block: true)[");
      }
      Event::End(TagEnd::BlockQuote(_)) => {
        buffers.last_mut().unwrap().push_str("]\n");
      }

      Event::Start(Tag::CodeBlock(kind)) => {
        in_code_block = true;
        let lang = match kind {
          CodeBlockKind::Indented => String::new(),
          CodeBlockKind::Fenced(l) => l.to_string(),
        };
        let top = buffers.last_mut().unwrap();
        ensure_blank_line(top);
        top.push_str("```");
        top.push_str(&lang);
        top.push('\n');
      }
      Event::End(TagEnd::CodeBlock) => {
        in_code_block = false;
        let top = buffers.last_mut().unwrap();
        if !top.ends_with('\n') {
          top.push('\n');
        }
        top.push_str("```\n");
      }

      Event::Start(Tag::List(start)) => {
        list_stack.push(start);
        let top = buffers.last_mut().unwrap();
        if !top.ends_with('\n') {
          top.push('\n');
        }
      }
      Event::End(TagEnd::List(_)) => {
        list_stack.pop();
        if list_stack.is_empty() {
          buffers.last_mut().unwrap().push('\n');
        }
      }
      Event::Start(Tag::Item) => {
        let depth = list_stack.len().saturating_sub(1);
        let marker = match list_stack.last().copied().flatten() {
          Some(_) => "+ ",
          None => "- ",
        };
        let top = buffers.last_mut().unwrap();
        for _ in 0..depth {
          top.push_str("  ");
        }
        top.push_str(marker);
      }
      Event::End(TagEnd::Item) => {
        let top = buffers.last_mut().unwrap();
        if !top.ends_with('\n') {
          top.push('\n');
        }
      }

      Event::Start(Tag::Emphasis) => {
        buffers.last_mut().unwrap().push_str("#emph[");
      }
      Event::End(TagEnd::Emphasis) => {
        buffers.last_mut().unwrap().push(']');
      }
      Event::Start(Tag::Strong) => {
        buffers.last_mut().unwrap().push_str("#strong[");
      }
      Event::End(TagEnd::Strong) => {
        buffers.last_mut().unwrap().push(']');
      }
      Event::Start(Tag::Strikethrough) => {
        buffers.last_mut().unwrap().push_str("#strike[");
      }
      Event::End(TagEnd::Strikethrough) => {
        buffers.last_mut().unwrap().push(']');
      }

      Event::Start(Tag::Link { dest_url, .. }) => {
        link_stack.push(dest_url.to_string());
        buffers.push(String::new());
      }
      Event::End(TagEnd::Link) => {
        let text = buffers.pop().unwrap_or_default();
        let url = link_stack.pop().unwrap_or_default();
        let rendered = if text.trim().is_empty() {
          format!("#link(\"{}\")", escape_string(&url))
        } else {
          format!("#link(\"{}\")[{}]", escape_string(&url), text)
        };
        buffers.last_mut().unwrap().push_str(&rendered);
      }

      Event::Start(Tag::Image { dest_url, .. }) => {
        image_stack.push(dest_url.to_string());
        buffers.push(String::new());
      }
      Event::End(TagEnd::Image) => {
        let alt = buffers.pop().unwrap_or_default();
        let url = image_stack.pop().unwrap_or_default();
        let top = buffers.last_mut().unwrap();
        if is_remote(&url) || url.is_empty() {
          // Can't fetch remotes from the Typst world; fall back to alt text.
          if !alt.trim().is_empty() {
            top.push_str("#emph[");
            top.push_str(&alt);
            top.push(']');
          }
        } else {
          top.push_str(&format!("#image(\"{}\")", escape_string(&url)));
        }
      }

      Event::Start(Tag::Table(aligns)) => {
        table_cols = aligns.len().max(1);
        table_cells.clear();
      }
      Event::End(TagEnd::Table) => {
        let top = buffers.last_mut().unwrap();
        ensure_blank_line(top);
        top.push_str("#table(\n  columns: ");
        top.push_str(&table_cols.to_string());
        top.push_str(",\n");
        for (i, cell) in table_cells.iter().enumerate() {
          top.push_str("  [");
          top.push_str(cell.trim());
          top.push_str("],");
          if (i + 1) % table_cols == 0 {
            top.push('\n');
          } else {
            top.push(' ');
          }
        }
        if !top.ends_with('\n') {
          top.push('\n');
        }
        top.push_str(")\n");
        table_cells.clear();
        table_cols = 0;
      }
      Event::Start(Tag::TableHead)
      | Event::End(TagEnd::TableHead)
      | Event::Start(Tag::TableRow)
      | Event::End(TagEnd::TableRow) => {}
      Event::Start(Tag::TableCell) => {
        buffers.push(String::new());
      }
      Event::End(TagEnd::TableCell) => {
        let cell = buffers.pop().unwrap_or_default();
        table_cells.push(cell);
      }

      Event::Text(t) => {
        let top = buffers.last_mut().unwrap();
        if in_code_block {
          top.push_str(&t);
        } else {
          top.push_str(&escape_markup(&t));
        }
      }
      Event::Code(t) => {
        let rendered = render_inline_raw(&t);
        buffers.last_mut().unwrap().push_str(&rendered);
      }
      Event::SoftBreak => buffers.last_mut().unwrap().push(' '),
      Event::HardBreak => {
        buffers.last_mut().unwrap().push_str("#linebreak()\n");
      }
      Event::Rule => {
        let top = buffers.last_mut().unwrap();
        ensure_blank_line(top);
        top.push_str("#line(length: 100%)\n");
      }

      // Drop raw HTML blocks/spans and constructs we don't translate.
      Event::Html(_)
      | Event::InlineHtml(_)
      | Event::FootnoteReference(_)
      | Event::TaskListMarker(_)
      | Event::Start(Tag::FootnoteDefinition(_))
      | Event::End(TagEnd::FootnoteDefinition)
      | Event::Start(Tag::HtmlBlock)
      | Event::End(TagEnd::HtmlBlock)
      | Event::Start(Tag::MetadataBlock(_))
      | Event::End(TagEnd::MetadataBlock(_))
      | Event::Start(Tag::DefinitionList)
      | Event::End(TagEnd::DefinitionList)
      | Event::Start(Tag::DefinitionListTitle)
      | Event::End(TagEnd::DefinitionListTitle)
      | Event::Start(Tag::DefinitionListDefinition)
      | Event::End(TagEnd::DefinitionListDefinition) => {}
      Event::InlineMath(_) | Event::DisplayMath(_) => {}
    }
  }

  buffers.pop().unwrap_or_default()
}

fn heading_depth(level: HeadingLevel) -> usize {
  match level {
    HeadingLevel::H1 => 1,
    HeadingLevel::H2 => 2,
    HeadingLevel::H3 => 3,
    HeadingLevel::H4 => 4,
    HeadingLevel::H5 => 5,
    HeadingLevel::H6 => 6,
  }
}

fn ensure_blank_line(s: &mut String) {
  if s.is_empty() {
    return;
  }
  if s.ends_with("\n\n") {
    return;
  }
  if s.ends_with('\n') {
    s.push('\n');
  } else {
    s.push_str("\n\n");
  }
}

fn is_remote(url: &str) -> bool {
  let u = url.trim_start();
  u.starts_with("http://")
    || u.starts_with("https://")
    || u.starts_with("data:")
    || u.starts_with("//")
}

fn escape_markup(s: &str) -> String {
  let mut out = String::with_capacity(s.len());
  for c in s.chars() {
    match c {
      '\\' | '#' | '$' | '*' | '_' | '`' | '<' | '>' | '@' | '~' | '['
      | ']' => {
        out.push('\\');
        out.push(c);
      }
      _ => out.push(c),
    }
  }
  out
}

fn escape_string(s: &str) -> String {
  s.replace('\\', "\\\\").replace('"', "\\\"")
}

/// Wrap `text` in Typst inline raw syntax using a backtick run long enough to
/// avoid collisions with backticks inside the text itself.
fn render_inline_raw(text: &str) -> String {
  let mut max_run = 0usize;
  let mut current = 0usize;
  for c in text.chars() {
    if c == '`' {
      current += 1;
      max_run = max_run.max(current);
    } else {
      current = 0;
    }
  }
  let fence: String = "`".repeat(max_run + 1);
  let needs_padding =
    text.starts_with('`') || text.ends_with('`') || max_run >= 1;
  if needs_padding {
    format!("{fence} {text} {fence}")
  } else {
    format!("{fence}{text}{fence}")
  }
}

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

  #[test]
  fn strip_does_not_swallow_header_tag() {
    // <header> begins with <head — make sure the prefix check requires a
    // tag-terminating character so real content isn't dropped.
    let html = "<html><head><title>t</title></head><body>\
      <header>hi</header><p>keep</p></body></html>";
    let stripped = strip_non_content(html);
    assert!(
      stripped.contains("<header>"),
      "stripper lost <header>: {stripped}"
    );
    assert!(stripped.contains("keep"));
    assert!(!stripped.contains("<title>"));
  }

  #[test]
  fn strip_removes_head_script_style() {
    let html = "<html><head><style>x{}</style></head>\
      <body><script>z()</script><p>ok</p></body></html>";
    let stripped = strip_non_content(html);
    assert!(!stripped.contains("x{}"));
    assert!(!stripped.contains("z()"));
    assert!(stripped.contains("ok"));
  }

  #[test]
  fn html_to_typst_produces_content_for_header_heavy_page() {
    let html = "<html><head><title>t</title></head><body>\
      <header><h1>Hello</h1></header>\
      <p>world</p></body></html>";
    let typ = html_to_typst(html);
    assert!(typ.contains("Hello"), "missing heading: {typ}");
    assert!(typ.contains("world"), "missing paragraph: {typ}");
  }
}