Compare commits
5 Commits
c786466d65
...
b02f59c17f
Author | SHA1 | Date | |
---|---|---|---|
b02f59c17f | |||
38a420d649 | |||
ea9a3b8fb7 | |||
e3590502a9 | |||
724dc95086 |
28
gem300.rkt
28
gem300.rkt
@@ -7,15 +7,20 @@
|
|||||||
;; rendered out gemtext document as it shall be shown to the user
|
;; rendered out gemtext document as it shall be shown to the user
|
||||||
(define document-buffer null)
|
(define document-buffer null)
|
||||||
|
|
||||||
|
;; global state which will be the document structure, before rendering
|
||||||
|
(define document null)
|
||||||
|
|
||||||
(define (go-cmd url)
|
(define (go-cmd url)
|
||||||
(if (non-empty-string? url)
|
(if (non-empty-string? url)
|
||||||
(let-values ([(status meta c-in) (net:get 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)])
|
[(db-in db-out) (make-pipe #f)])
|
||||||
|
|
||||||
|
(set! document doc)
|
||||||
|
|
||||||
(set! document-buffer db-in)
|
(set! document-buffer db-in)
|
||||||
(parameterize ([current-output-port db-out])
|
(parameterize ([current-output-port db-out])
|
||||||
(gmi:render document))
|
(gmi:render doc))
|
||||||
|
|
||||||
(let ([remaining (pipe-content-length db-in)])
|
(let ([remaining (pipe-content-length db-in)])
|
||||||
(printf "document retrieved. ~a bytes\n" remaining))
|
(printf "document retrieved. ~a bytes\n" remaining))
|
||||||
@@ -34,20 +39,29 @@
|
|||||||
(iter 10)
|
(iter 10)
|
||||||
(newline)
|
(newline)
|
||||||
(let ([remaining (pipe-content-length document-buffer)])
|
(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)
|
(define (repl)
|
||||||
(display "G-300 > ")
|
(display "G-300 > ")
|
||||||
|
|
||||||
(let ([matches (regexp-match #px"(\\w+)\\s*(.*)" (read-line))])
|
(let ([matches (regexp-match #px"(\\w+)\\s*(.*)" (read-line))])
|
||||||
(cond
|
(cond
|
||||||
;; nothing was given. default command
|
;; next command. also default
|
||||||
[(not matches)
|
[(or (not matches)
|
||||||
|
(string=? (cadr matches) "next")
|
||||||
|
(string=? (cadr matches) "n"))
|
||||||
(next-cmd)]
|
(next-cmd)]
|
||||||
|
|
||||||
;; go command
|
;; go command
|
||||||
[(string=? (cadr matches) "go")
|
[(or (string=? (cadr matches) "go")
|
||||||
(go-cmd (caddr matches))]))
|
(string=? (cadr matches) "g"))
|
||||||
|
(go-cmd (caddr matches))]
|
||||||
|
|
||||||
|
;; visit link command
|
||||||
|
[(andmap char-numeric? (string->list (cadr matches)))
|
||||||
|
(visit-cmd (cadr matches))]))
|
||||||
|
|
||||||
(repl))
|
(repl))
|
||||||
|
27
gmi.rkt
27
gmi.rkt
@@ -1,9 +1,15 @@
|
|||||||
#lang racket
|
#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
|
;; 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 text (str))
|
(struct text (str))
|
||||||
(struct link (url str ord))
|
(struct link (url str ord))
|
||||||
(struct pre (str))
|
(struct pre (str))
|
||||||
@@ -87,7 +93,7 @@
|
|||||||
'normal
|
'normal
|
||||||
link-#)])))
|
link-#)])))
|
||||||
|
|
||||||
(iter (list) lines 'normal 1))
|
(document (iter (list) lines 'normal 1)))
|
||||||
|
|
||||||
;;;
|
;;;
|
||||||
;;; RENDERING
|
;;; RENDERING
|
||||||
@@ -141,4 +147,19 @@
|
|||||||
|
|
||||||
[(link? line)
|
[(link? line)
|
||||||
(printf "[~a] ~a\n" (link-ord line) (link-str 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)))
|
||||||
|
Reference in New Issue
Block a user