diff options
| author | Jan Tuomi <jan@jantuomi.fi> | 2024-04-22 21:58:10 +0300 |
|---|---|---|
| committer | Jan Tuomi <jan@jantuomi.fi> | 2024-04-22 21:58:10 +0300 |
| commit | e3b21ffd6b88008fd1cc3599a0f816773dc11f9d (patch) | |
| tree | 6843abb968954cce59e43ebf606bda2ab39b2692 /md-parser.scm | |
| parent | d369f1411c8cf0a07686ed944c8f9f98521129fd (diff) | |
Refactor whole thing to use in-memory DB as source of truth
Diffstat (limited to 'md-parser.scm')
| -rw-r--r-- | md-parser.scm | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/md-parser.scm b/md-parser.scm new file mode 100644 index 0000000..accc927 --- /dev/null +++ b/md-parser.scm @@ -0,0 +1,49 @@ +(module md-parser (read-md-frontmatter) + (import scheme + (chicken base) + (chicken format) + (chicken file) + (chicken io) + (chicken irregex) + (chicken sort) + srfi-1 + srfi-13 + + utils + ) + + (define (collect-fm-lines lines #!optional (acc '())) + (if (string=? "---" (car lines)) + acc + (collect-fm-lines (cdr lines) (cons (car lines) acc)))) + + (define (extract-field field fm) (call/cc (@ extract-field* field fm))) + (define (extract-field* field fm return) + (define (search line) + (define irx (format "^~A: (.+?)$" field)) + (define m (irregex-search irx line)) + (if m + (return (submatch m)))) + + (for-each search fm) + #f) + + (define (parse-line line) + (define irx (format "^(.+?): (.+?)$" line)) + (define m (irregex-search irx line)) + (if m + (cons (submatch m 1) (submatch m 2)) + #f)) + + (define (read-md-frontmatter md-path) + (define lines (with-input-from-file md-path + (λ () (read-lines)))) + + (define (inner return) + (if (not (string=? "---" (car lines))) + (return #f)) + + (define fm (collect-fm-lines (cdr lines))) + (map parse-line fm)) + + (call/cc (@ inner)))) |
