1use crate::{CharVerticalAlignment, InlineContent, ListStyle};
4use frontend::common::parser_tools::content_parser::{
5 ParsedBlock, ParsedElement, ParsedSpan, split_block_at_line_breaks,
6};
7use frontend::common::parser_tools::fragment_schema::{
8 FragmentBlock, FragmentData, FragmentElement, FragmentQuoting, FragmentTable,
9 FragmentTableCell, as_a_whole_text, fragment_to_json, whole_text_to_json,
10};
11
12#[derive(Debug, Clone)]
18pub struct DocumentFragment {
19 data: String,
20 plain_text: String,
21}
22
23impl DocumentFragment {
24 pub fn new() -> Self {
26 Self {
27 data: String::new(),
28 plain_text: String::new(),
29 }
30 }
31
32 pub fn from_plain_text(text: &str) -> Self {
37 let blocks: Vec<FragmentBlock> = text
38 .split('\n')
39 .map(|line| FragmentBlock {
40 plain_text: line.to_string(),
41 elements: vec![FragmentElement {
42 content: InlineContent::Text(line.to_string()),
43 fmt_font_family: None,
44 fmt_font_point_size: None,
45 fmt_font_weight: None,
46 fmt_font_bold: None,
47 fmt_font_italic: None,
48 fmt_font_underline: None,
49 fmt_font_overline: None,
50 fmt_font_strikeout: None,
51 fmt_letter_spacing: None,
52 fmt_word_spacing: None,
53 fmt_anchor_href: None,
54 fmt_anchor_names: vec![],
55 fmt_is_anchor: None,
56 fmt_tooltip: None,
57 fmt_underline_style: None,
58 fmt_vertical_alignment: None,
59 }],
60 heading_level: None,
61 list: None,
62 alignment: None,
63 indent: None,
64 text_indent: None,
65 marker: None,
66 top_margin: None,
67 bottom_margin: None,
68 left_margin: None,
69 right_margin: None,
70 tab_positions: vec![],
71 line_height: None,
72 non_breakable_lines: None,
73 page_break_before: None,
74 direction: None,
75 background_color: None,
76 is_code_block: None,
77 code_language: None,
78 hyphenate: None,
79 language: None,
80 })
81 .collect();
82
83 let data = serde_json::to_string(&FragmentData {
84 blocks,
85 tables: vec![],
86 })
87 .expect("fragment serialization should not fail");
88
89 Self {
90 data,
91 plain_text: text.to_string(),
92 }
93 }
94
95 pub fn from_html(html: &str) -> Self {
103 let parsed = frontend::common::parser_tools::content_parser::parse_html_elements(html);
104 parsed_elements_to_fragment(parsed, ParsedFrom::Html)
105 }
106
107 pub fn from_markdown(markdown: &str) -> Self {
110 let parsed = frontend::common::parser_tools::content_parser::parse_markdown(markdown);
111 parsed_elements_to_fragment(parsed, ParsedFrom::SavedSyntax)
112 }
113
114 pub fn from_djot(djot: &str) -> Self {
122 let parsed = frontend::common::parser_tools::content_parser::parse_djot(
123 djot,
124 &frontend::common::parser_tools::DjotImportOptions::default(),
125 );
126 parsed_elements_to_fragment(parsed, ParsedFrom::SavedSyntax)
127 }
128
129 pub fn from_document(doc: &crate::TextDocument) -> crate::Result<Self> {
132 let inner = doc.inner.lock();
133 let dto = frontend::document_inspection::ExtractFragmentDto {
137 position: 0,
138 anchor: i64::MAX,
139 };
140 let result =
141 frontend::commands::document_inspection_commands::extract_fragment(&inner.ctx, &dto)?;
142 Ok(Self::from_raw(
143 as_a_whole_text(&result.fragment_data),
144 result.plain_text,
145 ))
146 }
147
148 pub(crate) fn from_raw(data: String, plain_text: String) -> Self {
150 Self { data, plain_text }
151 }
152
153 pub fn to_plain_text(&self) -> &str {
155 &self.plain_text
156 }
157
158 pub fn to_html(&self) -> String {
160 if self.data.is_empty() {
161 return String::from("<html><head><meta charset=\"utf-8\"></head><body></body></html>");
162 }
163
164 let fragment_data: FragmentData = match serde_json::from_str(&self.data) {
165 Ok(d) => d,
166 Err(_) => {
167 return String::from(
168 "<html><head><meta charset=\"utf-8\"></head><body></body></html>",
169 );
170 }
171 };
172
173 let mut body = String::new();
174 let blocks = &fragment_data.blocks;
175
176 if blocks.len() == 1 && blocks[0].is_inline_only() && fragment_data.tables.is_empty() {
178 push_inline_html(&mut body, &blocks[0].elements);
179 return format!(
180 "<html><head><meta charset=\"utf-8\"></head><body>{}</body></html>",
181 body
182 );
183 }
184
185 let mut sorted_tables: Vec<&FragmentTable> = fragment_data.tables.iter().collect();
187 sorted_tables.sort_by_key(|t| t.block_insert_index);
188 let mut table_cursor = 0;
189
190 let mut i = 0;
191
192 while i < blocks.len() {
193 while table_cursor < sorted_tables.len()
195 && sorted_tables[table_cursor].block_insert_index <= i
196 {
197 push_table_html(&mut body, sorted_tables[table_cursor]);
198 table_cursor += 1;
199 }
200
201 let block = &blocks[i];
202
203 if let Some(ref list) = block.list {
204 let is_ordered = is_ordered_list_style(&list.style);
205 let list_tag = if is_ordered { "ol" } else { "ul" };
206 body.push('<');
207 body.push_str(list_tag);
208 body.push('>');
209
210 while i < blocks.len() {
211 let b = &blocks[i];
212 match &b.list {
213 Some(l) if is_ordered_list_style(&l.style) == is_ordered => {
214 body.push_str("<li>");
215 push_inline_html(&mut body, &b.elements);
216 body.push_str("</li>");
217 i += 1;
218 }
219 _ => break,
220 }
221 }
222
223 body.push_str("</");
224 body.push_str(list_tag);
225 body.push('>');
226 } else if let Some(level) = block.heading_level {
227 let n = level.clamp(1, 6);
228 body.push_str(&format!("<h{}>", n));
229 push_inline_html(&mut body, &block.elements);
230 body.push_str(&format!("</h{}>", n));
231 i += 1;
232 } else {
233 let style = block_style_attr(block);
235 if style.is_empty() {
236 body.push_str("<p>");
237 } else {
238 body.push_str(&format!("<p style=\"{}\">", style));
239 }
240 push_inline_html(&mut body, &block.elements);
241 body.push_str("</p>");
242 i += 1;
243 }
244 }
245
246 while table_cursor < sorted_tables.len() {
248 push_table_html(&mut body, sorted_tables[table_cursor]);
249 table_cursor += 1;
250 }
251
252 format!(
253 "<html><head><meta charset=\"utf-8\"></head><body>{}</body></html>",
254 body
255 )
256 }
257
258 pub fn to_markdown(&self) -> String {
260 if self.data.is_empty() {
261 return String::new();
262 }
263
264 let fragment_data: FragmentData = match serde_json::from_str(&self.data) {
265 Ok(d) => d,
266 Err(_) => return String::new(),
267 };
268
269 let mut parts: Vec<(String, bool)> = Vec::new();
271 let mut prev_was_list = false;
272 let mut list_counter: u32 = 0;
273
274 let mut sorted_tables: Vec<&FragmentTable> = fragment_data.tables.iter().collect();
276 sorted_tables.sort_by_key(|t| t.block_insert_index);
277 let mut table_cursor = 0;
278
279 for (blk_idx, block) in fragment_data.blocks.iter().enumerate() {
280 while table_cursor < sorted_tables.len()
282 && sorted_tables[table_cursor].block_insert_index <= blk_idx
283 {
284 parts.push((render_table_markdown(sorted_tables[table_cursor]), false));
285 prev_was_list = false;
286 list_counter = 0;
287 table_cursor += 1;
288 }
289
290 let inline_text = render_inline_markdown(&block.elements);
291 let is_list = block.list.is_some();
292
293 let indent_prefix = match block.indent {
294 Some(n) if n > 0 => " ".repeat(n as usize),
295 _ => String::new(),
296 };
297
298 if let Some(level) = block.heading_level {
299 let n = level.clamp(1, 6) as usize;
300 let prefix = "#".repeat(n);
301 parts.push((format!("{} {}", prefix, inline_text), false));
302 prev_was_list = false;
303 list_counter = 0;
304 } else if let Some(ref list) = block.list {
305 let is_ordered = is_ordered_list_style(&list.style);
306 if !prev_was_list {
307 list_counter = 0;
308 }
309 if is_ordered {
310 list_counter += 1;
311 parts.push((
312 format!("{}{}. {}", indent_prefix, list_counter, inline_text),
313 true,
314 ));
315 } else {
316 parts.push((format!("{}- {}", indent_prefix, inline_text), true));
317 }
318 prev_was_list = true;
319 } else {
320 if indent_prefix.is_empty() {
321 parts.push((inline_text, false));
322 } else {
323 parts.push((format!("{}{}", indent_prefix, inline_text), false));
324 }
325 prev_was_list = false;
326 list_counter = 0;
327 }
328
329 if !is_list {
330 prev_was_list = false;
331 }
332 }
333
334 while table_cursor < sorted_tables.len() {
336 parts.push((render_table_markdown(sorted_tables[table_cursor]), false));
337 table_cursor += 1;
338 }
339
340 let mut result = String::new();
342 for (idx, (text, is_list)) in parts.iter().enumerate() {
343 if idx > 0 {
344 let (_, prev_is_list) = &parts[idx - 1];
345 if *prev_is_list && *is_list {
346 result.push('\n');
347 } else {
348 result.push_str("\n\n");
349 }
350 }
351 result.push_str(text);
352 }
353
354 result
355 }
356
357 pub fn is_empty(&self) -> bool {
359 self.plain_text.is_empty()
360 }
361
362 pub(crate) fn raw_data(&self) -> &str {
364 &self.data
365 }
366}
367
368impl Default for DocumentFragment {
369 fn default() -> Self {
370 Self::new()
371 }
372}
373
374fn is_ordered_list_style(style: &ListStyle) -> bool {
379 matches!(
380 style,
381 ListStyle::Decimal
382 | ListStyle::LowerAlpha
383 | ListStyle::UpperAlpha
384 | ListStyle::LowerRoman
385 | ListStyle::UpperRoman
386 )
387}
388
389fn escape_html(s: &str) -> String {
392 let mut out = String::with_capacity(s.len());
393 for c in s.chars() {
394 match c {
395 '&' => out.push_str("&"),
396 '<' => out.push_str("<"),
397 '>' => out.push_str(">"),
398 '"' => out.push_str("""),
399 '\'' => out.push_str("'"),
400 '\r' => out.push_str(" "),
405 _ => out.push(c),
406 }
407 }
408 out
409}
410
411fn block_style_attr(block: &FragmentBlock) -> String {
413 use crate::Alignment;
414
415 let mut parts = Vec::new();
416 if let Some(ref alignment) = block.alignment {
417 let value = match alignment {
418 Alignment::Left => "left",
419 Alignment::Right => "right",
420 Alignment::Center => "center",
421 Alignment::Justify => "justify",
422 };
423 parts.push(format!("text-align: {}", value));
424 }
425 if let Some(n) = block.indent
426 && n > 0
427 {
428 parts.push(format!("margin-left: {}em", n));
429 }
430 if let Some(px) = block.text_indent
431 && px != 0
432 {
433 parts.push(format!("text-indent: {}px", px));
434 }
435 if let Some(px) = block.top_margin {
436 parts.push(format!("margin-top: {}px", px));
437 }
438 if let Some(px) = block.bottom_margin {
439 parts.push(format!("margin-bottom: {}px", px));
440 }
441 if let Some(px) = block.left_margin {
442 parts.push(format!("margin-left: {}px", px));
443 }
444 if let Some(px) = block.right_margin {
445 parts.push(format!("margin-right: {}px", px));
446 }
447 parts.join("; ")
448}
449
450fn push_inline_html(out: &mut String, elements: &[FragmentElement]) {
451 for elem in elements {
452 let text = match &elem.content {
453 InlineContent::Text(t) => escape_html(t),
454 InlineContent::FootnoteRef { label } => {
457 let id = escape_html(label);
458 out.push_str(&format!(
459 "<a epub:type=\"noteref\" role=\"doc-noteref\" href=\"#fn-{id}\"><sup>{id}</sup></a>"
460 ));
461 continue;
462 }
463 InlineContent::Image {
464 name,
465 alt,
466 width,
467 height,
468 ..
469 } => {
470 let mut tag = format!(
475 "<img src=\"{}\" alt=\"{}\"",
476 escape_html(name),
477 escape_html(alt)
478 );
479 if *width > 0 {
480 tag.push_str(&format!(" width=\"{width}\""));
481 }
482 if *height > 0 {
483 tag.push_str(&format!(" height=\"{height}\""));
484 }
485 tag.push('>');
486 tag
487 }
488 InlineContent::Empty => String::new(),
489 };
490
491 let is_monospace = elem
492 .fmt_font_family
493 .as_deref()
494 .is_some_and(|f| f == "monospace");
495 let is_bold = elem.fmt_font_bold.unwrap_or(false);
496 let is_italic = elem.fmt_font_italic.unwrap_or(false);
497 let is_underline = elem.fmt_font_underline.unwrap_or(false);
498 let is_strikeout = elem.fmt_font_strikeout.unwrap_or(false);
499 let is_anchor = elem.fmt_is_anchor.unwrap_or(false);
500 let vertical = elem.fmt_vertical_alignment.as_ref();
505
506 let mut result = text;
507
508 if is_monospace {
509 result = format!("<code>{}</code>", result);
510 }
511 if is_bold {
512 result = format!("<strong>{}</strong>", result);
513 }
514 if is_italic {
515 result = format!("<em>{}</em>", result);
516 }
517 if is_underline {
518 result = format!("<u>{}</u>", result);
519 }
520 if is_strikeout {
521 result = format!("<s>{}</s>", result);
522 }
523 match vertical {
524 Some(CharVerticalAlignment::SuperScript) => result = format!("<sup>{result}</sup>"),
525 Some(CharVerticalAlignment::SubScript) => result = format!("<sub>{result}</sub>"),
526 _ => {}
527 }
528 if is_anchor && let Some(ref href) = elem.fmt_anchor_href {
529 result = format!("<a href=\"{}\">{}</a>", escape_html(href), result);
530 }
531
532 out.push_str(&result);
533 }
534}
535
536fn push_table_html(out: &mut String, table: &FragmentTable) {
538 out.push_str("<table>");
539 for row in 0..table.rows {
540 out.push_str("<tr>");
541 for col in 0..table.columns {
542 if let Some(cell) = table.cells.iter().find(|c| c.row == row && c.column == col) {
543 out.push_str("<td");
544 if cell.row_span > 1 {
545 out.push_str(&format!(" rowspan=\"{}\"", cell.row_span));
546 }
547 if cell.column_span > 1 {
548 out.push_str(&format!(" colspan=\"{}\"", cell.column_span));
549 }
550 out.push('>');
551 for (i, block) in cell.blocks.iter().enumerate() {
552 if i > 0 {
553 out.push_str("<br>");
554 }
555 push_inline_html(out, &block.elements);
556 }
557 out.push_str("</td>");
558 }
559 }
561 out.push_str("</tr>");
562 }
563 out.push_str("</table>");
564}
565
566fn escape_markdown(s: &str) -> String {
569 let mut out = String::with_capacity(s.len());
570 for c in s.chars() {
571 if matches!(
572 c,
573 '\\' | '`'
574 | '*'
575 | '_'
576 | '{'
577 | '}'
578 | '['
579 | ']'
580 | '('
581 | ')'
582 | '#'
583 | '+'
584 | '-'
585 | '.'
586 | '!'
587 | '|'
588 | '~'
589 | '<'
590 | '>'
591 ) {
592 out.push('\\');
593 }
594 out.push(c);
595 }
596 out
597}
598
599fn render_inline_markdown(elements: &[FragmentElement]) -> String {
600 let mut out = String::new();
601 for elem in elements {
602 let raw_text = match &elem.content {
603 InlineContent::Text(t) => t.clone(),
604 InlineContent::Image { name, alt, .. } => format!(""),
607 InlineContent::FootnoteRef { label } => format!("[^{label}]"),
608 InlineContent::Empty => String::new(),
609 };
610
611 let is_monospace = elem
612 .fmt_font_family
613 .as_deref()
614 .is_some_and(|f| f == "monospace");
615 let is_bold = elem.fmt_font_bold.unwrap_or(false);
616 let is_italic = elem.fmt_font_italic.unwrap_or(false);
617 let is_strikeout = elem.fmt_font_strikeout.unwrap_or(false);
618 let is_anchor = elem.fmt_is_anchor.unwrap_or(false);
619
620 if is_monospace {
621 out.push('`');
622 out.push_str(&raw_text);
623 out.push('`');
624 } else {
625 let mut text = escape_markdown(&raw_text);
626 if is_bold && is_italic {
627 text = format!("***{}***", text);
628 } else if is_bold {
629 text = format!("**{}**", text);
630 } else if is_italic {
631 text = format!("*{}*", text);
632 }
633 if is_strikeout {
634 text = format!("~~{}~~", text);
635 }
636 if is_anchor {
637 let href = elem.fmt_anchor_href.as_deref().unwrap_or("");
638 out.push_str(&format!("[{}]({})", text, href));
639 } else {
640 out.push_str(&text);
641 }
642 }
643 }
644 out
645}
646
647fn render_table_markdown(table: &FragmentTable) -> String {
649 let mut rows: Vec<Vec<String>> = vec![vec![String::new(); table.columns]; table.rows];
650
651 for cell in &table.cells {
652 let text: String = cell
653 .blocks
654 .iter()
655 .map(|b| render_inline_markdown(&b.elements))
656 .collect::<Vec<_>>()
657 .join(" ");
658 if cell.row < table.rows && cell.column < table.columns {
659 rows[cell.row][cell.column] = text;
660 }
661 }
662
663 let mut out = String::new();
664 for (i, row) in rows.iter().enumerate() {
665 out.push_str("| ");
666 out.push_str(&row.join(" | "));
667 out.push_str(" |");
668 if i == 0 {
669 out.push('\n');
671 out.push('|');
672 for _ in 0..table.columns {
673 out.push_str(" --- |");
674 }
675 }
676 if i + 1 < rows.len() {
677 out.push('\n');
678 }
679 }
680 out
681}
682
683fn spans_plain_text(spans: &[ParsedSpan]) -> String {
695 spans
696 .iter()
697 .map(|s| {
698 if s.image.is_some() || s.footnote_ref.is_some() {
703 "\u{FFFC}"
704 } else {
705 s.text.as_str()
706 }
707 })
708 .collect()
709}
710
711fn span_to_fragment_element(span: &ParsedSpan) -> FragmentElement {
712 let content = match (&span.footnote_ref, &span.image) {
721 (Some(label), _) => InlineContent::FootnoteRef {
722 label: label.clone(),
723 },
724 (None, Some(img)) => InlineContent::Image {
725 name: img.src.clone(),
726 alt: img.alt.clone(),
727 width: img.width,
728 height: img.height,
729 quality: 100,
732 },
733 (None, None) => InlineContent::Text(span.text.clone()),
734 };
735 let fmt_font_family = if span.code {
736 Some("monospace".into())
737 } else {
738 None
739 };
740 let fmt_font_bold = if span.bold { Some(true) } else { None };
741 let fmt_font_italic = if span.italic { Some(true) } else { None };
742 let fmt_font_underline = if span.underline { Some(true) } else { None };
743 let fmt_font_strikeout = if span.strikeout { Some(true) } else { None };
744 let (fmt_anchor_href, fmt_is_anchor) = if let Some(ref href) = span.link_href {
745 (Some(href.clone()), Some(true))
746 } else {
747 (None, None)
748 };
749 let fmt_vertical_alignment = if span.superscript {
755 Some(CharVerticalAlignment::SuperScript)
756 } else if span.subscript {
757 Some(CharVerticalAlignment::SubScript)
758 } else {
759 None
760 };
761
762 FragmentElement {
763 content,
764 fmt_font_family,
765 fmt_font_point_size: None,
766 fmt_font_weight: None,
767 fmt_font_bold,
768 fmt_font_italic,
769 fmt_font_underline,
770 fmt_font_overline: None,
771 fmt_font_strikeout,
772 fmt_letter_spacing: None,
773 fmt_word_spacing: None,
774 fmt_anchor_href,
775 fmt_anchor_names: vec![],
776 fmt_is_anchor,
777 fmt_tooltip: None,
778 fmt_underline_style: None,
779 fmt_vertical_alignment,
780 }
781}
782
783fn parsed_block_to_fragment_block(pb: ParsedBlock, keep_code_block: bool) -> FragmentBlock {
790 use frontend::common::parser_tools::fragment_schema::FragmentList;
791
792 let elements: Vec<FragmentElement> = pb.spans.iter().map(span_to_fragment_element).collect();
793 let plain_text: String = spans_plain_text(&pb.spans);
794 let list = pb.list_style.map(|style| FragmentList {
795 style,
796 indent: pb.list_indent as i64,
797 prefix: pb.list_prefix,
798 suffix: pb.list_suffix,
799 });
800 let is_code_block = keep_code_block && pb.is_code_block;
801 FragmentBlock {
802 plain_text,
803 elements,
804 heading_level: pb.heading_level,
805 list,
806 alignment: pb.alignment,
807 indent: None,
808 text_indent: pb.text_indent,
809 marker: pb.marker,
810 top_margin: pb.top_margin,
811 bottom_margin: None,
812 left_margin: None,
813 right_margin: None,
814 tab_positions: vec![],
815 line_height: pb.line_height,
816 non_breakable_lines: pb.non_breakable_lines,
817 page_break_before: pb.page_break_before,
818 direction: pb.direction,
819 background_color: pb.background_color,
820 is_code_block: is_code_block.then_some(true),
821 code_language: if is_code_block {
822 pb.code_language
823 } else {
824 None
825 },
826 hyphenate: None,
827 language: None,
828 }
829}
830
831#[derive(Debug, Clone, Copy, PartialEq, Eq)]
833enum ParsedFrom {
834 SavedSyntax,
838 Html,
843}
844
845fn parsed_elements_to_fragment(parsed: Vec<ParsedElement>, from: ParsedFrom) -> DocumentFragment {
848 let mut blocks: Vec<FragmentBlock> = Vec::new();
849 let mut tables: Vec<FragmentTable> = Vec::new();
850 let mut quoting = FragmentQuoting::default();
852
853 for elem in parsed {
854 match elem {
855 ParsedElement::FootnoteDefinition { .. } => {}
860 ParsedElement::Block(pb) => {
867 let depth = pb.blockquote_depth;
868 let mut role = pb.semantic_role.clone();
870 let keep_code_block = pb.is_code_block && from == ParsedFrom::SavedSyntax;
871 let lines = if keep_code_block {
872 vec![pb]
873 } else {
874 split_block_at_line_breaks(pb)
875 };
876 for pb in lines {
877 blocks.push(parsed_block_to_fragment_block(pb, keep_code_block));
878 if depth > 0 {
879 quoting.set_block(blocks.len() - 1, depth, role.take());
880 }
881 }
882 }
883 ParsedElement::Table(pt) => {
884 let block_insert_index = blocks.len();
885 let num_columns = pt.column_count();
886 let num_rows = pt.rows.len();
887
888 let mut frag_cells: Vec<FragmentTableCell> = Vec::new();
889 for (row_idx, row) in pt.rows.iter().enumerate() {
890 for col_idx in 0..num_columns {
894 let spans = row
895 .get(col_idx)
896 .map_or(&[][..], |cell| cell.spans.as_slice());
897 let cell_elements: Vec<FragmentElement> =
898 spans.iter().map(span_to_fragment_element).collect();
899 let cell_text: String = spans_plain_text(spans);
900
901 frag_cells.push(FragmentTableCell {
902 row: row_idx,
903 column: col_idx,
904 row_span: 1,
905 column_span: 1,
906 blocks: vec![FragmentBlock {
907 plain_text: cell_text,
908 elements: cell_elements,
909 heading_level: None,
910 list: None,
911 alignment: None,
912 indent: None,
913 text_indent: None,
914 marker: None,
915 top_margin: None,
916 bottom_margin: None,
917 left_margin: None,
918 right_margin: None,
919 tab_positions: vec![],
920 line_height: None,
921 non_breakable_lines: None,
922 page_break_before: None,
923 direction: None,
924 background_color: None,
925 is_code_block: None,
926 code_language: None,
927 hyphenate: None,
928 language: None,
929 }],
930 fmt_padding: None,
931 fmt_border: None,
932 fmt_vertical_alignment: None,
933 fmt_background_color: None,
934 });
935 }
936 }
937
938 tables.push(FragmentTable {
939 rows: num_rows,
940 columns: num_columns,
941 cells: frag_cells,
942 block_insert_index,
943 fmt_border: None,
944 fmt_cell_spacing: None,
945 fmt_cell_padding: None,
946 fmt_width: None,
947 fmt_alignment: None,
948 column_widths: vec![],
949 });
950 quoting.set_table(tables.len() - 1, pt.blockquote_depth);
951 }
952 }
953 }
954
955 let plain_text = blocks
958 .iter()
959 .map(|b| b.plain_text.as_str())
960 .collect::<Vec<_>>()
961 .join("\n");
962 let data = FragmentData { blocks, tables };
963 let data = match from {
964 ParsedFrom::SavedSyntax => whole_text_to_json(&data, "ing),
965 ParsedFrom::Html => fragment_to_json(&data, "ing),
966 }
967 .expect("fragment serialization should not fail");
968
969 DocumentFragment { data, plain_text }
970}
971
972#[cfg(test)]
973mod tests {
974 use super::*;
975
976 fn paragraph(i: usize) -> String {
978 format!(
979 "Paragraph {i} with *emphasis* and a [link](https://example.org/{i}) in it, and a \
980 note[^n{i}] after the words that run on to a full line of prose."
981 )
982 }
983
984 #[test]
990 fn a_fragment_holds_little_more_than_its_text() {
991 let djot: String = (0..200).map(|i| format!("{}\n\n", paragraph(i))).collect();
992 let fragment = DocumentFragment::from_djot(&djot);
993 let ratio = fragment.raw_data().len() as f64 / djot.len() as f64;
994 println!("fragment JSON: {ratio:.2} times the Djot");
995 assert!(
996 ratio < 4.0,
997 "the fragment's JSON is {ratio:.1} times the Djot it was made from"
998 );
999 assert_eq!(fragment.to_plain_text().lines().count(), 200);
1000 }
1001
1002 #[test]
1005 fn a_fragment_written_in_full_still_reads() {
1006 let full = r#"{"blocks":[{"plain_text":"Old","elements":[{"content":{"Text":"Old"},
1007 "fmt_font_family":null,"fmt_font_point_size":null,"fmt_font_weight":null,
1008 "fmt_font_bold":true,"fmt_font_italic":null,"fmt_font_underline":null,
1009 "fmt_font_overline":null,"fmt_font_strikeout":null,"fmt_letter_spacing":null,
1010 "fmt_word_spacing":null,"fmt_anchor_href":null,"fmt_anchor_names":[],
1011 "fmt_is_anchor":null,"fmt_tooltip":null,"fmt_underline_style":null,
1012 "fmt_vertical_alignment":null}],"heading_level":2,"list":null,"alignment":null,
1013 "indent":null,"text_indent":null,"marker":null,"top_margin":null,
1014 "bottom_margin":null,"left_margin":null,"right_margin":null,"tab_positions":[],
1015 "line_height":null,"non_breakable_lines":null,"direction":null,
1016 "background_color":null,"is_code_block":null,"code_language":null}]}"#;
1017 let data: FragmentData = serde_json::from_str(full).unwrap();
1018 assert_eq!(data.blocks[0].heading_level, Some(2));
1019 assert_eq!(data.blocks[0].elements[0].fmt_font_bold, Some(true));
1020 let compact = serde_json::to_string(&data).unwrap();
1021 let again: FragmentData = serde_json::from_str(&compact).unwrap();
1022 assert_eq!(again.blocks[0].heading_level, Some(2));
1023 assert_eq!(again.blocks[0].elements[0].fmt_font_bold, Some(true));
1024 assert!(compact.len() < full.len() / 3, "{compact}");
1025 }
1026}