markdown-org-extract 0.2.0

CLI utility for extracting tasks from markdown files with Emacs Org-mode support
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
use std::fmt::Write;

use crate::types::{DayAgenda, Task, TaskWithOffset};

/// Escape markdown special characters in plain text. Used for headings and
/// labels that originate from user input — keeps formatting from being broken
/// or hijacked (e.g. a heading containing `*` would otherwise render as italic).
fn md_escape(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for ch in s.chars() {
        match ch {
            '\\' | '`' | '*' | '_' | '#' | '[' | ']' | '<' | '>' | '|' => {
                out.push('\\');
                out.push(ch);
            }
            _ => out.push(ch),
        }
    }
    out
}

/// Render day agendas as Markdown
pub fn render_days_markdown(days: &[DayAgenda]) -> String {
    let mut output = String::from("# Agenda\n\n");

    for day in days {
        let _ = writeln!(output, "## {}\n", day.date);

        if !day.overdue.is_empty() {
            output.push_str("### Overdue\n\n");
            for task_with_offset in &day.overdue {
                render_task_with_offset_md(&mut output, task_with_offset);
            }
            output.push('\n');
        }

        if !day.scheduled_timed.is_empty() {
            output.push_str("### Scheduled\n\n");
            for task_with_offset in &day.scheduled_timed {
                render_task_with_offset_md(&mut output, task_with_offset);
            }
            output.push('\n');
        }

        if !day.scheduled_no_time.is_empty() {
            if day.scheduled_timed.is_empty() {
                output.push_str("### Scheduled\n\n");
            }
            for task_with_offset in &day.scheduled_no_time {
                render_task_with_offset_md(&mut output, task_with_offset);
            }
            output.push('\n');
        }

        if !day.upcoming.is_empty() {
            output.push_str("### Upcoming\n\n");
            for task_with_offset in &day.upcoming {
                render_task_with_offset_md(&mut output, task_with_offset);
            }
            output.push('\n');
        }
    }

    output
}

fn render_task_with_offset_md(output: &mut String, task_with_offset: &TaskWithOffset) {
    let task = &task_with_offset.task;

    let _ = write!(output, "#### {}", md_escape(&task.heading));
    if let Some(offset) = task_with_offset.days_offset {
        if offset > 0 {
            let _ = write!(output, " (in {offset} days)");
        } else {
            let _ = write!(output, " ({} days ago)", -offset);
        }
    }
    output.push('\n');

    let _ = writeln!(output, "**File:** `{}:{}`", task.file, task.line);
    if let Some(ref t) = task.task_type {
        let _ = writeln!(output, "**Type:** {t}");
    }
    if let Some(ref p) = task.priority {
        let _ = writeln!(output, "**Priority:** {p}");
    }
    if let Some(ref ts) = task.timestamp {
        let _ = writeln!(output, "**Time:** `{ts}`");
    }
    if !task.content.is_empty() {
        let _ = write!(output, "\n{}\n\n", task.content);
    } else {
        output.push('\n');
    }
}

/// Render day agendas as HTML
pub fn render_days_html(days: &[DayAgenda]) -> String {
    let mut output = String::from("<html><body><h1>Agenda</h1>\n");

    for day in days {
        let _ = writeln!(output, "<h2>{}</h2>", html_escape(&day.date));

        if !day.overdue.is_empty() {
            output.push_str("<h3>Overdue</h3>\n");
            for task_with_offset in &day.overdue {
                render_task_with_offset_html(&mut output, task_with_offset);
            }
        }

        if !day.scheduled_timed.is_empty() {
            output.push_str("<h3>Scheduled</h3>\n");
            for task_with_offset in &day.scheduled_timed {
                render_task_with_offset_html(&mut output, task_with_offset);
            }
        }

        if !day.scheduled_no_time.is_empty() {
            if day.scheduled_timed.is_empty() {
                output.push_str("<h3>Scheduled</h3>\n");
            }
            for task_with_offset in &day.scheduled_no_time {
                render_task_with_offset_html(&mut output, task_with_offset);
            }
        }

        if !day.upcoming.is_empty() {
            output.push_str("<h3>Upcoming</h3>\n");
            for task_with_offset in &day.upcoming {
                render_task_with_offset_html(&mut output, task_with_offset);
            }
        }
    }

    output.push_str("</body></html>");
    output
}

fn render_task_with_offset_html(output: &mut String, task_with_offset: &TaskWithOffset) {
    let task = &task_with_offset.task;

    let _ = write!(output, "<h4>{}", html_escape(&task.heading));
    if let Some(offset) = task_with_offset.days_offset {
        let label = if offset > 0 {
            format!(" (in {offset} days)")
        } else {
            format!(" ({} days ago)", -offset)
        };
        let _ = write!(output, "{}", html_escape(&label));
    }
    output.push_str("</h4>\n");

    let _ = writeln!(
        output,
        "<p><strong>File:</strong> {}:{}</p>",
        html_escape(&task.file),
        task.line
    );
    if let Some(ref t) = task.task_type {
        let _ = writeln!(output, "<p><strong>Type:</strong> {t}</p>");
    }
    if let Some(ref p) = task.priority {
        let _ = writeln!(output, "<p><strong>Priority:</strong> {p}</p>");
    }
    if let Some(ref ts) = task.timestamp {
        let _ = writeln!(output, "<p><strong>Time:</strong> {}</p>", html_escape(ts));
    }
    if !task.content.is_empty() {
        let _ = writeln!(output, "<p>{}</p>", html_escape(&task.content));
    }
}

