JFLib

ui/md_doc.rs#

Source: lib/ui/md_doc.rs

md_doc -- the document of the Markdown / HTML viewer (md_view.rs): blocks of formatted text,
read from Markdown (CommonMark with GitHub's tables, task lists, strikethrough and bare
links) or from HTML (the reader of the rich-text editor, rich_html.rs)

  MdDoc::markdown(s) / MdDoc::html(s)  parse; never panics, takes linear time: nesting, input
                                       size and the inline parser's work are capped
  doc.blocks() -> &[MdBlock]           the blocks in reading order (see MdBlock)
  doc.links() / images() / tables()    link targets (MdSpan::link - 1), images (MdSpan::image
                                       - 1: src, alt, title, size), table column alignments
  doc.plain() / doc.to_html()          everything as text (list markers, cells by tabs) / HTML
  doc.plain_range(a, b) / html_range(a, b)   a part: TextPos { line: block, byte }
  doc.link_spots()                     every link in reading order (block, bytes, link)
  doc.forms() / fields() / field_spots()   the forms of HTML, their fields (MdSpan::field - 1;
                                       hidden ones have no character; their first values:
                                       md_view keeps the current ones) and where the visible
                                       ones stand in reading order; field.caption(): a
                                       button's caption
  doc.truncated()                      a limit cut the input
  HTML documents keep their element tree (md_dom.rs: queries, changes of text, attributes,
  classes and structure; MdNode handles that stay the same across changes); the blocks,
  fields and forms name their elements (MdBlock::node, MdField::node, MdForm::node).
  doc.update() -> the blocks were derived again after changes: in place when every change
                                       lies inside block elements that show one paragraph of
                                       text by themselves (no links, images, fields, blocks in
                                       it: O(that paragraph)), else all of them (linear, no
                                       layout); md_view.rs keeps its place, selection, focus,
                                       the fields' values and the layouts that still hold
  doc.field_of(n) -> the field of element n
  doc.boxes()                          the boxes of CSS (style sheets, rich_html.rs): a block
                                       element's margins, padding, borders and background
                                       around its blocks (MdBlock::boxes, outermost first)

The model: a block is a paragraph, a heading (level 1..6), a code block (its text, the
language of the fence), a rule or a table row (cells: byte ranges of the text, '\t' between
them).  Its place: `quote` (block quotes around it), `list` (lists around it), `marker` /
`number` / `task` (it starts a list item), `space` (the gap before it: 0 none, 1 a tight
list's, 2 a paragraph's).  Its text holds the visible characters (an image is one U+FFFC,
a hard break '\n'); spans give ranges their style (MS_*), link, image and colours.

