-
Notifications
You must be signed in to change notification settings - Fork 14
/
tables.lisp
220 lines (202 loc) · 7.81 KB
/
tables.lisp
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
;;; PHP Markdown Extra style tables
;; Only these table styles are supported, because they are unambigous to parse:
;;
;; | First Header | Second Header |
;; | ------------- | ------------- |
;; | Content Cell | Content Cell |
;; | Content Cell | Content Cell |
;;
;; Note that the dashes at the top don't need to match the length of the header text exactly:
;;
;; | Name | Description |
;; | ------------- | ----------- |
;; | Help | Display the help window.|
;; | Close | Closes a window |
;;
;; By including colons : within the header row, you can define text to be
;; left-aligned, right-aligned, or center-aligned:
;;
;; | Left-Aligned | Center Aligned | Right Aligned |
;; | :------------ |:---------------:| -----:|
;; | col 3 is | some wordy text | $1600 |
;; | col 2 is | centered | $12 |
;; | zebra stripes | are neat | $1 |
(defpackage #:3bmd-tables
(:export #:*tables*
#:*table-class*))
(in-package #:3bmd-grammar)
(defparameter 3bmd-tables:*table-class* nil
"Table class to be used in the rendered HTML")
(defrule table-cell (and #\|
sp
(* (and (! (or (and sp #\|) endline)) %inline))
sp
(& #\|))
(:destructure (_ __ content &rest ___)
(declare (ignore _ __ ___))
(mapcar 'second content)))
(defrule table-row (and (& #\|) (+ table-cell) #\| sp newline)
(:destructure (_ cells &rest __)
(declare (ignore _ __))
(mapcar (lambda (a) (cons :plain a)) cells)))
(defrule table-align-cell (and sp (? #\:) (+ #\-) (? #\:) sp #\|)
(:destructure (_ left __ right &rest ___)
(declare (ignore _ __ ___))
(if right (if left 'center 'right) (when left 'left))))
(defrule table-align-row (and #\| (+ table-align-cell) sp newline)
(:destructure (_ aligns &rest __)
(declare (ignore _ __))
aligns))
(defrule table-head (and table-row table-align-row))
(define-extension-block 3bmd-tables:*tables* table
(and (? nonindent-space) (? table-head) (+ table-row))
(:destructure (_ (&optional header aligns) rows)
(declare (ignore _))
(let* ((max-length (reduce 'max (mapcar 'length (cons header rows))))
(aligns (append aligns
(make-list (max 0 (- max-length (length aligns)))
:initial-element nil))))
(list '3bmd::table
:head (list (mapcar (lambda (content align)
(list 'th content align))
header aligns))
:body (mapcar (lambda (row)
(mapcar (lambda (content align)
(list 'td content align))
row aligns))
rows))))
(:escape-char-rule table-escaped-characters #\|)
(:character-rule table-extended-characters #\|))
(in-package #:3bmd)
(defmethod print-tagged-element ((tag (eql 'table)) stream rest)
(padded (1 stream)
(format stream "<table ~@[class=\"~a\"~]>" 3bmd-tables:*table-class*))
(when (getf rest :head)
(padded (1 stream)
(format stream "<thead>"))
(loop for row in (getf rest :head)
do (print-tagged-element 'tr stream row))
(padded (1 stream)
(format stream "</thead>")))
(padded (1 stream)
(format stream "<tbody>"))
(loop for row in (getf rest :body)
do (print-tagged-element 'tr stream row))
(padded (1 stream)
(format stream "</tbody>"))
(padded (1 stream)
(format stream "</table>")))
(defmethod print-tagged-element ((tag (eql 'tr)) stream rest)
(padded (1 stream)
(format stream "<tr>"))
(dolist (cell rest)
(format stream "<~(~A~)~A>"
(first cell)
(if (third cell)
(format nil " align=\"~(~A~)\"" (third cell))
""))
(print-element (second cell) stream)
(format stream "</~(~A~)>" (first cell)))
(format stream "</tr>"))
;;; tests
#+should-test (progn
(use-package :should-test)
(shadowing-import '(3bmd-grammar::th 3bmd-grammar::td
3bmd-grammar::left 3bmd-grammar::center
3bmd-grammar::right))
(deftest parse-table ()
(should be equal '((table
:head
(((th (:plain "First" " " "Header") nil)
(th (:plain "Second" " " "Header") nil)))
:body
(((td (:plain "Content" " " "Cell") nil)
(td (:plain "Content" " " "Cell") nil))
((td (:plain "Content" " " "Cell") nil)
(td (:plain "Content" " " "Cell") nil)))))
(parse-doc "
| First Header | Second Header |
| ------------- | ------------- |
| Content Cell | Content Cell |
| Content Cell | Content Cell |
"))
(should be equal '((table
:head
(((th (:plain "Name") nil)
(th (:plain "Description") nil)))
:body
(((td (:plain "Help") nil)
(td (:plain "Display" " " "the" " " "help" " " "window.") nil))
((td (:plain "Close") nil)
(td (:plain "Closes" " " "a" " " "window") nil)))))
(parse-doc "
| Name | Description |
| ------------- | ----------- |
| Help | Display the help window.|
| Close | Closes a window |
")) (should be equal '((table
:head
(((th (:plain "Left-Aligned") left)
(th (:plain "Center" " " "Aligned") center)
(th (:plain "Right" " " "Aligned") right)))
:body
(((td (:plain "col" " " (:code "3") " " "is") left)
(td (:plain "some" " " "wordy" " " "text") center)
(td (:plain "$1600") right))
((td (:plain "col" " " "2" " " "is") left)
(td (:plain "centered") center)
(td (:plain "$12") right))
((td (:plain "zebra" " " "stripes") left)
(td (:plain "are" " " "neat") center)
(td (:plain "$1") right)))))
(parse-doc "
| Left-Aligned | Center Aligned | Right Aligned |
| :------------ |:---------------:| -----:|
| col `3` is | some wordy text | $1600 |
| col 2 is | centered | $12 |
| zebra stripes | are neat | $1 |
"))
(should be equal '((TABLE
:head
(((TH (:PLAIN "a") NIL)
(TH (:PLAIN "b") NIL)))
:body
(((TD (:PLAIN "1" " " "a" " " "b" (:CODE "|")) NIL)
(TD (:PLAIN "2") NIL))
((TD (:PLAIN "3" " " "|") NIL)
(TD (:PLAIN "4") NIL))
((TD (:PLAIN ">" " " "5") NIL)
(TD (:PLAIN "#" "#" "#" " " "6") NIL)))))
(parse-doc "
| a | b |
|---|---|
| 1 a b`|`| 2 |
| 3 \\|| 4 |
| > 5 | ### 6 |
"))
(should be equal '((TABLE
:HEAD
(((3BMD-GRAMMAR::TH (:PLAIN "a") NIL)
(3BMD-GRAMMAR::TH (:PLAIN) NIL)))
:BODY
(((3BMD-GRAMMAR::TD (:PLAIN) NIL)
(3BMD-GRAMMAR::TD (:PLAIN "b") NIL)))))
(parse-doc "
| a | |
| - | - |
| | b |
"))
(should be equal '((TABLE
:HEAD
(((3BMD-GRAMMAR::TH (:PLAIN "|") NIL)
(3BMD-GRAMMAR::TH (:PLAIN) NIL)))
:BODY
(((3BMD-GRAMMAR::TD (:PLAIN) NIL)
(3BMD-GRAMMAR::TD (:PLAIN ".") NIL)))))
(parse-doc "
|\\|||
| - | - |
||.|
"))
)
)