/// Render tasks as Markdown
pub fn render_markdown(tasks: &[Task]) -> String {
    let mut output = String::from("# Tasks\n\n");
    for task in tasks {
        let _ = writeln!(output, "## {}", md_escape(&task.heading));
        let _ = writeln!(output, "**File:** `{}:{}`", task.file, task.line);
        if let Some(ref t) = task.task_type {
            let _ = writeln!(output, "**Type:** {t}");
        }
        if let Some(ref p) = task.priority {
            let _ = writeln!(output, "**Priority:** {p}");
        }
        if let Some(ref c) = task.created {
            let _ = writeln!(output, "**Created:** `{c}`");
        }
        if let Some(ref ts) = task.timestamp {
            let _ = writeln!(output, "**Time:** `{ts}`");
        }
        if let Some(ref total) = task.total_clock_time {
            let _ = writeln!(output, "**Total Time:** {total}");
        }
        if let Some(ref clocks) = task.clocks {
            output.push_str("\n**Clock:**\n");
            for clock in clocks {
                if let Some(ref end) = clock.end {
                    if let Some(ref dur) = clock.duration {
                        let _ = writeln!(output, "- `{}` → `{}` ({})", clock.start, end, dur);
                    } else {
                        let _ = writeln!(output, "- `{}` → `{}`", clock.start, end);
                    }
                } else {
                    let _ = writeln!(output, "- `{}` (active)", clock.start);
                }
            }
        }
        if !task.content.is_empty() {
            let _ = write!(output, "\n{}\n\n", task.content);
        } else {
            output.push('\n');
        }
    }
    output
}

/// Render tasks as HTML
pub fn render_html(tasks: &[Task]) -> String {
    let mut output = String::from("<html><body><h1>Tasks</h1>\n");
    for task in tasks {
        let _ = writeln!(output, "<h2>{}</h2>", html_escape(&task.heading));
        let _ = writeln!(
            output,
            "<p><strong>File:</strong> {}:{}</p>",
            html_escape(&task.file),
            task.line
        );
        if let Some(ref t) = task.task_type {
            let _ = writeln!(output, "<p><strong>Type:</strong> {t}</p>");
        }
        if let Some(ref p) = task.priority {
            let _ = writeln!(output, "<p><strong>Priority:</strong> {p}</p>");
        }
        if let Some(ref c) = task.created {
            let _ = writeln!(
                output,
                "<p><strong>Created:</strong> {}</p>",
                html_escape(c)
            );
        }
        if let Some(ref ts) = task.timestamp {
            let _ = writeln!(output, "<p><strong>Time:</strong> {}</p>", html_escape(ts));
        }
        if let Some(ref total) = task.total_clock_time {
            let _ = writeln!(
                output,
                "<p><strong>Total Time:</strong> {}</p>",
                html_escape(total)
            );
        }
        if let Some(ref clocks) = task.clocks {
            output.push_str("<p><strong>Clock:</strong></p>\n<ul>\n");
            for clock in clocks {
                if let Some(ref end) = clock.end {
                    if let Some(ref dur) = clock.duration {
                        let _ = writeln!(
                            output,
                            "<li>{}{} ({})</li>",
                            html_escape(&clock.start),
                            html_escape(end),
                            html_escape(dur)
                        );
                    } else {
                        let _ = writeln!(
                            output,
                            "<li>{}{}</li>",
                            html_escape(&clock.start),
                            html_escape(end)
                        );
                    }
                } else {
                    let _ = writeln!(output, "<li>{} (active)</li>", html_escape(&clock.start));
                }
            }
            output.push_str("</ul>\n");
        }
        if !task.content.is_empty() {
            let _ = writeln!(output, "<p>{}</p>", html_escape(&task.content));
        }
    }
    output.push_str("</body></html>");
    output
}

