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