1use crate::{CharVerticalAlignment, InlineContent, LINE_SEPARATOR, 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 line_break = if block.heading_level.is_some() {
297 " "
298 } else {
299 "\\\n"
300 };
301 let inline_text = render_inline_markdown(&block.elements, line_break);
302 let is_list = block.list.is_some();
303
304 let indent_prefix = match block.indent {
305 Some(n) if n > 0 => " ".repeat(n as usize),
306 _ => String::new(),
307 };
308
309 if let Some(level) = block.heading_level {
310 let n = level.clamp(1, 6) as usize;
311 let prefix = "#".repeat(n);
312 parts.push((format!("{} {}", prefix, inline_text), false));
313 prev_was_list = false;
314 list_counter = 0;
315 } else if let Some(ref list) = block.list {
316 let is_ordered = is_ordered_list_style(&list.style);
317 if !prev_was_list {
318 list_counter = list_starts
320 .get(blk_idx)
321 .and_then(|start| u32::try_from(start.saturating_sub(1)).ok())
322 .unwrap_or(0);
323 }
324 if is_ordered {
325 list_counter += 1;
326 parts.push((
327 format!("{}{}. {}", indent_prefix, list_counter, inline_text),
328 true,
329 ));
330 } else {
331 parts.push((format!("{}- {}", indent_prefix, inline_text), true));
332 }
333 prev_was_list = true;
334 } else {
335 if indent_prefix.is_empty() {
336 parts.push((inline_text, false));
337 } else {
338 parts.push((format!("{}{}", indent_prefix, inline_text), false));
339 }
340 prev_was_list = false;
341 list_counter = 0;
342 }
343
344 if !is_list {
345 prev_was_list = false;
346 }
347 }
348
349 while table_cursor < sorted_tables.len() {
351 parts.push((render_table_markdown(sorted_tables[table_cursor]), false));
352 table_cursor += 1;
353 }
354
355 let mut result = String::new();
357 for (idx, (text, is_list)) in parts.iter().enumerate() {
358 if idx > 0 {
359 let (_, prev_is_list) = &parts[idx - 1];
360 if *prev_is_list && *is_list {
361 result.push('\n');
362 } else {
363 result.push_str("\n\n");
364 }
365 }
366 result.push_str(text);
367 }
368
369 result
370 }
371
372 pub fn is_empty(&self) -> bool {
374 self.plain_text.is_empty()
375 }
376
377 pub(crate) fn raw_data(&self) -> &str {
379 &self.data
380 }
381}
382
383impl Default for DocumentFragment {
384 fn default() -> Self {
385 Self::new()
386 }
387}
388
389fn is_ordered_list_style(style: &ListStyle) -> bool {
394 matches!(
395 style,
396 ListStyle::Decimal
397 | ListStyle::LowerAlpha
398 | ListStyle::UpperAlpha
399 | ListStyle::LowerRoman
400 | ListStyle::UpperRoman
401 )
402}
403
404fn escape_html(s: &str) -> String {
407 let mut out = String::with_capacity(s.len());
408 for c in s.chars() {
409 match c {
410 '&' => out.push_str("&"),
411 '<' => out.push_str("<"),
412 '>' => out.push_str(">"),
413 '"' => out.push_str("""),
414 '\'' => out.push_str("'"),
415 '\r' => out.push_str(" "),
420 _ => out.push(c),
421 }
422 }
423 out
424}
425
426fn block_style_attr(block: &FragmentBlock) -> String {
428 use crate::Alignment;
429
430 let mut parts = Vec::new();
431 if let Some(ref alignment) = block.alignment {
432 let value = match alignment {
433 Alignment::Left => "left",
434 Alignment::Right => "right",
435 Alignment::Center => "center",
436 Alignment::Justify => "justify",
437 };
438 parts.push(format!("text-align: {}", value));
439 }
440 if let Some(n) = block.indent
441 && n > 0
442 {
443 parts.push(format!("margin-left: {}em", n));
444 }
445 if let Some(px) = block.text_indent
446 && px != 0
447 {
448 parts.push(format!("text-indent: {}px", px));
449 }
450 if let Some(px) = block.top_margin {
451 parts.push(format!("margin-top: {}px", px));
452 }
453 if let Some(px) = block.bottom_margin {
454 parts.push(format!("margin-bottom: {}px", px));
455 }
456 if let Some(px) = block.left_margin {
457 parts.push(format!("margin-left: {}px", px));
458 }
459 if let Some(px) = block.right_margin {
460 parts.push(format!("margin-right: {}px", px));
461 }
462 parts.join("; ")
463}
464
465fn push_inline_html(out: &mut String, elements: &[FragmentElement]) {
466 for elem in elements {
467 let text = match &elem.content {
468 InlineContent::Text(t) => escape_html(t).replace(LINE_SEPARATOR, "<br>"),
469 InlineContent::FootnoteRef { label } => {
472 let id = escape_html(label);
473 out.push_str(&format!(
474 "<a epub:type=\"noteref\" role=\"doc-noteref\" href=\"#fn-{id}\"><sup>{id}</sup></a>"
475 ));
476 continue;
477 }
478 InlineContent::Image {
479 name,
480 alt,
481 width,
482 height,
483 ..
484 } => {
485 let mut tag = format!(
490 "<img src=\"{}\" alt=\"{}\"",
491 escape_html(name),
492 escape_html(alt)
493 );
494 if *width > 0 {
495 tag.push_str(&format!(" width=\"{width}\""));
496 }
497 if *height > 0 {
498 tag.push_str(&format!(" height=\"{height}\""));
499 }
500 tag.push('>');
501 tag
502 }
503 InlineContent::Empty => String::new(),
504 };
505
506 let is_monospace = elem
507 .fmt_font_family
508 .as_deref()
509 .is_some_and(|f| f == "monospace");
510 let is_bold = elem.fmt_font_bold.unwrap_or(false);
511 let is_italic = elem.fmt_font_italic.unwrap_or(false);
512 let is_underline = elem.fmt_font_underline.unwrap_or(false);
513 let is_strikeout = elem.fmt_font_strikeout.unwrap_or(false);
514 let is_anchor = elem.fmt_is_anchor.unwrap_or(false);
515 let vertical = elem.fmt_vertical_alignment.as_ref();
520
521 let mut result = text;
522
523 if is_monospace {
524 result = format!("<code>{}</code>", result);
525 }
526 if is_bold {
527 result = format!("<strong>{}</strong>", result);
528 }
529 if is_italic {
530 result = format!("<em>{}</em>", result);
531 }
532 if is_underline {
533 result = format!("<u>{}</u>", result);
534 }
535 if is_strikeout {
536 result = format!("<s>{}</s>", result);
537 }
538 match vertical {
539 Some(CharVerticalAlignment::SuperScript) => result = format!("<sup>{result}</sup>"),
540 Some(CharVerticalAlignment::SubScript) => result = format!("<sub>{result}</sub>"),
541 _ => {}
542 }
543 if is_anchor && let Some(ref href) = elem.fmt_anchor_href {
544 result = format!("<a href=\"{}\">{}</a>", escape_html(href), result);
545 }
546
547 out.push_str(&result);
548 }
549}
550
551fn push_table_html(out: &mut String, table: &FragmentTable) {
553 out.push_str("<table>");
554 for row in 0..table.rows {
555 out.push_str("<tr>");
556 for col in 0..table.columns {
557 if let Some(cell) = table.cells.iter().find(|c| c.row == row && c.column == col) {
558 out.push_str("<td");
559 if cell.row_span > 1 {
560 out.push_str(&format!(" rowspan=\"{}\"", cell.row_span));
561 }
562 if cell.column_span > 1 {
563 out.push_str(&format!(" colspan=\"{}\"", cell.column_span));
564 }
565 out.push('>');
566 for (i, block) in cell.blocks.iter().enumerate() {
567 if i > 0 {
568 out.push_str("<br>");
569 }
570 push_inline_html(out, &block.elements);
571 }
572 out.push_str("</td>");
573 }
574 }
576 out.push_str("</tr>");
577 }
578 out.push_str("</table>");
579}
580
581fn escape_markdown(s: &str) -> String {
584 let mut out = String::with_capacity(s.len());
585 for c in s.chars() {
586 if matches!(
587 c,
588 '\\' | '`'
589 | '*'
590 | '_'
591 | '{'
592 | '}'
593 | '['
594 | ']'
595 | '('
596 | ')'
597 | '#'
598 | '+'
599 | '-'
600 | '.'
601 | '!'
602 | '|'
603 | '~'
604 | '<'
605 | '>'
606 ) {
607 out.push('\\');
608 }
609 out.push(c);
610 }
611 out
612}
613
614fn render_inline_markdown(elements: &[FragmentElement], line_break: &str) -> String {
623 let mut trailing = 0usize;
625 for elem in elements.iter().rev() {
626 match &elem.content {
627 InlineContent::Text(t) => {
628 let kept = t.trim_end_matches(LINE_SEPARATOR);
629 trailing += t.len() - kept.len();
630 if !kept.is_empty() {
631 break;
632 }
633 }
634 InlineContent::Empty => {}
635 _ => break,
636 }
637 }
638 let mut to_drop = trailing;
639 let trimmed: Vec<FragmentElement> = elements
640 .iter()
641 .rev()
642 .map(|elem| match &elem.content {
643 InlineContent::Text(t) if to_drop > 0 => {
644 let keep = t.len().saturating_sub(to_drop);
645 to_drop -= t.len() - keep;
646 FragmentElement {
647 content: InlineContent::Text(t[..keep].to_string()),
648 ..elem.clone()
649 }
650 }
651 _ => elem.clone(),
652 })
653 .collect::<Vec<_>>()
654 .into_iter()
655 .rev()
656 .collect();
657 let mut out = String::new();
658 for elem in &trimmed {
659 let raw_text = match &elem.content {
660 InlineContent::Text(t) => t.clone(),
661 InlineContent::Image { name, alt, .. } => format!(""),
664 InlineContent::FootnoteRef { label } => format!("[^{label}]"),
665 InlineContent::Empty => String::new(),
666 };
667
668 let is_monospace = elem
669 .fmt_font_family
670 .as_deref()
671 .is_some_and(|f| f == "monospace");
672 let is_bold = elem.fmt_font_bold.unwrap_or(false);
673 let is_italic = elem.fmt_font_italic.unwrap_or(false);
674 let is_strikeout = elem.fmt_font_strikeout.unwrap_or(false);
675 let is_anchor = elem.fmt_is_anchor.unwrap_or(false);
676
677 if is_monospace {
678 let spans: Vec<String> = raw_text
680 .split(LINE_SEPARATOR)
681 .map(|line| {
682 if line.is_empty() {
683 String::new()
684 } else {
685 format!("`{line}`")
686 }
687 })
688 .collect();
689 out.push_str(&spans.join(line_break));
690 } else {
691 let mut text = escape_markdown(&raw_text).replace(LINE_SEPARATOR, line_break);
692 if is_bold && is_italic {
693 text = format!("***{}***", text);
694 } else if is_bold {
695 text = format!("**{}**", text);
696 } else if is_italic {
697 text = format!("*{}*", text);
698 }
699 if is_strikeout {
700 text = format!("~~{}~~", text);
701 }
702 if is_anchor {
703 let href = elem.fmt_anchor_href.as_deref().unwrap_or("");
704 out.push_str(&format!("[{}]({})", text, href));
705 } else {
706 out.push_str(&text);
707 }
708 }
709 }
710 out
711}
712
713fn render_table_markdown(table: &FragmentTable) -> String {
715 let mut rows: Vec<Vec<String>> = vec![vec![String::new(); table.columns]; table.rows];
716
717 for cell in &table.cells {
718 let text: String = cell
719 .blocks
720 .iter()
721 .map(|b| render_inline_markdown(&b.elements, " "))
722 .collect::<Vec<_>>()
723 .join(" ");
724 if cell.row < table.rows && cell.column < table.columns {
725 rows[cell.row][cell.column] = text;
726 }
727 }
728
729 let mut out = String::new();
730 for (i, row) in rows.iter().enumerate() {
731 out.push_str("| ");
732 out.push_str(&row.join(" | "));
733 out.push_str(" |");
734 if i == 0 {
735 out.push('\n');
737 out.push('|');
738 for _ in 0..table.columns {
739 out.push_str(" --- |");
740 }
741 }
742 if i + 1 < rows.len() {
743 out.push('\n');
744 }
745 }
746 out
747}
748
749fn spans_plain_text(spans: &[ParsedSpan]) -> String {
761 spans
762 .iter()
763 .map(|s| {
764 if s.image.is_some() || s.footnote_ref.is_some() {
769 "\u{FFFC}"
770 } else {
771 s.text.as_str()
772 }
773 })
774 .collect()
775}
776
777fn span_to_fragment_element(span: &ParsedSpan) -> FragmentElement {
778 let content = match (&span.footnote_ref, &span.image) {
787 (Some(label), _) => InlineContent::FootnoteRef {
788 label: label.clone(),
789 },
790 (None, Some(img)) => InlineContent::Image {
791 name: img.src.clone(),
792 alt: img.alt.clone(),
793 width: img.width,
794 height: img.height,
795 quality: 100,
798 },
799 (None, None) => InlineContent::Text(span.text.clone()),
800 };
801 let fmt_font_family = if span.code {
802 Some("monospace".into())
803 } else {
804 None
805 };
806 let fmt_font_bold = if span.bold { Some(true) } else { None };
807 let fmt_font_italic = if span.italic { Some(true) } else { None };
808 let fmt_font_underline = if span.underline { Some(true) } else { None };
809 let fmt_font_strikeout = if span.strikeout { Some(true) } else { None };
810 let (fmt_anchor_href, fmt_is_anchor) = if let Some(ref href) = span.link_href {
811 (Some(href.clone()), Some(true))
812 } else {
813 (None, None)
814 };
815 let fmt_vertical_alignment = if span.superscript {
821 Some(CharVerticalAlignment::SuperScript)
822 } else if span.subscript {
823 Some(CharVerticalAlignment::SubScript)
824 } else {
825 None
826 };
827
828 FragmentElement {
829 content,
830 fmt_font_family,
831 fmt_font_point_size: None,
832 fmt_font_weight: None,
833 fmt_font_bold,
834 fmt_font_italic,
835 fmt_font_underline,
836 fmt_font_overline: None,
837 fmt_font_strikeout,
838 fmt_letter_spacing: None,
839 fmt_word_spacing: None,
840 fmt_anchor_href,
841 fmt_anchor_names: vec![],
842 fmt_is_anchor,
843 fmt_tooltip: None,
844 fmt_underline_style: None,
845 fmt_vertical_alignment,
846 }
847}
848
849fn parsed_block_to_fragment_block(pb: ParsedBlock, keep_code_block: bool) -> FragmentBlock {
856 use frontend::common::parser_tools::fragment_schema::FragmentList;
857
858 let elements: Vec<FragmentElement> = pb.spans.iter().map(span_to_fragment_element).collect();
859 let plain_text: String = spans_plain_text(&pb.spans);
860 let list = pb.list_style.map(|style| FragmentList {
861 style,
862 indent: pb.list_indent as i64,
863 prefix: pb.list_prefix,
864 suffix: pb.list_suffix,
865 });
866 let is_code_block = keep_code_block && pb.is_code_block;
867 FragmentBlock {
868 plain_text,
869 elements,
870 heading_level: pb.heading_level,
871 list,
872 alignment: pb.alignment,
873 indent: None,
874 text_indent: pb.text_indent,
875 marker: pb.marker,
876 top_margin: pb.top_margin,
877 bottom_margin: None,
878 left_margin: None,
879 right_margin: None,
880 tab_positions: vec![],
881 line_height: pb.line_height,
882 non_breakable_lines: pb.non_breakable_lines,
883 page_break_before: pb.page_break_before,
884 direction: pb.direction,
885 background_color: pb.background_color,
886 is_code_block: is_code_block.then_some(true),
887 code_language: if is_code_block {
888 pb.code_language
889 } else {
890 None
891 },
892 hyphenate: None,
893 language: None,
894 }
895}
896
897#[derive(Debug, Clone, Copy, PartialEq, Eq)]
899enum ParsedFrom {
900 SavedSyntax,
904 Html,
909}
910
911fn parsed_elements_to_fragment(parsed: Vec<ParsedElement>, from: ParsedFrom) -> DocumentFragment {
914 let mut blocks: Vec<FragmentBlock> = Vec::new();
915 let mut tables: Vec<FragmentTable> = Vec::new();
916 let mut quoting = FragmentQuoting::default();
918 let mut list_starts = FragmentListStarts::default();
921
922 for elem in parsed {
923 match elem {
924 ParsedElement::FootnoteDefinition { .. } => {}
929 ParsedElement::Block(pb) => {
937 let depth = pb.blockquote_depth;
938 let mut opens = pb.blockquote_opens;
940 let mut role = pb.semantic_role.clone();
942 let keep_code_block = pb.is_code_block && from == ParsedFrom::SavedSyntax;
943 let lines = if keep_code_block {
944 vec![pb]
945 } else {
946 split_block_at_line_breaks(pb)
947 };
948 for pb in lines {
949 if let Some(start) = pb.list_start.filter(|_| pb.list_style.is_some()) {
950 list_starts.set(blocks.len(), start);
951 }
952 blocks.push(parsed_block_to_fragment_block(pb, keep_code_block));
953 if depth > 0 {
954 quoting.set_block(blocks.len() - 1, depth, role.take());
955 quoting.set_block_opens(blocks.len() - 1, std::mem::take(&mut opens));
956 }
957 }
958 }
959 ParsedElement::Table(pt) => {
960 let block_insert_index = blocks.len();
961 let num_columns = pt.column_count();
962 let num_rows = pt.rows.len();
963
964 let mut frag_cells: Vec<FragmentTableCell> = Vec::new();
965 for (row_idx, row) in pt.rows.iter().enumerate() {
966 for col_idx in 0..num_columns {
970 let spans = row
971 .get(col_idx)
972 .map_or(&[][..], |cell| cell.spans.as_slice());
973 let cell_elements: Vec<FragmentElement> =
974 spans.iter().map(span_to_fragment_element).collect();
975 let cell_text: String = spans_plain_text(spans);
976
977 frag_cells.push(FragmentTableCell {
978 row: row_idx,
979 column: col_idx,
980 row_span: 1,
981 column_span: 1,
982 blocks: vec![FragmentBlock {
983 plain_text: cell_text,
984 elements: cell_elements,
985 heading_level: None,
986 list: None,
987 alignment: None,
988 indent: None,
989 text_indent: None,
990 marker: None,
991 top_margin: None,
992 bottom_margin: None,
993 left_margin: None,
994 right_margin: None,
995 tab_positions: vec![],
996 line_height: None,
997 non_breakable_lines: None,
998 page_break_before: None,
999 direction: None,
1000 background_color: None,
1001 is_code_block: None,
1002 code_language: None,
1003 hyphenate: None,
1004 language: None,
1005 }],
1006 fmt_padding: None,
1007 fmt_border: None,
1008 fmt_vertical_alignment: None,
1009 fmt_background_color: None,
1010 });
1011 }
1012 }
1013
1014 tables.push(FragmentTable {
1015 rows: num_rows,
1016 columns: num_columns,
1017 cells: frag_cells,
1018 block_insert_index,
1019 fmt_border: None,
1020 fmt_cell_spacing: None,
1021 fmt_cell_padding: None,
1022 fmt_width: None,
1023 fmt_alignment: None,
1024 column_widths: vec![],
1025 });
1026 quoting.set_table(tables.len() - 1, pt.blockquote_depth);
1027 quoting.set_table_opens(tables.len() - 1, pt.blockquote_opens);
1028 }
1029 }
1030 }
1031
1032 let plain_text = blocks
1035 .iter()
1036 .map(|b| b.plain_text.as_str())
1037 .collect::<Vec<_>>()
1038 .join("\n");
1039 let data = FragmentData { blocks, tables };
1040 let data = fragment_with_list_starts_to_json(
1041 &data,
1042 "ing,
1043 &list_starts,
1044 from == ParsedFrom::SavedSyntax,
1045 )
1046 .expect("fragment serialization should not fail");
1047
1048 DocumentFragment { data, plain_text }
1049}
1050
1051#[cfg(test)]
1052mod tests {
1053 use super::*;
1054
1055 fn paragraph(i: usize) -> String {
1057 format!(
1058 "Paragraph {i} with *emphasis* and a [link](https://example.org/{i}) in it, and a \
1059 note[^n{i}] after the words that run on to a full line of prose."
1060 )
1061 }
1062
1063 #[test]
1069 fn a_fragment_holds_little_more_than_its_text() {
1070 let djot: String = (0..200).map(|i| format!("{}\n\n", paragraph(i))).collect();
1071 let fragment = DocumentFragment::from_djot(&djot);
1072 let ratio = fragment.raw_data().len() as f64 / djot.len() as f64;
1073 println!("fragment JSON: {ratio:.2} times the Djot");
1074 assert!(
1075 ratio < 4.0,
1076 "the fragment's JSON is {ratio:.1} times the Djot it was made from"
1077 );
1078 assert_eq!(fragment.to_plain_text().lines().count(), 200);
1079 }
1080
1081 #[test]
1084 fn a_fragment_written_in_full_still_reads() {
1085 let full = r#"{"blocks":[{"plain_text":"Old","elements":[{"content":{"Text":"Old"},
1086 "fmt_font_family":null,"fmt_font_point_size":null,"fmt_font_weight":null,
1087 "fmt_font_bold":true,"fmt_font_italic":null,"fmt_font_underline":null,
1088 "fmt_font_overline":null,"fmt_font_strikeout":null,"fmt_letter_spacing":null,
1089 "fmt_word_spacing":null,"fmt_anchor_href":null,"fmt_anchor_names":[],
1090 "fmt_is_anchor":null,"fmt_tooltip":null,"fmt_underline_style":null,
1091 "fmt_vertical_alignment":null}],"heading_level":2,"list":null,"alignment":null,
1092 "indent":null,"text_indent":null,"marker":null,"top_margin":null,
1093 "bottom_margin":null,"left_margin":null,"right_margin":null,"tab_positions":[],
1094 "line_height":null,"non_breakable_lines":null,"direction":null,
1095 "background_color":null,"is_code_block":null,"code_language":null}]}"#;
1096 let data: FragmentData = serde_json::from_str(full).unwrap();
1097 assert_eq!(data.blocks[0].heading_level, Some(2));
1098 assert_eq!(data.blocks[0].elements[0].fmt_font_bold, Some(true));
1099 let compact = serde_json::to_string(&data).unwrap();
1100 let again: FragmentData = serde_json::from_str(&compact).unwrap();
1101 assert_eq!(again.blocks[0].heading_level, Some(2));
1102 assert_eq!(again.blocks[0].elements[0].fmt_font_bold, Some(true));
1103 assert!(compact.len() < full.len() / 3, "{compact}");
1104 }
1105}