| 1 | //! Render HELP.md as formatted egui widgets.
|
| 2 |
|
| 3 | use eframe::egui;
|
| 4 | use pulldown_cmark::{Event, HeadingLevel, Options, Parser, Tag, TagEnd};
|
| 5 |
|
| 6 | pub fn show_help(ui: &mut egui::Ui, markdown: &str) {
|
| 7 | egui::ScrollArea::vertical().show(ui, |ui| {
|
| 8 | render_markdown(ui, markdown);
|
| 9 | });
|
| 10 | }
|
| 11 |
|
| 12 | fn render_markdown(ui: &mut egui::Ui, markdown: &str) {
|
| 13 | let mut opts = Options::empty();
|
| 14 | opts.insert(Options::ENABLE_TABLES);
|
| 15 | opts.insert(Options::ENABLE_STRIKETHROUGH);
|
| 16 |
|
| 17 | let parser = Parser::new_ext(markdown, opts);
|
| 18 |
|
| 19 | let mut in_code_block = false;
|
| 20 | let mut code_buf = String::new();
|
| 21 | let mut para_buf = String::new();
|
| 22 | let mut heading_buf = String::new();
|
| 23 | let mut heading_level = HeadingLevel::H1;
|
| 24 | let mut in_heading = false;
|
| 25 | let mut list_depth = 0usize;
|
| 26 | let mut list_item_buf = String::new();
|
| 27 | let mut in_list_item = false;
|
| 28 |
|
| 29 | let flush_para = |ui: &mut egui::Ui, buf: &mut String| {
|
| 30 | let text = buf.trim();
|
| 31 | if text.is_empty() {
|
| 32 | buf.clear();
|
| 33 | return;
|
| 34 | }
|
| 35 | ui.add(egui::Label::new(text).wrap().selectable(true));
|
| 36 | ui.add_space(4.0);
|
| 37 | buf.clear();
|
| 38 | };
|
| 39 |
|
| 40 | let flush_code = |ui: &mut egui::Ui, buf: &mut String| {
|
| 41 | if buf.is_empty() {
|
| 42 | return;
|
| 43 | }
|
| 44 | egui::Frame::new()
|
| 45 | .fill(ui.visuals().extreme_bg_color)
|
| 46 | .inner_margin(8.0)
|
| 47 | .show(ui, |ui| {
|
| 48 | ui.add(
|
| 49 | egui::Label::new(egui::RichText::new(buf.trim_end()).monospace())
|
| 50 | .wrap()
|
| 51 | .selectable(true),
|
| 52 | );
|
| 53 | });
|
| 54 | ui.add_space(6.0);
|
| 55 | buf.clear();
|
| 56 | };
|
| 57 |
|
| 58 | let flush_list_item = |ui: &mut egui::Ui, buf: &mut String, depth: usize| {
|
| 59 | let text = buf.trim();
|
| 60 | if text.is_empty() {
|
| 61 | buf.clear();
|
| 62 | return;
|
| 63 | }
|
| 64 | ui.horizontal(|ui| {
|
| 65 | ui.add_space((depth.saturating_sub(1)) as f32 * 12.0);
|
| 66 | ui.label("•");
|
| 67 | ui.add(egui::Label::new(text).wrap().selectable(true));
|
| 68 | });
|
| 69 | ui.add_space(2.0);
|
| 70 | buf.clear();
|
| 71 | };
|
| 72 |
|
| 73 | for event in parser {
|
| 74 | match event {
|
| 75 | Event::Start(Tag::Heading { level, .. }) => {
|
| 76 | flush_para(ui, &mut para_buf);
|
| 77 | in_heading = true;
|
| 78 | heading_level = level;
|
| 79 | heading_buf.clear();
|
| 80 | }
|
| 81 | Event::End(TagEnd::Heading(_)) => {
|
| 82 | let size = match heading_level {
|
| 83 | HeadingLevel::H1 => 22.0,
|
| 84 | HeadingLevel::H2 => 18.0,
|
| 85 | HeadingLevel::H3 => 16.0,
|
| 86 | HeadingLevel::H4 => 15.0,
|
| 87 | HeadingLevel::H5 => 14.0,
|
| 88 | HeadingLevel::H6 => 13.0,
|
| 89 | };
|
| 90 | ui.add_space(8.0);
|
| 91 | ui.label(
|
| 92 | egui::RichText::new(heading_buf.trim())
|
| 93 | .size(size)
|
| 94 | .strong(),
|
| 95 | );
|
| 96 | heading_buf.clear();
|
| 97 | in_heading = false;
|
| 98 | ui.add_space(6.0);
|
| 99 | }
|
| 100 | Event::Start(Tag::Paragraph) => {}
|
| 101 | Event::End(TagEnd::Paragraph) => {
|
| 102 | if in_list_item {
|
| 103 | flush_list_item(ui, &mut list_item_buf, list_depth);
|
| 104 | } else {
|
| 105 | flush_para(ui, &mut para_buf);
|
| 106 | }
|
| 107 | }
|
| 108 | Event::Start(Tag::List(_)) => {
|
| 109 | flush_para(ui, &mut para_buf);
|
| 110 | list_depth += 1;
|
| 111 | }
|
| 112 | Event::End(TagEnd::List(_)) => {
|
| 113 | if in_list_item {
|
| 114 | flush_list_item(ui, &mut list_item_buf, list_depth);
|
| 115 | in_list_item = false;
|
| 116 | }
|
| 117 | list_depth = list_depth.saturating_sub(1);
|
| 118 | }
|
| 119 | Event::Start(Tag::Item) => {
|
| 120 | in_list_item = true;
|
| 121 | list_item_buf.clear();
|
| 122 | }
|
| 123 | Event::End(TagEnd::Item) => {
|
| 124 | flush_list_item(ui, &mut list_item_buf, list_depth);
|
| 125 | in_list_item = false;
|
| 126 | }
|
| 127 | Event::Start(Tag::CodeBlock(_)) => {
|
| 128 | flush_para(ui, &mut para_buf);
|
| 129 | in_code_block = true;
|
| 130 | code_buf.clear();
|
| 131 | }
|
| 132 | Event::End(TagEnd::CodeBlock) => {
|
| 133 | flush_code(ui, &mut code_buf);
|
| 134 | in_code_block = false;
|
| 135 | }
|
| 136 | Event::Start(Tag::Link { dest_url, .. }) => {
|
| 137 | if !in_code_block && !in_heading {
|
| 138 | para_buf.push('[');
|
| 139 | }
|
| 140 | let _ = dest_url;
|
| 141 | }
|
| 142 | Event::End(TagEnd::Link) => {
|
| 143 | if !in_code_block && !in_heading {
|
| 144 | para_buf.push(']');
|
| 145 | }
|
| 146 | }
|
| 147 | Event::Code(text) => {
|
| 148 | if in_code_block {
|
| 149 | code_buf.push_str(&text);
|
| 150 | } else if in_heading {
|
| 151 | heading_buf.push_str(&text);
|
| 152 | } else if in_list_item {
|
| 153 | list_item_buf.push('`');
|
| 154 | list_item_buf.push_str(&text);
|
| 155 | list_item_buf.push('`');
|
| 156 | } else {
|
| 157 | para_buf.push('`');
|
| 158 | para_buf.push_str(&text);
|
| 159 | para_buf.push('`');
|
| 160 | }
|
| 161 | }
|
| 162 | Event::Text(text) => {
|
| 163 | if in_code_block {
|
| 164 | code_buf.push_str(&text);
|
| 165 | } else if in_heading {
|
| 166 | heading_buf.push_str(&text);
|
| 167 | } else if in_list_item {
|
| 168 | list_item_buf.push_str(&text);
|
| 169 | } else {
|
| 170 | para_buf.push_str(&text);
|
| 171 | }
|
| 172 | }
|
| 173 | Event::SoftBreak | Event::HardBreak => {
|
| 174 | if in_code_block {
|
| 175 | code_buf.push('\n');
|
| 176 | } else if in_list_item {
|
| 177 | list_item_buf.push(' ');
|
| 178 | } else {
|
| 179 | para_buf.push('\n');
|
| 180 | }
|
| 181 | }
|
| 182 | Event::Rule => {
|
| 183 | flush_para(ui, &mut para_buf);
|
| 184 | ui.separator();
|
| 185 | ui.add_space(4.0);
|
| 186 | }
|
| 187 | Event::Start(Tag::Strong) => {
|
| 188 | if !in_code_block {
|
| 189 | para_buf.push_str("**");
|
| 190 | }
|
| 191 | }
|
| 192 | Event::End(TagEnd::Strong) => {
|
| 193 | if !in_code_block {
|
| 194 | para_buf.push_str("**");
|
| 195 | }
|
| 196 | }
|
| 197 | Event::Start(Tag::Emphasis) => {
|
| 198 | if !in_code_block {
|
| 199 | para_buf.push('*');
|
| 200 | }
|
| 201 | }
|
| 202 | Event::End(TagEnd::Emphasis) => {
|
| 203 | if !in_code_block {
|
| 204 | para_buf.push('*');
|
| 205 | }
|
| 206 | }
|
| 207 | _ => {}
|
| 208 | }
|
| 209 | }
|
| 210 |
|
| 211 | if in_list_item {
|
| 212 | flush_list_item(ui, &mut list_item_buf, list_depth);
|
| 213 | }
|
| 214 | flush_code(ui, &mut code_buf);
|
| 215 | flush_para(ui, &mut para_buf);
|
| 216 | }
|