diff --git a/gem300.rkt b/gem300.rkt index 724c5f0..99d6363 100644 --- a/gem300.rkt +++ b/gem300.rkt @@ -4,21 +4,22 @@ (require net/url) (struct text (str)) -(struct link (url str)) +(struct link (url str ord)) (struct preformatted (str)) -(define (parse-url line) +(define (parse-url line link-#) (let ([split (string-split (substring line 2))]) (if (empty? split) (text line) (link (car split) (if (>= (length split) 2) (string-join (cdr split)) - (car split)))))) + (car split)) + link-#)))) (define (gemtext-parse lines) - (define (iter document lines state) + (define (iter document lines state link-#) ;; when there are no more lines, we have finished parsing. (if (empty? lines) ;; consing inherently makes everything backwards @@ -39,7 +40,8 @@ (iter (cons (preformatted (reverse (preformatted-str (car document)))) (cdr document)) (cdr lines) - 'normal)] + 'normal + link-#)] ;; add line to most recent preformat block [(symbol=? 'preformatted state) @@ -51,31 +53,37 @@ (preformatted-str (car document)))) (cdr document)) (cdr lines) - 'preformatted)] + 'preformatted + link-#)] ;; rest of this is normal mode ;; link lines [(string-prefix? (car lines) "=>") - (iter (cons (parse-url (car lines)) - document) - (cdr lines) - 'normal)] + (let ([parsed (parse-url (car lines) link-#)]) + (iter (cons parsed document) + (cdr lines) + 'normal + (if (link? parsed) + (add1 link-#) + link-#)))] ;; preformatting toggle lines [(string-prefix? (car lines) "```") ;; add preformatted block to document and toggle mode (iter (cons (preformatted (list)) document) (cdr lines) - 'preformatted)] + 'preformatted + link-#)] [else (iter (cons (text (car lines)) document) (cdr lines) - 'normal)]))) + 'normal + link-#)]))) - (iter (list) lines 'normal)) + (iter (list) lines 'normal 1)) (define (request url-str) (define url (string->url url-str)) @@ -157,7 +165,7 @@ (preformatted-str line))] [(link? line) - (printf "[#] ~a\n" (link-str line))])) + (printf "[~a] ~a\n" (link-ord line) (link-str line))])) document)) (define commands