Skip to content

Commit 3a0ce3e

Browse files
committed
add cell-padding-property as a style property for table cells
Also, add a `#:pad` optional argument to `tabular`, which provides a simple way of adding padding to cells --- especially as an alternative to `#:sep`, which has turned out to be awkward and bad in many cases. Padding is currently rendered for HTML, Latex/PDF, and text.
1 parent 85f4f3f commit 3a0ce3e

File tree

9 files changed

+212
-78
lines changed

9 files changed

+212
-78
lines changed

scribble-doc/scribblings/scribble/base.scrbl

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ Returns @racket[#t] if @racket[v] is an item produced by
269269

270270
@defproc[(tabular [cells (listof (listof (or/c block? content? 'cont)))]
271271
[#:style style (or/c style? string? symbol? #f) #f]
272+
[#:pad pad (or/c real? (list/c real? real?) (list/c real? real? real? real?)) 0]
272273
[#:sep sep (or/c block? content? #f) #f]
273274
[#:column-properties column-properties (listof any/c) '()]
274275
[#:row-properties row-properties (listof any/c) '()]
@@ -288,8 +289,17 @@ cell in a row.
288289
The @racket[style] argument is handled the same as @racket[para].
289290
See @racket[table] for a list of recognized @tech{style names} and @tech{style properties}.
290291

291-
The default style places no space between table columns. If
292-
@racket[sep] is not @racket[#f], it is inserted as a new column
292+
The default style places no space between table columns. Specify a
293+
@racket[pad] list to add padding around each cell's content, where the
294+
numbers in a @racket[pad] list are in ``ex'' units (roughly
295+
corresponding to the size of a character). If @racket[pad] is a single
296+
number, it is used for all sides of the cell. If @racket[pad] is a
297+
list of two numbers, the first is used on the left and right, and the
298+
last is used on the top and bottom. If @racket[pad] is a list of four
299+
numbers, they are used in order for left, top, right, and bottom
300+
padding.
301+
302+
If @racket[sep] is not @racket[#f], it is inserted as a new column
293303
between every column in the table; the new column's properties are the
294304
same as the preceding column's, unless @racket[sep-properties]
295305
provides a list of @tech{style properties} to use. When @racket[sep]
@@ -351,22 +361,18 @@ redundant information. In that case, @racket[column-attributes]
351361
properties will be used from @racket[table-columns], while other
352362
properties will be used from the merger into @racket[table-cells].}
353363

354-
@history[#:changed "1.1" @elem{Added the @racket[#:column-properties],
355-
@racket[#:row-properties],
356-
and @racket[#:cell-properties] arguments.}
357-
#:changed "1.12" @elem{Changed @racket[sep] insertion before a
358-
@racket['cont].}
359-
#:changed "1.28" @elem{Added @racket[sep-properties] and made
360-
the preceding column's properties used
361-
consistently if not specified.}]
362-
363364
Examples:
364365
@codeblock[#:keep-lang-line? #f]|{
365366
#lang scribble/manual
366367
@tabular[#:sep @hspace[1]
367368
(list (list "soup" "gazpacho")
368369
(list "soup" "tonjiru"))]
369370

371+
@tabular[#:pad '(1 0)
372+
#:column-properties '(border)
373+
(list (list "gazpacho" "cold")
374+
(list "tonjiru" "hot"))]
375+
370376
@tabular[#:style 'boxed
371377
#:column-properties '(left right)
372378
#:row-properties '(bottom-border ())
@@ -380,13 +386,28 @@ Examples:
380386
(list (list "soup" "gazpacho")
381387
(list "soup" "tonjiru"))]
382388

389+
@tabular[#:pad '(1 0)
390+
#:column-properties '(border)
391+
(list (list "gazpacho" "cold")
392+
(list "tonjiru" "hot"))]
393+
383394
@tabular[#:style 'boxed
384395
#:column-properties '(left right)
385396
#:row-properties '(bottom-border ())
386397
(list (list @bold{recipe} @bold{vegetable})
387398
(list "caldo verde" "kale")
388399
(list "kinpira gobō" "burdock")
389400
(list "makizushi" 'cont))]]
401+
402+
@history[#:changed "1.1" @elem{Added the @racket[#:column-properties],
403+
@racket[#:row-properties],
404+
and @racket[#:cell-properties] arguments.}
405+
#:changed "1.12" @elem{Changed @racket[sep] insertion before a
406+
@racket['cont].}
407+
#:changed "1.28" @elem{Added the @racket[sep-properties] argument and made
408+
the preceding column's properties used
409+
consistently if not specified.}
410+
#:changed "1.59" @elem{Added the @racket[pad] argument.}]
390411
}
391412

392413
@defproc[(verbatim [#:indent indent exact-nonnegative-integer? 0] [elem content?] ...+)

scribble-doc/scribblings/scribble/core.scrbl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,9 @@ The following are recognized as cell-@tech{style properties}:
13551355
@item{@racket[background-color-property] structure --- For HTML,
13561356
applies a color to the background of the cell.}
13571357

1358+
@item{@racket[cell-padding-property] structure --- Specifies padding
1359+
to add around a cell's content.}
1360+
13581361
@item{@racket[attributes] --- Provides additional HTML attributes
13591362
for the cell's @tt{<td>} tag.}
13601363

@@ -1382,6 +1385,15 @@ For HTML table rendering, for each column that has a
13821385
within the table.}
13831386

13841387

1388+
@defstruct[cell-padding-property ([left real?] [top real?] [right real?] [bottom real?])]{
1389+
1390+
When used as a cell property for a @racket[table], specifies padding
1391+
to add around the cell content. Padding is expressed in ``ex'' units,
1392+
which corresponds roughly to the width of a character.
1393+
1394+
@history[#:added "1.58"]}
1395+
1396+
13851397
@deftogether[(
13861398
@defstruct[box-mode ([top-name string?]
13871399
[center-name string?]

scribble-lib/info.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
(define pkg-authors '(mflatt eli))
2525

26-
(define version "1.57")
26+
(define version "1.58")
2727

2828
(define license
2929
'((Apache-2.0 OR MIT)

scribble-lib/scribble/base.rkt

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@
338338
compound-paragraph?)]
339339
[tabular (->* ((listof (listof (or/c 'cont block? content?))))
340340
(#:style (or/c style? string? symbol? #f)
341+
#:pad (or/c real? (list/c real? real?) (list/c real? real? real? real?))
341342
#:sep (or/c content? block? #f)
342343
#:column-properties (listof any/c)
343344
#:row-properties (listof any/c)
@@ -364,6 +365,7 @@
364365
(decode-flow c)))
365366

366367
(define (tabular #:style [style #f]
368+
#:pad [pad 0]
367369
#:sep [sep #f]
368370
#:sep-properties [sep-props #f]
369371
#:column-properties [column-properties null]
@@ -486,7 +488,7 @@
486488
(define all-column-properties
487489
(and (pair? column-properties)
488490
full-column-properties))
489-
;; Will werge `cell-properties` and `column-properties` into
491+
;; Will merge `cell-properties` and `column-properties` into
490492
;; `s`. Start by finding any existing `table-columns`
491493
;; and `table-cells` properties with the right number of
492494
;; styles:
@@ -510,6 +512,21 @@
510512
(table-cells-styless tl)))
511513
tl
512514
#f))))
515+
(define pad-prop
516+
(cond
517+
[(number? pad)
518+
(and (not (= 0 pad)) (cell-padding-property pad pad pad pad))]
519+
[(= 2 (length pad))
520+
(and (not (= 0 (car pad) (cadr pad)))
521+
(cell-padding-property (car pad) (cadr pad) (car pad) (cadr pad)))]
522+
[(not (= 0 (list-ref pad 0) (list-ref pad 1) (list-ref pad 2) (list-ref pad 3)))
523+
(cell-padding-property (list-ref pad 0) (list-ref pad 1)
524+
(list-ref pad 2) (list-ref pad 3))]))
525+
(define (add-padding props)
526+
(if (or (not pad-prop)
527+
(ormap cell-padding-property? props))
528+
props
529+
(cons pad-prop props)))
513530
;; Merge:
514531
(define (cons-maybe v l) (if v (cons v l) l))
515532
(make-style (style-name s)
@@ -520,22 +537,31 @@
520537
(for/list ([ps (in-list all-column-properties)]
521538
[cs (in-list (table-columns-styles tc))])
522539
(make-style (style-name cs)
523-
(append ps (style-properties cs))))
540+
(add-padding
541+
(append ps (style-properties cs)))))
524542
(for/list ([ps (in-list all-column-properties)])
525-
(make-style #f ps)))))
543+
(make-style #f (add-padding ps))))))
526544
(cons-maybe
527-
(and all-cell-properties
528-
(table-cells
529-
(if tl
530-
(for/list ([pss (in-list all-cell-properties)]
531-
[css (in-list (table-cells-styless tl))])
532-
(for/list ([ps (in-list pss)]
533-
[cs (in-list css)])
534-
(make-style (style-name cs)
535-
(append ps (style-properties cs)))))
536-
(for/list ([pss (in-list all-cell-properties)])
537-
(for/list ([ps (in-list pss)])
538-
(make-style #f ps))))))
545+
(if all-cell-properties
546+
(table-cells
547+
(if tl
548+
(for/list ([pss (in-list all-cell-properties)]
549+
[css (in-list (table-cells-styless tl))])
550+
(for/list ([ps (in-list pss)]
551+
[cs (in-list css)])
552+
(make-style (style-name cs)
553+
(add-padding
554+
(append ps (style-properties cs))))))
555+
(for/list ([pss (in-list all-cell-properties)])
556+
(for/list ([ps (in-list pss)])
557+
(make-style #f (add-padding ps))))))
558+
(if (and pad-prop
559+
(not all-column-properties))
560+
(table-cells
561+
(for/list ([row (in-list cells)])
562+
(for/list ([cell (in-list row)])
563+
(make-style #f (list pad-prop)))))
564+
#f))
539565
(remq tc (remq tl props))))))
540566
;; Process cells:
541567
(map (lambda (row)

scribble-lib/scribble/core.rkt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@
299299
[color-property ([color (or/c string? (list/c byte? byte? byte?))])]
300300
[background-color-property ([color (or/c string? (list/c byte? byte? byte?))])]
301301
[numberer-property ([numberer numberer?] [argument any/c])]
302+
[cell-padding-property ([left real?] [top real?] [right real?] [bottom real?])]
302303

303304
[table-columns ([styles (listof style?)])]
304305
[table-cells ([styless (listof (listof style?))])]

scribble-lib/scribble/html-render.rkt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,13 @@
184184
`((style ,(format "background-color: ~a" (color->string (background-color-property-color v)))))]
185185
[(hover-property? v)
186186
`((title ,(hover-property-text v)))]
187+
[(cell-padding-property? v)
188+
(define (ex n) (exact->inexact n))
189+
`((style ,(format "padding: ~aex ~aex ~aex ~aex;"
190+
(ex (cell-padding-property-top v))
191+
(ex (cell-padding-property-right v))
192+
(ex (cell-padding-property-bottom v))
193+
(ex (cell-padding-property-left v)))))]
187194
[else null]))
188195
(style-properties style))))])
189196
(let ([name (style-name style)])
@@ -1735,7 +1742,8 @@
17351742
(filter (lambda (a)
17361743
(or (attributes? a)
17371744
(color-property? a)
1738-
(background-color-property? a)))
1745+
(background-color-property? a)
1746+
(cell-padding-property? a)))
17391747
(style-properties column-style)))
17401748
(let ([ps (style-properties column-style)])
17411749
(cond

0 commit comments

Comments
 (0)