Skip to content

Commit 28206e3

Browse files
committed
markdown: Support ordered lists that start past 1
This is needed by “ADTs in Typed Racket with macros”.
1 parent 4dac709 commit 28206e3

File tree

6 files changed

+35
-30
lines changed

6 files changed

+35
-30
lines changed

blog/build.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"2015-08-30-managing-application-configuration-with-envy.md"
5252
"2015-09-23-canonical-factories-for-testing-with-factory-girl-api.md"
5353
"2015-11-06-functionally-updating-record-types-in-elm.md"
54-
#;"2015-12-21-adts-in-typed-racket-with-macros.md" ; <ol start="5">
54+
"2015-12-21-adts-in-typed-racket-with-macros.md"
5555
"2016-02-18-simple-safe-multimethods-in-racket.md"
5656
"2016-06-03-four-months-with-haskell.md"
5757
"2016-08-11-climbing-the-infinite-ladder-of-abstraction.md"

blog/build/render/page.rkt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
[page (->* [#:title string? #:body xexpr/c] [#:tag (or/c string? #f)] xexpr/c)]
1818
[post-page (-> rendered-post? xexpr/c)]
1919
[index-page-title (->* [] [#:tag (or/c string? #f)] string?)]
20-
[index-page (->i ([total-pages (and/c exact-integer? (>=/c 1))]
21-
[page-number (total-pages) (and/c exact-integer? (>=/c 1) (<=/c total-pages))]
20+
[index-page (->i ([total-pages exact-positive-integer?]
21+
[page-number (total-pages) (and/c exact-positive-integer? (<=/c total-pages))]
2222
[posts (listof rendered-post?)])
2323
(#:tag [tag (or/c string? #f)])
2424
[result xexpr/c])]))

blog/markdown/parse.rkt

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
(struct paragraph ([content content?]))
2727
(struct code-block ([content content?] [language (or/c #f string?)]))
2828
(struct unordered-list ([blockss (listof (listof block?))]))
29-
(struct ordered-list ([blockss (listof (listof block?))]))
29+
(struct ordered-list ([start exact-positive-integer?]
30+
[blockss (listof (listof block?))]))
3031
(struct blockquote ([blocks (listof block?)]))
3132
(struct html-block ([xexpr xexpr/c]))
3233

@@ -39,7 +40,7 @@
3940
(struct paragraph (content) #:transparent)
4041
(struct code-block (content language) #:transparent)
4142
(struct unordered-list (blockss) #:transparent)
42-
(struct ordered-list (blockss) #:transparent)
43+
(struct ordered-list (start blockss) #:transparent)
4344
(struct blockquote (blocks) #:transparent)
4445
(struct html-block (xexpr) #:transparent)
4546
(define-values [horizontal-rule horizontal-rule?]
@@ -62,8 +63,8 @@
6263
(define (spaces/p n) (repeat/p n (char/p #\space)))
6364
(define maybe-indent/p (map/p length (many/p (char/p #\space))))
6465

65-
(define unordered-list-bullet/p (or/p (string/p "* ") (string/p "- ")))
66-
(define ordered-list-bullet/p (do integer/p (string/p ". ")))
66+
(define unordered-list-bullet/p (do (or/p (string/p "* ") (string/p "- ")) (pure #f)))
67+
(define ordered-list-bullet/p (do [n <- integer/p] (string/p ". ") (pure n)))
6768

6869
;; -----------------------------------------------------------------------------
6970

@@ -125,7 +126,7 @@ As it parses the indentation for each block, it transfers it from the
125126
`out-blocks` stack to the `in-blocks` stack, morally “entering” the block. |#
126127

127128
; see Note [Open blocks]
128-
(struct open-list (type indent blockss) #:transparent)
129+
(struct open-list (start indent blockss) #:transparent)
129130
(struct list-element (indent blocks) #:transparent)
130131
(struct footnote (name blocks) #:transparent)
131132

@@ -137,10 +138,10 @@ As it parses the indentation for each block, it transfers it from the
137138

138139
(define (close-block block)
139140
(match block
140-
[(open-list 'unordered _ blockss) (unordered-list blockss)]
141-
[(open-list 'ordered _ blockss) (ordered-list blockss)]
142-
[(paragraph content) (paragraph (simplify-content content))]
143-
[_ block]))
141+
[(open-list #f _ blockss) (unordered-list blockss)]
142+
[(open-list start _ blockss) (ordered-list start blockss)]
143+
[(paragraph content) (paragraph (simplify-content content))]
144+
[_ block]))
144145

145146
; Closes a sequence of open blocks. Returns two values:
146147
; 1. a list of (closed) blocks
@@ -158,11 +159,11 @@ As it parses the indentation for each block, it transfers it from the
158159

159160
(define (add-blocks open-block new-blocks)
160161
(match open-block
161-
[(document blocks targets notes) (document (append blocks new-blocks) targets notes)]
162-
[(open-list type indent blockss) (open-list type indent (append blockss (map list-element-blocks new-blocks)))]
163-
[(list-element indent blocks) (list-element indent (append blocks new-blocks))]
164-
[(blockquote blocks) (blockquote (append blocks new-blocks))]
165-
[(footnote name blocks) (footnote name (append blocks new-blocks))]))
162+
[(document blocks targets notes) (document (append blocks new-blocks) targets notes)]
163+
[(open-list start indent blockss) (open-list start indent (append blockss (map list-element-blocks new-blocks)))]
164+
[(list-element indent blocks) (list-element indent (append blocks new-blocks))]
165+
[(blockquote blocks) (blockquote (append blocks new-blocks))]
166+
[(footnote name blocks) (footnote name (append blocks new-blocks))]))
166167

167168
(define (add-footnotes doc new-notes)
168169
(define notes (append (document-footnotes doc) new-notes))
@@ -261,10 +262,10 @@ As it parses the indentation for each block, it transfers it from the
261262

262263
; If we’re in a list (but not a list element), parse a new list element.
263264
; See Note [Unconditionally parse list elements] for why we don’t have to handle anything else.
264-
[(open-list 'unordered _ _)
265+
[(open-list #f _ _)
265266
(do unordered-list-bullet/p
266267
(scan-block-content (list-element 2 '()) (cons inner-block in-blocks)))]
267-
[(open-list 'ordered _ _)
268+
[(open-list _ _ _)
268269
(do [bullet <- (syntax-box/p ordered-list-bullet/p)]
269270
(scan-block-content (list-element (srcloc-span (syntax-box-srcloc bullet)) '())
270271
(cons inner-block in-blocks)))]
@@ -306,8 +307,8 @@ As it parses the indentation for each block, it transfers it from the
306307
(scan-line-start (reverse (list* (code-block "" language) inner-block in-blocks))))
307308

308309
; lists
309-
(scan-list-start (cons inner-block in-blocks) 'unordered unordered-list-bullet/p)
310-
(scan-list-start (cons inner-block in-blocks) 'ordered ordered-list-bullet/p)
310+
(scan-list-start (cons inner-block in-blocks) unordered-list-bullet/p)
311+
(scan-list-start (cons inner-block in-blocks) ordered-list-bullet/p)
311312

312313
; html blocks
313314
(do (lookahead/p (or/p (try/p (string/p "<div"))
@@ -334,11 +335,12 @@ As it parses the indentation for each block, it transfers it from the
334335
(many/p (char/p #\space))
335336
(scan-block-content (footnote name '()) in-blocks)))
336337

337-
(define (scan-list-start in-blocks type start-p)
338-
(do [indent <- (try/p (do [indent <- maybe-indent/p]
339-
(lookahead/p start-p)
340-
(pure indent)))]
341-
(scan-block-content (open-list type indent '()) in-blocks)))
338+
(define (scan-list-start in-blocks start-p)
339+
(do [(list indent start)
340+
<- (try/p (do [indent <- maybe-indent/p]
341+
[start <- (lookahead/p start-p)]
342+
(pure (list indent start))))]
343+
(scan-block-content (open-list start indent '()) in-blocks)))
342344

343345
#| Note [Unconditionally parse list elements]
344346
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

blog/markdown/to-scribble.rkt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#lang racket/base
22

33
(require racket/contract
4+
racket/format
45
racket/list
56
racket/match
67
scribble/base
@@ -89,8 +90,10 @@
8990
(code-block content))]
9091
[(md:unordered-list md-blockss)
9192
(itemization plain (map blocks->blocks md-blockss))]
92-
[(md:ordered-list md-blockss)
93-
(itemization (style 'ordered '()) (map blocks->blocks md-blockss))]
93+
[(md:ordered-list start md-blockss)
94+
(define props (if (= start 1) '()
95+
(list (attributes (list (cons 'start (~a start)))))))
96+
(itemization (style 'ordered props) (map blocks->blocks md-blockss))]
9497
[(md:blockquote md-blocks)
9598
(nested-flow (style 'nested '()) (blocks->blocks md-blocks))]
9699
[(? md:horizontal-rule?)

blog/paths.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
[posts-dir path?]
1515

1616
[site-path? predicate/c]
17-
[index-path (->* [] [(and/c exact-integer? (>=/c 1))
17+
[index-path (->* [] [exact-positive-integer?
1818
#:tag (or/c string? #f)
1919
#:file? any/c]
2020
site-path?)]

blog/posts/2015-12-21-adts-in-typed-racket-with-macros.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ data Tree a = Empty
4141

4242
With this in mind, we can add a fifth and final point to our list:
4343

44-
<ol start="5"><li>ADTs must be able to be parametrically polymorphic.</li></ol>
44+
5. ADTs must be able to be parametrically polymorphic.
4545

4646
That covers all of our requirements for basic ADTs. Now we're ready to port this idea to Racket.
4747

0 commit comments

Comments
 (0)