-
Notifications
You must be signed in to change notification settings - Fork 158
/
MarkdownParser.fs
1378 lines (1173 loc) · 58.6 KB
/
MarkdownParser.fs
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// --------------------------------------------------------------------------------------
// F# Markdown (MarkdownParser.fs)
// (c) Tomas Petricek, 2012, Available under Apache 2.0 license.
// --------------------------------------------------------------------------------------
module internal FSharp.Formatting.Markdown.Parser
open System
open System.Collections.Generic
open System.Text.RegularExpressions
open FSharp.Patterns
open FSharp.Collections
open FSharp.Formatting.Common
// --------------------------------------------------------------------------------------
// Parsing of Markdown - first part handles inline formatting
// --------------------------------------------------------------------------------------
/// Splits a link formatted as `http://link "title"` into a link part
/// and an optional title part (may be wrapped using quote or double-quotes)
let getLinkAndTitle (StringPosition.TrimBoth(input, _n)) =
let url, title =
if input.Length = 0 then
"", None
else
let c = input.[input.Length - 1]
if c = '\'' || c = '"' then
let start = input.IndexOf(c)
input.Substring(0, start).Trim(), Some(input.Substring(start + 1, input.Length - 2 - start).Trim())
else
input, None
url.TrimStart('<').TrimEnd('>'), title
/// Succeeds when the specified character list starts with an escaped
/// character - in that case, returns the character and the tail of the list
let (|EscapedChar|_|) input =
match input with
| '\\' :: (('*' | '\\' | '`' | '_' | '{' | '}' | '[' | ']' | '(' | ')' | '>' | '#' | '.' | '!' | '+' | '-' | '$') as c) :: rest ->
Some(c, rest)
| _ -> None
/// Escape dollar inside a LaTex inline math span.
let (|EscapedLatexInlineMathChar|_|) input =
match input with
| '\\' :: (('$') as c) :: rest -> Some(c, rest)
| _ -> None
/// Succeeds when the specificed character list starts with non-escaped punctuation.
let (|Punctuation|_|) input =
match input with
| EscapedChar _ -> None
| _ ->
// from https://github.com/commonmark/commonmark.js/blob/master/lib/inlines.js#L38
let re =
"""^[!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~\xA1\xA7\xAB\xB6\xB7\xBB\xBF\u037E\u0387\u055A-\u055F\u0589\u058A\u05BE\u05C0\u05C3\u05C6\u05F3\u05F4\u0609\u060A\u060C\u060D\u061B\u061E\u061F\u066A-\u066D\u06D4\u0700-\u070D\u07F7-\u07F9\u0830-\u083E\u085E\u0964\u0965\u0970\u0AF0\u0DF4\u0E4F\u0E5A\u0E5B\u0F04-\u0F12\u0F14\u0F3A-\u0F3D\u0F85\u0FD0-\u0FD4\u0FD9\u0FDA\u104A-\u104F\u10FB\u1360-\u1368\u1400\u166D\u166E\u169B\u169C\u16EB-\u16ED\u1735\u1736\u17D4-\u17D6\u17D8-\u17DA\u1800-\u180A\u1944\u1945\u1A1E\u1A1F\u1AA0-\u1AA6\u1AA8-\u1AAD\u1B5A-\u1B60\u1BFC-\u1BFF\u1C3B-\u1C3F\u1C7E\u1C7F\u1CC0-\u1CC7\u1CD3\u2010-\u2027\u2030-\u2043\u2045-\u2051\u2053-\u205E\u207D\u207E\u208D\u208E\u2308-\u230B\u2329\u232A\u2768-\u2775\u27C5\u27C6\u27E6-\u27EF\u2983-\u2998\u29D8-\u29DB\u29FC\u29FD\u2CF9-\u2CFC\u2CFE\u2CFF\u2D70\u2E00-\u2E2E\u2E30-\u2E42\u3001-\u3003\u3008-\u3011\u3014-\u301F\u3030\u303D\u30A0\u30FB\uA4FE\uA4FF\uA60D-\uA60F\uA673\uA67E\uA6F2-\uA6F7\uA874-\uA877\uA8CE\uA8CF\uA8F8-\uA8FA\uA8FC\uA92E\uA92F\uA95F\uA9C1-\uA9CD\uA9DE\uA9DF\uAA5C-\uAA5F\uAADE\uAADF\uAAF0\uAAF1\uABEB\uFD3E\uFD3F\uFE10-\uFE19\uFE30-\uFE52\uFE54-\uFE61\uFE63\uFE68\uFE6A\uFE6B\uFF01-\uFF03\uFF05-\uFF0A\uFF0C-\uFF0F\uFF1A\uFF1B\uFF1F\uFF20\uFF3B-\uFF3D\uFF3F\uFF5B\uFF5D\uFF5F-\uFF65]|\uD800[\uDD00-\uDD02\uDF9F\uDFD0]|\uD801\uDD6F|\uD802[\uDC57\uDD1F\uDD3F\uDE50-\uDE58\uDE7F\uDEF0-\uDEF6\uDF39-\uDF3F\uDF99-\uDF9C]|\uD804[\uDC47-\uDC4D\uDCBB\uDCBC\uDCBE-\uDCC1\uDD40-\uDD43\uDD74\uDD75\uDDC5-\uDDC9\uDDCD\uDDDB\uDDDD-\uDDDF\uDE38-\uDE3D\uDEA9]|\uD805[\uDCC6\uDDC1-\uDDD7\uDE41-\uDE43\uDF3C-\uDF3E]|\uD809[\uDC70-\uDC74]|\uD81A[\uDE6E\uDE6F\uDEF5\uDF37-\uDF3B\uDF44]|\uD82F\uDC9F|\uD836[\uDE87-\uDE8B]"""
let match' = Regex.Match(Array.ofList input |> String, re)
if match'.Success then
let entity = match'.Value
let _, rest = List.splitAt entity.Length input
Some(char entity, rest)
else
None
let (|NotPunctuation|_|) input =
match input with
| Punctuation _ -> None
| _ -> Some input
module Char =
let (|WhiteSpace|_|) (input: char list) =
match input with
| [] -> Some input
| x :: _xs -> if Char.IsWhiteSpace x then Some input else None
let (|NotWhiteSpace|_|) input =
match input with
| WhiteSpace _ -> None
| _ -> Some input
/// Succeeds when the specificed character list starts with a delimeter run.
let (|DelimiterRun|_|) input =
match input with
| ('*' | '_') :: _tail as (h :: t) ->
let run, rest = List.partitionWhile (fun x -> x = h) (h :: t)
Some(run, rest)
| _ -> None
/// Succeeds when there's a match to a string of * or _ that could
/// open emphasis.
let (|LeftDelimiterRun|_|) input =
match input with
// (1) Not followed by [Unicode whitespace] and
// (2a) not followed by a [Unicode punctuation character] or
// (2b) followed by a [Unicode punctuation character] and
// preceded by [Unicode whitespace] or a [Unicode punctuation character].
//
// Passes 1 and 2a.
| DelimiterRun(_, Char.NotWhiteSpace _) & DelimiterRun(run, NotPunctuation xs) -> Some([], run, xs)
| _ :: DelimiterRun(_, Char.NotWhiteSpace _) & h :: DelimiterRun(run, NotPunctuation xs) -> Some([ h ], run, xs)
// Passes 1 and 2b
| h :: DelimiterRun(run, Punctuation(x, xs)) ->
match [ h ] with
| Char.WhiteSpace _
| Punctuation _ -> Some([ h ], run, x :: xs)
| _ -> None
// Passes 1 and 2b when the run is at the start of the line.
// |CannotStartEmphasis| ensures that we don't match this
// when we've previously discarded a leading character.
| DelimiterRun(run, Punctuation(x, xs)) -> Some([], run, x :: xs)
| _ -> None
/// Succeeds when there's a match to a string of * or _ that could
/// close emphasis.
let (|RightDelimiterRun|_|) input =
match input with
// A right-flanking delimiter run is
// 1. not preceded by [Unicode whitepace]
// 2. And either
// a. not preceded by a [Unicode punctuation character], or
// b. preceded by a [Unicode punctuation character] and
// followed by [Unicode whitespace] or a [Unicode punctuation character]
//
// An escaped character followed by delimiter run matches 1 and 2a.
| EscapedChar(x, DelimiterRun(run, xs)) -> Some([ '\\'; x ], run, xs)
| EscapedChar _ -> None
| Char.NotWhiteSpace _ & x :: DelimiterRun(run, xs) ->
match input with
// 1 and 2a
| NotPunctuation _ -> Some([ x ], run, xs)
// 1 and 2b
| Punctuation(x, DelimiterRun(run, Char.WhiteSpace ys)) -> Some([ x ], run, ys)
// 1 and 2b
| Punctuation(x, DelimiterRun(run, Punctuation(y, ys))) -> Some([ x ], run, y :: ys)
| _ -> None
| _ -> None
/// Matches ['c',LeftDelimiterRun]::xs that should
/// not open emphasis. This is useful because the
/// parser iterates through characters one by one and
/// in this case we need to skip both 'c' and the LeftDelimiterRun.
/// If we only skipped 'c' then we could match LeftDelimiterRun
/// on the next iteration and we do not want that to happen.
let (|CannotOpenEmphasis|_|) input =
match input with
// Rule #2: A single `_` character [can open emphasis] iff
// it is part of a [left-flanking delimiter run]
// and either (a) not part of a [right-flanking delimiter run]
// or (b) part of a [right-flanking delimiter run]
// preceded by a [Unicode punctuation character].
| LeftDelimiterRun _ & RightDelimiterRun(pre, [ '_' ], post) ->
match List.rev pre with
| Punctuation _ -> None
| revPre -> Some('_' :: revPre, post)
// We cannot pass 1 and 2b of the left flanking rule
// when h is neither white space nor punctuation.
| h :: DelimiterRun(run, Punctuation(x, xs)) ->
match [ h ] with
| Char.WhiteSpace _
| Punctuation _ -> None
| _ -> Some(List.rev (h :: run), x :: xs)
| _ -> None
/// Matches a list if it starts with a sub-list that is delimited
/// using the specified delimiters. Returns a wrapped list and the rest.
///
/// This is similar to `List.Delimited`, but it skips over escaped characters.
let (|DelimitedMarkdown|_|) bracket input =
let _startl, endl = bracket, bracket
// Like List.partitionUntilEquals, but skip over escaped characters
let rec loop acc count =
function
| (RightDelimiterRun(pre, [ '_' ], post) as input) when endl = [ '_' ] ->
match input with
| LeftDelimiterRun(pre, run, (Punctuation _ as post)) ->
if count = 0 then
Some((List.rev acc) @ pre, run @ post)
else
loop ((List.rev (pre @ run)) @ acc) (count - 1) post
| LeftDelimiterRun(pre, run, post) -> loop ((List.rev (pre @ run)) @ acc) (count + 1) post
| _ -> Some((List.rev acc) @ pre, [ '_' ] @ post)
| RightDelimiterRun(pre, run, post) when endl = run ->
if count = 0 then
Some((List.rev acc) @ pre, run @ post)
else
loop ((List.rev (pre @ run)) @ acc) (count - 1) post
| EscapedChar(x, xs) -> loop (x :: '\\' :: acc) count xs
| LeftDelimiterRun(pre, run, post) when run = endl -> loop ((List.rev (pre @ run)) @ acc) (count + 1) post
| x :: xs -> loop (x :: acc) count xs
| [] -> None
// If it starts with 'startl', let's search for 'endl'
if List.startsWith bracket input then
match loop [] 0 (List.skip bracket.Length input) with
| Some(pre, post) -> Some(pre, List.skip bracket.Length post)
| None -> None
else
None
/// This is similar to `List.Delimited`, but it skips over Latex inline math characters.
let (|DelimitedLatexDisplayMath|_|) bracket input =
let _startl, endl = bracket, bracket
// Like List.partitionUntilEquals, but skip over escaped characters
let rec loop acc =
function
| EscapedLatexInlineMathChar(x, xs) -> loop (x :: '\\' :: acc) xs
| input when List.startsWith endl input -> Some(List.rev acc, input)
| x :: xs -> loop (x :: acc) xs
| [] -> None
// If it starts with 'startl', let's search for 'endl'
if List.startsWith bracket input then
match loop [] (List.skip bracket.Length input) with
| Some(pre, post) -> Some(pre, List.skip bracket.Length post)
| None -> None
else
None
/// This is similar to `List.Delimited`, but it skips over Latex inline math characters.
let (|DelimitedLatexInlineMath|_|) bracket input =
let _startl, endl = bracket, bracket
// Like List.partitionUntilEquals, but skip over escaped characters
let rec loop acc =
function
| EscapedLatexInlineMathChar(x, xs) -> loop (x :: '\\' :: acc) xs
| input when List.startsWith endl input -> Some(List.rev acc, input)
| x :: xs -> loop (x :: acc) xs
| [] -> None
// If it starts with 'startl', let's search for 'endl'
if List.startsWith bracket input then
match loop [] (List.skip bracket.Length input) with
| Some(pre, post) -> Some(pre, List.skip bracket.Length post)
| None -> None
else
None
/// Recognizes an indirect link written using `[body][key]` or just `[key]`
/// The key can be preceeded by a space or other single whitespace thing.
let (|IndirectLink|_|) =
function
| List.BracketDelimited '[' ']' (body, '\r' :: '\n' :: (List.BracketDelimited '[' ']' (List.AsString link, rest))) ->
Some(body, link, "\r\n[" + link + "]", rest)
| List.BracketDelimited '[' ']' (body,
((' ' | '\n') as c) :: (List.BracketDelimited '[' ']' (List.AsString link, rest))) ->
Some(body, link, c.ToString() + "[" + link + "]", rest)
| List.BracketDelimited '[' ']' (body, List.BracketDelimited '[' ']' (List.AsString link, rest)) ->
Some(body, link, "[" + link + "]", rest)
| List.BracketDelimited '[' ']' (body, rest) -> Some(body, "", "", rest)
| _ -> None
/// Recognize a direct link written using `[body](http://url "with title")`
let (|DirectLink|_|) =
function
| List.BracketDelimited '[' ']' (body, List.BracketDelimited '(' ')' (link, rest)) -> Some(body, link, rest)
| _ -> None
/// Recognizes an automatic link written using `http://url` or `https://url`
let (|AutoLink|_|) input =
let linkFor (scheme: string) =
let prefix = scheme.ToCharArray() |> Array.toList
match input with
| List.DelimitedWith prefix [ ' ' ] (List.AsString link, rest, _s, _e) -> Some(scheme + link, ' ' :: rest)
| List.StartsWith prefix (List.AsString link) -> Some(link, [])
| _ -> None
[ "http://"; "https://" ] |> List.tryPick linkFor
/// Recognizes some form of emphasis using `**bold**` or `*italic*`
/// (both can be also marked using underscore).
/// TODO: This does not handle nested emphasis well.
let (|Emphasised|_|) =
function
| LeftDelimiterRun(pre, run, post) ->
match run @ post with
| DelimitedMarkdown [ '_'; '_'; '_' ] (body, rest)
| DelimitedMarkdown [ '*'; '*'; '*' ] (body, rest) ->
Some(pre, body, Emphasis >> List.singleton >> (fun s -> Strong(s, None)), rest)
| DelimitedMarkdown [ '_'; '_' ] (body, rest)
| DelimitedMarkdown [ '*'; '*' ] (body, rest) -> Some(pre, body, Strong, rest)
| DelimitedMarkdown [ '_' ] (body, rest)
| DelimitedMarkdown [ '*' ] (body, rest) -> Some(pre, body, Emphasis, rest)
| _ -> None
| _ -> None
let (|HtmlEntity|_|) input =
match input with
| '&' :: _ ->
// regex from reference implementation: https://github.com/commonmark/commonmark.js/blob/da1db1e/lib/common.js#L10
let re =
"^&" // beginning expect '&'
+ "(?:" // start non-capturing group
+ "#x[a-f0-9]{1,8}" // hex
+ "|#[0-9]{1,8}" // or decimal
+ "|[a-z][a-z0-9]{1,31}" // or name
+ ")" // end non-capturing group
+ ";" // expect ';'
let match' = Regex.Match(Array.ofList input |> String, re)
if match'.Success then
let entity = match'.Value
let _, rest = List.splitAt entity.Length input
Some(entity, rest)
else
None
| _ -> None
/// Defines a context for the main `parseParagraphs` function
type ParsingContext =
{ Links: Dictionary<string, string * string option>
Newline: string
IsFirst: bool
CurrentRange: MarkdownRange option
ParseOptions: MarkdownParseOptions }
member x.ParseCodeAsOther = (x.ParseOptions &&& MarkdownParseOptions.ParseCodeAsOther) <> enum 0
member x.ParseNonCodeAsOther = (x.ParseOptions &&& MarkdownParseOptions.ParseNonCodeAsOther) <> enum 0
member x.AllowYamlFrontMatter = (x.ParseOptions &&& MarkdownParseOptions.AllowYamlFrontMatter) <> enum 0
/// Parses a body of a paragraph and recognizes all inline tags.
let rec parseChars acc input (ctx: ParsingContext) =
seq {
// Zero or one literals, depending whether there is some accumulated input and update the ctx
let accLiterals =
Lazy<_>.Create(fun () ->
if List.isEmpty acc then
([], ctx)
else
let range =
match ctx.CurrentRange with
| Some(n) ->
Some(
{ n with
EndColumn = n.StartColumn + acc.Length }
)
| None -> None
let ctx =
{ ctx with
CurrentRange =
match ctx.CurrentRange with
| Some(n) ->
Some(
{ n with
StartColumn = n.StartColumn + acc.Length }
)
| None -> None }
let text = String(List.rev acc |> Array.ofList)
([ Literal(text, range) ], ctx))
match input with
// Recognizes explicit line-break at the end of line
| ' ' :: ' ' :: '\r' :: '\n' :: rest
| ' ' :: ' ' :: ('\n' | '\r') :: rest ->
let (value, ctx) = accLiterals.Value
yield! value
yield HardLineBreak(ctx.CurrentRange)
yield! parseChars [] rest ctx
| HtmlEntity(entity, rest) ->
let (value, ctx) = accLiterals.Value
yield! value
yield Literal(entity, ctx.CurrentRange)
yield! parseChars [] rest ctx
| '&' :: rest -> yield! parseChars (';' :: 'p' :: 'm' :: 'a' :: '&' :: acc) rest ctx
// Ignore escaped characters that might mean something else
| EscapedChar(c, rest) -> yield! parseChars (c :: acc) rest ctx
// Inline code delimited either using double `` or single `
// (if there are spaces around, then body can contain more backticks)
| List.DelimitedWith [ '`'; ' ' ] [ ' '; '`' ] (body, rest, s, e)
| List.DelimitedNTimes '`' (body, rest, s, e) ->
let (value, ctx) = accLiterals.Value
yield! value
let rng =
match ctx.CurrentRange with
| Some(n) ->
Some
{ n with
StartColumn = n.StartColumn + s
EndColumn = n.EndColumn - e }
| None -> None
yield InlineCode(String(Array.ofList body).Trim(), rng)
yield! parseChars [] rest ctx
// Display Latex inline math mode
| DelimitedLatexDisplayMath [ '$'; '$' ] (body, rest) ->
let (value, ctx) = accLiterals.Value
yield! value
yield LatexDisplayMath(String(Array.ofList body).Trim(), ctx.CurrentRange)
yield! parseChars [] rest ctx
// Inline Latex inline math mode
| DelimitedLatexInlineMath [ '$' ] (body, rest) ->
let (value, ctx) = accLiterals.Value
let ctx =
{ ctx with
CurrentRange =
match ctx.CurrentRange with
| Some(n) ->
Some(
{ n with
StartColumn = n.StartColumn + 1 }
)
| None -> None }
yield! value
let code = String(Array.ofList body).Trim()
yield
LatexInlineMath(
code,
match ctx.CurrentRange with
| Some(n) ->
Some(
{ n with
EndColumn = n.StartColumn + code.Length }
)
| None -> None
)
yield! parseChars [] rest ctx
// Inline link wrapped as <http://foo.bar>
| List.DelimitedWith [ '<' ] [ '>' ] (List.AsString link, rest, _s, _e) when
Seq.forall (Char.IsWhiteSpace >> not) link
&& (link.Contains("@") || link.Contains("://"))
->
let (value, ctx) = accLiterals.Value
yield! value
yield DirectLink([ Literal(link, ctx.CurrentRange) ], link, None, ctx.CurrentRange)
yield! parseChars [] rest ctx
// Not an inline link - leave as an inline HTML tag
| List.DelimitedWith [ '<' ] [ '>' ] (tag, rest, _s, _e) ->
yield! parseChars ('>' :: (List.rev tag) @ '<' :: acc) rest ctx
// Recognize direct link [foo](http://bar) or indirect link [foo][bar] or auto link http://bar
| DirectLink(body, link, rest) ->
let (value, ctx) = accLiterals.Value
yield! value
let link, title = getLinkAndTitle (String(Array.ofList link), MarkdownRange.zero)
yield DirectLink(parseChars [] body ctx |> List.ofSeq, link, title, ctx.CurrentRange)
yield! parseChars [] rest ctx
| IndirectLink(body, link, original, rest) ->
let (value, ctx) = accLiterals.Value
yield! value
let key =
if String.IsNullOrEmpty(link) then
String(body |> Array.ofSeq)
else
link
yield IndirectLink(parseChars [] body ctx |> List.ofSeq, original, key, ctx.CurrentRange)
yield! parseChars [] rest ctx
| AutoLink(link, rest) ->
let (value, ctx) = accLiterals.Value
yield! value
yield DirectLink([ Literal(link, ctx.CurrentRange) ], link, None, ctx.CurrentRange)
yield! parseChars [] rest ctx
// Recognize image - this is a link prefixed with the '!' symbol
| '!' :: DirectLink(body, link, rest) ->
let (value, ctx) = accLiterals.Value
yield! value
let link, title = getLinkAndTitle (String(Array.ofList link), MarkdownRange.zero)
yield DirectImage(String(Array.ofList body), link, title, ctx.CurrentRange)
yield! parseChars [] rest ctx
| '!' :: IndirectLink(body, link, original, rest) ->
let (value, ctx) = accLiterals.Value
yield! value
let key =
if String.IsNullOrEmpty(link) then
String(body |> Array.ofSeq)
else
link
yield IndirectImage(String(Array.ofList body), original, key, ctx.CurrentRange)
yield! parseChars [] rest ctx
// Handle Emphasis
| CannotOpenEmphasis(revPre, post) -> yield! parseChars (revPre @ acc) post ctx
| Emphasised(pre, body, f, rest) ->
let (value, ctx) = accLiterals.Value
yield! value
yield! parseChars [] pre ctx
let body = parseChars [] body ctx |> List.ofSeq
yield f (body, ctx.CurrentRange)
yield! parseChars [] rest ctx
// Encode '<' char if it is not link or inline HTML
| '<' :: rest -> yield! parseChars (';' :: 't' :: 'l' :: '&' :: acc) rest ctx
| '>' :: rest -> yield! parseChars (';' :: 't' :: 'g' :: '&' :: acc) rest ctx
| x :: xs -> yield! parseChars (x :: acc) xs ctx
| [] ->
let (value, _ctx) = accLiterals.Value
yield! value
}
/// Parse body of a paragraph into a list of Markdown inline spans
let parseSpans (StringPosition.TrimBoth(s, n)) ctx =
let ctx = { ctx with CurrentRange = Some(n) }
parseChars [] (s.ToCharArray() |> List.ofArray) ctx |> List.ofSeq
let rec trimSpaces numSpaces (s: string) =
if numSpaces <= 0 then
s
elif s.StartsWith ' ' then
trimSpaces (numSpaces - 1) (s.Substring(1))
elif s.StartsWith '\t' then
trimSpaces (numSpaces - 4) (s.Substring(1))
else
s
// --------------------------------------------------------------------------------------
// Parsing of Markdown - second part handles paragraph-level formatting (headings, etc.)
// --------------------------------------------------------------------------------------
/// Recognizes heading, either prefixed with #s or followed by === or --- line
let (|Heading|_|) lines =
match lines with
| ((StringPosition.TrimBoth header) as line1) :: ((StringPosition.TrimEnd(StringPosition.EqualsRepeated("=",
MarkdownRange.zero))) as line2) :: rest ->
Some(1, header, [ line1; line2 ], rest)
| ((StringPosition.TrimBoth header) as line1) :: ((StringPosition.TrimEnd(StringPosition.EqualsRepeated("-",
MarkdownRange.zero))) as line2) :: rest ->
Some(2, header, [ line1; line2 ], rest)
| (StringPosition.StartsWithRepeated "#" (n, StringPosition.TrimBoth(header, ln)) as line1) :: rest ->
let header =
// Drop "##" at the end, but only when it is preceded by some whitespace
// (For example "## Hello F#" should be "Hello F#")
if header.EndsWith '#' then
let noHash = header.TrimEnd [| '#' |]
if noHash.Length > 0 && Char.IsWhiteSpace(noHash.Chars(noHash.Length - 1)) then
noHash
else
header
else
header
Some(n, (header, ln), [ line1 ], rest)
| _rest -> None
let (|YamlFrontmatter|_|) lines =
match lines with
| ("---", p) :: rest ->
let yaml = rest |> List.takeWhile (fun (l, _) -> l <> "---")
let yamlTextLines = yaml |> List.map fst
let rest =
rest
|> List.skipWhile (fun (l, _) -> l <> "---")
|> (function
| (("---", _) :: t) -> t
| l -> l)
Some(yamlTextLines, MarkdownRange.mergeRanges (p :: List.map snd yaml), rest)
| _ -> None
/// Recognizes a horizontal rule written using *, _ or -
let (|HorizontalRule|_|) (line: string, _n: MarkdownRange) =
let rec loop ((h, a, u) as arg) i =
if (h >= 3 || a >= 3 || u >= 3) && i = line.Length then
Some(line.[0])
elif i = line.Length then
None
elif Char.IsWhiteSpace line.[i] then
loop arg (i + 1)
elif line.[i] = '-' && a = 0 && u = 0 then
loop (h + 1, a, u) (i + 1)
elif line.[i] = '*' && h = 0 && u = 0 then
loop (h, a + 1, u) (i + 1)
elif line.[i] = '_' && a = 0 && h = 0 then
loop (h, a, u + 1) (i + 1)
else
None
loop (0, 0, 0) 0
/// Recognizes a code block - lines starting with four spaces (including blank)
let (|NestedCodeBlock|_|) lines =
match lines with
| Lines.TakeCodeBlock(_numspaces, (Lines.TrimBlank lines as takenLines), rest) when lines <> [] ->
let code = [ for (l, _n) in lines -> if String.IsNullOrEmpty l then "" else trimSpaces 4 l ]
Some(code @ [ "" ], takenLines, rest, None, "", "")
| _ -> None
/// Recognizes a fenced code block - starting and ending with at least ``` or ~~~
let (|FencedCodeBlock|_|) lines =
match lines with
| (StringPosition.StartsWithNTimesTrimIgnoreStartWhitespace "~" (Let "~" (start, num), indent, header) as takenLine) :: lines
// when num > 2
| (StringPosition.StartsWithNTimesTrimIgnoreStartWhitespace "`" (Let "`" (start, num), indent, header) as takenLine) :: lines when
num > 2
->
let mutable fenceString = String.replicate num start
if header.Contains(start) then
None // info string cannot contain backspaces
else
let codeLines, rest =
lines
|> List.partitionUntil (fun line ->
match [ line ] with
// end cannot contain info string afterwards (see http://spec.commonmark.org/0.23/#example-104)
// end must be indended with less then 4 spaces: http://spec.commonmark.org/0.23/#example-95
| StringPosition.StartsWithNTimesTrimIgnoreStartWhitespace start (n, i, h) :: _ when
n >= num && i < 4 && String.IsNullOrWhiteSpace h
->
fenceString <- String.replicate n start
true
| _ -> false)
let handleIndent (codeLine: string) =
if codeLine.Length <= indent && String.IsNullOrWhiteSpace codeLine then
""
elif
codeLine.Length > indent
&& String.IsNullOrWhiteSpace(codeLine.Substring(0, indent))
then
codeLine.Substring(indent, codeLine.Length - indent)
else
codeLine.TrimStart()
let codeWithoutIndent = [ for (codeLine, _n) in codeLines -> handleIndent codeLine ]
// langString is the part after ``` and ignoredString is the rest until the line ends.
let langString, ignoredString =
if String.IsNullOrWhiteSpace header then
"", ""
else
let splits = header.Split((null: char array), StringSplitOptions.RemoveEmptyEntries)
match splits |> Array.tryFind (fun _ -> true) with
| None -> "", ""
| Some langString ->
let ignoredString =
header.Substring(header.IndexOf(langString, StringComparison.Ordinal) + langString.Length)
langString,
(if String.IsNullOrWhiteSpace ignoredString then
""
else
ignoredString)
// Handle the ending line
let takenLines2, codeWithoutIndent, rest =
match rest with
| ((hd, n) as takenLine2) :: tl ->
let idx = hd.IndexOf(fenceString, StringComparison.Ordinal)
if idx > -1 && idx + fenceString.Length <= hd.Length then
let _pre = hd.Substring(0, idx)
let after = hd.Substring(idx + fenceString.Length)
[ takenLine2 ],
codeWithoutIndent @ [ "" ],
(if String.IsNullOrWhiteSpace after then
tl
else
(after, n) :: tl)
else
[ takenLine2 ], codeWithoutIndent @ [ "" ], tl
| _ -> [], codeWithoutIndent, rest
Some(
codeWithoutIndent,
(takenLine :: codeLines @ takenLines2),
rest,
Some fenceString,
langString,
ignoredString
)
| _ -> None
/// Matches when the input starts with a number. Returns the
/// rest of the input, following the last number.
let (|SkipSomeNumbers|_|) (input: string, _n: MarkdownRange) =
match List.ofSeq input with
| x :: xs when Char.IsDigit x ->
let _, rest = List.partitionUntil (Char.IsDigit >> not) xs
Some(input.Length - rest.Length, rest)
| _ -> None
/// Recognizes a staring of a list (either 1. or +, *, -).
/// Returns the rest of the line, together with the indent.
let (|ListStart|_|) =
function
| StringPosition.TrimStartAndCount(startIndent,
_spaces,
// NOTE: a tab character after +, * or - isn't supported by the reference implementation
// (it will be parsed as paragraph for 0.22)
(StringPosition.StartsWithAny [ "+ "; "* "; "- " (*; "+\t"; "*\t"; "-\t"*) ] as item)) ->
let range = snd item
let li =
((fst item).Substring(2),
{ range with
StartColumn = range.StartColumn + 2 })
let (StringPosition.TrimStartAndCount(startIndent2, _spaces2, _)) = li
let endIndent =
startIndent
+ 2
+
// Handle case of code block
if startIndent2 >= 5 then 1 else startIndent2
Some(Unordered, startIndent, endIndent, li)
| StringPosition.TrimStartAndCount(startIndent,
_spaces,
(SkipSomeNumbers(skipNumCount, '.' :: ' ' :: List.AsString item))) ->
let (StringPosition.TrimStartAndCount(startIndent2, _spaces2, _)) = (item, MarkdownRange.zero)
let endIndent =
startIndent
+ 2
+ skipNumCount
+
// Handle case of code block
if startIndent2 >= 5 then 1 else startIndent2
Some(Ordered, startIndent, endIndent, (item, MarkdownRange.zero))
| _ -> None
/// Splits input into lines until whitespace or starting of a list and the rest.
let (|LinesUntilListOrWhite|) lines =
lines
|> List.partitionUntil (function
| ListStart _
| StringPosition.WhiteSpace -> true
| _ -> false)
/// Splits input into lines until not-indented line or starting of a list and the rest.
let (|LinesUntilListOrUnindented|) lines =
lines
|> List.partitionUntilLookahead (function
| (ListStart _ | StringPosition.Unindented) :: _
| StringPosition.WhiteSpace :: StringPosition.WhiteSpace :: _ -> true
| _ -> false)
/// Recognizes a list item until the next list item (possibly nested) or end of a list.
/// The parameter specifies whether the previous line was simple (single-line not
/// separated by a white line - simple items are not wrapped in <p>)
let (|ListItem|_|) prevSimple lines =
match lines with
// Take remaining lines that belong to the same item
// (everything until an empty line or start of another list item)
//
// Then take more things that belong to the item -
// the value 'more' will contain indented paragraphs
| (ListStart(kind, startIndent, endIndent, item) as takenLine) :: LinesUntilListOrWhite(continued,
(LinesUntilListOrUnindented(more,
rest) as next)) ->
let simple =
match item with
| StringPosition.TrimStartAndCount(_, spaces, _) when spaces >= 4 ->
// Code Block
false
| _ ->
match next, rest with
| StringPosition.WhiteSpace :: _, (ListStart _) :: _ -> false
| (ListStart _) :: _, _ -> true
| [], _ -> true
| [ StringPosition.WhiteSpace ], _ -> true
| StringPosition.WhiteSpace :: StringPosition.WhiteSpace :: _, _ -> true
| _, StringPosition.Unindented :: _ -> prevSimple
| _, _ -> false
let lines =
[ yield item
for (line, n) in continued do
yield (line.Trim(), n)
for (line, n) in more do
let trimmed = trimSpaces endIndent line
yield
(trimmed,
{ n with
StartColumn = n.StartColumn + line.Length - trimmed.Length }) ]
//let trimmed = line.TrimStart()
//if trimmed.Length >= line.Length - endIndent then yield trimmed
//else yield line.Substring(endIndent) ]
Some(startIndent, (simple, kind, lines), (takenLine :: continued @ more), rest)
| _ -> None
/// Recognizes a list - returns list items with information about
/// their indents - these need to be turned into a tree structure later.
let rec (|ListItems|_|) prevSimple lines =
match lines with
| ListItem prevSimple (indent, ((nextSimple, _, _) as info), takenLines, rest) ->
match rest with
| ((HorizontalRule _) as takenLine2) :: _ -> Some([ indent, info ], takenLines @ [ takenLine2 ], rest)
| ListItems nextSimple (items, takenLines2, rest) ->
Some((indent, info) :: items, (takenLines @ takenLines2), rest)
| _ -> Some([ indent, info ], takenLines, rest)
| _ -> None
// Code for parsing pipe tables
// Splits table row into deliminated parts escaping inline code and latex
let rec pipeTableFindSplits (delim: char array) (line: char list) =
let cLstToStr (x: char list) =
x |> Array.ofList |> System.String.Concat
let rec ptfs delim line =
match line with
| DelimitedLatexDisplayMath [ '$'; '$' ] (_body, rest) -> ptfs delim rest
| DelimitedLatexInlineMath [ '$' ] (_body, rest) -> ptfs delim rest
| List.DelimitedWith [ '`'; ' ' ] [ ' '; '`' ] (_body, rest, _s, _e) -> ptfs delim rest
| List.DelimitedNTimes '`' (_body, rest, _s, _e) -> ptfs delim rest
| x :: rest when Array.exists ((=) x) delim -> Some rest
| '\\' :: _ :: rest
| _ :: rest -> ptfs delim rest
| [] -> None
let rest = ptfs delim line
match rest with
| None -> [ cLstToStr line ]
| Some _x when List.isEmpty line -> [ "" ]
| Some x ->
let chunkSize = List.length line - List.length x - 1
cLstToStr (Seq.take chunkSize line |> Seq.toList) :: pipeTableFindSplits delim x
/// Recognizes alignment specified in the passed separator line.
let (|TableCellSeparator|_|) =
function
| StringPosition.StartsAndEndsWith (":", ":") (StringPosition.EqualsRepeated("-", MarkdownRange.zero)) ->
Some(AlignCenter)
| StringPosition.StartsWith ":" (StringPosition.EqualsRepeated("-", MarkdownRange.zero)) -> Some(AlignLeft)
| StringPosition.StartsAndEndsWith ("", ":") (StringPosition.EqualsRepeated("-", MarkdownRange.zero)) ->
Some(AlignRight)
| StringPosition.EqualsRepeated("-", MarkdownRange.zero) -> Some(AlignDefault)
| _ -> None
/// Recognizes row of pipe table.
/// The function takes number of expected columns and array of delimiters.
/// Returns list of strings between delimiters.
let (|PipeTableRow|_|) (size: int option) delimiters (line: string, n: MarkdownRange) =
let parts =
pipeTableFindSplits delimiters (line.ToCharArray() |> Array.toList)
|> List.toArray
|> Array.map (fun s -> (s.Trim(), n))
let n = parts.Length
let m = size |> Option.defaultValue 1
let x =
if String.IsNullOrEmpty(fst parts.[0]) && n > m then
1
else
0
let y =
if String.IsNullOrEmpty(fst parts.[n - 1]) && n - x > m then
n - 2
else
n - 1
if n = 1 || (size.IsSome && y - x + 1 <> m) then
None
else
Some(parts.[x..y] |> Array.toList)
/// Recognizes separator row of pipe table.
/// Returns list of alignments.
let (|PipeSeparatorRow|_|) size =
function
| PipeTableRow size [| '|'; '+' |] parts ->
let alignments = parts |> List.choose (|TableCellSeparator|_|)
if parts.Length <> alignments.Length then
None
else
(Some alignments)
| _ -> None
/// Recognizes pipe table
let (|PipeTableBlock|_|) input =
let rec getTableRows size acc takenLinesAcc lines =
match lines with
| (PipeTableRow size [| '|' |] columns) as takenLine :: rest ->
getTableRows size (List.map (fun l -> [ l ]) columns :: acc) (takenLine :: takenLinesAcc) rest
| rest -> (List.rev acc, List.rev takenLinesAcc, rest)
match input with
| (PipeSeparatorRow None alignments) as takenLine :: rest ->
let rows, takenLines, others = getTableRows (Some alignments.Length) [] [] rest
Some((None, alignments, rows), takenLine :: takenLines, others)
| ((PipeTableRow None [| '|' |] headers) as takenLine) :: rest ->
match rest with
| ((PipeSeparatorRow (Some headers.Length) alignments) as takenLine2) :: rest ->
let rows, takenLines, others = getTableRows (Some headers.Length) [] [] rest
let header_paragraphs = headers |> List.map (fun l -> [ l ])
Some((Some(header_paragraphs), alignments, rows), takenLine :: takenLine2 :: takenLines, others)
| _ -> None
| _ -> None
// Code for parsing emacs tables
/// Recognizes one line of emacs table. It can be line with content or separator line.
/// The function takes positions of grid columns (if known) and expected grid separator.
/// Passed function is used to check whether all parts within grid are valid.
/// Retuns tuple (position of grid columns, text between grid columns).
let (|EmacsTableLine|_|)
(grid: int array option)
(c: char)
(check: string * MarkdownRange -> bool)
(line: string, _n: MarkdownRange)
=
let p =
grid
|> Option.defaultValue (Array.FindAll([| 0 .. line.Length - 1 |], (fun i -> line.[i] = c)))
let n = p.Length - 1
if n < 2 || line.Length <= p.[n] || Array.exists (fun i -> line.[i] <> c) p then
None
else
let parts =
[ 1..n ]
|> List.map (fun i ->
let rng =
{ StartLine = n
StartColumn = 0
EndLine = n
EndColumn = p.[i] - p.[i - 1] - 1 }
line.Substring(p.[i - 1] + 1, p.[i] - p.[i - 1] - 1), rng)
if List.forall check parts then Some(p, parts) else None
/// Recognizes emacs table
let (|EmacsTableBlock|_|) (lines) =
let isCellSep s =
match s with
| StringPosition.EqualsRepeated ("-", MarkdownRange.zero) _ -> true
| _ -> false
let isAlignedCellSep = (|TableCellSeparator|_|) >> Option.isSome
let isHeadCellSep s =
match s with
| StringPosition.EqualsRepeated ("=", MarkdownRange.zero) _ -> true
| _ -> false
let isText (_s: string, _n: MarkdownRange) = true
match lines with
| ((EmacsTableLine None '+' isAlignedCellSep (grid, parts)) as takenLine) :: rest ->
let alignments = List.choose (|TableCellSeparator|_|) parts
// iterate over rows and go from state to state
// headers - the content of head row (initially none)
// prevRow - content of the processed rows
// cur - list of paragraphs in the current row (list of empty lists after each separator line)
// flag indicates whether current row is empty (similar to List.forall (List.isEmpty) cur)
let emptyCur = List.replicate<(string * MarkdownRange) list> (grid.Length - 1) []
let rec loop
flag
takenLines2
headers
(prevRows: (string * MarkdownRange) list list list)
(cur: (string * MarkdownRange) list list)