1 (in-package #:phos/gemtext)
4 ((text :initarg :text)))
6 (defclass title (element)
7 ((level :initarg :level)))
9 (defclass link (element)
10 ((url :initarg :url)))
12 (defclass item (element)
15 (defclass paragraph (element)
18 (defclass verbatim (element)
19 ((alt :initarg :alt)))
21 (defun parse-title (s)
22 "Parse a line into a title."
23 (destructuring-bind (h text)
24 (cl-ppcre:split "\\s+" s :limit 2)
25 (make-instance 'title :level (length h)
28 (defun make-link (url &optional text)
29 (make-instance 'link :url (quri:uri url)
33 "Parse a line into link."
34 (match (cl-ppcre:split "\\s+" s :limit 3)
35 ((list _ url) (make-link url))
36 ((list _ url text) (make-link url text))))
39 "Parse a line into an item"
40 (match (cl-ppcre:split "\\s+" s :limit 2)
41 ((list _ text) (make-instance 'item :text text))))
45 (make-instance 'paragraph :text s)
50 (otherwise (make-instance 'paragraph :text s)))))
52 (defmacro markerp (line)
53 `(uiop:string-prefix-p "```" ,line))
56 "Parse gemtext from the stream IN."
58 for line = (read-line in nil)
63 (loop with label = (subseq line 3)
65 for line = (read-line in nil)
67 do (error "non-closed verbatim")
69 return (make-instance 'verbatim
71 :text (format nil "~{~A~%~^~}"
73 do (push line content))
77 (defun parse-string (str)
78 "Parse the string STR as gemtext."
79 (with-input-from-string (s str)
83 ;; (with-open-file (stream #P"~/quicklisp/local-projects/phos/test.gmi")
87 (defgeneric unparse (obj stream)
88 (:documentation "Print a textual representation of OBJ onto STREAM."))
90 (defmethod unparse ((l list) stream)
92 (unparse item stream)))
94 (defmethod unparse ((title title) stream)
95 (with-slots (text level) title
98 (format stream " ~a~%" text)))
100 (defmethod unparse ((link link) stream)
101 (with-slots (url text) link
102 (format stream "=> ~a ~a~%" url text)))
104 (defmethod unparse ((item item) stream)
105 (with-slots (text) item
106 (format stream "* ~a" text)
109 (defmethod unparse ((p paragraph) stream)
111 (format stream "~a" text)
114 (defmethod unparse ((v verbatim) stream)
115 (with-slots (alt text) v
116 (format stream "```~a~%~a```~%" alt text)))