/// Escape HTML special characters in pre-existing text content.
/// Also drops C0 control characters (except `\t \n \r`) to protect downstream
/// renderers from null bytes and other invisible glyphs sneaked through markdown.
fn html_escape(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for ch in s.chars() {
        match ch {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            '"' => out.push_str("&quot;"),
            '\'' => out.push_str("&#39;"),
            '\t' | '\n' | '\r' => out.push(ch),
            c if (c as u32) < 0x20 || c == '\u{7f}' => {}
            _ => out.push(ch),
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{Priority, TaskType};

    #[test]
    fn test_html_escape() {
        assert_eq!(html_escape("<script>"), "&lt;script&gt;");
        assert_eq!(html_escape("A & B"), "A &amp; B");
    }

    #[test]
    fn test_html_escape_strips_control_chars() {
        assert_eq!(html_escape("A\u{0000}B"), "AB");
        assert_eq!(html_escape("A\u{0007}B"), "AB"); // BEL
        assert_eq!(html_escape("A\u{007f}B"), "AB"); // DEL
        assert_eq!(html_escape("line1\nline2\tx"), "line1\nline2\tx");
    }

    #[test]
    fn test_render_markdown_basic() {
        let tasks = vec![Task {
            file: "test.md".to_string(),
            line: 1,
            heading: "Test Task".to_string(),
            content: "Description".to_string(),
            task_type: Some(TaskType::Todo),
            priority: Some(Priority::A),
            created: None,
            timestamp: None,
            timestamp_type: None,
            timestamp_date: None,
            timestamp_time: None,
            timestamp_end_time: None,
            clocks: None,
            total_clock_time: None,
        }];

        let output = render_markdown(&tasks);
        assert!(output.contains("# Tasks"));
        assert!(output.contains("## Test Task"));
        assert!(output.contains("**Type:** TODO"));
        assert!(output.contains("**Priority:** A"));
    }

    #[test]
    fn test_md_escape_specials() {
        assert_eq!(md_escape("plain"), "plain");
        assert_eq!(md_escape("a*b"), "a\\*b");
        assert_eq!(md_escape("a_b"), "a\\_b");
        assert_eq!(md_escape("# hi"), "\\# hi");
        assert_eq!(md_escape("[link]"), "\\[link\\]");
        assert_eq!(md_escape("<tag>"), "\\<tag\\>");
        assert_eq!(md_escape("a|b"), "a\\|b");
        assert_eq!(md_escape("`code`"), "\\`code\\`");
        assert_eq!(md_escape("back\\slash"), "back\\\\slash");
    }

    #[test]
    fn test_render_markdown_escapes_heading() {
        let tasks = vec![Task {
            file: "test.md".to_string(),
            line: 1,
            heading: "Fix *important* [#issue]".to_string(),
            content: String::new(),
            task_type: None,
            priority: None,
            created: None,
            timestamp: None,
            timestamp_type: None,
            timestamp_date: None,
            timestamp_time: None,
            timestamp_end_time: None,
            clocks: None,
            total_clock_time: None,
        }];
        let out = render_markdown(&tasks);
        assert!(
            out.contains("## Fix \\*important\\* \\[\\#issue\\]"),
            "heading must be escaped: {out}"
        );
    }

    fn fixture_task() -> Task {
        Task {
            file: "notes.md".to_string(),
            line: 42,
            heading: "Test task".to_string(),
            content: "Body text.".to_string(),
            task_type: Some(TaskType::Todo),
            priority: Some(Priority::A),
            created: Some("CREATED: <2025-09-01 Mon>".to_string()),
            timestamp: Some("DEADLINE: <2025-10-01 Wed>".to_string()),
            timestamp_type: Some("DEADLINE".to_string()),
            timestamp_date: Some("2025-10-01".to_string()),
            timestamp_time: None,
            timestamp_end_time: None,
            clocks: None,
            total_clock_time: None,
        }
    }

    #[test]
    fn snapshot_render_markdown_full_task() {
        let out = render_markdown(&[fixture_task()]);
        let expected = "# Tasks\n\n\
## Test task\n\
**File:** `notes.md:42`\n\
**Type:** TODO\n\
**Priority:** A\n\
**Created:** `CREATED: <2025-09-01 Mon>`\n\
**Time:** `DEADLINE: <2025-10-01 Wed>`\n\
\n\
Body text.\n\n";
        assert_eq!(out, expected);
    }

    #[test]
    fn snapshot_render_html_full_task() {
        let out = render_html(&[fixture_task()]);
        let expected = "<html><body><h1>Tasks</h1>\n\
<h2>Test task</h2>\n\
<p><strong>File:</strong> notes.md:42</p>\n\
<p><strong>Type:</strong> TODO</p>\n\
<p><strong>Priority:</strong> A</p>\n\
<p><strong>Created:</strong> CREATED: &lt;2025-09-01 Mon&gt;</p>\n\
<p><strong>Time:</strong> DEADLINE: &lt;2025-10-01 Wed&gt;</p>\n\
<p>Body text.</p>\n\
</body></html>";
        assert_eq!(out, expected);
    }

    #[test]
    fn test_render_html_escapes() {
        let tasks = vec![Task {
            file: "<script>.md".to_string(),
            line: 1,
            heading: "Test & Task".to_string(),
            content: String::new(),
            task_type: None,
            priority: None,
            created: None,
            timestamp: None,
            timestamp_type: None,
            timestamp_date: None,
            timestamp_time: None,
            timestamp_end_time: None,
            clocks: None,
            total_clock_time: None,
        }];

        let output = render_html(&tasks);
        assert!(output.contains("&lt;script&gt;"));
        assert!(output.contains("Test &amp; Task"));
    }
}