improve nomenclature of gmi handling module somewhat?

This commit is contained in:
2025-09-05 14:12:06 -07:00
parent 87e273fe00
commit f5cfbe76ea

53
gmi.rkt
View File

@@ -4,15 +4,15 @@
[render (-> document? void?)] [render (-> document? void?)]
[parse (-> (listof string?) document?)] [parse (-> (listof string?) document?)]
[match-link (-> document? integer? (or/c string? #f))] [match-link (-> document? integer? (or/c string? #f))]
[struct document ((structure (listof (or/c text? link? pre?))))])) [struct document ((items (listof (or/c text? link? pre?))))]))
;; a gemtext document is represented as a list of structs, a struct ;; a gemtext document is represented as a list of structs, a struct
;; for each type of item in a document. ;; for each type of item in a document.
(struct document (structure)) (struct document (items))
(struct text (str)) (struct text (line))
(struct link (url str ord)) (struct link (url line id))
(struct pre (str)) (struct pre (lines))
;;; ;;;
;;; PARSING ;;; PARSING
@@ -29,7 +29,7 @@
link-#)))) link-#))))
(define (parse lines) (define (parse lines)
(define (iter document lines state link-#) (define (iter document lines state link-id)
;; when there are no more lines, we have finished parsing. ;; when there are no more lines, we have finished parsing.
(if (empty? lines) (if (empty? lines)
;; consing inherently makes everything backwards ;; consing inherently makes everything backwards
@@ -47,11 +47,11 @@
;; and the contents of that block are backwards ;; and the contents of that block are backwards
;; 2. take those contents, reverse them, append them to the ;; 2. take those contents, reverse them, append them to the
;; cdr of the document ;; cdr of the document
(iter (cons (pre (reverse (pre-str (car document)))) (iter (cons (pre (reverse (pre-lines (car document))))
(cdr document)) (cdr document))
(cdr lines) (cdr lines)
'normal 'normal
link-#)] link-id)]
;; add line to most recent preformat block ;; add line to most recent preformat block
[(symbol=? 'preformatted state) [(symbol=? 'preformatted state)
@@ -60,23 +60,23 @@
;; then, cons the new preformatted block to the cdr of ;; then, cons the new preformatted block to the cdr of
;; the document ;; the document
(iter (cons (pre (cons (car lines) (iter (cons (pre (cons (car lines)
(pre-str (car document)))) (pre-lines (car document))))
(cdr document)) (cdr document))
(cdr lines) (cdr lines)
'preformatted 'preformatted
link-#)] link-id)]
;; rest of this is normal mode ;; rest of this is normal mode
;; link lines ;; link lines
[(string-prefix? (car lines) "=>") [(string-prefix? (car lines) "=>")
(let ([parsed (parse-url (car lines) link-#)]) (let ([parsed (parse-url (car lines) link-id)])
(iter (cons parsed document) (iter (cons parsed document)
(cdr lines) (cdr lines)
'normal 'normal
(if (link? parsed) (if (link? parsed)
(add1 link-#) (add1 link-id)
link-#)))] link-id)))]
;; preformatting toggle lines ;; preformatting toggle lines
[(string-prefix? (car lines) "```") [(string-prefix? (car lines) "```")
@@ -84,14 +84,14 @@
(iter (cons (pre (list)) document) (iter (cons (pre (list)) document)
(cdr lines) (cdr lines)
'preformatted 'preformatted
link-#)] link-id)]
[else [else
(iter (cons (text (car lines)) (iter (cons (text (car lines))
document) document)
(cdr lines) (cdr lines)
'normal 'normal
link-#)]))) link-id)])))
(document (iter (list) lines 'normal 1))) (document (iter (list) lines 'normal 1)))
@@ -135,25 +135,24 @@
;; the current output port exactly as it will be shown to the user ;; the current output port exactly as it will be shown to the user
(define (render document) (define (render document)
(for-each (for-each
(λ (line) (λ (item)
(cond (cond
[(text? line) [(text? item)
(render-paragraph (text-str line))] (render-paragraph (text-line item))]
[(pre? line) [(pre? item)
(for-each (λ (line) (for-each displayln (pre-lines item))]
(displayln line))
(pre-str line))]
[(link? line) [(link? item)
(printf "[~a] ~a\n" (link-ord line) (link-str line))])) (printf "[~a] ~a\n" (link-id item) (link-line item))]))
(document-structure document)))
(document-items document)))
(define (match-link document id) (define (match-link document id)
(define (iter next-structure) (define (iter next-structure)
(cond (cond
[(and (link? (car next-structure)) [(and (link? (car next-structure))
(= (link-ord (car next-structure)) id)) (= (link-id (car next-structure)) id))
(link-url (car next-structure))] (link-url (car next-structure))]
[(empty? (cdr next-structure)) [(empty? (cdr next-structure))
@@ -162,4 +161,4 @@
[else [else
(iter (cdr next-structure))])) (iter (cdr next-structure))]))
(iter (document-structure document))) (iter (document-items document)))