rat_markdown/
lib.rs

1#![doc = include_str!("../readme.md")]
2
3use crate::op::{md_backtab, md_line_break, md_make_header, md_strong, md_surround, md_tab};
4use rat_event::{ct_event, flow, HandleEvent, Regular};
5use rat_focus::HasFocus;
6use rat_text::event::TextOutcome;
7use rat_text::text_area::TextAreaState;
8
9pub mod dump;
10mod format;
11mod operations;
12pub mod parser;
13pub mod styles;
14mod util;
15
16pub mod op {
17    pub use crate::format::{md_format, reformat};
18    pub use crate::operations::{
19        md_backtab, md_line_break, md_make_header, md_strong, md_surround, md_tab,
20    };
21}
22
23/// Event qualifier.
24#[derive(Debug)]
25pub struct MarkDown {
26    text_width: u16,
27}
28
29impl Default for MarkDown {
30    fn default() -> Self {
31        Self { text_width: 65 }
32    }
33}
34
35impl MarkDown {
36    pub fn new(text_width: u16) -> Self {
37        Self { text_width }
38    }
39
40    pub fn text_width(mut self, width: u16) -> Self {
41        self.text_width = width;
42        self
43    }
44}
45
46impl HandleEvent<crossterm::event::Event, MarkDown, TextOutcome> for TextAreaState {
47    fn handle(&mut self, event: &crossterm::event::Event, _qualifier: MarkDown) -> TextOutcome {
48        if self.is_focused() {
49            flow!(match event {
50                ct_event!(key press ALT-'1') => md_make_header(self, 1),
51                ct_event!(key press ALT-'2') => md_make_header(self, 2),
52                ct_event!(key press ALT-'3') => md_make_header(self, 3),
53                ct_event!(key press ALT-'4') => md_make_header(self, 4),
54                ct_event!(key press ALT-'5') => md_make_header(self, 5),
55                ct_event!(key press ALT-'6') => md_make_header(self, 6),
56
57                ct_event!(key press ANY-'*') => md_strong(self, '*'),
58                ct_event!(key press ANY-'_') => md_strong(self, '_'),
59                ct_event!(key press ANY-'~') => md_strong(self, '~'),
60                ct_event!(key press ALT-'c') => md_surround(self, "```\n", None, "\n```", Some(0)),
61                ct_event!(key press ALT-'i') => md_surround(self, "![", None, "]()", Some(2)),
62                ct_event!(key press ALT-'l') => md_surround(self, "[", None, "]()", Some(2)),
63                ct_event!(key press ALT-'k') => md_surround(self, "[", None, "][]", Some(2)),
64                ct_event!(key press ALT-'r') => md_surround(self, "[", Some(1), "]: ", None),
65                ct_event!(key press ALT-'f') => md_surround(self, "[^1]", Some(4), "", None),
66
67                ct_event!(keycode press Enter) => md_line_break(self),
68                ct_event!(keycode press Tab) => md_tab(self),
69                ct_event!(keycode press SHIFT-BackTab) => md_backtab(self),
70                _ => TextOutcome::Continue,
71            });
72        }
73
74        self.handle(event, Regular)
75    }
76}