Skip to content

Commit e2d2137

Browse files
committed
core: add itemization-ordered style property
`itemization-ordered` makes it possible to start an ordered itemization at an arbitrary number. This style property is supported in all backends. Also implement ordered itemization for the Markdown and text backends (previously, all itemizations are considered unordered).
1 parent 4ab0b00 commit e2d2137

File tree

12 files changed

+223
-32
lines changed

12 files changed

+223
-32
lines changed

scribble-doc/scribblings/scribble/base.scrbl

+5
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ content.}
255255
Returns @racket[#t] if @racket[v] is an item produced by
256256
@racket[item], @racket[#f] otherwise.}
257257

258+
@defproc[(ordered [#:start start number? 1]) style?]{
259+
260+
Constructs a @tech{style} for an @racket[itemization] to make it
261+
an ordered itemization. @racket[start] indicates the starting number
262+
in the ordered itemization.}
258263

259264
@defproc[(tabular [cells (listof (listof (or/c block? content? 'cont)))]
260265
[#:style style (or/c style? string? symbol? #f) #f]

scribble-doc/scribblings/scribble/core.scrbl

+10
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,9 @@ The following @tech{style properties} are currently recognized:
717717
@item{@indexed-racket['never-indents] --- For Latex and @tech{compound
718718
paragraphs}; see @racket[compound-paragraph].}
719719

720+
@item{@racket[itemization-ordered] structure --- Indicates the starting number.
721+
This property should always be used with the @racket['ordered] style name.}
722+
720723
]}
721724

722725

@@ -1262,6 +1265,13 @@ latter case)or cell in a @racket[table].}
12621265

12631266
Like @racket[color-property], but sets the background color.}
12641267

1268+
@defstruct[itemization-ordered ([start number?])]{
1269+
1270+
Used as a @tech{style property} for an @racket[itemization] to indicate
1271+
that the ordered itemization should start with @racket[_start].
1272+
The associated style name @bold{must} be @racket['ordered].
1273+
The function @racket[ordered] creates a style with
1274+
both @racket['ordered] style name and @racket[itemization-ordered] style property.}
12651275

12661276
@defstruct[table-cells ([styless (listof (listof style?))])]{
12671277

scribble-lib/scribble/base.rkt

+7-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@
161161
[item (->* ()
162162
()
163163
#:rest (listof pre-flow?)
164-
item?)])
164+
item?)]
165+
[ordered (->* ()
166+
(#:start exact-nonnegative-integer?)
167+
style?)])
165168
(provide/contract
166169
[item? (any/c . -> . boolean?)])
167170

@@ -185,6 +188,9 @@
185188
(define (item . str)
186189
(make-an-item (decode-flow str)))
187190

191+
(define (ordered #:start [start 1])
192+
(make-style 'ordered (list (itemization-ordered start))))
193+
188194
;; ----------------------------------------
189195

190196
(provide ._ .__ ~ ?- -~-)

scribble-lib/scribble/core.rkt

+1
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@
293293
[color-property ([color (or/c string? (list/c byte? byte? byte?))])]
294294
[background-color-property ([color (or/c string? (list/c byte? byte? byte?))])]
295295
[numberer-property ([numberer numberer?] [argument any/c])]
296+
[itemization-ordered ([start number?])]
296297

297298
[table-columns ([styles (listof style?)])]
298299
[table-cells ([styless (listof (listof style?))])]

scribble-lib/scribble/html-render.rkt

+22-13
Original file line numberDiff line numberDiff line change
@@ -167,24 +167,27 @@
167167
(cdr l))]
168168
[else (cons (car l) (merge-styles s cls (cdr l)))]))
169169

170-
(define (style->attribs style [extras null])
170+
(define (style->attribs style [extras null]
171+
#:transform-property [transform-property (λ (v proc) (proc v))])
172+
(define (default-transform-property v)
173+
(cond
174+
[(attributes? v)
175+
(map (lambda (v) (list (car v) (cdr v))) (attributes-assoc v))]
176+
[(color-property? v)
177+
`((style ,(format "color: ~a" (color->string (color-property-color v)))))]
178+
[(background-color-property? v)
179+
`((style ,(format "background-color: ~a" (color->string (background-color-property-color v)))))]
180+
[(hover-property? v)
181+
`((title ,(hover-property-text v)))]
182+
[else null]))
183+
171184
(let ([a (merge-styles
172185
#f
173186
#f
174187
(apply
175188
append
176189
extras
177-
(map (lambda (v)
178-
(cond
179-
[(attributes? v)
180-
(map (lambda (v) (list (car v) (cdr v))) (attributes-assoc v))]
181-
[(color-property? v)
182-
`((style ,(format "color: ~a" (color->string (color-property-color v)))))]
183-
[(background-color-property? v)
184-
`((style ,(format "background-color: ~a" (color->string (background-color-property-color v)))))]
185-
[(hover-property? v)
186-
`((title ,(hover-property-text v)))]
187-
[else null]))
190+
(map (λ (v) (transform-property v default-transform-property))
188191
(style-properties style))))])
189192
(let ([name (style-name style)])
190193
(if (string? name)
@@ -1830,7 +1833,13 @@
18301833
`((,(if (eq? 'ordered (style-name (itemization-style t)))
18311834
'ol
18321835
'ul)
1833-
(,@(style->attribs (itemization-style t))
1836+
(,@(style->attribs (itemization-style t)
1837+
#:transform-property
1838+
(λ (v transform-property)
1839+
(cond
1840+
[(itemization-ordered? v)
1841+
`((start ,(number->string (itemization-ordered-start v))))]
1842+
[else (transform-property v)])))
18341843
,@(if (eq? 'compact (style-name (itemization-style t)))
18351844
`([class "compact"])
18361845
'()))

scribble-lib/scribble/latex-render.rkt

+9-3
Original file line numberDiff line numberDiff line change
@@ -924,16 +924,22 @@
924924
[(b) box-mode-bottom-name]))
925925

926926
(define/override (render-itemization t part ri)
927-
(let* ([style-str (let ([s (style-name (itemization-style t))])
927+
(let* ([st (itemization-style t)]
928+
[style-str (let ([s (style-name st)])
928929
(if (eq? s 'compact)
929930
"compact"
930931
s))]
931932
[mode (or (and (string? style-str)
932933
style-str)
933934
(if (eq? 'ordered style-str)
934935
"enumerate"
935-
"itemize"))])
936-
(printf "\\begin{~a}\\atItemizeStart" mode)
936+
"itemize"))]
937+
[ordered-option (or (for/first ([prop (in-list (style-properties st))]
938+
#:when (itemization-ordered? prop))
939+
(format "\\setcounter{enumi}{~a}"
940+
(sub1 (itemization-ordered-start prop))))
941+
"")])
942+
(printf "\\begin{~a}~a\\atItemizeStart" mode ordered-option)
937943
(for ([flow (in-list (itemization-blockss t))])
938944
(printf "\n\n\\~a" (if (string? style-str)
939945
(format "~aItem{" style-str)

scribble-lib/scribble/markdown-render.rkt

+29-8
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,39 @@
151151
null)
152152

153153
(define/override (render-itemization i part ht)
154+
;; make-render-marker :: style? -> (-> number?)
155+
(define (make-render-marker st)
156+
(case (style-name st)
157+
[(ordered)
158+
(define offset 0)
159+
(define start
160+
(or (for/first ([prop (in-list (style-properties st))]
161+
#:when (itemization-ordered? prop))
162+
(itemization-ordered-start prop))
163+
1))
164+
(λ ()
165+
(define output (format "~a) " (+ offset start)))
166+
(set! offset (add1 offset))
167+
(write-string output))]
168+
[else (λ () (write-string "* "))]))
169+
(define render-marker (make-render-marker (itemization-style i)))
154170
(let ([flows (itemization-blockss i)])
155171
(if (null? flows)
156172
null
157173
(append*
158-
(begin (printf "* ")
159-
(parameterize ([current-indent (make-indent 2)])
160-
(render-flow (car flows) part ht #t)))
161-
(for/list ([d (in-list (cdr flows))])
162-
(indented-newline)
163-
(printf "* ")
164-
(parameterize ([current-indent (make-indent 2)])
165-
(render-flow d part ht #f)))))))
174+
(let ([marker-size (render-marker)])
175+
(parameterize ([current-indent (make-indent marker-size)])
176+
(render-flow (car flows) part ht #t)))
177+
(begin0 (for/list ([d (in-list (cdr flows))])
178+
(indented-newline)
179+
(define marker-size (render-marker))
180+
(parameterize ([current-indent (make-indent marker-size)])
181+
(render-flow d part ht #f)))
182+
(when (eq? 'ordered (style-name (itemization-style i)))
183+
(indented-newline)
184+
;; we add this to force Markdown to reset
185+
;; the consecutive numbering
186+
(display "<!-- end of ordered itemization -->")))))))
166187

167188
(define/override (render-paragraph p part ri)
168189
(define (write-note)

scribble-lib/scribble/text-render.rkt

+21-5
Original file line numberDiff line numberDiff line change
@@ -233,17 +233,33 @@
233233
null)))
234234

235235
(define/override (render-itemization i part ht)
236+
;; make-render-marker :: style? -> (-> number?)
237+
(define (make-render-marker st)
238+
(case (style-name st)
239+
[(ordered)
240+
(define offset 0)
241+
(define start
242+
(or (for/first ([prop (in-list (style-properties st))]
243+
#:when (itemization-ordered? prop))
244+
(itemization-ordered-start prop))
245+
1))
246+
(λ ()
247+
(define output (format "~a) " (+ offset start)))
248+
(set! offset (add1 offset))
249+
(write-string output))]
250+
[else (λ () (write-string "* "))]))
251+
(define render-marker (make-render-marker (itemization-style i)))
236252
(let ([flows (itemization-blockss i)])
237253
(if (null? flows)
238254
null
239255
(append*
240-
(begin (printf "* ")
241-
(parameterize ([current-indent (make-indent 2)])
242-
(render-flow (car flows) part ht #t)))
256+
(let ([marker-size (render-marker)])
257+
(parameterize ([current-indent (make-indent marker-size)])
258+
(render-flow (car flows) part ht #t)))
243259
(for/list ([d (in-list (cdr flows))])
244260
(indented-newline)
245-
(printf "* ")
246-
(parameterize ([current-indent (make-indent 2)])
261+
(define marker-size (render-marker))
262+
(parameterize ([current-indent (make-indent marker-size)])
247263
(render-flow d part ht #f)))))))
248264

249265
(define/override (render-paragraph p part ri)

scribble-test/tests/scribble/docs/text.scrbl

+41
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,47 @@ Run the @exec{scribble} command(s) from the old section
8585
again. You may notice the curly double-quotes in the output, and
8686
the @litchar{---} turned into an em dash.
8787

88+
Ordered itemization is supported, too, and it can be started at a specified number.
89+
Here, we start at 10.
90+
91+
@itemlist[
92+
#:style (ordered #:start 10)
93+
94+
@item{Run
95+
@commandline{scribble --pdf mouse.scrbl}
96+
to generate PDF as @filepath{mouse.pdf}. This will
97+
work only if you have @exec{pdflatex} installed.
98+
If you'd like to see the intermediate Latex, try
99+
@commandline{scribble --latex mouse.scrbl}
100+
to generate @filepath{mouse.tex}.}
101+
102+
@item{Run
103+
@commandline{scribble --html mouse.scrbl}
104+
to generate HTML as @filepath{mouse.html}. You may
105+
notice that the apostrophe in ``he's'' turned into a
106+
curly apostrophe.}
107+
]
108+
109+
Subsequent ordered itemization should not continue the earlier itemization.
110+
111+
@itemlist[
112+
#:style (ordered)
113+
114+
@item{Run
115+
@commandline{scribble --pdf mouse.scrbl}
116+
to generate PDF as @filepath{mouse.pdf}. This will
117+
work only if you have @exec{pdflatex} installed.
118+
If you'd like to see the intermediate Latex, try
119+
@commandline{scribble --latex mouse.scrbl}
120+
to generate @filepath{mouse.tex}.}
121+
122+
@item{Run
123+
@commandline{scribble --html mouse.scrbl}
124+
to generate HTML as @filepath{mouse.html}. You may
125+
notice that the apostrophe in ``he's'' turned into a
126+
curly apostrophe.}
127+
]
128+
88129
@section{C}
89130

90131
@subsection{Inside C}

scribble-test/tests/scribble/docs/text.txt

+42
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,48 @@ Run the scribble command(s) from the old section again. You may notice
7474
the curly double-quotes in the output, and the --- turned into an em
7575
dash.
7676

77+
Ordered itemization is supported, too, and it can be started at a
78+
specified number. Here, we start at 10.
79+
80+
10) Run
81+
82+
  scribble --pdf mouse.scrbl
83+
84+
to generate PDF as "mouse.pdf". This will work only if you have
85+
pdflatex installed. If you’d like to see the intermediate Latex, try
86+
87+
  scribble --latex mouse.scrbl
88+
89+
to generate "mouse.tex".
90+
91+
11) Run
92+
93+
  scribble --html mouse.scrbl
94+
95+
to generate HTML as "mouse.html". You may notice that the
96+
apostrophe in “he’s” turned into a curly apostrophe.
97+
98+
Subsequent ordered itemization should not continue the earlier
99+
itemization.
100+
101+
1) Run
102+
103+
  scribble --pdf mouse.scrbl
104+
105+
to generate PDF as "mouse.pdf". This will work only if you have
106+
pdflatex installed. If you’d like to see the intermediate Latex, try
107+
108+
  scribble --latex mouse.scrbl
109+
110+
to generate "mouse.tex".
111+
112+
2) Run
113+
114+
  scribble --html mouse.scrbl
115+
116+
to generate HTML as "mouse.html". You may notice that the apostrophe
117+
in “he’s” turned into a curly apostrophe.
118+
77119
3. C
78120

79121
3.1. Inside C

scribble-test/tests/scribble/markdown-docs/example.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,27 @@ This is a subsubsection.
1414

1515
Here is an itemize:
1616

17-
* Item 1.
17+
* This sentence is really long and should really be broken up into
18+
multiple lines because wow it is so long.
1819

1920
* Item 2.
2021

22+
Here is an ordered itemize, starting at 10.
23+
24+
10) This sentence is really long and should really be broken up into
25+
multiple lines because wow it is so long.
26+
27+
11) Item 11.
28+
29+
<!-- end of ordered itemization -->
30+
Here is an ordered itemize, with the normal start.
31+
32+
1) This sentence is really long and should really be broken up into
33+
multiple lines because wow it is so long.
34+
35+
2) Item 2.
36+
37+
<!-- end of ordered itemization -->
2138
Here is a hyperlink:
2239

2340
[I am a hyperlink to Racket.](http://racket-lang.org/)

scribble-test/tests/scribble/markdown-docs/example.scrbl

+18-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,24 @@ This is a subsubsection.
2222
Here is an itemize:
2323

2424
@itemize[
25-
@item{Item 1.}
25+
@item{This sentence is really long and should really be broken up into multiple lines
26+
because wow it is so long.}
27+
@item{Item 2.}
28+
]
29+
30+
Here is an ordered itemize, starting at 10.
31+
32+
@itemize[#:style (ordered #:start 10)
33+
@item{This sentence is really long and should really be broken up into multiple lines
34+
because wow it is so long.}
35+
@item{Item 11.}
36+
]
37+
38+
Here is an ordered itemize, with the normal start.
39+
40+
@itemize[#:style (ordered)
41+
@item{This sentence is really long and should really be broken up into multiple lines
42+
because wow it is so long.}
2643
@item{Item 2.}
2744
]
2845

0 commit comments

Comments
 (0)