| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use loro::LoroDoc; |
| 6 | use note_model::flat; |
| 7 | use note_model::marks::Marks; |
| 8 | use note_model::write::{Block, Inline}; |
| 9 | |
| 10 | use super::model::{Blk, Model}; |
| 11 | use super::pos::{Pos, Selection}; |
| 12 | |
| 13 | pub fn t(s: &str) -> Inline { |
| 14 | Inline::Text(s.into(), Marks::default()) |
| 15 | } |
| 16 | pub fn strong(s: &str) -> Inline { |
| 17 | Inline::Text(s.into(), Marks { strong: true, ..Marks::default() }) |
| 18 | } |
| 19 | pub fn br() -> Inline { |
| 20 | Inline::HardBreak |
| 21 | } |
| 22 | pub fn p(s: &str) -> Block { |
| 23 | Block::Paragraph(if s.is_empty() { Vec::new() } else { vec![t(s)] }) |
| 24 | } |
| 25 | pub fn pi(v: Vec<Inline>) -> Block { |
| 26 | Block::Paragraph(v) |
| 27 | } |
| 28 | pub fn h(n: i64, s: &str) -> Block { |
| 29 | Block::Heading(n, vec![t(s)]) |
| 30 | } |
| 31 | pub fn hr() -> Block { |
| 32 | Block::Rule |
| 33 | } |
| 34 | pub fn quote(v: Vec<Block>) -> Block { |
| 35 | Block::Quote(v) |
| 36 | } |
| 37 | pub fn ul(items: Vec<Vec<Block>>) -> Block { |
| 38 | Block::List { ordered: false, start: 1, items } |
| 39 | } |
| 40 | |
| 41 | pub fn build(blocks: Vec<Block>) -> Model { |
| 42 | let doc = LoroDoc::new(); |
| 43 | super::marks::configure(&doc); |
| 44 | let text = flat::body(&doc); |
| 45 | if blocks.is_empty() { |
| 46 | flat::init(&doc).unwrap(); |
| 47 | } else { |
| 48 | flat::write(&text, 0, &flat::flatten(&blocks), false).unwrap(); |
| 49 | } |
| 50 | doc.commit(); |
| 51 | Model::new(doc) |
| 52 | } |
| 53 | |
| 54 | pub fn dump(m: &Model) -> String { |
| 55 | m.check(); |
| 56 | flat::doc_blocks(&m.doc).iter().map(block).collect::<Vec<_>>().join(" ") |
| 57 | } |
| 58 | |
| 59 | fn block(b: &Block) -> String { |
| 60 | match b { |
| 61 | Block::Paragraph(v) => format!("p({})", inline(v)), |
| 62 | Block::Heading(n, v) => format!("h{n}({})", inline(v)), |
| 63 | Block::Code { text, .. } => format!("code({text})"), |
| 64 | Block::Rule => "hr".into(), |
| 65 | Block::Quote(v) => format!("quote({})", v.iter().map(block).collect::<Vec<_>>().join(" ")), |
| 66 | Block::List { ordered, items, .. } => format!( |
| 67 | "{}({})", |
| 68 | if *ordered { "ol" } else { "ul" }, |
| 69 | items.iter().map(|i| format!("li({})", i.iter().map(block).collect::<Vec<_>>().join(" "))).collect::<Vec<_>>().join(" ") |
| 70 | ), |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | fn inline(v: &[Inline]) -> String { |
| 75 | let mut s = String::new(); |
| 76 | for i in v { |
| 77 | match i { |
| 78 | Inline::Text(t, m) => { |
| 79 | let mut x = t.clone(); |
| 80 | if m.code { |
| 81 | x = format!("{{c:{x}}}"); |
| 82 | } |
| 83 | if m.strong { |
| 84 | x = format!("{{s:{x}}}"); |
| 85 | } |
| 86 | if m.em { |
| 87 | x = format!("{{e:{x}}}"); |
| 88 | } |
| 89 | if let Some((href, _)) = &m.link { |
| 90 | x = format!("{{l={href}:{x}}}"); |
| 91 | } |
| 92 | s.push_str(&x); |
| 93 | } |
| 94 | Inline::HardBreak => s.push('|'), |
| 95 | Inline::Image { .. } => s.push_str("[img]"), |
| 96 | } |
| 97 | } |
| 98 | s |
| 99 | } |
| 100 | |
| 101 | |
| 102 | pub fn tb(m: &Model, n: usize) -> Blk { |
| 103 | let all = m.blocks(0, m.len()); |
| 104 | all.into_iter().filter(|b| b.kind().is_text()).nth(n).expect("text block") |
| 105 | } |
| 106 | |
| 107 | pub fn at(m: &Model, n: usize, off: usize) -> Selection { |
| 108 | Selection::caret(tb(m, n).start + off) |
| 109 | } |
| 110 | |
| 111 | pub fn pos(m: &Model, n: usize, off: usize) -> Pos { |
| 112 | tb(m, n).start + off |
| 113 | } |