-
Notifications
You must be signed in to change notification settings - Fork 0
/
cavemark.py
1558 lines (1411 loc) · 61.4 KB
/
cavemark.py
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
# Copyright 2018 caveman <[email protected]>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import re
# parser states
_S_START = 0
_S_PARAGRAPH_IN = 1
_S_EMPHASIZE_IN = 2
_S_STRIKE_IN = 3
_S_CITATION_IN = 4
_S_CITATIONDATA_IN = 5
_S_HEADING_IN = 6
_S_LIST_IN = 7
_S_LISTPARAGRAPH_IN = 8
_S_RESOURCE_IN = 9
_S_RESOURCE_IGNORED_KEY_IN = 10
_S_CODE_IN = 11
# tag indices
_I_OPEN_ANY_SEP = 1
_I_OPEN_ANY_EMPHASIZE = 2
_I_OPEN_ANY_STRIKE = 3
_I_OPEN_ANY_CITATION = 4
_I_OPEN_ANY_HEADING_LEVEL = 5
_I_OPEN_ANY_HEADING_BOOKMARK = 6
_I_OPEN_ANY_LIST_LEVEL = 7
_I_OPEN_ANY_LIST_TYPE = 8
_I_OPEN_ANY_RESOURCE_TYPE = 9
_I_OPEN_ANY_RESOURCE_ID = 10
_I_OPEN_ANY_CODE = 11
_I_OPEN_ANY_SHORTCUT = 12
_I_CLOSE_HEADING = 1
_I_CLOSE_HEADING_EMPHASIZE = 2
_I_CLOSE_HEADING_STRIKE = 3
_I_CLOSE_HEADING_CITATION = 4
_I_CLOSE_HEADING_CODE = 5
_I_CLOSE_HEADING_SHORTCUT = 6
_I_CLOSE_LIST_PARAGRAPH = 1
_I_CLOSE_LIST = 2
_I_CLOSE_LIST_EMPHASIZE = 3
_I_CLOSE_LIST_STRIKE = 4
_I_CLOSE_LIST_CITATION = 5
_I_CLOSE_LIST_LIST_LEVEL = 6
_I_CLOSE_LIST_LIST_TYPE = 7
_I_CLOSE_LIST_CODE = 8
_I_CLOSE_LIST_SHORTCUT = 9
_I_CLOSE_LISTPARAGRAPH_PARAGRAPH = 1
_I_CLOSE_LISTPARAGRAPH = 2
_I_CLOSE_LISTPARAGRAPH_LIST_LEVEL = 3
_I_CLOSE_LISTPARAGRAPH_LIST_TYPE = 4
_I_CLOSE_PARAGRAPH = 1
_I_CLOSE_PARAGRAPH_EMPHASIZE = 2
_I_CLOSE_PARAGRAPH_STRIKE = 3
_I_CLOSE_PARAGRAPH_CITATION = 4
_I_CLOSE_PARAGRAPH_CODE = 5
_I_CLOSE_PARAGRAPH_SHORTCUT = 6
_I_CLOSE_RESOURCE = 1
_I_CLOSE_RESOURCE_ENTRY_KEY = 2
_I_CLOSE_RESOURCE_EMPHASIZE = 3
_I_CLOSE_RESOURCE_STRIKE = 4
_I_CLOSE_RESOURCE_CITATION = 5
_I_CLOSE_RESOURCE_CODE = 6
_I_CLOSE_RESOURCE_SHORTCUT = 7
_I_CLOSE_RESOURCEIGNK = 1
_I_CLOSE_RESOURCEIGNK_ENTRY_KEY = 2
_I_CLOSE_EMPHASIZE = 1
_I_CLOSE_EMPHASIZE_STRIKE = 2
_I_CLOSE_EMPHASIZE_CITATION = 3
_I_CLOSE_EMPHASIZE_CODE = 4
_I_CLOSE_EMPHASIZE_SHORTCUT = 5
_I_CLOSE_STRIKE = 1
_I_CLOSE_STRIKE_EMPHASIZE = 2
_I_CLOSE_STRIKE_CITATION = 3
_I_CLOSE_STRIKE_CODE = 4
_I_CLOSE_STRIKE_SHORTCUT = 5
_I_CLOSE_CITATION = 1
_I_CLOSE_CITATION_DATA = 2
_I_CLOSE_CITATIONDATA = 1
_I_CLOSE_CITATIONDATA_EMPHASIZE = 2
_I_CLOSE_CITATIONDATA_STRIKE = 3
_I_CLOSE_CITATIONDATA_CITATION = 4
_I_CLOSE_CITATIONDATA_CODE = 5
_I_CLOSE_CITATIONDATA_SHORTCUT = 6
_I_CLOSE_CITATIONDATAIGNORED = 1
_I_CLOSE_CODE = 1
class CaveMark:
"""Create a new CaveMark string parser object,
"""
def __init__(
self, resources=None, resource_keys_ignored=None,
resource_counters=None, escape=None, code=None, code_inline=None,
code_unescape=None, heading_offset=None, shortcuts=None,
cite_id_default=None, cite_type_default=None, cite_key_default=None,
frmt_cite_inline=None, frmt_cite_box=None, frmt_cite_data_default=None,
frmt_cite_error_inline=None, frmt_cite_error_box=None,
frmt_bibliography_prefix=None, frmt_bibliography_suffix=None,
frmt_bibliography_item=None, frmt_bibliography_error_item=None,
frmt_footnote_prefix=None, frmt_footnote_suffix=None,
frmt_footnote_item=None, frmt_footnote_error_item=None,
frmt_paragraph_prefix=None, frmt_paragraph_suffix=None,
frmt_emph_prefix=None, frmt_emph_suffix=None, frmt_strike_prefix=None,
frmt_strike_suffix=None, frmt_code_prefix=None, frmt_code_suffix=None,
frmt_olist_prefix=None, frmt_olist_suffix=None, frmt_ulist_prefix=None,
frmt_ulist_suffix=None, frmt_list_item_prefix=None,
frmt_list_item_suffix=None, frmt_heading_prefix=None,
frmt_heading_suffix=None
):
# references/resources dictionary
if resources is None:
self.resources = {}
else:
self.resources = resources
# ignored resource keys
if resource_keys_ignored is None:
self.resource_keys_ignored = {'url'}
else:
self.resource_keys_ignored = resource_keys_ignored
# counters per resource
if resource_counters is None:
self.resource_counters = {
'link' :'counter_a',
'book' :'counter_a',
'image' :'counter_c',
'note' :'counter_d',
'quotation' :'counter_e',
'translation':'counter_f',
'arabic' :'counter_g',
'rule' :'counter_h',
'definition' :'counter_i',
'axiom' :'counter_j',
'assumption' :'counter_k',
'theorem' :'counter_l',
'corollary' :'counter_m',
'conjecture' :'counter_n',
'hypothesis' :'counter_o',
'SECTION' :'counter_p',
'question' :'counter_q',
'answer' :'counter_r',
'footnote' :'counter_s',
}
else:
self.resource_counters = resource_counters
# escape char is used to literally print stuff
if escape is None:
self.escape = '\\'
else:
self.escape = escape
# opening/closing tags that define substrings to ignore parsing them
if code is None:
self.code = {
'$$' : '$$',
'$' : '$',
'`' : '`',
'```': '```',
}
else:
self.code = code
# specify which code is inline so that when it appears alone, it gets
# encapsulated in a paragraph
if code_inline is None:
self.code_inline = {'$$', '$', '`'}
else:
self.code_inline = code_inline
# ignored text intervals to unescape
if code_unescape is None:
self.code_unescape = {'`', '```'}
else:
self.code_unescape = code_unescape
# offset heading level. e.g. if offset=1, "# title" becomes
# "<h2>title</h2>" instead of "<h1>..."
if heading_offset is None:
self.heading_offset = 1
else:
self.heading_offset = heading_offset
# inline quotes format
if shortcuts is None:
self.shortcuts = {
'(c)' : '©',
'(tm)' : '™',
'(R)' : '®',
'"' : '“',
"''" : '”',
'---' : '—',
'...' : '…',
}
else:
self.shortcuts = shortcuts
# default id for citations that lack an identifier
if cite_id_default is None:
self.cite_id_default = '__'
else:
self.cite_id_default = cite_id_default
# default type for citations that lack an identifier
if cite_type_default is None:
self.cite_type_default = 'footnote'
else:
self.cite_type_default = cite_type_default
# default data key for citations that lack an identifier
if cite_key_default is None:
self.cite_key_default = {
'link' : 'text',
'footnote' : 'text',
}
else:
self.cite_key_default = cite_key_default
# inline citation format
if frmt_cite_inline is None:
self.frmt_cite_inline = {
'link' :' <a href="{url}">{text}</a>',
'book' :' <a href="#cite_{ID}">[{INDEX}]</a>',
'image' :' <a href="#cite_{ID}">Figure {INDEX}</a>',
'note' :' <a href="#cite_{ID}">Note {INDEX}</a>',
'quotation' :' <a href="#cite_{ID}">Quote {INDEX}</a>',
'translaton' :' <a href="#cite_{ID}">Translation {INDEX}</a>',
'arabic' :' <a href="#cite_{ID}">Arabic text {INDEX}</a>',
'rule' :' <a href="#cite_{ID}">Rule {INDEX}</a>',
'definition' :' <a href="#cite_{ID}">Definition {INDEX}</a>',
'axiom' :' <a href="#cite_{ID}">Axiom {INDEX}</a>',
'assumption' :' <a href="#cite_{ID}">Assumption {INDEX}</a>',
'theorem' :' <a href="#cite_{ID}">Theorem {INDEX}</a>',
'corollary' :' <a href="#cite_{ID}">Corollary {INDEX}</a>',
'conjecture' :' <a href="#cite_{ID}">Conjecture {INDEX}</a>',
'hypothesis' :' <a href="#cite_{ID}">Hypothesis {INDEX}</a>',
'SECTION' :' <a href="#{BOOKMARK}">Section {INDEX}</a>',
'question' :' <a href="#cite_{ID}">Question {INDEX}</a>',
'answer' :' <a href="#cite_{ID}">Answer {INDEX}</a>',
'footnote' :'<sup><a href="#fn_{ID}">{INDEX}</a></sup>',
}
else:
self.frmt_cite_inline = frmt_cite_inline
# box citation format.
if frmt_cite_box is None:
self.frmt_cite_box = {
'image' :'<figure id="cite_{ID}" '
'style="text-align:center;">'
'<img alt="{alt}" src="{url}" />'
'<figcaption>'
'<strong><a href="#cite_{ID}">'
'Figure {INDEX}.'
'</a></strong>'
' {caption}'
'</figcaption>'
'</figure>\n\n',
'note' :'<p id="cite_{ID}">'
'<strong><a href="#cite_{ID}">'
'Note {INDEX}.'
'</a></strong>'
' <em>{text}</em>'
'</p>\n\n',
'quotation' :'<figure id="cite_{ID}">'
'<strong><a href="#cite_{ID}">'
'Quote {INDEX}:'
'</a></strong>'
'<blockquote><em>'
'“{text}”'
'</em></blockquote>'
'<footer style="text-align:right;">'
'— <cite>{author}</cite>'
'</footer>'
'</figure>\n\n',
'translation':'<figure id="cite_{ID}">'
'<strong><a href="#cite_{ID}">'
'Translation {INDEX}:'
'</a></strong>'
'<blockquote><em>'
'“{text}”'
'</em></blockquote>'
'<footer style="text-align:right;">'
'— <cite>{author}</cite>'
'</footer>'
'</figure>\n\n',
'arabic' :'<figure id="cite_{ID}">'
'<strong><a href="#cite_{ID}">'
'Arabic text {INDEX}:'
'</a></strong>'
'<blockquote class="arabic">'
'”{text}“'
'</blockquote>'
'<footer style="text-align:right;">'
'— <cite>{author}</cite>'
'</footer>'
'</figure>\n\n',
'rule' :'<p id="cite_{ID}">'
'<strong><a href="#cite_{ID}">'
'Rule {INDEX}.'
'</a></strong>'
' <em>{text}</em>'
'</p>\n\n',
'definition':'<p id="cite_{ID}">'
'<strong><a href="#cite_{ID}">'
'Definition {INDEX}.'
'</a></strong>'
' <em>{text}</em>'
'</p>\n\n',
'axiom' :'<p id="cite_{ID}">'
'<strong><a href="#cite_{ID}">'
'Axiom {INDEX}.'
'</a></strong>'
' <em>{text}</em>'
'</p>\n\n',
'assumption' :'<p id="cite_{ID}">'
'<strong><a href="#cite_{ID}">'
'Assumption {INDEX}.'
'</a></strong>'
' <em>{text}</em>'
'</p>\n\n',
'theorem' :'<p id="cite_{ID}">'
'<strong><a href="#cite_{ID}">'
'Theorem {INDEX}.'
'</a></strong>'
' <em>{text}</em>'
'</p>\n\n',
'corollary' :'<p id="cite_{ID}">'
'<strong><a href="#cite_{ID}">'
'Corollary {INDEX}.'
'</a></strong>'
' <em>{text}</em>'
'</p>\n\n',
'conjecture':'<p id="cite_{ID}">'
'<strong><a href="#cite_{ID}">'
'Conjecture {INDEX}.'
'</a></strong>'
' <em>{text}</em>'
'</p>\n\n',
'hypothesis':'<p id="cite_{ID}">'
'<strong><a href="#cite_{ID}">'
'Hypothesis {INDEX}.'
'</a></strong>'
' <em>{text}</em>'
'</p>\n\n',
'question' :'<p id="cite_{ID}">'
'<strong><a href="#cite_{ID}">'
'Question {INDEX}.'
'</a></strong>'
' <em>{text}</em>'
'</p>\n\n',
'answer' :'<p id="cite_{ID}">'
'<strong><a href="#cite_{ID}">'
'Answer {INDEX}.'
'</a></strong>'
' <em>{text}</em>'
'</p>\n\n',
}
else:
self.frmt_cite_box = frmt_cite_box
# default resource data when data is not supplied
if frmt_cite_data_default is None:
self.frmt_cite_data_default = {
'link' :'[{INDEX}]',
'book' :' <a href="#cite_{ID}">[{INDEX}]</a>',
}
else:
self.frmt_cite_data_default = frmt_cite_data_default
# errornous inline citation format
if frmt_cite_error_inline is None:
self.frmt_cite_error_inline = '<span class="error">'\
'[err: {ERROR}]</span>'
else:
self.frmt_cite_error_inline = frmt_cite_error_inline
# errornous box citation format
if frmt_cite_error_box is None:
self.frmt_cite_error_box = '<p class="error" '\
'style="text-align:center">'\
'[err: {ERROR}]'\
'</p>'
else:
self.frmt_cite_error_box = frmt_cite_error_box
# bibliography container prefix
if frmt_bibliography_prefix is None:
self.frmt_bibliography_prefix = '<footer>\n'\
'<h4>Bibliography</h4>\n'\
'<ul style="list-style:none;'\
'padding:0; margin:0;">\n'
else:
self.frmt_bibliography_prefix = frmt_bibliography_prefix
# bibliography container suffix
if frmt_bibliography_suffix is None:
self.frmt_bibliography_suffix = '</ul>\n</footer>\n\n'
else:
self.frmt_bibliography_suffix = frmt_bibliography_suffix
# bibliography items format
if frmt_bibliography_item is None:
self.frmt_bibliography_item = {
'book' :'<li id="cite_{ID}"><small>'
'[{INDEX}] {authors}, '
'“<em>{title}</em>“, {publisher}, '
'{year}.</small></li>\n',
}
else:
self.frmt_bibliography_item = frmt_bibliography_item
# errornous bibliography item format
if frmt_bibliography_error_item is None:
self.frmt_bibliography_error_item = '<li class="error">'\
'<small>err: {ERROR}</small>'\
'</li>'
else:
self.frmt_bibliography_error_item = frmt_bibliography_error_item
# footnote container prefix
if frmt_footnote_prefix is None:
self.frmt_footnote_prefix = '<footer>\n<hr/>\n'\
'<ul style="list-style:none;'\
'padding:0; margin:0;">'
else:
self.frmt_footnote_prefix = frmt_footnote_prefix
# footnote container suffix
if frmt_footnote_suffix is None:
self.frmt_footnote_suffix = '</ul>\n</footer>\n\n'
else:
self.frmt_footnote_suffix = frmt_footnote_suffix
# footnote items format
if frmt_footnote_item is None:
self.frmt_footnote_item = {
'footnote' :'<li id="fn_{ID}">'\
'<small>{INDEX}. {text}</small>'\
'</li>\n',
}
else:
self.frmt_footnote_item = frmt_footnote_item
# errornous footnote item format
if frmt_footnote_error_item is None:
self.frmt_footnote_error_item = '<li class="error">'\
'<small>err: {ERROR}</small>'\
'</li>'
else:
self.frmt_footnote_error_item = frmt_footnote_error_item
# paragraphs format
if frmt_paragraph_prefix is None:
self.frmt_paragraph_prefix = '<p>'
else:
self.frmt_paragraph_prefix = frmt_paragraph_prefix
if frmt_paragraph_suffix is None:
self.frmt_paragraph_suffix = '</p>\n\n'
else:
self.frmt_paragraph_suffix = frmt_paragraph_suffix
# emphasized text format
if frmt_emph_prefix is None:
self.frmt_emph_prefix = '<em>'
else:
self.frmt_emph_prefix = frmt_emph_prefix
if frmt_emph_suffix is None:
self.frmt_emph_suffix = '</em>'
else:
self.frmt_emph_suffix = frmt_emph_suffix
# struck-through text format
if frmt_strike_prefix is None:
self.frmt_strike_prefix = '<s>'
else:
self.frmt_strike_prefix = frmt_strike_prefix
if frmt_strike_suffix is None:
self.frmt_strike_suffix = '</s>'
else:
self.frmt_strike_suffix = frmt_strike_suffix
# code prefix format
if frmt_code_prefix is None:
self.frmt_code_prefix = {
'$$' : '<span class="ignored">{OPEN}',
'$' : '<span class="ignored">{OPEN}',
'`' : '<code>',
'```' : '<pre><code>',
}
else:
self.frmt_code_prefix = frmt_code_prefix
# code suffix format
if frmt_code_suffix is None:
self.frmt_code_suffix = {
'$$' : '{CLOSE}</span>',
'$' : '{CLOSE}</span>',
'`' : '</code>',
'```' : '</code></pre>',
}
else:
self.frmt_code_suffix = frmt_code_suffix
# ordered lists format
if frmt_olist_prefix is None:
self.frmt_olist_prefix = '<ol>\n'
else:
self.frmt_olist_prefix = frmt_olist_prefix
if frmt_olist_suffix is None:
self.frmt_olist_suffix = '</ol>\n'
else:
self.frmt_olist_suffix = frmt_olist_suffix
# unordered lists format
if frmt_ulist_prefix is None:
self.frmt_ulist_prefix = '<ul>\n'
else:
self.frmt_ulist_prefix = frmt_ulist_prefix
if frmt_ulist_suffix is None:
self.frmt_ulist_suffix = '</ul>\n'
else:
self.frmt_ulist_suffix = frmt_ulist_suffix
# listed items format
if frmt_list_item_prefix is None:
self.frmt_list_item_prefix = '<li>'
else:
self.frmt_list_item_prefix = frmt_list_item_prefix
if frmt_list_item_suffix is None:
self.frmt_list_item_suffix = '</li>\n'
else:
self.frmt_list_item_suffix = frmt_list_item_suffix
# headings format
if frmt_heading_prefix is None:
self.frmt_heading_prefix = '<h{LEVEL} id="{BOOKMARK}">'\
'<a class="hindex"'\
' href="#{BOOKMARK}">{INDEX}.</a> '
else:
self.frmt_heading_prefix = frmt_heading_prefix
if frmt_heading_suffix is None:
self.frmt_heading_suffix = '</h{LEVEL}>\n\n'
else:
self.frmt_heading_suffix = frmt_heading_suffix
# states that need to be defined early
self._state = [_S_START]
self._html = [[]]
self._resource_cur_entry_key = None
self._citations_last_index = {}
self._citations_last_default_id_prefix = 0
self._citations_pending_boxes = []
self._citation_cur_id = None
self._citation_cur_data = None
self.resources_cited = {}
self._footnote_items = []
self._bibliography_items = []
self._list = [[-1, None]]
self._heading_index = [0] * 6
self._heading_bookmark = [''] * 6
# compile re and other stuff
self.update()
def update(self):
"""Run this when you modify self.code or add/remove resource types.
"""
# open tag any
tags_open_code = list(self.code)
tags_open_code.sort(key=len, reverse=True)
shortcuts_raw = list(self.shortcuts)
shortcuts_raw.sort(key=len, reverse=True)
resource_types = list(self.resource_counters)
self._re_tag_open_any = re.compile(
r'(?<!{0})(?:{1})'.format(
re.escape(self.escape),
r'|'.join([
r'(^\s*\n|\n\s*\n)', # unit separator
r'(_)', # emphasize open
r'(\~\~)', # strike open
r'(\[)', # cite open
r'^ *(#+)(?:(\S+)|)(?: +| *\n)(?=\S)', # heading open
r'^( *)(\*|\+)', # list open
r'^ *({}) *: *(\S+)'.format( # resource definition open
r'|'.join(resource_types)
),
r'({})\n?'.format( # code open
r'|'.join(re.escape(o) for o in tags_open_code)
),
r'({})'.format( # shortcuts
r'|'.join(re.escape(s) for s in shortcuts_raw)
),
r'\Z',
])
)
)
# close tag heading
self._re_tag_close_heading = re.compile(
r'(?<!{0})(?:{1})'.format(
re.escape(self.escape),
r'|'.join([
r'(\n\s*\n)', # heading close
r'(_)', # emphasize open
r'(\~\~)', # strike open
r'(\[)', # cite open
r'({})\n?'.format( # code open
r'|'.join(re.escape(o) for o in tags_open_code)
),
r'({})'.format( # shortcuts
r'|'.join(re.escape(s) for s in shortcuts_raw)
),
r'\Z',
])
)
)
# close tag list
self._re_tag_close_list = re.compile(
r'(?<!{0})(?:{1})'.format(
re.escape(self.escape),
r'|'.join([
r'(\n\s*\n +)(?=[^\*\+\s])',# open paragraph
r'(\n\s*\n)(?=[^\*\+\s])', # list close
r'(_)', # emphasize open
r'(\~\~)', # strike open
r'(\[)', # cite open
r'(?:^|\n)( *)(\*|\+)', # list open
r'({})\n?'.format( # code open
r'|'.join(re.escape(o) for o in tags_open_code)
),
r'({})'.format( # shortcuts
r'|'.join(re.escape(s) for s in shortcuts_raw)
),
r'\Z',
])
)
)
# close tag listparagraph
self._re_tag_close_listparagraph = re.compile(
r'(?<!{0})(?:{1})'.format(
re.escape(self.escape),
r'|'.join([
r'^( +)(?=[^\*\+\s])', # open paragraph
r'^()(?=[^\*\+\s])', # list close
r'^( *)(\*|\+)', # list open
r'\Z',
])
)
)
# close tag paragraph
self._re_tag_close_paragraph = re.compile(
r'(?<!{0})(?:{1})'.format(
re.escape(self.escape),
r'|'.join([
r'(\n\s*\n)', # paragraph close
r'(_)', # emphasize open
r'(\~\~)', # strike open
r'(\[)', # cite open
r'({})\n?'.format( # code open
r'|'.join(re.escape(o) for o in tags_open_code)
),
r'({})'.format( # shortcuts
r'|'.join(re.escape(s) for s in shortcuts_raw)
),
r'\Z',
])
)
)
# close tag resource
self._re_tag_close_resource = re.compile(
r'(?<!{0})(?:{1})'.format(
re.escape(self.escape),
r'|'.join([
r'(\n\s*\n)', # resource close
r'\n *([^\[\s]\S*?) *: *',# resource entry key
r'(_)', # emphasize open
r'(\~\~)', # strike open
r'(\[)', # cite open
r'({})\n?'.format( # code open
r'|'.join(re.escape(o) for o in tags_open_code)
),
r'({})'.format( # shortcuts
r'|'.join(re.escape(s) for s in shortcuts_raw)
),
r'\Z',
])
)
)
# close tag resource ignored key
self._re_tag_close_resource_ignored_key = re.compile(
r'(?<!{0})(?:{1})'.format(
re.escape(self.escape),
r'|'.join([
r'(\n\s*\n)', # resource close
r'\n *(\S+) *: *', # resource entry key
r'\Z',
])
)
)
# close tag emphasize
self._re_tag_close_emphasize = re.compile(
r'(?<!{0})(?:{1})'.format(
re.escape(self.escape),
r'|'.join([
r'(_)', # emphasize close
r'(\~\~)', # strike open
r'(\[)', # cite open
r'({})\n?'.format( # code open
r'|'.join(re.escape(o) for o in tags_open_code)
),
r'({})'.format( # shortcuts
r'|'.join(re.escape(s) for s in shortcuts_raw)
),
r'\Z',
])
)
)
# close tag strike
self._re_tag_close_strike = re.compile(
r'(?<!{0})(?:{1})'.format(
re.escape(self.escape),
r'|'.join([
r'(\~\~)', # strike close
r'(_)', # emphasize open
r'(\[)', # cite open
r'({})\n?'.format( # code open
r'|'.join(re.escape(o) for o in tags_open_code)
),
r'({})'.format( # shortcuts
r'|'.join(re.escape(s) for s in shortcuts_raw)
),
r'\Z',
])
)
)
# close tag citation
self._re_tag_close_citation = re.compile(
r'(?<!{0})(?:{1})'.format(
re.escape(self.escape),
r'|'.join([
r'(\])', # cite close
r'(:)', # data open
r'\Z',
])
)
)
# close tag citationdata
self._re_tag_close_citationdata = re.compile(
r'(?<!{0})(?:{1})'.format(
re.escape(self.escape),
r'|'.join([
r'(\])', # citationdata close
r'(_)', # emphasize open
r'(\~\~)', # strike open
r'(\[)', # cite open
r'({})\n?'.format( # code open
r'|'.join(re.escape(o) for o in tags_open_code)
),
r'({})'.format( # shortcuts
r'|'.join(re.escape(s) for s in shortcuts_raw)
),
r'\Z',
])
)
)
# close tag citationdata when the data key is to be ignored
self._re_tag_close_citationdata_ignored = re.compile(
r'(?<!{0})(?:{1})'.format(
re.escape(self.escape),
r'|'.join([
r'(\])', # citationdata close
r'\Z',
])
)
)
# close tag code
self._re_tag_close_code = {
o : re.compile(
r'(?<!{0})(?:{1})'.format(
re.escape(self.escape),
r'|'.join([
r'\n?({})'.format( # code close
re.escape(self.code[o])
),
r'\Z',
])
)
)
for o in tags_open_code
}
# unescape
self._re_unescape = re.compile(
r'{}(.)'.format(re.escape(self.escape))
)
def parse(self, text):
"""Parse input CaveMark string.
"""
text = self._replace_unsafe(text)
while True:
if len(text) == 0:
break
# parse new unit
elif self._state[-1] == _S_START:
m = self._re_tag_open_any.search(text)
start, endo = m.span()
text_behind = text[0:start]
text_behind = self._unescape(text_behind)
text = text[endo:]
if len(self._citations_pending_boxes):
self._flush_pending_boxes()
if len(text_behind.strip()):
self._paragraph_open()
self._html[-1].append(text_behind)
if m.group(_I_OPEN_ANY_SEP) is not None:
self._paragraph_close()
continue
if m.group(_I_OPEN_ANY_EMPHASIZE) is not None:
self._emphasize_open()
elif m.group(_I_OPEN_ANY_STRIKE) is not None:
self._strike_open()
elif m.group(_I_OPEN_ANY_CODE) is not None:
self._code_open(m.group(_I_OPEN_ANY_CODE))
elif m.group(_I_OPEN_ANY_SHORTCUT) is not None:
self._shortcut_add(m.group(_I_OPEN_ANY_SHORTCUT))
elif m.group(_I_OPEN_ANY_CITATION) is not None:
self._citation_open()
elif self._state[-1] == _S_START:
if m.group(_I_OPEN_ANY_HEADING_LEVEL) is not None:
self._heading_open(
m.group(_I_OPEN_ANY_HEADING_LEVEL),
m.group(_I_OPEN_ANY_HEADING_BOOKMARK)
)
elif m.group(_I_OPEN_ANY_LIST_LEVEL) is not None:
self._list_open(
len(m.group(_I_OPEN_ANY_LIST_LEVEL)),
m.group(_I_OPEN_ANY_LIST_TYPE)
)
elif m.group(_I_OPEN_ANY_RESOURCE_TYPE) is not None:
self._resource_open(
m.group(_I_OPEN_ANY_RESOURCE_TYPE),
m.group(_I_OPEN_ANY_RESOURCE_ID),
)
# resume parsing paragraph
elif self._state[-1] == _S_PARAGRAPH_IN:
m = self._re_tag_close_paragraph.search(text)
start, endo = m.span()
text_behind = text[0:start]
text_behind = self._unescape(text_behind)
text = text[endo:]
self._html[-1].append(text_behind)
if m.group(_I_CLOSE_PARAGRAPH) is not None:
self._paragraph_close()
elif m.group(_I_CLOSE_PARAGRAPH_EMPHASIZE) is not None:
self._emphasize_open()
elif m.group(_I_CLOSE_PARAGRAPH_STRIKE) is not None:
self._strike_open()
elif m.group(_I_CLOSE_PARAGRAPH_CODE) is not None:
self._code_open(m.group(_I_CLOSE_PARAGRAPH_CODE))
elif m.group(_I_CLOSE_PARAGRAPH_SHORTCUT) is not None:
self._shortcut_add(m.group(_I_CLOSE_PARAGRAPH_SHORTCUT))
elif m.group(_I_CLOSE_PARAGRAPH_CITATION) is not None:
self._citation_open()
# resume parsing emphasized text
elif self._state[-1] == _S_EMPHASIZE_IN:
m = self._re_tag_close_emphasize.search(text)
start, endo = m.span()
text_behind = text[0:start]
text_behind = self._unescape(text_behind)
text = text[endo:]
self._html[-1].append(text_behind)
if m.group(_I_CLOSE_EMPHASIZE) is not None:
self._emphasize_close()
elif m.group(_I_CLOSE_EMPHASIZE_CODE) is not None:
self._code_open(m.group(_I_CLOSE_EMPHASIZE_CODE))
elif m.group(_I_CLOSE_EMPHASIZE_SHORTCUT) is not None:
self._shortcut_add(m.group(_I_CLOSE_EMPHASIZE_SHORTCUT))
elif m.group(_I_CLOSE_EMPHASIZE_CITATION) is not None:
self._citation_open()
# resume parsing struck-through text
elif self._state[-1] == _S_STRIKE_IN:
m = self._re_tag_close_strike.search(text)
start, endo = m.span()
text_behind = text[0:start]
text_behind = self._unescape(text_behind)
text = text[endo:]
self._html[-1].append(text_behind)
if m.group(_I_CLOSE_STRIKE) is not None:
self._strike_close()
elif m.group(_I_CLOSE_STRIKE_EMPHASIZE) is not None:
self._emphasize_open()
elif m.group(_I_CLOSE_STRIKE_CODE) is not None:
self._code_open(m.group(_I_CLOSE_STRIKE_CODE))
elif m.group(_I_CLOSE_STRIKE_SHORTCUT) is not None:
self._shortcut_add(m.group(_I_CLOSE_STRIKE_SHORTCUT))
elif m.group(_I_CLOSE_STRIKE_CITATION) is not None:
self._citation_open()
# resume parsing code
elif self._state[-1] == _S_CODE_IN:
m = self._re_tag_close_code[self._code_tag_open].search(text)
start, endo = m.span()
text_behind = text[0:start]
text_behind = self._unescape(
text_behind,
tag=self.code[self._code_tag_open]
)
text = text[endo:]
self._html[-1].append(text_behind)
if m.group(_I_CLOSE_CODE) is not None:
self._code_close()
# resume parsing list
elif self._state[-1] == _S_LIST_IN:
m = self._re_tag_close_list.search(text)
start, endo = m.span()
text_behind = text[0:start]
text_behind = self._unescape(text_behind)
text = text[endo:]
self._html[-1].append(text_behind)
if m.group(_I_CLOSE_LIST) is not None:
while len(self._list) > 1:
self._list_close()
elif m.group(_I_CLOSE_LIST_PARAGRAPH) is not None:
self._listparagraph_open()
self._paragraph_open()
elif m.group(_I_CLOSE_LIST_EMPHASIZE) is not None:
self._emphasize_open()
elif m.group(_I_CLOSE_LIST_STRIKE) is not None:
self._strike_open()
elif m.group(_I_CLOSE_LIST_CODE) is not None:
self._code_open(m.group(_I_CLOSE_LIST_CODE))
elif m.group(_I_CLOSE_LIST_SHORTCUT) is not None:
self._shortcut_add(m.group(_I_CLOSE_LIST_SHORTCUT))
elif m.group(_I_CLOSE_LIST_CITATION) is not None:
self._citation_open()
elif m.group(_I_CLOSE_LIST_LIST_LEVEL) is not None:
self._list_item_new(
len(m.group(_I_CLOSE_LIST_LIST_LEVEL)),
m.group(_I_CLOSE_LIST_LIST_TYPE)
)