Files
racket-wiki/private/todo.rkt
T
2026-08-29 22:22:49 +02:00

40 lines
1.5 KiB
Racket

#lang racket/base
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Extraction of Todo(...) markers from Markdown source.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require racket/list
racket/string)
(provide extract-todos)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; goal : Extract wiki todo(...) markers from Markdown source.
; pre : markdown is a string.
; post : Fenced code blocks are ignored and source is unchanged.
; result : A list of hashes containing item number, line number and text.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (extract-todos markdown)
(let ((lines (string-split markdown "\n" #:trim? #f))
(in-fence? #f)
(item-number 0)
(result '()))
(for ((line (in-list lines))
(line-number (in-naturals 1)))
(let ((trimmed (string-trim line)))
(cond
((regexp-match? #px"^(```|~~~)" trimmed)
(set! in-fence? (not in-fence?)))
((not in-fence?)
(for ((match (in-list (regexp-match* #px"[Tt][Oo][Dd][Oo]\\([^()]+\\)" line))))
(let ((text (string-trim (substring match 5 (- (string-length match) 1)))))
(when (not (string=? text ""))
(set! item-number (+ item-number 1))
(set! result
(cons (hash 'number item-number
'line line-number
'text text)
result)))))))))
(reverse result)))