-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmark-blocks.el
1243 lines (1135 loc) · 47.3 KB
/
cmark-blocks.el
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
;;; cmark-blocks.el --- CommonMark parser. -*- lexical-binding: t -*-
;; Copyright (C) 2019 taku0, John MacFarlane
;; Author: taku0 (http://github.com/taku0)
;; John MacFarlane
;; URL: https://github.com/taku0/cmark-el
;; This file is not part of GNU Emacs.
;; BSD 2-Clause License
;;
;; All rights reserved.
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions are met:
;;
;; 1. Redistributions of source code must retain the above copyright notice,
;; this list of conditions and the following disclaimer.
;;
;; 2. Redistributions in binary form must reproduce the above copyright notice,
;; this list of conditions and the following disclaimer in the documentation
;; and/or other materials provided with the distribution.
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
;; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
;; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
;; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
;; POSSIBILITY OF SUCH DAMAGE.
;;; Commentary:
;; A CommonMark parser.
;;
;; Usage:
;;
;; (let ((parser (cmark-create-Parser)))
;; (cmark-Parser-parse parser source-string))
;;; Code
(require 'cmark-compat)
(require 'cmark-node)
(require 'cmark-common)
(defconst cmark--CODE_INDENT 4)
(defconst cmark--C_TAB 9)
(defconst cmark--C_NEWLINE 10)
(defconst cmark--C_GREATERTHAN 62)
(defconst cmark--C_LESSTHAN 60)
(defconst cmark--C_SPACE 32)
(defconst cmark--C_OPEN_BRACKET 91)
(require 'cmark-inlines)
(cl-defstruct cmark-Parser
doc
blocks
blockStarts
tip
oldtip
currentLine
lineNumber
offset
column
nextNonspace
nextNonspaceColumn
indent
indented
blank
partiallyConsumedTab
allClosed
lastMatchedContainer
refmap
lastLineLength
inlineParser
options)
(cl-defstruct cmark--listMarker
type
tight
bulletChar
start
delimiter
padding
markerOffset)
(defconst cmark--reHtmlBlockOpen
(vector
"." ;; dummy for 0
(list (concat "\\`"
"<"
"\\(?:script\\|pre\\|textarea\\|style\\)"
"\\(?:"
cmark--SPACE
"\\|"
">"
"\\|"
"$"
"\\)")
:ignore-case)
"\\`<!--"
"\\`<[?]"
"\\`<![A-Z]"
"\\`<!\\[CDATA\\["
(list (concat "\\`"
"<[/]?"
"\\(?:"
"address\\|article\\|aside\\|base\\|basefont\\|blockquote\\|body\\|caption\\|center\\|col\\|colgroup\\|dd\\|details\\|dialog\\|dir\\|div\\|dl\\|dt\\|fieldset\\|figcaption\\|figure\\|footer\\|form\\|frame\\|frameset\\|h[123456]\\|head\\|header\\|hr\\|html\\|iframe\\|legend\\|li\\|link\\|main\\|menu\\|menuitem\\|nav\\|noframes\\|ol\\|optgroup\\|option\\|p\\|param\\|section\\|source\\|summary\\|table\\|tbody\\|td\\|tfoot\\|th\\|thead\\|title\\|tr\\|track\\|ul"
"\\)"
"\\(?:"
cmark--SPACE
"\\|"
"[/]?[>]"
"\\|"
"$"
"\\)")
:ignore-case)
(list (concat "\\`"
"\\(?:"
cmark--OPENTAG
"\\|"
cmark--CLOSETAG
"\\)"
cmark--SPACE
"*"
"$")
:ignore-case)))
(defconst cmark--reHtmlBlockClose
(list
"." ;; dummy for 0
(list "<\\/\\(?:script\\|pre\\|textarea\\|style\\)>" :ignore-case)
"-->"
"\\?>"
">"
"\\]\\]>"))
(defconst cmark--reThematicBreak
(concat "\\`\\(?:\\*[ \t]*\\)\\{3,\\}$"
"\\|"
"\\`\\(?:_[ \t]*\\)\\{3,\\}$"
"\\|"
"\\`\\(?:-[ \t]*\\)\\{3,\\}$"))
(defconst cmark--reMaybeSpecial "\\`[#`~*+_=<>0-9-]")
(defconst cmark--reNonSpace "[^ \t\f\v\r\n]")
(defconst cmark--reBulletListMarker "\\`[*+-]")
(defconst cmark--reOrderedListMarker "\\`\\([[:digit:]]\\{1,9\\}\\)\\([.)]\\)")
(defconst cmark--reATXHeadingMarker "\\`#\\{1,6\\}\\(?:[ \t]+\\|$\\)")
(defconst cmark--reCodeFence "\\``\\{3,\\}\\|\\`~\\{3,\\}")
(defconst cmark--reClosingCodeFence "\\`\\(?:`\\{3,\\}\\|~\\{3,\\}\\)")
(defconst cmark--reSetextHeadingLine "\\`\\(?:=+\\|-+\\)[ \t]*$")
(defconst cmark--reLineEnding "\r\n\\|\n\\|\r")
(defun cmark--isBlank (s)
"Returns true if string contains only space characters."
(not (cmark--string-match cmark--reNonSpace s)))
(defun cmark--isSpaceOrTab (c)
(or (eq c cmark--C_SPACE) (eq c cmark--C_TAB)))
(defun cmark--peek (ln pos)
(if (< pos (length ln))
(elt ln pos)
-1))
;;; DOC PARSER
;; These are methods of a Parser object, defined below.
(defun cmark--endsWithBlankLine (block)
"Returns t if block ends with a blank line, descending if needed
into lists and sublists."
(cl-block nil
(cl-block while
(while block
(when (cmark-Node-_lastLineBlank block)
(cl-return t))
(let ((type (cmark-Node-type block)))
(if (and (not (cmark-Node-_lastLineChecked block))
(or (equal type "list") (equal type "item")))
(progn
(setf (cmark-Node-_lastLineChecked block) t)
(setq block (cmark-Node-_lastChild block)))
(setf (cmark-Node-_lastLineChecked block) t)
(cl-return-from while)))))
nil))
(defun cmark--Parser-addLine (this)
"Add a line to the block at the tip. We assume the tip
can accept lines -- that check should be done before calling this"
(when (cmark-Parser-partiallyConsumedTab this)
(cl-incf (cmark-Parser-offset this) 1) ;; skip over tab
;; add space characters:
(let ((charsToTab (- 4 (% (cmark-Parser-column this) 4))))
(cl-callf concat (cmark-Node-_string_content (cmark-Parser-tip this))
(cmark--repeat " " charsToTab))))
(cl-callf concat (cmark-Node-_string_content (cmark-Parser-tip this))
(substring (cmark-Parser-currentLine this) (cmark-Parser-offset this))
"\n"))
(defun cmark--Parser-addChild (this tag offset)
"Add block of type tag as a child of the tip. If the tip can't
accept children, close and finalize it and try its parent,
and so on til we find a block that can accept children."
(while (not (funcall
(cmark--block-canContain
(gethash (cmark-Node-type (cmark-Parser-tip this))
(cmark-Parser-blocks this)))
tag))
(cmark--Parser-finalize this
(cmark-Parser-tip this)
(1- (cmark-Parser-lineNumber this))))
(let* ((column_number (1+ offset)) ;; offset 0 = column 1
(newBlock (cmark-create-Node
tag
(cons (cons (cmark-Parser-lineNumber this) column_number)
(cons 0 0)))))
(setf (cmark-Node-_string_content newBlock) "")
(cmark-Node-appendChild (cmark-Parser-tip this) newBlock)
(setf (cmark-Parser-tip this) newBlock)
newBlock))
(defun cmark--Parser-parseListMarker (parser container)
"Parse a list marker and return data on the marker (type,
start, delimiter, bullet character, padding) or nil."
(cl-block nil
(let ((rest (substring (cmark-Parser-currentLine parser)
(cmark-Parser-nextNonspace parser)))
nextc
spacesStartCol
spacesStartOffset
(data (make-cmark--listMarker
:type nil
:tight t ;; lists are tight by default
:bulletChar nil
:start nil
:delimiter nil
:padding nil
:markerOffset (cmark-Parser-indent parser)))
blank_item
spaces_after_marker
match-string)
(when (>= (cmark-Parser-indent parser) 4)
(cl-return nil))
(cond
((cmark--string-match cmark--reBulletListMarker rest)
(setf (cmark--listMarker-type data) "bullet")
(setf (cmark--listMarker-bulletChar data)
(cmark--charAt (match-string 0 rest) 0)))
((and (cmark--string-match cmark--reOrderedListMarker rest)
(or (not (equal (cmark-Node-type container) "paragraph"))
(eq (string-to-number (match-string 1 rest)) 1)))
(setf (cmark--listMarker-type data) "ordered")
(setf (cmark--listMarker-start data)
(string-to-number (match-string 1 rest)))
(setf (cmark--listMarker-delimiter data) (match-string 2 rest)))
(t
(cl-return nil)))
(setq match-string (match-string 0 rest))
;; make sure we have spaces after
(setq nextc (cmark--peek (cmark-Parser-currentLine parser)
(+ (cmark-Parser-nextNonspace parser)
(length match-string))))
(when (not (or (eq nextc -1)
(eq nextc cmark--C_TAB)
(eq nextc cmark--C_SPACE)))
(cl-return nil))
;; if it interrupts paragraph, make sure first line isn't blank
(when (and (equal (cmark-Node-type container) "paragraph")
(not (cmark--string-match
cmark--reNonSpace
(substring (cmark-Parser-currentLine parser)
(+ (cmark-Parser-nextNonspace parser)
(length match-string))))))
(cl-return nil))
;; we've got a match! advance offset and calculate padding
(cmark--Parser-advanceNextNonspace parser) ;; to start of marker
(cmark--Parser-advanceOffset parser (length match-string) t) ;; to end of marker
(setq spacesStartCol (cmark-Parser-column parser))
(setq spacesStartOffset (cmark-Parser-offset parser))
(while (progn
(cmark--Parser-advanceOffset parser 1 t)
(setq nextc (cmark--peek (cmark-Parser-currentLine parser)
(cmark-Parser-offset parser)))
(and (< (- (cmark-Parser-column parser) spacesStartCol) 5)
(cmark--isSpaceOrTab nextc))))
(setq blank_item (eq (cmark--peek (cmark-Parser-currentLine parser)
(cmark-Parser-offset parser))
-1))
(setq spaces_after_marker (- (cmark-Parser-column parser)
spacesStartCol))
(if (or (>= spaces_after_marker 5)
(< spaces_after_marker 1)
blank_item)
(progn
(setf (cmark--listMarker-padding data) (1+ (length match-string)))
(setf (cmark-Parser-column parser) spacesStartCol)
(setf (cmark-Parser-offset parser) spacesStartOffset)
(when (cmark--isSpaceOrTab
(cmark--peek (cmark-Parser-currentLine parser)
(cmark-Parser-offset parser)))
(cmark--Parser-advanceOffset parser 1 t)))
(setf (cmark--listMarker-padding data)
(+ (length match-string) spaces_after_marker)))
data)))
(defun cmark--listsMatch (list_data item_data)
"Returns t if the two list items are of the same type,
with the same delimiter and bullet character. This is used
in agglomerating list items into lists."
(and (equal (cmark--listMarker-type list_data)
(cmark--listMarker-type item_data))
(equal (cmark--listMarker-delimiter list_data)
(cmark--listMarker-delimiter item_data))
(equal (cmark--listMarker-bulletChar list_data)
(cmark--listMarker-bulletChar item_data))))
(defun cmark--Parser-closeUnmatchedBlocks (this)
"Finalize and close any unmatched blocks."
(when (not (cmark-Parser-allClosed this))
;; finalize any blocks not matched
(while (not (eq (cmark-Parser-oldtip this)
(cmark-Parser-lastMatchedContainer this)))
(let ((parent (cmark-Node-_parent (cmark-Parser-oldtip this))))
(cmark--Parser-finalize this
(cmark-Parser-oldtip this)
(1- (cmark-Parser-lineNumber this)))
(setf (cmark-Parser-oldtip this) parent)))
(setf (cmark-Parser-allClosed this) t)))
(cl-defstruct cmark--block
continue
finalize
canContain
acceptsLines)
(defconst cmark--blocks
(let ((blocks (make-hash-table :test 'equal)))
(puthash
"document"
(make-cmark--block
:continue (lambda (_parser _container) 0)
:finalize (lambda (_parser _block))
:canContain (lambda (type) (not (equal type "item")))
:acceptsLines nil)
blocks)
(puthash
"list"
(make-cmark--block
:continue (lambda (_parser _container) 0)
:finalize
(lambda (_parser block)
(let ((item (cmark-Node-_firstChild block))
subitem)
(cl-block nil
(while item
;; check for non-final list item ending with blank line:
(when (and (cmark--endsWithBlankLine item)
(cmark-Node-_next item))
(setf (cmark--listMarker-tight (cmark-Node-_listData block))
nil)
(cl-return))
;; recurse into children of list item, to see if there are
;; spaces between any of them:
(setq subitem (cmark-Node-_firstChild item))
(cl-block while
(while subitem
(when (and (cmark--endsWithBlankLine subitem)
(or (cmark-Node-_next item)
(cmark-Node-_next subitem)))
(setf (cmark--listMarker-tight
(cmark-Node-_listData block))
nil)
(cl-return-from while))
(setq subitem (cmark-Node-_next subitem))))
(setq item (cmark-Node-_next item))))))
:canContain (lambda (type) (equal type "item"))
:acceptsLines nil)
blocks)
(puthash
"block_quote"
(make-cmark--block
:continue
(lambda (parser _container)
(let ((ln (cmark-Parser-currentLine parser)))
(if (and (not (cmark-Parser-indented parser))
(eq (cmark--peek ln (cmark-Parser-nextNonspace parser))
cmark--C_GREATERTHAN))
(progn
(cmark--Parser-advanceNextNonspace parser)
(cmark--Parser-advanceOffset parser 1 nil)
(when (cmark--isSpaceOrTab
(cmark--peek ln (cmark-Parser-offset parser)))
(cmark--Parser-advanceOffset parser 1 t))
0)
1)))
:finalize (lambda (_parser _block))
:canContain (lambda (type) (not (equal type "item")))
:acceptsLines nil)
blocks)
(puthash
"item"
(make-cmark--block
:continue
(lambda (parser container)
(cond
((cmark-Parser-blank parser)
(if (null (cmark-Node-_firstChild container))
;; Blank line after empty list item
1
(cmark--Parser-advanceNextNonspace parser)
0))
((>= (cmark-Parser-indent parser)
(+ (cmark--listMarker-markerOffset
(cmark-Node-_listData container))
(cmark--listMarker-padding
(cmark-Node-_listData container))))
(cmark--Parser-advanceOffset
parser
(+ (cmark--listMarker-markerOffset
(cmark-Node-_listData container))
(cmark--listMarker-padding
(cmark-Node-_listData container)))
t)
0)
(t
1)))
:finalize (lambda (_parser _block))
:canContain (lambda (type) (not (equal type "item")))
:acceptsLines nil)
blocks)
(puthash
"heading"
(make-cmark--block
:continue
(lambda (_parser _container)
;; a heading can never container > 1 line, so fail to match:
1)
:finalize (lambda (_parser _block))
:canContain (lambda () nil)
:acceptsLines nil)
blocks)
(puthash
"thematic_break"
(make-cmark--block
:continue
(lambda (_parser _container)
;; a thematic break can never container > 1 line, so fail to match:
1)
:finalize (lambda (_parser _block))
:canContain (lambda () nil)
:acceptsLines nil)
blocks)
(puthash
"code_block"
(make-cmark--block
:continue
(lambda (parser container)
(let ((ln (cmark-Parser-currentLine parser))
(indent (cmark-Parser-indent parser))
match
i
subln)
(cond
((cmark-Node-_isFenced container)
;; fenced
(setq subln (substring
ln
(cmark-Parser-nextNonspace parser)))
(setq match
(and (<= indent 3)
(equal (cmark--charAt
ln
(cmark-Parser-nextNonspace parser))
(cmark-Node-_fenceChar container))
(cmark--string-match cmark--reClosingCodeFence subln)
;; Since Emacs Lisp doesn't support positive lookahead,
;; check it manually.
(save-match-data
(cmark--string-match
(concat cmark--reClosingCodeFence " *\\'")
subln))))
(if (and match
(>= (length (match-string 0 subln))
(cmark-Node-_fenceLength container)))
(progn
;; closing fence - we're at end of line, so we can return
(setf (cmark-Parser-lastLineLength parser)
(+ (cmark-Parser-offset parser)
indent
(length (match-string 0 subln))))
(cmark--Parser-finalize parser
container
(cmark-Parser-lineNumber parser))
2)
;; skip optional spaces of fence offset
(setq i (cmark-Node-_fenceOffset container))
(while (and (> i 0)
(cmark--isSpaceOrTab
(cmark--peek ln (cmark-Parser-offset parser))))
(cmark--Parser-advanceOffset parser 1 t)
(setq i (1- i)))
0))
;; indented
((>= indent cmark--CODE_INDENT)
(cmark--Parser-advanceOffset parser cmark--CODE_INDENT t)
0)
((cmark-Parser-blank parser)
(cmark--Parser-advanceNextNonspace parser)
0)
(t
1))))
:finalize
(lambda (_parser block)
(if (cmark-Node-_isFenced block)
(progn ;; fenced
;; first line becomes info string
(let* ((content (cmark-Node-_string_content block))
(newlinePos (cl-search "\n" content))
(firstLine (substring content 0 newlinePos))
(rest (substring content (1+ newlinePos))))
(setf (cmark-Node-info block)
(cmark--unescapeString (cmark--trim firstLine)))
(setf (cmark-Node-_literal block) rest)))
;; indented
(setf (cmark-Node-_literal block)
(cmark--replaceAll "\\(\n *\\)+\\'"
"\n"
(cmark-Node-_string_content block))))
(setf (cmark-Node-_string_content block) nil)) ;; allow GC
:canContain (lambda () nil)
:acceptsLines t)
blocks)
(puthash
"html_block"
(make-cmark--block
:continue
(lambda (parser container)
(if (and (cmark-Parser-blank parser)
(or (eq (cmark-Node-_htmlBlockType container) 6)
(eq (cmark-Node-_htmlBlockType container) 7)))
1
0))
:finalize
(lambda (_parser block)
(setf (cmark-Node-_literal block)
(cmark--replaceAll "\\(\n *\\)+\\'"
""
(cmark-Node-_string_content block)))
(setf (cmark-Node-_string_content block) nil)) ;; allow GC
:canContain (lambda () nil)
:acceptsLines t)
blocks)
(puthash
"paragraph"
(make-cmark--block
:continue
(lambda (parser _container)
(if (cmark-Parser-blank parser) 1 0))
:finalize
(lambda (parser block)
(let (pos
(hasReferenceDefs nil))
;; try parsing the beginning as link reference definitions.
(while (and
(eq (cmark--peek (cmark-Node-_string_content block) 0)
cmark--C_OPEN_BRACKET)
(not (zerop (setq pos
(cmark--InlineParser-parseReference
(cmark-Parser-inlineParser parser)
(cmark-Node-_string_content block)
(cmark-Parser-refmap parser))))))
(setf (cmark-Node-_string_content block)
(substring (cmark-Node-_string_content block) pos))
(setq hasReferenceDefs t))
(when (and hasReferenceDefs
(cmark--isBlank (cmark-Node-_string_content block)))
(cmark-Node-unlink block))))
:canContain (lambda (_type) nil)
:acceptsLines t)
blocks)
blocks)
"\"finalize\" is run when the block is closed.
\"continue\" is run to check whether the block is continuing
at a certain line and offset (e.g. whether a block quote
contains a `>`. It returns 0 for matched, 1 for not matched,
and 2 for \"we've dealt with this line completely, go to next.\"")
(defconst cmark--blockStarts
(vector
;; block quote
(lambda (parser _container)
(if (and (not (cmark-Parser-indented parser))
(eq (cmark--peek (cmark-Parser-currentLine parser)
(cmark-Parser-nextNonspace parser))
cmark--C_GREATERTHAN))
(progn
(cmark--Parser-advanceNextNonspace parser)
(cmark--Parser-advanceOffset parser 1 nil)
;; optional following space
(when (cmark--isSpaceOrTab (cmark--peek
(cmark-Parser-currentLine parser)
(cmark-Parser-offset parser)))
(cmark--Parser-advanceOffset parser 1 t))
(cmark--Parser-closeUnmatchedBlocks parser)
(cmark--Parser-addChild parser
"block_quote"
(cmark-Parser-nextNonspace parser))
1)
0))
;; ATX heading
(lambda (parser _container)
(let (current-line-after-spaces)
(if (and (not (cmark-Parser-indented parser))
(cmark--string-match
cmark--reATXHeadingMarker
(setq current-line-after-spaces
(substring (cmark-Parser-currentLine parser)
(cmark-Parser-nextNonspace parser)))))
(let ((match-string (match-string
0
current-line-after-spaces))
container)
(cmark--Parser-advanceNextNonspace parser)
(cmark--Parser-advanceOffset parser (length match-string) nil)
(cmark--Parser-closeUnmatchedBlocks parser)
(setq container (cmark--Parser-addChild
parser
"heading"
(cmark-Parser-nextNonspace parser)))
(setf (cmark-Node-level container)
(length (cmark--trim match-string))) ;; number of #s
;; remove trailing ###s:
(setf (cmark-Node-_string_content container)
(cmark--replaceAll
"[ \t]+#+[ \t]*\\'"
""
(cmark--replaceAll
"\\`[ \t]*#+[ \t]*\\'"
""
(substring (cmark-Parser-currentLine parser)
(cmark-Parser-offset parser)))))
(cmark--Parser-advanceOffset
parser
(- (length (cmark-Parser-currentLine parser))
(cmark-Parser-offset parser))
nil)
2)
0)))
;; Fenced code block
(lambda (parser _container)
(let (current-line-after-spaces)
(if (and (not (cmark-Parser-indented parser))
(cmark--string-match
cmark--reCodeFence
(setq current-line-after-spaces
(substring (cmark-Parser-currentLine parser)
(cmark-Parser-nextNonspace parser))))
;; Since Emacs Lisp doesn't support negative lookahead,
;; check it manually.
(save-match-data
(not (cmark--string-match
"`\\{3,\\}[^`].*`"
current-line-after-spaces))))
(progn
(let* ((match-string (match-string 0 current-line-after-spaces))
(fenceLength (length match-string))
container)
(cmark--Parser-closeUnmatchedBlocks parser)
(setq container
(cmark--Parser-addChild
parser
"code_block"
(cmark-Parser-nextNonspace parser)))
(setf (cmark-Node-_isFenced container) t)
(setf (cmark-Node-_fenceLength container) fenceLength)
(setf (cmark-Node-_fenceChar container)
(cmark--charAt match-string 0))
(setf (cmark-Node-_fenceOffset container)
(cmark-Parser-indent parser))
(cmark--Parser-advanceNextNonspace parser)
(cmark--Parser-advanceOffset parser fenceLength nil)
2))
0)))
;; HTML block
(lambda (parser container)
(cl-block nil
(if (and (not (cmark-Parser-indented parser))
(eq (cmark--peek (cmark-Parser-currentLine parser)
(cmark-Parser-nextNonspace parser))
cmark--C_LESSTHAN))
(progn
(let ((s (substring (cmark-Parser-currentLine parser)
(cmark-Parser-nextNonspace parser)))
(blockType 1)
b)
(while (<= blockType 7)
(when (and
(cmark--string-match
(elt cmark--reHtmlBlockOpen blockType)
s)
(or
(< blockType 7)
(and
(not (equal (cmark-Node-type container) "paragraph"))
;; maybe lazy
(not
(and
(not (cmark-Parser-allClosed parser))
(not (cmark-Parser-blank parser))
(equal
(cmark-Node-type (cmark-Parser-tip parser))
"paragraph"))))))
(cmark--Parser-closeUnmatchedBlocks parser)
;; We don't adjust (cmark-Parser-offset parser)
;; spaces are part of the HTML block:
(setq b (cmark--Parser-addChild
parser
"html_block"
(cmark-Parser-offset parser)))
(setf (cmark-Node-_htmlBlockType b) blockType)
(cl-return 2))
(setq blockType (1+ blockType)))
0))
0)))
;; Setext heading
(lambda (parser container)
(let (current-line-after-spaces)
(if (and (not (cmark-Parser-indented parser))
(equal (cmark-Node-type container) "paragraph")
(cmark--string-match
cmark--reSetextHeadingLine
(setq current-line-after-spaces
(substring (cmark-Parser-currentLine parser)
(cmark-Parser-nextNonspace parser)))))
(progn
(let ((match-string (match-string 0 current-line-after-spaces))
heading)
(cmark--Parser-closeUnmatchedBlocks parser)
;; resolve reference link definitiosn
(let (pos)
(while (and (eq (cmark--peek
(cmark-Node-_string_content container)
0)
cmark--C_OPEN_BRACKET)
(not
(zerop
(setq pos (cmark--InlineParser-parseReference
(cmark-Parser-inlineParser parser)
(cmark-Node-_string_content container)
(cmark-Parser-refmap parser))))))
(setf (cmark-Node-_string_content container)
(substring (cmark-Node-_string_content container)
pos))))
(if (> (length (cmark-Node-_string_content container)) 0)
(progn
(setq heading (cmark-create-Node
"heading"
(cmark-Node-sourcepos container)))
(setf (cmark-Node-level heading)
(if (equal (cmark--charAt match-string 0) "=") 1 2))
(setf (cmark-Node-_string_content heading)
(cmark-Node-_string_content container))
(cmark-Node-insertAfter container heading)
(cmark-Node-unlink container)
(setf (cmark-Parser-tip parser) heading)
(cmark--Parser-advanceOffset
parser
(- (length (cmark-Parser-currentLine parser))
(cmark-Parser-offset parser))
nil)
2)
0)))
0)))
;; thematic break
(lambda (parser _container)
(if (and (not (cmark-Parser-indented parser))
(cmark--string-match
cmark--reThematicBreak
(substring (cmark-Parser-currentLine parser)
(cmark-Parser-nextNonspace parser))))
(progn
(cmark--Parser-closeUnmatchedBlocks parser)
(cmark--Parser-addChild
parser
"thematic_break"
(cmark-Parser-nextNonspace parser))
(cmark--Parser-advanceOffset
parser
(- (length (cmark-Parser-currentLine parser))
(cmark-Parser-offset parser))
nil)
2)
0))
;; list item
(lambda (parser container)
(let (data)
(if (and (or (not (cmark-Parser-indented parser))
(equal (cmark-Node-type container) "list"))
(setq data (cmark--Parser-parseListMarker parser container)))
(progn
(cmark--Parser-closeUnmatchedBlocks parser)
;; add the list if needed
(when (or (not (equal (cmark-Node-type
(cmark-Parser-tip parser))
"list"))
(not (cmark--listsMatch
(cmark-Node-_listData container)
data)))
(setq container (cmark--Parser-addChild
parser
"list"
(cmark-Parser-nextNonspace parser)))
(setf (cmark-Node-_listData container) data))
;; add the list item
(setq container (cmark--Parser-addChild
parser
"item"
(cmark-Parser-nextNonspace parser)))
(setf (cmark-Node-_listData container) data)
1)
0)))
;; indented code block
(lambda (parser _container)
(if (and (cmark-Parser-indented parser)
(not (equal (cmark-Node-type (cmark-Parser-tip parser))
"paragraph"))
(not (cmark-Parser-blank parser)))
(progn
;; indented code
(cmark--Parser-advanceOffset parser cmark--CODE_INDENT t)
(cmark--Parser-closeUnmatchedBlocks parser)
(cmark--Parser-addChild
parser
"code_block"
(cmark-Parser-offset parser))
2)
0)))
"block start functions. Return values:
0 = no match
1 = matched container, keep going
2 = matched leaf, no more block starts")
(defun cmark--Parser-advanceOffset (this count columns)
(let ((currentLine (cmark-Parser-currentLine this))
charsToTab
charsToAdvance
c)
(while (and (> count 0)
(setq c (cmark--charAt currentLine
(cmark-Parser-offset this))))
(if (equal c "\t")
(progn
(setq charsToTab (- 4 (% (cmark-Parser-column this) 4)))
(if columns
(progn
(setf (cmark-Parser-partiallyConsumedTab this)
(> charsToTab count))
(setq charsToAdvance
(if (> charsToTab count) count charsToTab))
(cl-incf (cmark-Parser-column this) charsToAdvance)
(cl-incf (cmark-Parser-offset this)
(if (cmark-Parser-partiallyConsumedTab this) 0 1))
(setq count (- count charsToAdvance)))
(setf (cmark-Parser-partiallyConsumedTab this) nil)
(cl-incf (cmark-Parser-column this) charsToTab)
(cl-incf (cmark-Parser-offset this) 1)
(setq count (1- count))))
(setf (cmark-Parser-partiallyConsumedTab this) nil)
(cl-incf (cmark-Parser-offset this) 1)
(cl-incf (cmark-Parser-column this) 1) ;; assume ascii; block starts are ascii
(setq count (1- count))))))
(defun cmark--Parser-advanceNextNonspace (this)
(setf (cmark-Parser-offset this) (cmark-Parser-nextNonspace this))
(setf (cmark-Parser-column this) (cmark-Parser-nextNonspaceColumn this))
(setf (cmark-Parser-partiallyConsumedTab this) nil))
(defun cmark--Parser-findNextNonspace (this)
(let ((currentLine (cmark-Parser-currentLine this))
(i (cmark-Parser-offset this))
(cols (cmark-Parser-column this))
c)
(cl-block while
(while (not (equal (setq c (cmark--charAt currentLine i)) ""))
(cond
((equal c " ")
(setq i (1+ i))
(setq cols (1+ cols)))
((equal c "\t")
(setq i (1+ i))
(setq cols (+ cols (- 4 (% cols 4)))))
(t
(cl-return-from while)))))
(setf (cmark-Parser-blank this) (or (equal c "\n")
(equal c "\r")
(equal c "")))
(setf (cmark-Parser-nextNonspace this) i)
(setf (cmark-Parser-nextNonspaceColumn this) cols)
(setf (cmark-Parser-indent this) (- (cmark-Parser-nextNonspaceColumn this)
(cmark-Parser-column this)))
(setf (cmark-Parser-indented this) (>= (cmark-Parser-indent this)
cmark--CODE_INDENT))))
(defun cmark--Parser-incorporateLine (this ln)
"Analyze a line of text and update the document appropriately.
We parse markdown text by calling this on each line of input,
then finalizing the document."
(cl-block nil
(let ((all_matched t)
type
(container (cmark-Parser-doc this))
lastChild
matchedLeaf
starts
startsLen
i
lastLineBlank
cont)
(setf (cmark-Parser-oldtip this) (cmark-Parser-tip this))
(setf (cmark-Parser-offset this) 0)
(setf (cmark-Parser-column this) 0)
(setf (cmark-Parser-blank this) nil)
(setf (cmark-Parser-partiallyConsumedTab this) nil)
(cl-incf (cmark-Parser-lineNumber this) 1)
;; replace NUL characters for security
(when (cl-find ?\u0000 ln)
(setq ln (cmark--replaceAll "\0" "\uFFFD" ln)))
(setf (cmark-Parser-currentLine this) ln)
;; For each containing block, try to parse the associated line start.
;; Bail out on failure: container will point to the last matching block.
;; Set all_matched to nil if not all containers match.
(cl-block while
(while (and (setq lastChild (cmark-Node-_lastChild container))
(cmark-Node-_open lastChild))
(setq container lastChild)
(cmark--Parser-findNextNonspace this)
(cl-case (funcall
(cmark--block-continue
(gethash (cmark-Node-type container)
(cmark-Parser-blocks this)))
this
container)
(0 ;; we've matched, keep going
nil)
(1 ;; we've failed to match a block
(setq all_matched nil))
(2 ;; we've hit end of line for fenced code close and can return
(cl-return))