lowdown / lowdown-impl.scm

  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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
(import chicken scheme)
(use srfi-1 srfi-13 srfi-14 data-structures irregex
     comparse clojurian-syntax latch sxml-transforms char-set-literals)

(define (node el parser)
  (sequence* ((content parser))
    (result (if (list? content)
                (cons el content)
                (list el content)))))

(define end-of-input
  (none-of* item (result #\newline)))

(define space-chars
  (char-set #\space #\tab))

(define space-char
  (preceded-by (in space-chars)
               (result #\space)))

(define space*
  (zero-or-more space-char))

(define space+
  (one-or-more space-char))

(define new-line
  (->> (preceded-by (is #\return) (maybe (is #\newline)))
       (any-of (is #\newline))))

(define space-new-line
  (sequence space* (maybe (preceded-by new-line space*))))

(define normal-line-end
  (preceded-by space* new-line))

(define terminal-line-end
  (preceded-by space* new-line end-of-input))

(define line-break
  (preceded-by (char-seq "  ") normal-line-end (result '(br))))

(define line-end
  (any-of line-break
          terminal-line-end
          normal-line-end))

(define blank-line
  (preceded-by space* new-line))

(define non-indent-space
  (repeated (is #\space) max: 3))

(define special-chars
  (char-set #\* #\_ #\`
            #\& #\[ #\]
            #\( #\) #\<
            #\! #\# #\\
            #\' #\| #\"
            #\- #\. #\^))

(define special-char
  (in special-chars))

(define normal-chars
  (char-set-complement!
   (char-set-union
    special-chars
    space-chars
    (char-set #\newline))))

(define normal-char
  (in normal-chars))

(define non-space-char
  (none-of* space-char new-line item))

(define link-title
  (sequence* ((delimiter (in #\' #\"))
              (title (-> (preceded-by
                          (is delimiter)
                          space*
                          (any-of (is #\)) new-line))
                         (none-of* item)
                         (zero-or-more)
                         (as-string)))
              (_ (is delimiter)))
    (result title)))

(define link-label*
  (recursive-parser
   (enclosed-by (is #\[)
                (sequence* ((text (-> (none-of* (in #\[ #\]) inline)
                                      (zero-or-more)))
                            (label (maybe (link-label*))))
                  (result (if label
                              (append text (list "[") label (list "]"))
                              text)))
                (is #\]))))

(define link-label
  (memoize (link-label*)))

(define explicit-link-source-contents
  (recursive-parser
   (-> (in #\( #\) #\>)
       (none-of* non-space-char)
       (one-or-more)
       (any-of (sequence (is #\() (explicit-link-source-contents) (is #\))))
       (zero-or-more)
       (as-string))))

(define explicit-link-source
  (any-of (enclosed-by (is #\<)
                       (explicit-link-source-contents)
                       (is #\>))
          (explicit-link-source-contents)))

(define explicit-link
  (sequence*
      ((label link-label)
       (_ space-new-line)
       (href (preceded-by (is #\()
                          explicit-link-source))
       (title (maybe (preceded-by space+ link-title)))
       (_ (preceded-by space* (is #\)))))
    (result `(explicit-link
              (href    ,href)
              (label . ,label)
              (title   ,title)))))

(define shortcut-reference-link
  (sequence* ((label (-> link-label
                         (followed-by (none-of (is #\:))))))
    (result `(reference-link
              (input "[" ,@label "]")
              (ref   . ,label)
              (label . ,label)))))

(define reference-link
  (sequence* ((label link-label)
              (space space-new-line)
              (ref   link-label))
    (result `(reference-link
              (input "[" ,@label "]"
                     ,space
                     "[" ,@ref "]")
              (ref . ,(if (null? ref)
                          label
                          ref))
              (label . ,label)))))


(define auto-link-email
  (sequence* ((_ (preceded-by (is #\<) (maybe (char-seq "mailto:"))))
              (email (-> (sequence
                           (one-or-more (in #[-a-zA-Z0-9+_./!%~$]))
                           (is #\@)
                           (one-or-more (none-of* new-line (is #\>) item)))
                         (as-string)))
              (_ (is #\>)))
    (result `(auto-link
              (href ,(string-append "mailto:" email))
              (label ,email)))))

(define auto-link-url
  (sequence* ((_ (is #\<))
              (uri (-> (sequence (one-or-more (in #[a-zA-Z]))
                                 (char-seq "://")
                                 (-> (none-of* new-line (is #\>) item)
                                     (one-or-more)))
                       (as-string)))
              (_ (is #\>)))
    (result `(auto-link
              (href ,uri)
              (label ,uri)))))

(define auto-link
  (any-of auto-link-url auto-link-email))

(define link
  (any-of explicit-link
          reference-link
          shortcut-reference-link
          auto-link))

(define (line-of char)
  (any-of (repeated (is char) min: 4)
          (-> (sequence space-char (one-or-more (is char)))
              (followed-by space-char))))

(define (surrounded-by char parser)
  (let ((close (enclosed-by
                (none-of space-char new-line)
                inline
                parser)))
    (preceded-by (none-of (line-of char))
                 parser
                 (none-of space-char new-line)
                 (sequence* ((x (-> (none-of* close inline)
                                    (zero-or-more)))
                             (y close))
                   (result (append x (list y)))))))

(define emph
  (recursive-parser
   (->> (any-of (surrounded-by #\* (is #\*))
                (surrounded-by #\_ (is #\_)))
        (node 'emphasis))))

(define strong
  (recursive-parser
   (->> (any-of (surrounded-by #\* (char-seq "**"))
                (surrounded-by #\_ (char-seq "__")))
        (node 'strong))))

(define ul-or-star-line
  (any-of (line-of #\*) (line-of #\_)))

(define escaped-char
  (->> (in (string->char-set "-\\`|*_{}[]()#+.!><"))
       (preceded-by (is #\\) (none-of new-line))))

(define code
  (sequence* ((ticks (-> (is #\`)
                         (repeated min: 1 max: 5 (none-of (is #\`)))
                         (as-string)))
              (_  space*)
              (code (->> (any-of space-char (preceded-by new-line (none-of blank-line)))
                         (none-of* (preceded-by space* (char-seq ticks)))
                         (any-of (one-or-more (none-of* (is #\`) non-space-char))
                                 (none-of* (char-seq ticks) (one-or-more (is #\`))))
                         (one-or-more)
                         (as-string)
                         (node 'code)))
              (_ (preceded-by space* (char-seq ticks))))
    (result code)))

(define alphanumeric-ascii
  (in #[a-zA-Z0-9]))

(define entity
  (->> (enclosed-by (is #\&)
                    (any-of (sequence (is #\#)
                                      (in (char-set #\x #\x))
                                      (one-or-more (in char-set:hex-digit)))
                            (sequence (is #\#)
                                      (one-or-more (in #[0-9])))
                            (one-or-more alphanumeric-ascii))
                    (is #\;))
       (as-string)
       (node '&)))

(define quoted
  (let ((delimiters (in #\" #\')))
    (sequence* ((delimiter delimiters)
                (text (zero-or-more (none-of* (is delimiter) item)))
                (_ (is delimiter)))
      (result text))))

(define html-attribute-value
  (preceded-by (is #\=)
               space-new-line
               (->> non-space-char
                    (none-of* (is #\>))
                    (one-or-more)
                    (any-of quoted)
                    (as-string))))

(define html-attribute
  (sequence* ((name (-> (any-of alphanumeric-ascii (is #\-))
                        (one-or-more)
                        (as-string)))
              (_ space-new-line)
              (value (maybe html-attribute-value))
              (_ space-new-line))
    (let ((name (string->symbol name)))
      (if value
          (result (list name value))
          (result name)))))

(define html-comment
  (sequence* ((_ (char-seq "<!--"))
              (text (->> (none-of* (char-seq "-->") item)
                         (zero-or-more)
                         (as-string)))
              (_ (char-seq "-->")))
    (result `(comment ,(tabs->spaces text)))))

(define (html-element el attrs #!optional (contents '()))
  (append (list 'html-element (string->symbol el))
          (if (null? attrs)
              '()
              (list (cons '@ attrs)))
          contents))

(define (html-element-close-parser el)
  (preceded-by (is #\<) space-new-line (is #\/)
               (char-seq el) space-new-line (is #\>)))

(define (html-element-parser element-parsers contents-parser)
  (sequence* ((_ (preceded-by (is #\<) space-new-line))
              (el (any-of element-parsers))
              (close (result (html-element-close-parser el)))
              (_ space-new-line)
              (attrs (zero-or-more html-attribute))
              (_ (is #\>))
              (contents (contents-parser el close))
              (_ close))
    (result (html-element el attrs contents))))

(define html-inline-element
  (recursive-parser
   (html-element-parser
    (list (char-seq "a") (char-seq "span"))
    (lambda (_ close)
      (->> (none-of* close inline)
           (zero-or-more))))))

(define raw-html
  (any-of html-comment (html-inline-element)))

(define image
  (bind (->> (any-of explicit-link reference-link)
             (preceded-by (is #\!)))
        (lambda (link)
          (result
           (if (eq? 'reference-link (car link))
               (cons 'reference-image (cdr link))
               (cons 'image (cdr link)))))))

(define inline-hook
  (make-parameter '()))

(define (inline-hook-parser input)
  ((any-of (inline-hook)) input))

(define inline
  (any-of (is #\>)
          (as-string (one-or-more normal-char))
          line-end
          ul-or-star-line
          space+
          (strong)
          (emph)
          image
          link
          inline-hook-parser
          code
          raw-html
          entity
          (as-string escaped-char)
          (as-string special-char)))

(define inline-without-line-end
  (none-of* line-end inline))

(define inlines
  (one-or-more
   (any-of inline-without-line-end
           (followed-by line-end
                        inline-without-line-end))))

(define reference-title
  (let ((delimiters '((#\( . #\)))))
    (sequence* ((delimiter (->> (in #\( #\' #\")
                                (preceded-by space-new-line)))
                (closing-delimiter (result (or (alist-ref delimiter delimiters) delimiter)))
                (title (-> (preceded-by
                            (is closing-delimiter)
                            space*
                            (any-of new-line end-of-input))
                           (none-of* new-line item)
                           (zero-or-more)
                           (as-string)))
                (_ (is closing-delimiter)))
      (result title))))

(define reference
  (sequence* ((label (preceded-by non-indent-space
                                  (none-of (char-seq "[]"))
                                  link-label))
              (_ (preceded-by (is #\:) space-new-line))
              (href (as-string (one-or-more non-space-char)))
              (title (maybe reference-title))
              (_ (zero-or-more blank-line)))
    (result `(reference (label . ,label)
                        (href ,href)
                        (title ,title)))))

(define raw-line
  (any-of (-> (in #\return #\newline)
              (none-of* item)
              (zero-or-more)
              (sequence new-line))
          (sequence (one-or-more item) end-of-input)))

(define (tabs->spaces str)
  (irregex-replace/all
   '(seq (submatch (* (~ #\tab #\newline))) #\tab)
   str
   (lambda (m)
     (let* ((prefix (irregex-match-substring m 1))
            (size   (- 4 (modulo (string-length prefix) 4))))
       (string-append prefix (make-string size #\space))))))

(define line
  (sequence* ((line (as-string raw-line)))
    (result (tabs->spaces line))))

(define indent
  (any-of (is #\tab) (char-seq "    ")))

(define indented-line
  (preceded-by indent line))

(define non-blank-indented-line
  (none-of* blank-line indented-line))

(define optionally-indented-line
  (preceded-by (maybe indent) line))

(define verbatim-chunk
  (sequence* ((blank-lines (-> blank-line 
                               (preceded-by (result #\newline))
                               (zero-or-more)))
              (text (one-or-more non-blank-indented-line)))
    (result (append blank-lines text))))

(define verbatim
  (sequence* ((chunks (one-or-more verbatim-chunk)))
    (result `(verbatim . ,chunks))))

(define horizontal-rule-chars
  (char-set #\* #\- #\_))

(define horizontal-rule-char
  (in horizontal-rule-chars))

(define horizontal-rule
  (sequence* ((_ non-indent-space)
              (char horizontal-rule-char)
              (_ space*)
              (_ (repeated (preceded-by (is char) space*) min: 2))
              (_ (preceded-by space* new-line))
              (_ (one-or-more blank-line)))
    (result '(hr))))

(define enumerator
  (preceded-by non-indent-space
               (one-or-more (in char-set:digit))
               (is #\.) space+))

(define bullet-chars
  (char-set #\+ #\* #\-))

(define bullet-char
  (in bullet-chars))

(define bullet
  (enclosed-by (preceded-by non-indent-space (none-of horizontal-rule))
               bullet-char
               space+))

(define list-item-start
  (any-of bullet enumerator))

(define list-block-line
  (none-of* blank-line
            (preceded-by (maybe indent) list-item-start)
            horizontal-rule
            optionally-indented-line))

(define list-block
  (sequence (none-of* blank-line line)
            (zero-or-more list-block-line)))

(define list-continuation-block
  (sequence (zero-or-more blank-line)
            (one-or-more (preceded-by indent list-block))))

(define (list-item-node first-block-parser first-block more-blocks)
  (->> (parse document more-blocks)
       (cons (parse first-block-parser first-block))
       (result)
       (node 'item)))

(define list-item-tight
  (recursive-parser
   (sequence* ((_ list-item-start)
               (first-block (as-string list-block))
               (more-blocks (->> list-continuation-block
                                 (none-of* blank-line)
                                 (zero-or-more)
                                 (as-string)))
               (_ (none-of list-continuation-block)))
     (list-item-node inlines first-block more-blocks))))

(define list-tight
  (sequence* ((items (one-or-more (list-item-tight)))
              (_ (followed-by (zero-or-more blank-line)
                              (none-of list-item-start))))
    (result items)))

(define list-item-loose
  (recursive-parser
   (sequence* ((_ list-item-start)
               (first-block (as-string list-block))
               (more-blocks (-> (zero-or-more list-continuation-block)
                                (as-string))))
     (list-item-node block first-block more-blocks))))

(define list-loose
  (one-or-more
   (sequence* ((item (list-item-loose))
               (_ (zero-or-more blank-line)))
     (result item))))

(define (list* start node-type)
  (->> (any-of list-tight list-loose)
       (node node-type)
       (all-of start)))

(define bullet-list
  (list* bullet 'bullet-list))

(define ordered-list
  (list* enumerator 'ordered-list))

(define atx-inline
  (none-of* new-line
            (sequence space* (zero-or-more (is #\#)) space* new-line)
            inline))

(define atx-start
  (sequence* ((level (repeated (is #\#) min: 1 max: 6)))
    (result (length level))))

(define atx-heading
  (sequence* ((level atx-start)
              (_ space*)
              (text (as-string (one-or-more atx-inline)))
              (_ (maybe (preceded-by space* (zero-or-more (is #\#)) space*)))
              (_ new-line))
    (result `(heading ,level ,text))))

(define (setext-bottom char)
  (preceded-by (char-seq (make-string 3 char))
               (zero-or-more (is char))
               new-line))

(define (setext-heading* level bottom-char)
  (let ((bottom (setext-bottom bottom-char)))
    (sequence* ((text (-> (none-of* line-end inline)
                          (one-or-more)
                          (as-string)))
                (_ (preceded-by space* new-line bottom)))
      (result `(heading ,level ,text)))))

(define setext-heading
  (any-of (setext-heading* 1 #\=)
          (setext-heading* 2 #\-)))

(define heading
  (any-of atx-heading setext-heading))

(define paragraph
  (enclosed-by non-indent-space
               (node 'paragraph inlines)
               (one-or-more blank-line)))

(define plain
  (node 'paragraph inlines))

(define blockquote
  (sequence*
      ((text (->> (sequence
                    (preceded-by (is #\>) (maybe (is #\space)) line)
                    (zero-or-more (none-of* (is #\>) blank-line line))
                    (zero-or-more blank-line))
                  (one-or-more)
                  (as-string))))
    (result (cons 'blockquote (parse document text)))))

(define html-block-elements
  (->> '("address" "blockquote" "center" "dir" "div" "dl" "fieldset" "form" "h1" "h2" "h3"
         "h4" "h5" "h6" "hr" "isindex" "menu" "noframes" "noscript" "ol" "p" "pre" "table"
         "ul" "dd" "dt" "frameset" "li" "tbody" "td" "tfoot" "th" "thead" "tr" "script" )
       (append-map
        (lambda (el)
          (list (char-seq el)
                (char-seq (string-upcase el)))))))

(define (html-block-in-tags* element-parser)
  (html-element-parser
   element-parser
   (lambda (el close)
     (let ((element (html-block-in-tags* (char-seq el))))
       (->> (none-of* close item)
            (any-of element)
            (zero-or-more))))))

(define html-block-in-tags
  (html-block-in-tags* html-block-elements))

(define html-block-self-closing
  (sequence* ((_ (preceded-by (is #\<) space-new-line))
              (el (any-of html-block-elements))
              (_ space-new-line)
              (attrs (zero-or-more html-attribute))
              (_ (preceded-by (maybe (is #\/)) space-new-line (is #\>))))
    (result (html-element el attrs))))

(define html-block
  (any-of html-block-in-tags
          html-comment
          html-block-self-closing))

(define block
  (preceded-by
   (zero-or-more blank-line)
   (any-of blockquote
           verbatim
           reference
           horizontal-rule
           heading
           ordered-list
           bullet-list
           html-block
           paragraph
           plain)))

(define document
  (zero-or-more block))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.