|
8 | 8 | //! with [`rtf_to_slides`], straight into slides that keep empty titles and titles starting `- `. |
9 | 9 |
|
10 | 10 | use deckcraft_model::Presentation; |
11 | | -use deckcraft_model::text::{Paragraph, RunKind}; |
12 | | - |
13 | | -/// RTF-escape `s` (non-ASCII as `\uN?`, UTF-16, signed as RTF wants). |
14 | | -fn escape(out: &mut String, s: &str) { |
15 | | - // Keep outline text literal: the shared writer also supports page/column breaks and |
16 | | - // shorthand controls that this app's deliberately small outline reader does not consume. |
17 | | - // Delegate ordinary spans, including UTF-16 escaping, without allocating a second string. |
18 | | - let mut start = 0; |
19 | | - for (i, c) in s.char_indices() { |
20 | | - let replacement = match c { |
21 | | - '\u{000C}' | '\u{000E}' => "", |
22 | | - '\u{00A0}' => "\\u160?", |
23 | | - '\u{00AD}' => "\\u173?", |
24 | | - '\u{2011}' => "\\u8209?", |
25 | | - '\u{2028}' => "\\u8232?", |
26 | | - _ => continue, |
27 | | - }; |
28 | | - craft_rtf::push_esc(out, &s[start..i]); |
29 | | - out.push_str(replacement); |
30 | | - start = i + c.len_utf8(); |
31 | | - } |
32 | | - craft_rtf::push_esc(out, &s[start..]); |
33 | | -} |
| 11 | +use deckcraft_model::text::Paragraph; |
34 | 12 |
|
35 | | -fn runs(out: &mut String, p: &Paragraph) { |
| 13 | +fn write_runs(w: &mut craft_rtf::OutlineWriter, p: &Paragraph) { |
36 | 14 | for r in &p.runs { |
37 | | - if matches!(r.kind, RunKind::Break) { |
38 | | - out.push_str("\\line "); |
39 | | - continue; |
40 | | - } |
41 | | - let (b, i, u) = (r.props.bold == Some(true), r.props.italic == Some(true), r.props.underline.as_deref().is_some_and(|u| u != "none")); |
42 | | - if !(b || i || u) { |
43 | | - escape(out, &r.text); |
| 15 | + if matches!(r.kind, deckcraft_model::text::RunKind::Break) { |
| 16 | + w.line_break(); |
44 | 17 | continue; |
45 | 18 | } |
46 | | - out.push('{'); |
47 | | - out.push_str(if b { "\\b" } else { "" }); |
48 | | - out.push_str(if i { "\\i" } else { "" }); |
49 | | - out.push_str(if u { "\\ul" } else { "" }); |
50 | | - out.push(' '); |
51 | | - escape(out, &r.text); |
52 | | - out.push('}'); |
| 19 | + let b = r.props.bold == Some(true); |
| 20 | + let i = r.props.italic == Some(true); |
| 21 | + let u = r.props.underline.as_deref().is_some_and(|u| u != "none"); |
| 22 | + w.run(&r.text, b, i, u); |
53 | 23 | } |
54 | 24 | } |
55 | 25 |
|
56 | 26 | /// The presentation's outline (slide titles and body text) as RTF. |
57 | 27 | pub fn slides_to_rtf(p: &Presentation) -> String { |
58 | | - let mut out = String::from("{\\rtf1\\ansi\\ansicpg1252\\deff0\\uc1{\\fonttbl{\\f0\\fswiss Liberation Sans;}}\n{\\stylesheet"); |
59 | | - for n in 1..=9 { |
60 | | - out.push_str(&format!("{{\\s{n}\\li{}\\fi-360\\level{n} heading {n};}}", 360 * n)); |
61 | | - } |
62 | | - out.push_str("}\n"); |
| 28 | + let mut w = craft_rtf::OutlineWriter::new("Liberation Sans"); |
63 | 29 | for s in &p.slides { |
64 | 30 | let title = s.shapes.iter().find(|sh| sh.ph_type().is_some_and(|k| k.is_title())).and_then(|sh| sh.text.as_ref()); |
65 | | - out.push_str("\\pard\\s1\\outlinelevel0\\sb240\\b\\fs32 "); |
| 31 | + w.begin_title(); |
66 | 32 | // The title stays one outline paragraph (a `\par` would start another slide on re-import), |
67 | 33 | // so its paragraphs are joined with line breaks. |
68 | 34 | let mut tps = title.into_iter().flat_map(|t| &t.paragraphs).filter(|p| !p.is_empty()).peekable(); |
69 | 35 | if tps.peek().is_none() { |
70 | | - escape(&mut out, &s.title()); |
| 36 | + w.run(&s.title(), false, false, false); |
71 | 37 | } |
72 | 38 | for (k, tp) in tps.enumerate() { |
73 | 39 | if k > 0 { |
74 | | - out.push_str("\\line "); |
| 40 | + w.line_break(); |
75 | 41 | } |
76 | | - runs(&mut out, tp); |
| 42 | + write_runs(&mut w, tp); |
77 | 43 | } |
78 | | - out.push_str("\\b0\\par\n"); |
| 44 | + w.end_title(); |
79 | 45 | for sh in &s.shapes { |
80 | 46 | // As in the text outline: footer, date and slide-number placeholders are not body text. |
81 | 47 | if sh.ph_type().is_some_and(|k| k.is_title() || k.is_footer_kind()) { |
82 | 48 | continue; |
83 | 49 | } |
84 | 50 | let Some(t) = &sh.text else { continue }; |
85 | 51 | for para in t.paragraphs.iter().filter(|p| !p.is_empty()) { |
86 | | - let lvl = para.level.min(8) as u32; |
87 | | - out.push_str(&format!("\\pard\\s{}\\outlinelevel{}\\li{}\\fs24 ", (lvl + 2).min(9), lvl + 1, 360 * (lvl + 1))); |
88 | | - runs(&mut out, para); |
89 | | - out.push_str("\\par\n"); |
| 52 | + w.begin_body(para.level); |
| 53 | + write_runs(&mut w, para); |
| 54 | + w.end_body(); |
90 | 55 | } |
91 | 56 | } |
92 | 57 | } |
93 | | - out.push_str("}\n"); |
94 | | - out |
| 58 | + w.finish() |
95 | 59 | } |
96 | 60 |
|
97 | 61 | /// Whether `bytes` look like RTF. |
@@ -487,6 +451,24 @@ mod tests { |
487 | 451 | use super::*; |
488 | 452 |
|
489 | 453 | // Frozen pre-cutover implementations are test oracles, not production parsers. |
| 454 | + fn escape(out: &mut String, s: &str) { |
| 455 | + let mut start = 0; |
| 456 | + for (i, c) in s.char_indices() { |
| 457 | + let replacement = match c { |
| 458 | + '\u{000C}' | '\u{000E}' => "", |
| 459 | + '\u{00A0}' => "\\u160?", |
| 460 | + '\u{00AD}' => "\\u173?", |
| 461 | + '\u{2011}' => "\\u8209?", |
| 462 | + '\u{2028}' => "\\u8232?", |
| 463 | + _ => continue, |
| 464 | + }; |
| 465 | + craft_rtf::push_esc(out, &s[start..i]); |
| 466 | + out.push_str(replacement); |
| 467 | + start = i + c.len_utf8(); |
| 468 | + } |
| 469 | + craft_rtf::push_esc(out, &s[start..]); |
| 470 | + } |
| 471 | + |
490 | 472 | fn legacy_escape(out: &mut String, s: &str) { |
491 | 473 | for c in s.chars() { |
492 | 474 | match c { |
|
0 commit comments