Compare commits

..

5 Commits

Author SHA1 Message Date
b02f59c17f add a visit link command 2025-09-04 15:22:36 -07:00
38a420d649 add currently visited document, pre-rendering, to global state
its becoming apparent that this doesn't need to be global state, and
can be passed from iteration to iteration in the program loop. this
shall be revisited
2025-09-04 15:21:31 -07:00
ea9a3b8fb7 add abbreviations for existing commands 2025-09-04 15:13:25 -07:00
e3590502a9 defines a procedure used for looking up links by id in a document 2025-09-04 15:09:50 -07:00
724dc95086 start firmly defining module boundaries for gmi parser 2025-09-04 14:45:55 -07:00
2 changed files with 45 additions and 10 deletions

View File

@@ -7,15 +7,20 @@
;; rendered out gemtext document as it shall be shown to the user
(define document-buffer null)
;; global state which will be the document structure, before rendering
(define document null)
(define (go-cmd url)
(if (non-empty-string? url)
(let-values ([(status meta c-in) (net:get url)])
(let-values ([(document) (gmi:parse (port->lines c-in))]
(let-values ([(doc) (gmi:parse (port->lines c-in))]
[(db-in db-out) (make-pipe #f)])
(set! document doc)
(set! document-buffer db-in)
(parameterize ([current-output-port db-out])
(gmi:render document))
(gmi:render doc))
(let ([remaining (pipe-content-length db-in)])
(printf "document retrieved. ~a bytes\n" remaining))
@@ -34,20 +39,29 @@
(iter 10)
(newline)
(let ([remaining (pipe-content-length document-buffer)])
(printf "~a bytes remaining\n" remaining)))
(printf "~a bytes remaining\n" remaining)))
(define (visit-cmd line)
(go-cmd (gmi:match-link document (string->number line))))
(define (repl)
(display "G-300 > ")
(let ([matches (regexp-match #px"(\\w+)\\s*(.*)" (read-line))])
(cond
;; nothing was given. default command
[(not matches)
;; next command. also default
[(or (not matches)
(string=? (cadr matches) "next")
(string=? (cadr matches) "n"))
(next-cmd)]
;; go command
[(string=? (cadr matches) "go")
(go-cmd (caddr matches))]))
[(or (string=? (cadr matches) "go")
(string=? (cadr matches) "g"))
(go-cmd (caddr matches))]
;; visit link command
[(andmap char-numeric? (string->list (cadr matches)))
(visit-cmd (cadr matches))]))
(repl))

27
gmi.rkt
View File

@@ -1,9 +1,15 @@
#lang racket
(provide parse render)
(provide (contract-out
[render (-> document? void?)]
[parse (-> (listof string?) document?)]
[match-link (-> document? integer? (or/c string? #f))]
[struct document ((structure (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 text (str))
(struct link (url str ord))
(struct pre (str))
@@ -87,7 +93,7 @@
'normal
link-#)])))
(iter (list) lines 'normal 1))
(document (iter (list) lines 'normal 1)))
;;;
;;; RENDERING
@@ -141,4 +147,19 @@
[(link? line)
(printf "[~a] ~a\n" (link-ord line) (link-str line))]))
document))
(document-structure document)))
(define (match-link document id)
(define (iter next-structure)
(cond
[(and (link? (car next-structure))
(= (link-ord (car next-structure)) id))
(link-url (car next-structure))]
[(empty? (cdr next-structure))
#f]
[else
(iter (cdr next-structure))]))
(iter (document-structure document)))