File: extensions.lisp

package info (click to toggle)
cl-markdown 20101006-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 556 kB
  • sloc: lisp: 5,863; makefile: 11
file content (291 lines) | stat: -rw-r--r-- 8,242 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
(in-package #:cl-markdown)

;; {f a0 .. an} 
;; -> eval f a0 .. an  -- where ai are strings
;; -> returns string that is inserted into document
;; -> or nil (cound do insertions itself)

;; no recursive function embeddings {date-stamp {today}}
;; keywords handled separately?

;; could use a macro
;;
;; to specify a name, arguments, etc and use that to parse. and export

(defsimple-extension today 
  (let ((format (document-property :date-format "%e %B %Y")))
    (format-date format (get-universal-time))))

(defsimple-extension now 
  (let ((format (document-property :time-format "%H:%M")))
    (format-date format (get-universal-time))))

(defextension (comment :arguments ((text :required))
		       :insertp t)
  (ecase phase
    (:parse
     ;; no worries
     )
    (:render 
     (format nil "<!-- ~a -->" text))))

(defextension (remark :arguments ((text :required)) 
		      :insertp t)
  (ecase phase
    (:parse
     ;; no worries
     )
    (:render 
     ;; stil no worries
     )))

(defextension (anchor :arguments ((name :required) title) :insertp t)
  (setf name (ensure-string name))
  (let ((safe-name (html-safe-name name)))
    (ecase phase
      (:parse
       (setf (item-at (link-info *current-document*) name)
	     (make-instance 'link-info
			    :id name
			    :url (format nil "#~a" safe-name) 
			    :title (or title ""))))
      (:render 
       (format nil "<a name='~a' id='~a'></a>" safe-name safe-name)))))

(defextension (property :arguments ((name :required))
			:insertp t)
  (ecase phase
    (:parse)
    (:render
     (process-child-markdown (document-property name) phase))))

(defextension (ifdef :arguments ((keys :required) 
				 (text :required :whole))
		     :insertp t)
  (ecase phase
    (:parse)
    (:render
     (prog1
	 (if (or (and (atom keys) (document-property keys))
		 )
	     (process-child-markdown 
	      (format nil "~{~a~^ ~}" (ensure-list text)) phase)
	     "")))))

#|
(defvar *x*)

(defextension (property :arguments ((name :required)))
  (ecase phase
    (:parse)
    (:render
     (bind (((:values d s)
	     (markdown (document-property name) 
		       :parent *current-document*
		       :format *current-format*
		       :properties '((:omit-initial-paragraph t)
				     (:omit-final-paragraph t)
				     (:html . nil))
		       :stream nil)))
       (setf *x* d)
       (prog1
	   (strip-whitespace s))))))

(let ((*current-document* *x*))
  (document-property "html"))

(form-property-name "html")

(trace item-at-1)

|#


(defextension (set-property :arguments ((name :required) 
					(value :whole))
			    :insertp t)
  (when (eq phase :parse)
    (setf (document-property name) value))
  nil)

#+(or)
;;??
(defun set-property (phase args result)
  (declare (ignorable phase args result))
  (bind ((name (pop args))
	 (value
	  (progn (if (length-1-list-p args)
		     (first args)
		     args))))
    (assert name nil "name is required")
    (when (eq phase :parse)
      (setf (document-property name) value))
    nil))

