//! WRITING — one pure rule for a Markdown text being written: what each //! line is, how each run of it is styled, and what each formatting button //! does to the text and the selection. Cotton Notes draws from it; any other //! Cotton app that writes (Journal, Story's notes) can draw from it as is. //! The phones never parse Markdown themselves. //! //! **Positions are UTF-16 code units**, in and out: the unit both phones' //! strings count in (`NSString`, Kotlin `String`). Inside, the rule works on //! byte offsets and converts at the edge ([`Offsets`]). //! //! - [`style`]: a text and a cursor → its blocks (one per line) and its //! spans. A syntax mark is `dim` while the cursor is in what it marks and //! `hidden` away from it; a list marker, a checkbox and a divider are drawn //! as a `glyph`. //! - [`format`]: a text, a selection and a button → the new text, the one //! replacement that makes it (so the phone's own undo keeps it) and the new //! selection. Return is a button too: it continues a list, and ends one on //! an empty item. //! - [`table`]: pipe tables as rows and cells, edited by row, column and cell. //! - [`derive`]: a note's title, preview and hashtags, never stored by hand. //! - [`import`]: Markdown files, TextBundles and TextPacks → notes. //! - [`links`]: `[[note links]]`, the one being typed, and the rewrite on a rename. //! - [`folds`]: where headings and nested lists fold; a note's contents. //! - [`find`]: words found, any case, accents ignored. //! - [`search`]: the list's search words (`#tag`, `-word`, `@todo`, …). //! - [`icons`]: the tag icon catalogue, an icon picked by a tag's word. //! //! Markdown this rule does not know (footnotes, math) is a paragraph //! drawn as written: nothing is ever rewritten that a button did not touch. pub mod derive; pub mod find; pub mod folds; pub mod format; pub mod icons; pub mod import; pub mod inline; pub mod links; pub mod merge; pub mod search; pub mod style; pub mod table; #[cfg(test)] mod tests; #[cfg(test)] mod bear_tests; use serde::{Deserialize, Serialize}; /// A selection in UTF-16 code units; a cursor is `start == end`. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)] pub struct Selection { pub start: u32, pub end: u32, } impl Selection { pub fn cursor(at: u32) -> Self { Selection { start: at, end: at } } } /// Byte offsets ↔ UTF-16 offsets for one text. pub struct Offsets { ascii: bool, /// For every byte offset (and the end), its UTF-16 offset; empty when the /// text is ASCII (the two are the same). to16: Vec, } impl Offsets { pub fn new(text: &str) -> Self { if text.is_ascii() { return Offsets { ascii: true, to16: Vec::new() }; } let mut to16 = vec![0u32; text.len() + 1]; let mut unit = 0u32; for (at, ch) in text.char_indices() { for slot in to16.iter_mut().skip(at).take(ch.len_utf8()) { *slot = unit; } unit += ch.len_utf16() as u32; } to16[text.len()] = unit; Offsets { ascii: false, to16 } } /// The UTF-16 offset of a byte offset. pub fn u16(&self, byte: usize) -> u32 { if self.ascii { byte as u32 } else { self.to16[byte.min(self.to16.len() - 1)] } } } /// The byte offset of a UTF-16 offset, clamped to the text and to a char /// boundary (an offset inside a surrogate pair lands before the pair). pub fn byte_at(text: &str, unit: u32) -> usize { if text.is_ascii() { return (unit as usize).min(text.len()); } let mut seen = 0u32; for (at, ch) in text.char_indices() { let next = seen + ch.len_utf16() as u32; if next > unit { return at; } seen = next; } text.len() } /// The UTF-16 length of a text. pub fn len16(text: &str) -> u32 { text.encode_utf16().count() as u32 } /// The byte range of every line, without its `\n`. pub(crate) fn line_ranges(text: &str) -> Vec<(usize, usize)> { let mut out = Vec::new(); let mut start = 0; for (at, byte) in text.bytes().enumerate() { if byte == b'\n' { out.push((start, at)); start = at + 1; } } out.push((start, text.len())); out }