assign numbers to links in a document
This commit is contained in:
34
gem300.rkt
34
gem300.rkt
@@ -4,21 +4,22 @@
|
|||||||
(require net/url)
|
(require net/url)
|
||||||
|
|
||||||
(struct text (str))
|
(struct text (str))
|
||||||
(struct link (url str))
|
(struct link (url str ord))
|
||||||
(struct preformatted (str))
|
(struct preformatted (str))
|
||||||
|
|
||||||
(define (parse-url line)
|
(define (parse-url line link-#)
|
||||||
(let ([split (string-split (substring line 2))])
|
(let ([split (string-split (substring line 2))])
|
||||||
(if (empty? split)
|
(if (empty? split)
|
||||||
(text line)
|
(text line)
|
||||||
(link (car split)
|
(link (car split)
|
||||||
(if (>= (length split) 2)
|
(if (>= (length split) 2)
|
||||||
(string-join (cdr split))
|
(string-join (cdr split))
|
||||||
(car split))))))
|
(car split))
|
||||||
|
link-#))))
|
||||||
|
|
||||||
|
|
||||||
(define (gemtext-parse lines)
|
(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.
|
;; 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
|
||||||
@@ -39,7 +40,8 @@
|
|||||||
(iter (cons (preformatted (reverse (preformatted-str (car document))))
|
(iter (cons (preformatted (reverse (preformatted-str (car document))))
|
||||||
(cdr document))
|
(cdr document))
|
||||||
(cdr lines)
|
(cdr lines)
|
||||||
'normal)]
|
'normal
|
||||||
|
link-#)]
|
||||||
|
|
||||||
;; add line to most recent preformat block
|
;; add line to most recent preformat block
|
||||||
[(symbol=? 'preformatted state)
|
[(symbol=? 'preformatted state)
|
||||||
@@ -51,31 +53,37 @@
|
|||||||
(preformatted-str (car document))))
|
(preformatted-str (car document))))
|
||||||
(cdr document))
|
(cdr document))
|
||||||
(cdr lines)
|
(cdr lines)
|
||||||
'preformatted)]
|
'preformatted
|
||||||
|
link-#)]
|
||||||
|
|
||||||
;; rest of this is normal mode
|
;; rest of this is normal mode
|
||||||
|
|
||||||
;; link lines
|
;; link lines
|
||||||
[(string-prefix? (car lines) "=>")
|
[(string-prefix? (car lines) "=>")
|
||||||
(iter (cons (parse-url (car lines))
|
(let ([parsed (parse-url (car lines) link-#)])
|
||||||
document)
|
(iter (cons parsed document)
|
||||||
(cdr lines)
|
(cdr lines)
|
||||||
'normal)]
|
'normal
|
||||||
|
(if (link? parsed)
|
||||||
|
(add1 link-#)
|
||||||
|
link-#)))]
|
||||||
|
|
||||||
;; preformatting toggle lines
|
;; preformatting toggle lines
|
||||||
[(string-prefix? (car lines) "```")
|
[(string-prefix? (car lines) "```")
|
||||||
;; add preformatted block to document and toggle mode
|
;; add preformatted block to document and toggle mode
|
||||||
(iter (cons (preformatted (list)) document)
|
(iter (cons (preformatted (list)) document)
|
||||||
(cdr lines)
|
(cdr lines)
|
||||||
'preformatted)]
|
'preformatted
|
||||||
|
link-#)]
|
||||||
|
|
||||||
[else
|
[else
|
||||||
(iter (cons (text (car lines))
|
(iter (cons (text (car lines))
|
||||||
document)
|
document)
|
||||||
(cdr lines)
|
(cdr lines)
|
||||||
'normal)])))
|
'normal
|
||||||
|
link-#)])))
|
||||||
|
|
||||||
(iter (list) lines 'normal))
|
(iter (list) lines 'normal 1))
|
||||||
|
|
||||||
(define (request url-str)
|
(define (request url-str)
|
||||||
(define url (string->url url-str))
|
(define url (string->url url-str))
|
||||||
@@ -157,7 +165,7 @@
|
|||||||
(preformatted-str line))]
|
(preformatted-str line))]
|
||||||
|
|
||||||
[(link? line)
|
[(link? line)
|
||||||
(printf "[#] ~a\n" (link-str line))]))
|
(printf "[~a] ~a\n" (link-ord line) (link-str line))]))
|
||||||
document))
|
document))
|
||||||
|
|
||||||
(define commands
|
(define commands
|
||||||
|
Reference in New Issue
Block a user