(defextension (table-of-contents :arguments ((depth :required :keyword)
					     (start :required :keyword)
					     (label :keyword))
				  :insertp t)
  (ecase phase 
    (:parse
     (push (lambda (document)
	     (add-toc-anchors document :depth depth :start start))
	   (item-at-1 (properties *current-document*) :cleanup-functions))
     nil) 
    (:render
     (bind ((headers (collect-toc-headings depth start)))
       (when headers
	 (format *output-stream* 
		 "~&<a name='table-of-contents' id='table-of-contents'></a>")
	 (format *output-stream* "~&<div class='table-of-contents'>~%")
	 (when label
	   (format *output-stream* "<h1>~a</h1>" label))
	 (iterate-elements 
	  headers
	  (lambda (header)
	    (bind (((nil anchor text)
		    (item-at-1 (properties header) :anchor))
		   (save-header-lines (copy-list (lines header))))
	      (setf (slot-value header 'lines)
		    `(,(format nil
			       "~&<a href='~a~a' ~@[title='~a'~]>"
			       (if (char= (aref anchor 0) #\#) "" "#")
			       anchor
			       (encode-string-for-title text))
		       ,@(lines header)
		       ,(format nil "</a>")))
	      (render-to-html header nil)
	      (setf (slot-value header 'lines)
		    save-header-lines))))
	 (format *output-stream* "~&</div>~%"))))))

(defun collect-toc-headings (depth start)
  (collect-elements
   (chunks *current-document*)
   :filter (lambda (x) 
	     (header-p x :depth depth :start start))))

(defsimple-extension toc-link
  (format nil "~&<a href='#table-of-contents'>Top</a>"))

(defun make-ref (index level)
  (format nil "~(~a-~a~)" level index))

(defun add-toc-anchors (document &key depth start)
  (let* ((index -1)
         (header-level nil)
	 (last-anchor nil)
         (header-indexes
	  (nreverse
	   (collect-elements
	    (chunks document)
	    :transform
	    (lambda (chunk) 
	      (item-at-1 (properties chunk) :anchor))
	    :filter 
	    (lambda (chunk)
	      (incf index) 
	      (let ((it nil))
		(cond ((setf it (header-p chunk :depth depth 
					  :start start))
		       (setf header-level it)
		       (setf (item-at-1 (properties chunk) :anchor)
			     (list index
				   (or (and last-anchor
					    (url last-anchor))
				       (make-ref index header-level))
				   (with-output (*output-stream* nil) 
				     (render-plain chunk))))
		       (null last-anchor))
		     ((setf it (simple-anchor-p chunk))
		      (setf last-anchor it)
		      nil)
		     (t 
		      (setf last-anchor nil)))))))))
    (iterate-elements 
     header-indexes
     (lambda (datum)
;       (print datum)
       (bind (((index ref text) datum))
	 (anchor :parse `(,ref ,text) nil)
	 (insert-item-at 
	  (chunks document)
	  (make-instance 'chunk 
			 :lines `((eval anchor (,ref nil) nil t)))
	  index))))))
        
(defun simple-anchor-p (chunk)
  (and (< 0 (size (lines chunk)) 3)
       (length-at-least-p (first-element (lines chunk)) 2)
       (equal (subseq (first-element (lines chunk)) 0 2)
	      '(eval anchor))
       (fourth (first-element (lines chunk)))))

(defun header-p (chunk &key depth start)
  (let* ((header-elements  '(header1 header2 header3 
                             header4 header5 header6))
         (header-elements (subseq header-elements
                                  (1- (or start 1))
                                  (min (or depth (length header-elements))
                                         (length header-elements)))))
    (some-element-p (markup-class chunk)
                    (lambda (class)
                      (member class header-elements)))))

#+(or)
(markdown "{set-property html t}
html = {property html}
 I like {docs markdown function}, don't you." :additional-extensions '(docs))

#+(or)
(markdown 
 "{set-property docs-package asdf-install}
{set-property 
{docs install function}
{docs asdf-install:*gnu-tar-program* variable}
"
 :additional-extensions '(docs))

#|
{set-property docs-package asdf-install}
{set-property docs-heading-level 4}
{set-property docs-heading-format "%type %name:"}
{docs *gnu-tar-program* variable}
|#

(defextension (abbrev :arguments ((abbreviation :required)
				  (text :required :whole)))
  (ecase phase
    (:parse
     ;; no worries
     )
    (:render 
     (format nil "~a" abbreviation))))

(defextension (include :arguments ((pathname :required))
		       :insertp t)
  (ecase phase
    (:parse
     ;; no worries
     (let ((pathname (find-include-file pathname)))
       ;; FIXME - if I use a list, someone in markdown calls chunks on it.
       (make-array 1 :initial-contents 
		   (list (process-child-markdown 
			  pathname phase :transfer-data t)))))
    (:render
     (when result
       (render-to-stream (aref (first result) 0) *current-format* nil)))))

(defextension (include-if :arguments ((test :required) (pathname :required))
			  :insertp t)
  (ecase phase
    (:parse
     (when (document-property test)
       (let ((pathname (find-include-file pathname)))
	 ;; FIXME - if I use a list, someone in markdown calls chunks on it.
	 (make-array 1 :initial-contents 
		     (list (process-child-markdown 
			    pathname phase :transfer-data t))))))
    (:render
     (when result
       (render-to-stream (aref (first result) 0) *current-format* nil)))))