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 blockquote (element)
21 (defclass verbatim (element)
22 ((alt :initarg :alt)))
24 (defun parse-title (s)
25 "Parse a line into a title."
26 (destructuring-bind (h text)
27 (cl-ppcre:split "\\s+" s :limit 2)
28 (make-instance 'title :level (length h)
31 (defun make-link (url &optional text)
32 (make-instance 'link :url (quri:uri url)
36 "Parse a line into link."
37 (match (cl-ppcre:split "\\s+" s :limit 3)
38 ((list _ url) (make-link url))
39 ((list _ url text) (make-link url text))))
42 "Parse a line into an item"
43 (match (cl-ppcre:split "\\s+" s :limit 2)
44 ((list _ text) (make-instance 'item :text text))))
46 (defun parse-blockquote (s)
47 "Parse a line into a blockquote."
48 (match (cl-ppcre:split "\\s+" s :limit 2)
49 ((list _ text) (make-instance 'blockquote :text text))))
53 (make-instance 'paragraph :text s)
58 (#\> (parse-blockquote s))
59 (otherwise (make-instance 'paragraph :text s)))))
61 (defmacro markerp (line)
62 `(uiop:string-prefix-p "```" ,line))
65 "Parse gemtext from the stream IN."
67 for line = (read-line in nil)
72 (loop with label = (subseq line 3)
74 for line = (read-line in nil)
76 do (error "non-closed verbatim")
78 return (make-instance 'verbatim
80 :text (format nil "~{~A~%~^~}"
82 do (push line content))
86 (defun parse-string (str)
87 "Parse the string STR as gemtext."
88 (with-input-from-string (s str)
91 (defgeneric unparse (obj stream)
92 (:documentation "Print a textual representation of OBJ onto STREAM."))
94 (defmethod unparse ((l list) stream)
96 (unparse item stream)))
98 (defmethod unparse ((title title) stream)
99 (with-slots (text level) title
102 (format stream " ~a~%" text)))
104 (defmethod unparse ((link link) stream)
105 (with-slots (url text) link
106 (format stream "=> ~a ~a~%" url text)))
108 (defmethod unparse ((item item) stream)
109 (with-slots (text) item
110 (format stream "* ~a~%" text)))
112 (defmethod unparse ((p paragraph) stream)
114 (format stream "~a~%" text)))
116 (defmethod unparse ((v verbatim) stream)
117 (with-slots (alt text) v
118 (format stream "```~a~%~a```~%" alt text)))
120 (defmethod unparse ((b blockquote) stream)
122 (format stream "> ~a~%" text)))