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