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