Markdown: ATX and setext headings, paragraphs with lazy continuation lines, block quotes,
bullet (- + *) and ordered (1. 1)) lists with start numbers, tight / loose spacing and task
items ([ ] / [x]), fenced (``` ~~~ with an info string) and indented code, thematic breaks,
GFM tables (alignment row, escaped pipes), HTML blocks (through the HTML reader), link
reference definitions; inline: backslash escapes, entities, code spans, emphasis and strong
emphasis (the delimiter algorithm with the rule of three), ~~strikethrough~~, links and
images (inline, full / collapsed / shortcut references, titles), autolinks (<...>, bare
http:// https:// www.), hard breaks (two spaces, backslash) and soft breaks, a little inline
HTML (br, b / strong, i / em, s / del, u / ins, code / kbd, a href, img).  Other tags are
dropped, their text stays.  Tabs expand to 4 columns.

Forms (HTML, also in Markdown's HTML blocks): <form action method> (forms do not nest),
<input> of type text (and the other text types: email, search, url, tel, number ...),
password, checkbox, radio, hidden, submit, image (a submit button), reset and button,
<textarea> (its text read raw, one leading line end dropped), <select> with <option value
selected> (one choice), <button type> (its text the caption), <label> (around a field or
for= its id: the field's name; aria-label wins, title is the last resort) and form= (a field
outside its form).  A field is one U+FFFC in the text with a span of its own (MdSpan::field);
a disabled check box at the start of a list item is that item's task box (as Markdown's
"[x]" is written out by html_range).
/// MdBlock::kind: a paragraph
MB_PARA: u8 = 0
/// a heading (MdBlock::level 1 ..= 6)
MB_HEADING: u8 = 1
/// a code block (MdBlock::lang: the fence's language)
MB_CODE: u8 = 2
/// a thematic break (no text)
MB_RULE: u8 = 3
/// a table row (MdBlock::cells, table, head)
MB_ROW: u8 = 4
/// MdSpan::style: strong
MS_BOLD: u16 = 1
/// emphasis
MS_ITALIC: u16 = 2
/// struck through
MS_STRIKE: u16 = 4
/// code (monospace)
MS_CODE: u16 = 8
/// underlined (HTML)
MS_UNDER: u16 = 16
/// MdBlock::marker: no list item starts here
MK_NONE: u8 = 0
/// a bullet item
MK_BULLET: u8 = 1
/// a numbered item (MdBlock::number, delim)
MK_NUMBER: u8 = 2
/// MdTable::aligns: not given
MA_NONE: u8 = 0
/// left (start)
MA_LEFT: u8 = 1
/// centred
MA_CENTER: u8 = 2
/// right (end)
MA_RIGHT: u8 = 3
/// MdField::kind: a one-line text field (<input> of type text, email, search, url, tel,
/// number and every type not named below)
MF_TEXT: u8 = 0
/// a password field (its text never leaves the view but in the form's data)
MF_PASSWORD: u8 = 1
/// a check box
MF_CHECKBOX: u8 = 2
/// a radio button: one of the radios of its form with its name
MF_RADIO: u8 = 3
/// a hidden field (no character in the text)
MF_HIDDEN: u8 = 4
/// a text area (<textarea>: several lines)
MF_TEXTAREA: u8 = 5
/// a drop-down list (<select>)
MF_SELECT: u8 = 6
/// a submit button (<button>, <input type=submit> / image)
MF_SUBMIT: u8 = 7
/// a reset button: the form's fields get their first values back
MF_RESET: u8 = 8
/// a button without an action of its own (<button type=button>, <input type=button>)
MF_BUTTON: u8 = 9
  • struct MdSpan a range of a block's text with its style

    • a: u32 -- bytes a .. b of the block's text
    • b: u32
    • style: u16 -- MS_*
    • link: u32 -- a link: index + 1 into MdDoc::links (0: none)
    • image: u32 -- an image (the text is U+FFFC): index + 1 into MdDoc::images (0: none)
    • color: u32 -- HTML colours 0xAARRGGBB (0: the theme's)
    • bg: u32
    • field: u32 -- a form field (the text is U+FFFC): index + 1 into MdDoc::fields (0: none)
  • struct MdBlock a block of the document (see the module header)

    • kind: u8 -- MB_*
    • level: u8 -- headings: 1 ..= 6
    • text: String -- the visible text (U+FFFC an image, '\n' a hard break or a code line's end, '\t' between cells)
    • spans: Vec<MdSpan> -- styled ranges, ascending, not overlapping (the text between them is plain)
    • quote: u8 -- block quotes around it
    • list: u8 -- lists around it (0: none)
    • path: u32 -- the containers from the outside in: bit i set = the i-th is a block quote, clear = a list
    • marker: u8 -- MK_*: a list item starts with this block
    • number: u32 -- MK_NUMBER: the item's number and its delimiter ('.' or ')')
    • delim: u8
    • task: u8 -- a task item: 0 none, 1 open, 2 done
    • space: u8 -- the gap before it: 0 none, 1 a tight list's, 2 a paragraph's
    • lang: String -- code blocks: the language of the fence ("" none)
    • cells: Vec<(u32, u32)> -- table rows: the cells' byte ranges
    • table: u32 -- table rows: index into MdDoc::tables, a header row
    • head: bool
    • align: u8 -- paragraphs: 0 start, 1 centre, 2 end (HTML)
    • node: MdNode -- HTML: the element whose content it shows (the innermost block element: p, li, h1, the row of a cell ...; MdNode::NONE: none, Markdown)
    • boxes: Vec<u32> -- HTML: the boxes of CSS around it (indices of MdDoc::boxes, outermost first)
  • struct MdBox a box of CSS around blocks: what a block element's style gives (MdBlock::boxes); lengths in px at 100 % text, the sides top, right, bottom, left

    • margin: [i16; 4]
    • padding: [i16; 4]
    • border: [u8; 4] -- the borders' widths (a style none: 0; every visible style is drawn solid) and colours (0xAARRGGBB; 0: the text's)
    • border_color: [u32; 4]
    • bg: u32 -- the background (0: none)
    • mset: u8 -- the margins CSS gave (bit 1 << side): at the top and bottom they take the place of the gap between blocks
    • depth: u8 -- the block quotes and lists around its element (its place among their indents)
    • node: MdNode -- its element
  • struct MdImage an image of the document

    • src: String
    • alt: String
    • title: String
    • w: u32 -- size given by HTML attributes (px, 0: the picture's)
    • h: u32
  • struct MdTable a table: its columns' alignment (MA_*)

    • aligns: Vec<u8>
  • struct MdLinkSpot a link in reading order: block, bytes a .. b of its text, the link (index + 1)

    • block: usize
    • a: u32
    • b: u32
    • link: u32
  • struct MdForm a form of HTML

    • action: String -- where its data goes (as written: the app resolves it) and how ("get" / "post": lower case, "get" when not given)
    • method: String
    • id: String
    • name: String
    • node: MdNode -- its <form> element (an HTML document)
  • struct MdOption a choice of a drop-down list: the value sent, the text shown

    • value: String
    • label: String
  • struct MdField a field of a form (its first value: the view keeps the current one, MdViewState)

    • kind: u8 -- MF_*
    • form: u32 -- its form: index + 1 into MdDoc::forms (0: none -- it sends nothing)
    • name: String
    • id: String
    • value: String -- text fields: the text; check boxes, radios, buttons: the value sent ("on" for check boxes and radios without one)
    • label: String -- the name screen readers say (aria-label, the <label>, title); buttons: the caption
    • placeholder: String
    • checked: bool -- check boxes and radios: checked at first
    • options: Vec<MdOption> -- drop-down lists: the choices, the one chosen at first (-1: none)
    • selected: i32
    • size: u32 -- text fields: the width in characters (size; text areas: cols; 0: the default), text areas: the lines (rows)
    • rows: u32
    • maxlength: u32 -- at most this many characters (0: no limit)
    • disabled: bool
    • readonly: bool
    • node: MdNode -- its element (<input>, <select> ... of an HTML document; the view keeps its value while the element stays)

impl MdField

  • fn caption(&self) -> &str a button's caption: its own, else "Senden" / "Zurücksetzen" (submit / reset, in the UI language); "" for the other kinds

  • struct MdFieldSpot a visible field in reading order: block, byte of its U+FFFC, the field (index + 1)

    • block: usize
    • a: u32
    • field: u32
  • struct MdDoc the document (see the module header)

impl MdDoc

  • fn markdown(s: &str) -> MdDoc a document from Markdown (see the module header)
  • fn html(s: &str) -> MdDoc a document from HTML: what the rich-text editor reads (paragraphs, headings h1 .. h6, lists, block quotes, formats, links, colours) and rules, images, preformatted blocks (<pre><code class="language-x">), tables and forms (see the module header)
  • fn update(&mut self) -> bool derive the blocks (fields, forms, links ...) again after changes of the element tree (md_dom.rs; the view does it every frame) -> there were changes. In place when every change lies inside block elements that show one paragraph of plain formatted text by themselves (O(those paragraphs)), else all of them (linear in the document).
  • fn set_sheet(&mut self, href: &str, css: Option<&str>) the text of a linked style sheet (None: it could not be had, its rules go): it counts where its <link> stands among the <style> elements, from the next update on (a text for an address no link names is kept for a link that comes later)
  • fn style_of(&self, n: MdNode) -> String what the cascade gives an element ("property:value;" of the rules that match and its style attribute, inherited properties not among them); empty for none, Markdown or a gone node
  • fn boxes(&self) -> &[MdBox] the boxes of CSS (MdBlock::boxes)
  • fn field_of(&self, n: MdNode) -> Option<usize> the field whose element is n (an index of fields())
  • fn blocks(&self) -> &[MdBlock] the blocks in reading order
  • fn images(&self) -> &[MdImage] the images (MdSpan::image - 1)
  • fn tables(&self) -> &[MdTable] the tables (MdBlock::table)
  • fn forms(&self) -> &[MdForm] the forms (MdField::form - 1)
  • fn fields(&self) -> &[MdField] the fields of the forms in document order (MdSpan::field - 1)
  • fn field_spots(&self) -> &[MdFieldSpot] every visible field in reading order
  • fn truncated(&self) -> bool a limit cut the input (size, blocks)
  • fn end(&self) -> TextPos the place after the last character
  • fn plain(&self) -> String the whole document as text
  • fn to_html(&self) -> String the whole document as HTML
  • fn plain_range(&self, a: TextPos, b: TextPos) -> String the text of a .. b: blocks on lines of their own (a blank line before paragraphs), list markers ("- ", "1. ", "[x] "), cells by tabs, images as their alt texts, buttons as their captions (the other fields: nothing)
  • fn html_range(&self, a: TextPos, b: TextPos) -> String the HTML of a .. b: h1 .. h6, p, ul / ol / li (task items with a disabled check box), blockquote, pre / code (class language-x), hr, table / tr / th / td (align), strong, em, del, u, code, a href, img src alt, colours as span styles; of forms only the buttons' captions (as text)

Source: docs/API.md in the JFLib repository.