| 1 | //! todo-md parsing and kanban rendering. |
| 2 | //! |
| 3 | //! Implements the todo-md spec (the `todo-md` repo). A `TODO.md` is split into |
| 4 | //! sections by the shallowest heading level used (the *column level*): |
| 5 | //! |
| 6 | //! - **Sections** — headings at the column level (e.g. every `#`). They become |
| 7 | //! kanban columns. A heading containing `[x]` marks a done section; every |
| 8 | //! task in it counts as done. |
| 9 | //! - **Tasks** come in two forms inside a section: |
| 10 | //! - **checkbox tasks** — `- [ ]` / `- [x]` list items; lines indented under |
| 11 | //! them are their details (opaque markdown). |
| 12 | //! - **tickets** — a heading nested *deeper* than the column level (e.g. a |
| 13 | //! `## Implement Navbar` under `# Done`). The heading is the ticket title |
| 14 | //! and everything beneath it, until the next heading, is its detail body. |
| 15 | //! A ticket is done if its section is done or its own heading carries |
| 16 | //! `[x]`. |
| 17 | //! - Prose-only sections render as ordinary markdown below the board. |
| 18 | //! |
| 19 | //! This module is the first "custom renderer" for a well-known filename; the |
| 20 | //! filename → renderer match in `ui::blob` is the registry that routes here. |
| 21 | |
| 22 | use maud::{ |
| 23 | Markup, |
| 24 | html, |
| 25 | }; |
| 26 | |
| 27 | use crate::ui::render_markdown; |
| 28 | |
| 29 | pub struct Section { |
| 30 | /// Heading level (1–6); 0 for the implicit preamble section. |
| 31 | pub level: u8, |
| 32 | /// Heading text with any `[x]` / `✓` done-marker stripped. |
| 33 | pub title: String, |
| 34 | /// Done section: tasks in it count as done regardless of their checkbox. |
| 35 | pub done: bool, |
| 36 | pub tasks: Vec<Task>, |
| 37 | /// Non-task content of the section, verbatim (used for the notes area). |
| 38 | pub prose: String, |
| 39 | } |
| 40 | |
| 41 | pub struct Task { |
| 42 | pub done: bool, |
| 43 | pub title: String, |
| 44 | /// Raw markdown block: a checkbox task's indented lines (dedented one |
| 45 | /// level) or a ticket's body beneath its heading. |
| 46 | pub details: String, |
| 47 | /// True when the task came from a nested heading (a ticket) rather than a |
| 48 | /// `- [ ]` checkbox line. |
| 49 | pub ticket: bool, |
| 50 | } |
| 51 | |
| 52 | /// Where subsequent non-structural lines accumulate. |
| 53 | #[derive(Clone, Copy, Eq, PartialEq)] |
| 54 | enum Mode { |
| 55 | /// Section prose (no open task). |
| 56 | Prose, |
| 57 | /// Indented detail lines of a checkbox task (dedented one level). |
| 58 | TaskDetail, |
| 59 | /// Body of a ticket — runs until the next heading; verbatim. |
| 60 | TicketBody, |
| 61 | } |
| 62 | |
| 63 | /// Whether a path names a `TODO.md` (any directory, any case). |
| 64 | pub fn is_todo_md(path: &str) -> bool { |
| 65 | std::path::Path::new(path) |
| 66 | .file_name() |
| 67 | .is_some_and(|n| n.eq_ignore_ascii_case("TODO.md")) |
| 68 | } |
| 69 | |
| 70 | /// The shallowest heading level in the document (the column level), or 0 if |
| 71 | /// there are no headings. Fenced regions are skipped. |
| 72 | fn column_level(text: &str) -> u8 { |
| 73 | let mut in_fence = false; |
| 74 | let mut min = 0u8; |
| 75 | for line in text.lines() { |
| 76 | if line.trim_start().starts_with("```") { |
| 77 | in_fence = !in_fence; |
| 78 | continue; |
| 79 | } |
| 80 | if in_fence { |
| 81 | continue; |
| 82 | } |
| 83 | if let Some((level, _)) = heading(line) |
| 84 | && (min == 0 || level < min) |
| 85 | { |
| 86 | min = level; |
| 87 | } |
| 88 | } |
| 89 | min |
| 90 | } |
| 91 | |
| 92 | /// Parse a todo-md document into sections. The preamble (content before the |
| 93 | /// first heading) becomes a level-0 section with an empty title. |
| 94 | pub fn parse(text: &str) -> Vec<Section> { |
| 95 | let col = column_level(text); |
| 96 | let mut sections = vec![Section { |
| 97 | level: 0, |
| 98 | title: String::new(), |
| 99 | done: false, |
| 100 | tasks: Vec::new(), |
| 101 | prose: String::new(), |
| 102 | }]; |
| 103 | let mut in_fence = false; |
| 104 | let mut mode = Mode::Prose; |
| 105 | |
| 106 | for line in text.lines() { |
| 107 | let fence_toggle = line.trim_start().starts_with("```"); |
| 108 | let structural = !in_fence && !fence_toggle; |
| 109 | if fence_toggle { |
| 110 | in_fence = !in_fence; |
| 111 | } |
| 112 | |
| 113 | if structural { |
| 114 | if let Some((level, rest)) = heading(line) { |
| 115 | let cur = sections.last_mut().expect("never empty"); |
| 116 | if col != 0 && level > col { |
| 117 | // A heading deeper than the column level is a ticket in |
| 118 | // the current section. |
| 119 | cur.tasks.push(Task { |
| 120 | done: cur.done || marked_done(rest), |
| 121 | title: strip_marker(rest), |
| 122 | details: String::new(), |
| 123 | ticket: true, |
| 124 | }); |
| 125 | mode = Mode::TicketBody; |
| 126 | } else { |
| 127 | sections.push(Section { |
| 128 | level, |
| 129 | title: strip_marker(rest), |
| 130 | done: marked_done(rest), |
| 131 | tasks: Vec::new(), |
| 132 | prose: String::new(), |
| 133 | }); |
| 134 | mode = Mode::Prose; |
| 135 | } |
| 136 | continue; |
| 137 | } |
| 138 | |
| 139 | // Inside a ticket body only a heading (handled above) ends it; |
| 140 | // everything else — prose, checkbox lines, blanks — is body. |
| 141 | if mode != Mode::TicketBody { |
| 142 | if let Some((done, title)) = task_line(line) { |
| 143 | sections.last_mut().expect("never empty").tasks.push(Task { |
| 144 | done, |
| 145 | title: title.to_string(), |
| 146 | details: String::new(), |
| 147 | ticket: false, |
| 148 | }); |
| 149 | mode = Mode::TaskDetail; |
| 150 | continue; |
| 151 | } |
| 152 | // A non-indented, non-blank line ends a checkbox task's |
| 153 | // details and reverts to section prose. |
| 154 | if mode == Mode::TaskDetail && !line.is_empty() && !line.starts_with(" ") { |
| 155 | mode = Mode::Prose; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | append_line(sections.last_mut().expect("never empty"), mode, line); |
| 161 | } |
| 162 | |
| 163 | for s in &mut sections { |
| 164 | if s.done { |
| 165 | for t in &mut s.tasks { |
| 166 | t.done = true; |
| 167 | } |
| 168 | } |
| 169 | for t in &mut s.tasks { |
| 170 | // Drop trailing blank lines swallowed while the task was open. |
| 171 | while t.details.ends_with('\n') { |
| 172 | t.details.pop(); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | sections |
| 177 | } |
| 178 | |
| 179 | fn append_line(section: &mut Section, mode: Mode, line: &str) { |
| 180 | match mode { |
| 181 | Mode::Prose => { |
| 182 | section.prose.push_str(line); |
| 183 | section.prose.push('\n'); |
| 184 | } |
| 185 | Mode::TaskDetail => { |
| 186 | let t = section.tasks.last_mut().expect("open task exists"); |
| 187 | // Dedent one level (2–4 spaces) so details render as their own |
| 188 | // markdown rather than a code block. |
| 189 | let spaces = line.len() - line.trim_start_matches(' ').len(); |
| 190 | t.details.push_str(&line[spaces.min(4).min(line.len())..]); |
| 191 | t.details.push('\n'); |
| 192 | } |
| 193 | Mode::TicketBody => { |
| 194 | let t = section.tasks.last_mut().expect("open ticket exists"); |
| 195 | t.details.push_str(line); |
| 196 | t.details.push('\n'); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | fn heading(line: &str) -> Option<(u8, &str)> { |
| 202 | let hashes = line.bytes().take_while(|&b| b == b'#').count(); |
| 203 | if (1..=6).contains(&hashes) && line[hashes..].starts_with(' ') { |
| 204 | Some((hashes as u8, line[hashes..].trim())) |
| 205 | } else { |
| 206 | None |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /// Whether a heading's text carries a done marker (`[x]`, ASCII; `✓` accepted |
| 211 | /// for todomd compatibility). |
| 212 | fn marked_done(heading_rest: &str) -> bool { |
| 213 | heading_rest.contains("[x]") || heading_rest.contains("[X]") || heading_rest.contains('✓') |
| 214 | } |
| 215 | |
| 216 | fn strip_marker(heading_rest: &str) -> String { |
| 217 | heading_rest |
| 218 | .replace("[x]", "") |
| 219 | .replace("[X]", "") |
| 220 | .replace('✓', "") |
| 221 | .trim() |
| 222 | .to_string() |
| 223 | } |
| 224 | |
| 225 | fn task_line(line: &str) -> Option<(bool, &str)> { |
| 226 | let open = line.strip_prefix("- [ ] "); |
| 227 | let done = line.strip_prefix("- [x] ").or(line.strip_prefix("- [X] ")); |
| 228 | match (open, done) { |
| 229 | (Some(rest), _) => Some((false, rest.trim())), |
| 230 | (_, Some(rest)) => Some((true, rest.trim())), |
| 231 | _ => None, |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /// Render a todo-md document as a kanban board (columns = task-bearing |
| 236 | /// sections) with prose sections as a notes area below. `None` if the file |
| 237 | /// contains no tasks at all — callers fall back to plain markdown. |
| 238 | pub fn render_board(text: &str) -> Option<Markup> { |
| 239 | let sections = parse(text); |
| 240 | if sections.iter().all(|s| s.tasks.is_empty()) { |
| 241 | return None; |
| 242 | } |
| 243 | |
| 244 | // Prose-only sections (and stray preamble/column prose) become one |
| 245 | // markdown notes blob, headings preserved. |
| 246 | let mut notes = String::new(); |
| 247 | for s in §ions { |
| 248 | let prose_empty = s.prose.trim().is_empty(); |
| 249 | if s.tasks.is_empty() && s.level > 0 && prose_empty { |
| 250 | continue; // empty section: nothing to show either way |
| 251 | } |
| 252 | if s.tasks.is_empty() { |
| 253 | if s.level > 0 { |
| 254 | notes.push_str(&format!("{} {}\n", "#".repeat(s.level as usize), s.title)); |
| 255 | } |
| 256 | if !prose_empty { |
| 257 | notes.push_str(&s.prose); |
| 258 | notes.push('\n'); |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | Some(html! { |
| 264 | div.kanban { |
| 265 | @for s in sections.iter().filter(|s| !s.tasks.is_empty()) { |
| 266 | div.col { |
| 267 | h3 { |
| 268 | @if s.title.is_empty() { "Tasks" } @else { (s.title) } |
| 269 | span.count { |
| 270 | @if s.done { |
| 271 | ({ s.tasks.len().to_string() }) " done" |
| 272 | } @else { |
| 273 | ({ s.tasks.iter().filter(|t| !t.done).count().to_string() }) |
| 274 | " open" |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | @for t in &s.tasks { |
| 279 | div.card.done[t.done] { |
| 280 | div.title { (render_markdown(&t.title)) } |
| 281 | @if !t.details.trim().is_empty() { |
| 282 | details { |
| 283 | summary { "details" } |
| 284 | div.card-details { (render_markdown(&t.details)) } |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | @if !notes.trim().is_empty() { |
| 293 | details.todo-notes { |
| 294 | summary { "notes" } |
| 295 | div.md-body { (render_markdown(¬es)) } |
| 296 | } |
| 297 | } |
| 298 | }) |
| 299 | } |
| 300 | |
| 301 | #[cfg(test)] |
| 302 | mod tests { |
| 303 | use super::*; |
| 304 | |
| 305 | const DOC: &str = "\ |
| 306 | intro prose |
| 307 | |
| 308 | # Now |
| 309 | |
| 310 | - [ ] first task |
| 311 | - a detail line |
| 312 | - another `detail` |
| 313 | - [x] finished task |
| 314 | |
| 315 | # Done [x] |
| 316 | |
| 317 | - [ ] moved here, checkbox stale |
| 318 | |
| 319 | ## Implement Navbar |
| 320 | |
| 321 | Sticky top bar with the repo switcher. |
| 322 | |
| 323 | - shipped behind a flag |
| 324 | - needs a follow-up for mobile |
| 325 | |
| 326 | # Notes |
| 327 | |
| 328 | just prose, no tasks |
| 329 | |
| 330 | ``` |
| 331 | # not a heading |
| 332 | - [ ] not a task |
| 333 | ``` |
| 334 | "; |
| 335 | |
| 336 | #[test] |
| 337 | fn parses_sections_tasks_details() { |
| 338 | let s = parse(DOC); |
| 339 | assert_eq!(s.len(), 4); // preamble + 3 column headings (## is a ticket) |
| 340 | assert_eq!(s[0].level, 0); |
| 341 | assert_eq!(s[0].prose.trim(), "intro prose"); |
| 342 | |
| 343 | assert_eq!(s[1].title, "Now"); |
| 344 | assert!(!s[1].done); |
| 345 | assert_eq!(s[1].tasks.len(), 2); |
| 346 | assert_eq!(s[1].tasks[0].title, "first task"); |
| 347 | assert!(!s[1].tasks[0].done); |
| 348 | assert!(!s[1].tasks[0].ticket); |
| 349 | assert_eq!(s[1].tasks[0].details, "- a detail line\n- another `detail`"); |
| 350 | assert!(s[1].tasks[1].done); |
| 351 | |
| 352 | // Done section: heading marker wins over the task's own checkbox. |
| 353 | assert_eq!(s[2].title, "Done"); |
| 354 | assert!(s[2].done); |
| 355 | assert_eq!(s[2].tasks.len(), 2); // checkbox task + the ticket |
| 356 | assert!(s[2].tasks[0].done); |
| 357 | assert!(!s[2].tasks[0].ticket); |
| 358 | } |
| 359 | |
| 360 | #[test] |
| 361 | fn nested_heading_is_a_ticket() { |
| 362 | let s = parse(DOC); |
| 363 | let ticket = &s[2].tasks[1]; |
| 364 | assert!(ticket.ticket); |
| 365 | assert_eq!(ticket.title, "Implement Navbar"); |
| 366 | // Done inherited from the `# Done [x]` section. |
| 367 | assert!(ticket.done); |
| 368 | assert!(ticket.details.contains("Sticky top bar")); |
| 369 | assert!(ticket.details.contains("- needs a follow-up for mobile")); |
| 370 | // The body must not leak into a sibling section. |
| 371 | assert!(!ticket.details.contains("just prose")); |
| 372 | } |
| 373 | |
| 374 | #[test] |
| 375 | fn ticket_own_done_marker() { |
| 376 | let s = parse("# Backlog\n\n## Fix login [x]\n\nbody\n\n## Add search\n\nbody\n"); |
| 377 | assert!(!s[1].done); // section is open |
| 378 | assert_eq!(s[1].tasks.len(), 2); |
| 379 | assert!(s[1].tasks[0].done); // ticket marked done on its own heading |
| 380 | assert_eq!(s[1].tasks[0].title, "Fix login"); |
| 381 | assert!(!s[1].tasks[1].done); |
| 382 | } |
| 383 | |
| 384 | #[test] |
| 385 | fn todo_md_by_filename() { |
| 386 | assert!(is_todo_md("TODO.md")); |
| 387 | assert!(is_todo_md("docs/todo.MD")); |
| 388 | assert!(!is_todo_md("TODO.txt")); |
| 389 | assert!(!is_todo_md("NOT-TODO.md")); |
| 390 | } |
| 391 | |
| 392 | #[test] |
| 393 | fn board_renders_columns_tickets_and_falls_back() { |
| 394 | let board = render_board(DOC).expect("has tasks").into_string(); |
| 395 | assert!(board.contains("kanban")); |
| 396 | assert!(board.contains("Now")); |
| 397 | assert!(board.contains("first task")); |
| 398 | // Tickets render as ordinary cards — no checkbox input, no special |
| 399 | // card class. (The DOC text mentions "checkbox", so check elements.) |
| 400 | assert!(board.contains("Implement Navbar")); |
| 401 | assert!(!board.contains("<input"), "no checkbox clutter: {board}"); |
| 402 | assert!(!board.contains("ticket\""), "no ticket card class: {board}"); |
| 403 | assert!(board.contains("just prose"), "notes area kept: {board}"); |
| 404 | assert!(render_board("# readme\n\nonly prose\n").is_none()); |
| 405 | } |
| 406 | } |