-
-
Notifications
You must be signed in to change notification settings - Fork 789
/
asciidoctor.rb
1608 lines (1403 loc) · 54.5 KB
/
asciidoctor.rb
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
require 'set'
# NOTE RUBY_ENGINE == 'opal' conditional blocks like this are filtered by the Opal preprocessor
if RUBY_ENGINE == 'opal'
# this require is satisfied by the Asciidoctor.js build; it augments the Ruby environment for Asciidoctor.js
require 'asciidoctor/js'
else
autoload :Base64, 'base64'
autoload :OpenURI, 'open-uri'
autoload :Pathname, 'pathname'
autoload :StringScanner, 'strscan'
autoload :URI, 'uri'
end
# Public: Methods for parsing AsciiDoc input files and converting documents
# using eRuby templates.
#
# AsciiDoc documents comprise a header followed by zero or more sections.
# Sections are composed of blocks of content. For example:
#
# = Doc Title
#
# == Section 1
#
# This is a paragraph block in the first section.
#
# == Section 2
#
# This section has a paragraph block and an olist block.
#
# . Item 1
# . Item 2
#
# Examples:
#
# Use built-in converter:
#
# Asciidoctor.convert_file 'sample.adoc'
#
# Use custom (Tilt-supported) templates:
#
# Asciidoctor.convert_file 'sample.adoc', template_dir: 'path/to/templates'
#
module Asciidoctor
# alias the RUBY_ENGINE constant inside the Asciidoctor namespace and define a precomputed alias for runtime
RUBY_ENGINE_OPAL = (RUBY_ENGINE = ::RUBY_ENGINE) == 'opal'
module SafeMode
# A safe mode level that disables any of the security features enforced
# by Asciidoctor (Ruby is still subject to its own restrictions).
UNSAFE = 0;
# A safe mode level that closely parallels safe mode in AsciiDoc. This value
# prevents access to files which reside outside of the parent directory of
# the source file and disables any macro other than the include::[] directive.
SAFE = 1;
# A safe mode level that disallows the document from setting attributes
# that would affect the conversion of the document, in addition to all the
# security features of SafeMode::SAFE. For instance, this level disallows
# changing the backend or the source-highlighter using an attribute defined
# in the source document. This is the most fundamental level of security
# for server-side deployments (hence the name).
SERVER = 10;
# A safe mode level that disallows the document from attempting to read
# files from the file system and including the contents of them into the
# document, in additional to all the security features of SafeMode::SERVER.
# For instance, this level disallows use of the include::[] directive and the
# embedding of binary content (data uri), stylesheets and JavaScripts
# referenced by the document.(Asciidoctor and trusted extensions may still
# be allowed to embed trusted content into the document).
#
# Since Asciidoctor is aiming for wide adoption, this level is the default
# and is recommended for server-side deployments.
SECURE = 20;
# A planned safe mode level that disallows the use of passthrough macros and
# prevents the document from setting any known attributes, in addition to all
# the security features of SafeMode::SECURE.
#
# Please note that this level is not currently implemented (and therefore not
# enforced)!
#PARANOID = 100;
@names_by_value = accum = {}
(constants false).each {|sym| accum[const_get sym, false] = sym.to_s.downcase }
def self.value_for_name name
const_get name.upcase, false
end
def self.name_for_value value
@names_by_value[value]
end
def self.names
@names_by_value.values
end
end
# Flags to control compliance with the behavior of AsciiDoc
module Compliance
@keys = ::Set.new
class << self
attr_reader :keys
end
# Defines a new compliance key and assigns an initial value.
def self.define key, value
instance_variable_set %(@#{key}), value
class << self; self; end.send :attr_accessor, key
@keys << key
nil
end
# AsciiDoc terminates paragraphs adjacent to
# block content (delimiter or block attribute list)
# This option allows this behavior to be modified
# TODO what about literal paragraph?
# Compliance value: true
define :block_terminates_paragraph, true
# AsciiDoc does not parse paragraphs with a verbatim style
# (i.e., literal, listing, source, verse) as verbatim content.
# This options allows this behavior to be modified
# Compliance value: false
define :strict_verbatim_paragraphs, true
# AsciiDoc supports both atx (single-line) and setext (underlined) section titles.
# This option can be used to disable the setext variant.
# Compliance value: true
define :underline_style_section_titles, true
# Asciidoctor will unwrap the content in a preamble
# if the document has a title and no sections.
# Compliance value: false
define :unwrap_standalone_preamble, true
# AsciiDoc drops lines that contain references to missing attributes.
# This behavior is not intuitive to most writers
# Compliance value: 'drop-line'
define :attribute_missing, 'skip'
# AsciiDoc drops lines that contain an attribute unassignemnt.
# This behavior may need to be tuned depending on the circumstances.
# Compliance value: 'drop-line'
define :attribute_undefined, 'drop-line'
# Asciidoctor will allow the id, role and options to be set
# on blocks using a shorthand syntax (e.g., #idname.rolename%optionname)
# Compliance value: false
define :shorthand_property_syntax, true
# Asciidoctor will attempt to resolve the target of a cross reference by
# matching its reference text (reftext or title) (e.g., <<Section Title>>)
# Compliance value: false
define :natural_xrefs, true
# Asciidoctor will start counting at the following number
# when creating a unique id when there is a conflict
# Compliance value: 2
define :unique_id_start_index, 2
# Asciidoctor will recognize commonly-used Markdown syntax
# to the degree it does not interfere with existing
# AsciiDoc syntax and behavior.
# Compliance value: false
define :markdown_syntax, true
end
# The absolute root directory of the Asciidoctor RubyGem
ROOT_DIR = ::File.dirname ::File.absolute_path __dir__ unless defined? ROOT_DIR
# The absolute lib directory of the Asciidoctor RubyGem
LIB_DIR = ::File.join ROOT_DIR, 'lib'
# The absolute data directory of the Asciidoctor RubyGem
DATA_DIR = ::File.join ROOT_DIR, 'data'
# The user's home directory, as best we can determine it
USER_HOME = ::Dir.home
# The endline character used for output; stored in constant table as an optimization
LF = EOL = ?\n
# The null character to use for splitting attribute values
NULL = ?\0
# String for matching tab character
TAB = ?\t
# Maximum integer value for "boundless" operations; equal to MAX_SAFE_INTEGER in JavaScript
MAX_INT = 9007199254740991
# Alias UTF_8 encoding for convenience / speed
UTF_8 = ::Encoding::UTF_8
# Byte arrays for UTF-* Byte Order Marks
BOM_BYTES_UTF_8 = [0xef, 0xbb, 0xbf]
BOM_BYTES_UTF_16LE = [0xff, 0xfe]
BOM_BYTES_UTF_16BE = [0xfe, 0xff]
# The mode to use when opening a file for reading
FILE_READ_MODE = RUBY_ENGINE_OPAL ? 'r' : 'rb:utf-8:utf-8'
# The mode to use when opening a URI for reading
URI_READ_MODE = FILE_READ_MODE
# The mode to use when opening a file for writing
FILE_WRITE_MODE = RUBY_ENGINE_OPAL ? 'w' : 'w:utf-8'
# The default document type
# Can influence markup generated by the converters
DEFAULT_DOCTYPE = 'article'
# The backend determines the format of the converted output, default to html5
DEFAULT_BACKEND = 'html5'
DEFAULT_STYLESHEET_KEYS = ['', 'DEFAULT'].to_set
DEFAULT_STYLESHEET_NAME = 'asciidoctor.css'
# Pointers to the preferred version for a given backend.
BACKEND_ALIASES = {
'html' => 'html5',
'docbook' => 'docbook5'
}
# Default page widths for calculating absolute widths
DEFAULT_PAGE_WIDTHS = {
'docbook' => 425
}
# Default extensions for the respective base backends
DEFAULT_EXTENSIONS = {
'html' => '.html',
'docbook' => '.xml',
'pdf' => '.pdf',
'epub' => '.epub',
'manpage' => '.man',
'asciidoc' => '.adoc'
}
# Set of file extensions recognized as AsciiDoc documents (stored as a truth hash)
ASCIIDOC_EXTENSIONS = {
'.adoc' => true,
'.asciidoc' => true,
'.asc' => true,
'.ad' => true,
# TODO .txt should be deprecated
'.txt' => true
}
SETEXT_SECTION_LEVELS = {
'=' => 0,
'-' => 1,
'~' => 2,
'^' => 3,
'+' => 4
}
ADMONITION_STYLES = ['NOTE', 'TIP', 'IMPORTANT', 'WARNING', 'CAUTION'].to_set
ADMONITION_STYLE_HEADS = ['N', 'T', 'I', 'W', 'C'].to_set
PARAGRAPH_STYLES = ['comment', 'example', 'literal', 'listing', 'normal', 'open', 'pass', 'quote', 'sidebar', 'source', 'verse', 'abstract', 'partintro'].to_set
VERBATIM_STYLES = ['literal', 'listing', 'source', 'verse'].to_set
DELIMITED_BLOCKS = {
'--' => [:open, ['comment', 'example', 'literal', 'listing', 'pass', 'quote', 'sidebar', 'source', 'verse', 'admonition', 'abstract', 'partintro'].to_set],
'----' => [:listing, ['literal', 'source'].to_set],
'....' => [:literal, ['listing', 'source'].to_set],
'====' => [:example, ['admonition'].to_set],
'****' => [:sidebar, ::Set.new],
'____' => [:quote, ['verse'].to_set],
'""' => [:quote, ['verse'].to_set],
'++++' => [:pass, ['stem', 'latexmath', 'asciimath'].to_set],
'|===' => [:table, ::Set.new],
',===' => [:table, ::Set.new],
':===' => [:table, ::Set.new],
'!===' => [:table, ::Set.new],
'////' => [:comment, ::Set.new],
'```' => [:fenced_code, ::Set.new]
}
DELIMITED_BLOCK_HEADS = DELIMITED_BLOCKS.keys.map {|key| key.slice 0, 2 }.to_set
LAYOUT_BREAK_CHARS = {
'\'' => :thematic_break,
'<' => :page_break
}
MARKDOWN_THEMATIC_BREAK_CHARS = {
'-' => :thematic_break,
'*' => :thematic_break,
'_' => :thematic_break
}
HYBRID_LAYOUT_BREAK_CHARS = LAYOUT_BREAK_CHARS.merge MARKDOWN_THEMATIC_BREAK_CHARS
#LIST_CONTEXTS = [:ulist, :olist, :dlist, :colist]
NESTABLE_LIST_CONTEXTS = [:ulist, :olist, :dlist]
# TODO validate use of explicit style name above ordered list (this list is for selecting an implicit style)
ORDERED_LIST_STYLES = [:arabic, :loweralpha, :lowerroman, :upperalpha, :upperroman] #, :lowergreek]
ORDERED_LIST_KEYWORDS = {
#'arabic' => '1',
#'decimal' => '1',
'loweralpha' => 'a',
'lowerroman' => 'i',
#'lowergreek' => 'a',
'upperalpha' => 'A',
'upperroman' => 'I'
}
ATTR_REF_HEAD = '{'
LIST_CONTINUATION = '+'
# NOTE AsciiDoc Python allows + to be preceded by TAB; Asciidoctor does not
HARD_LINE_BREAK = ' +'
LINE_CONTINUATION = ' \\'
LINE_CONTINUATION_LEGACY = ' +'
MATHJAX_VERSION = '2.7.4'
BLOCK_MATH_DELIMITERS = {
asciimath: ['\$', '\$'],
latexmath: ['\[', '\]'],
}
INLINE_MATH_DELIMITERS = {
asciimath: ['\$', '\$'],
latexmath: ['\(', '\)'],
}
(STEM_TYPE_ALIASES = {
'latexmath' => 'latexmath',
'latex' => 'latexmath',
'tex' => 'latexmath'
}).default = 'asciimath'
FONT_AWESOME_VERSION = '4.7.0'
# attributes which be changed within the content of the document (but not
# header) because it has semantic meaning; ex. sectnums
FLEXIBLE_ATTRIBUTES = ['sectnums']
# A collection of regular expressions used by the parser.
#
# NOTE The following pattern, which appears frequently, captures the
# contents between square brackets, ignoring escaped closing brackets
# (closing brackets prefixed with a backslash '\' character)
#
# Pattern: \[(|#{CC_ALL}*?[^\\])\]
# Matches: [enclosed text] and [enclosed [text\]], not [enclosed text \\] or [\\] (as these require a trailing space)
#
# NOTE \w only matches ASCII word characters, whereas [[:word:]] or \p{Word} matches any character in the Unicode word category.
#(pseudo)module Rx
## Regular expression character classes (to ensure regexp compatibility between Ruby and JavaScript)
## CC stands for "character class", CG stands for "character class group"
unless RUBY_ENGINE == 'opal'
# CC_ALL is any character, including newlines (must be accompanied by multiline regexp flag)
CC_ALL = '.'
# CC_ANY is any character except newlines
CC_ANY = '.'
CC_EOL = '$'
CC_ALPHA = CG_ALPHA = '\p{Alpha}'
CC_ALNUM = CG_ALNUM = '\p{Alnum}'
CG_BLANK = '\p{Blank}'
CC_WORD = CG_WORD = '\p{Word}'
end
## Document header
# Matches the author info line immediately following the document title.
#
# Examples
#
# Doc Writer <doc@example.com>
# Mary_Sue Brontë
#
AuthorInfoLineRx = /^(#{CG_WORD}[#{CC_WORD}\-'.]*)(?: +(#{CG_WORD}[#{CC_WORD}\-'.]*))?(?: +(#{CG_WORD}[#{CC_WORD}\-'.]*))?(?: +<([^>]+)>)?$/
# Matches the revision info line, which appears immediately following
# the author info line beneath the document title.
#
# Examples
#
# v1.0
# 2013-01-01
# v1.0, 2013-01-01: Ring in the new year release
# 1.0, Jan 01, 2013
#
RevisionInfoLineRx = /^(?:[^\d{]*(#{CC_ANY}*?),)? *(?!:)(#{CC_ANY}*?)(?: *(?!^),?: *(#{CC_ANY}*))?$/
# Matches the title and volnum in the manpage doctype.
#
# Examples
#
# = asciidoctor(1)
# = asciidoctor ( 1 )
#
ManpageTitleVolnumRx = /^(#{CC_ANY}+?) *\( *(#{CC_ANY}+?) *\)$/
# Matches the name and purpose in the manpage doctype.
#
# Examples
#
# asciidoctor - converts AsciiDoc source files to HTML, DocBook and other formats
#
ManpageNamePurposeRx = /^(#{CC_ANY}+?) +- +(#{CC_ANY}+)$/
## Preprocessor directives
# Matches a conditional preprocessor directive (e.g., ifdef, ifndef, ifeval and endif).
#
# Examples
#
# ifdef::basebackend-html[]
# ifndef::theme[]
# ifeval::["{asciidoctor-version}" >= "0.1.0"]
# ifdef::asciidoctor[Asciidoctor!]
# endif::theme[]
# endif::basebackend-html[]
# endif::[]
#
ConditionalDirectiveRx = /^(\\)?(ifdef|ifndef|ifeval|endif)::(\S*?(?:([,+])\S*?)?)\[(#{CC_ANY}+)?\]$/
# Matches a restricted (read as safe) eval expression.
#
# Examples
#
# "{asciidoctor-version}" >= "0.1.0"
#
EvalExpressionRx = /^(#{CC_ANY}+?) *([=!><]=|[><]) *(#{CC_ANY}+)$/
# Matches an include preprocessor directive.
#
# Examples
#
# include::chapter1.ad[]
# include::example.txt[lines=1;2;5..10]
#
IncludeDirectiveRx = /^(\\)?include::([^\[][^\[]*)\[(#{CC_ANY}+)?\]$/
# Matches a trailing tag directive in an include file.
#
# Examples
#
# // tag::try-catch[]
# try {
# someMethod();
# catch (Exception e) {
# log(e);
# }
# // end::try-catch[]
# NOTE m flag is required for Asciidoctor.js
TagDirectiveRx = /\b(?:tag|(e)nd)::(\S+?)\[\](?=$|[ \r])/m
## Attribute entries and references
# Matches a document attribute entry.
#
# Examples
#
# :foo: bar
# :First Name: Dan
# :sectnums!:
# :!toc:
# :long-entry: Attribute value lines ending in ' \' \
# are joined together as a single value, \
# collapsing the line breaks and indentation to \
# a single space.
#
AttributeEntryRx = /^:(!?#{CG_WORD}[^:]*):(?:[ \t]+(#{CC_ANY}*))?$/
# Matches invalid characters in an attribute name.
InvalidAttributeNameCharsRx = /[^-#{CC_WORD}]/
# Matches a pass inline macro that surrounds the value of an attribute
# entry once it has been parsed.
#
# Examples
#
# pass:[text]
# pass:a[{a} {b} {c}]
#
if RUBY_ENGINE == 'opal'
# NOTE In JavaScript, ^ and $ match the boundaries of the string when the m flag is not set
AttributeEntryPassMacroRx = /^pass:([a-z]+(?:,[a-z]+)*)?\[([\S\s]*)\]$/
else
AttributeEntryPassMacroRx = /\Apass:([a-z]+(?:,[a-z]+)*)?\[(.*)\]\Z/m
end
# Matches an inline attribute reference.
#
# Examples
#
# {foobar} or {app_name} or {product-version}
# {counter:sequence-name:1}
# {set:foo:bar}
# {set:name!}
#
AttributeReferenceRx = /(\\)?\{(#{CG_WORD}[-#{CC_WORD}]*|(set|counter2?):#{CC_ANY}+?)(\\)?\}/
## Paragraphs and delimited blocks
# Matches an anchor (i.e., id + optional reference text) on a line above a block.
#
# Examples
#
# [[idname]]
# [[idname,Reference Text]]
#
BlockAnchorRx = /^\[\[(?:|([#{CC_ALPHA}_:][#{CC_WORD}:.-]*)(?:, *(#{CC_ANY}+))?)\]\]$/
# Matches an attribute list above a block element.
#
# Examples
#
# # strictly positional
# [quote, Adam Smith, Wealth of Nations]
#
# # name/value pairs
# [NOTE, caption="Good to know"]
#
# # as attribute reference
# [{lead}]
#
BlockAttributeListRx = /^\[(|[#{CC_WORD}.#%{,"']#{CC_ANY}*)\]$/
# A combined pattern that matches either a block anchor or a block attribute list.
#
# TODO this one gets hit a lot, should be optimized as much as possible
BlockAttributeLineRx = /^\[(?:|[#{CC_WORD}.#%{,"']#{CC_ANY}*|\[(?:|[#{CC_ALPHA}_:][#{CC_WORD}:.-]*(?:, *#{CC_ANY}+)?)\])\]$/
# Matches a title above a block.
#
# Examples
#
# .Title goes here
#
BlockTitleRx = /^\.(\.?[^ \t.]#{CC_ANY}*)$/
# Matches an admonition label at the start of a paragraph.
#
# Examples
#
# NOTE: Just a little note.
# TIP: Don't forget!
#
AdmonitionParagraphRx = /^(#{ADMONITION_STYLES.to_a.join '|'}):[ \t]+/
# Matches a literal paragraph, which is a line of text preceded by at least one space.
#
# Examples
#
# <SPACE>Foo
# <TAB>Foo
LiteralParagraphRx = /^([ \t]+#{CC_ANY}*)$/
# Matches a comment block.
#
# Examples
#
# ////
# This is a block comment.
# It can span one or more lines.
# ////
#CommentBlockRx = %r(^/{4,}$)
# Matches a comment line.
#
# Examples
#
# // note to author
#
#CommentLineRx = %r(^//(?=[^/]|$))
## Section titles
# Matches an Atx (single-line) section title.
#
# Examples
#
# == Foo
# // ^ a level 1 (h2) section title
#
# == Foo ==
# // ^ also a level 1 (h2) section title
#
AtxSectionTitleRx = /^(=={0,5})[ \t]+(#{CC_ANY}+?)(?:[ \t]+\1)?$/
# Matches an extended Atx section title that includes support for the Markdown variant.
ExtAtxSectionTitleRx = /^(=={0,5}|#\#{0,5})[ \t]+(#{CC_ANY}+?)(?:[ \t]+\1)?$/
# Matches the title only (first line) of an Setext (two-line) section title.
# The title cannot begin with a dot and must have at least one alphanumeric character.
SetextSectionTitleRx = /^((?!\.)#{CC_ANY}*?#{CG_WORD}#{CC_ANY}*)$/
# Matches an anchor (i.e., id + optional reference text) inside a section title.
#
# Examples
#
# Section Title [[idname]]
# Section Title [[idname,Reference Text]]
#
InlineSectionAnchorRx = / (\\)?\[\[([#{CC_ALPHA}_:][#{CC_WORD}:.-]*)(?:, *(#{CC_ANY}+))?\]\]$/
# Matches invalid ID characters in a section title.
#
# NOTE uppercase chars not included since expression is only run on a lowercase string
InvalidSectionIdCharsRx = /<[^>]+>|&(?:[a-z][a-z]+\d{0,2}|#\d\d\d{0,4}|#x[\da-f][\da-f][\da-f]{0,3});|[^ #{CC_WORD}\-.]+?/
# Matches an explicit section level style like sect1
#
SectionLevelStyleRx = /^sect\d$/
## Lists
# Detects the start of any list item.
#
# NOTE we only have to check as far as the blank character because we know it means non-whitespace follows.
# IMPORTANT if this regexp does not agree with the regexp for each list type, the parser will hang.
AnyListRx = %r(^(?:[ \t]*(?:-|\*\**|\.\.*|\u2022|\d+\.|[a-zA-Z]\.|[IVXivx]+\))[ \t]|(?!//[^/])#{CC_ANY}*?(?::::{0,2}|;;)(?:$|[ \t])|<?\d+>[ \t]))
# Matches an unordered list item (one level for hyphens, up to 5 levels for asterisks).
#
# Examples
#
# * Foo
# - Foo
#
# NOTE we know trailing (.*) will match at least one character because we strip trailing spaces
UnorderedListRx = /^[ \t]*(-|\*\**|\u2022)[ \t]+(#{CC_ANY}*)$/
# Matches an ordered list item (explicit numbering or up to 5 consecutive dots).
#
# Examples
#
# . Foo
# .. Foo
# 1. Foo (arabic, default)
# a. Foo (loweralpha)
# A. Foo (upperalpha)
# i. Foo (lowerroman)
# I. Foo (upperroman)
#
# NOTE leading space match is not always necessary, but is used for list reader
# NOTE we know trailing (.*) will match at least one character because we strip trailing spaces
OrderedListRx = /^[ \t]*(\.\.*|\d+\.|[a-zA-Z]\.|[IVXivx]+\))[ \t]+(#{CC_ANY}*)$/
# Matches the ordinals for each type of ordered list.
OrderedListMarkerRxMap = {
arabic: /\d+\./,
loweralpha: /[a-z]\./,
lowerroman: /[ivx]+\)/,
upperalpha: /[A-Z]\./,
upperroman: /[IVX]+\)/,
#lowergreek: /[a-z]\]/,
}
# Matches a description list entry.
#
# Examples
#
# foo::
# bar:::
# baz::::
# blah;;
#
# # the term may be followed by a description on the same line...
#
# foo:: The metasyntactic variable that commonly accompanies 'bar' (see also, <<bar>>).
#
# # ...or on a separate line, which may optionally be indented
#
# foo::
# The metasyntactic variable that commonly accompanies 'bar' (see also, <<bar>>).
#
# # attribute references may be used in both the term and the description
#
# {foo-term}:: {foo-desc}
#
# NOTE we know trailing (.*) will match at least one character because we strip trailing spaces
# NOTE must skip line comment when looking for next list item inside list
DescriptionListRx = %r(^(?!//[^/])[ \t]*(#{CC_ANY}*?)(:::{0,2}|;;)(?:$|[ \t]+(#{CC_ANY}*)$))
# Matches a sibling description list item (which does not include the type in the key).
# NOTE must skip line comment when looking for sibling list item
DescriptionListSiblingRx = {
'::' => %r(^(?!//[^/])[ \t]*(#{CC_ANY}*[^:]|)(::)(?:$|[ \t]+(#{CC_ANY}*)$)),
':::' => %r(^(?!//[^/])[ \t]*(#{CC_ANY}*[^:]|)(:::)(?:$|[ \t]+(#{CC_ANY}*)$)),
'::::' => %r(^(?!//[^/])[ \t]*(#{CC_ANY}*[^:]|)(::::)(?:$|[ \t]+(#{CC_ANY}*)$)),
';;' => %r(^(?!//[^/])[ \t]*(#{CC_ANY}*?)(;;)(?:$|[ \t]+(#{CC_ANY}*)$))
}
# Matches a callout list item.
#
# Examples
#
# <1> Explanation
#
# or
#
# <.> Explanation with automatic number
#
# NOTE we know trailing (.*) will match at least one character because we strip trailing spaces
CalloutListRx = /^<(\d+|\.)>[ \t]+(#{CC_ANY}*)$/
# Matches a callout reference inside literal text.
#
# Examples
# <1> (optionally prefixed by //, #, -- or ;; line comment chars)
# <1> <2> (multiple callouts on one line)
# <!--1--> (for XML-based languages)
# <.> (auto-numbered)
#
# NOTE extract regexps are applied line-by-line, so we can use $ as end-of-line char
CalloutExtractRx = %r(((?://|#|--|;;) ?)?(\\)?<!?(|--)(\d+|\.)\3>(?=(?: ?\\?<!?\3(?:\d+|\.)\3>)*$))
CalloutExtractRxt = '(\\\\)?<()(\\d+|\\.)>(?=(?: ?\\\\?<(?:\\d+|\\.)>)*$)'
CalloutExtractRxMap = ::Hash.new {|h, k| h[k] = /(#{::Regexp.escape k} ?)?#{CalloutExtractRxt}/ }
# NOTE special characters have not been replaced when scanning
CalloutScanRx = /\\?<!?(|--)(\d+|\.)\1>(?=(?: ?\\?<!?\1(?:\d+|\.)\1>)*#{CC_EOL})/
# NOTE special characters have already been replaced when converting to an SGML format
CalloutSourceRx = %r(((?://|#|--|;;) ?)?(\\)?<!?(|--)(\d+|\.)\3>(?=(?: ?\\?<!?\3(?:\d+|\.)\3>)*#{CC_EOL}))
CalloutSourceRxt = "(\\\\)?<()(\\d+|\\.)>(?=(?: ?\\\\?<(?:\\d+|\\.)>)*#{CC_EOL})"
CalloutSourceRxMap = ::Hash.new {|h, k| h[k] = /(#{::Regexp.escape k} ?)?#{CalloutSourceRxt}/ }
# A Hash of regexps for lists used for dynamic access.
ListRxMap = {
ulist: UnorderedListRx,
olist: OrderedListRx,
dlist: DescriptionListRx,
colist: CalloutListRx,
}
## Tables
# Parses the column spec (i.e., colspec) for a table.
#
# Examples
#
# 1*h,2*,^3e
#
ColumnSpecRx = /^(?:(\d+)\*)?([<^>](?:\.[<^>]?)?|(?:[<^>]?\.)?[<^>])?(\d+%?|~)?([a-z])?$/
# Parses the start and end of a cell spec (i.e., cellspec) for a table.
#
# Examples
#
# 2.3+<.>m
#
# FIXME use step-wise scan (or treetop) rather than this mega-regexp
CellSpecStartRx = /^[ \t]*(?:(\d+(?:\.\d*)?|(?:\d*\.)?\d+)([*+]))?([<^>](?:\.[<^>]?)?|(?:[<^>]?\.)?[<^>])?([a-z])?$/
CellSpecEndRx = /[ \t]+(?:(\d+(?:\.\d*)?|(?:\d*\.)?\d+)([*+]))?([<^>](?:\.[<^>]?)?|(?:[<^>]?\.)?[<^>])?([a-z])?$/
# Block macros
# Matches the custom block macro pattern.
#
# Examples
#
# gist::123456[]
#
#--
# NOTE we've relaxed the match for target to accomodate the short format (e.g., name::[attrlist])
CustomBlockMacroRx = /^(#{CG_WORD}[-#{CC_WORD}]*)::(|\S|\S#{CC_ANY}*?\S)\[(#{CC_ANY}+)?\]$/
# Matches an image, video or audio block macro.
#
# Examples
#
# image::filename.png[Caption]
# video::http://youtube.com/12345[Cats vs Dogs]
#
BlockMediaMacroRx = /^(image|video|audio)::(\S|\S#{CC_ANY}*?\S)\[(#{CC_ANY}+)?\]$/
# Matches the TOC block macro.
#
# Examples
#
# toc::[]
# toc::[levels=2]
#
BlockTocMacroRx = /^toc::\[(#{CC_ANY}+)?\]$/
## Inline macros
# Matches an anchor (i.e., id + optional reference text) in the flow of text.
#
# Examples
#
# [[idname]]
# [[idname,Reference Text]]
# anchor:idname[]
# anchor:idname[Reference Text]
#
InlineAnchorRx = /(\\)?(?:\[\[([#{CC_ALPHA}_:][#{CC_WORD}:.-]*)(?:, *(#{CC_ANY}+?))?\]\]|anchor:([#{CC_ALPHA}_:][#{CC_WORD}:.-]*)\[(?:\]|(#{CC_ANY}*?[^\\])\]))/
# Scans for a non-escaped anchor (i.e., id + optional reference text) in the flow of text.
InlineAnchorScanRx = /(?:^|[^\\\[])\[\[([#{CC_ALPHA}_:][#{CC_WORD}:.-]*)(?:, *(#{CC_ANY}+?))?\]\]|(?:^|[^\\])anchor:([#{CC_ALPHA}_:][#{CC_WORD}:.-]*)\[(?:\]|(#{CC_ANY}*?[^\\])\])/
# Scans for a leading, non-escaped anchor (i.e., id + optional reference text).
LeadingInlineAnchorRx = /^\[\[([#{CC_ALPHA}_:][#{CC_WORD}:.-]*)(?:, *(#{CC_ANY}+?))?\]\]/
# Matches a bibliography anchor at the start of the list item text (in a bibliography list).
#
# Examples
#
# [[[Fowler_1997]]] Fowler M. ...
#
InlineBiblioAnchorRx = /^\[\[\[([#{CC_ALPHA}_:][#{CC_WORD}:.-]*)(?:, *(#{CC_ANY}+?))?\]\]\]/
# Matches an inline e-mail address.
#
# doc.writer@example.com
#
InlineEmailRx = %r(([\\>:/])?#{CG_WORD}[#{CC_WORD}.%+-]*@#{CG_ALNUM}[#{CC_ALNUM}.-]*\.#{CG_ALPHA}{2,4}\b)
# Matches an inline footnote macro, which is allowed to span multiple lines.
#
# Examples
# footnote:[text] (not referenceable)
# footnote:id[text] (referenceable)
# footnote:id[] (reference)
# footnoteref:[id,text] (legacy)
# footnoteref:[id] (legacy)
#
InlineFootnoteMacroRx = /\\?footnote(?:(ref):|:([\w-]+)?)\[(?:|(#{CC_ALL}*?[^\\]))\]/m
# Matches an image or icon inline macro.
#
# Examples
#
# image:filename.png[Alt Text]
# image:http://example.com/images/filename.png[Alt Text]
# image:filename.png[More [Alt\] Text] (alt text becomes "More [Alt] Text")
# icon:github[large]
#
# NOTE be as non-greedy as possible by not allowing endline or left square bracket in target
InlineImageMacroRx = /\\?i(?:mage|con):([^:\s\[](?:[^\n\[]*[^\s\[])?)\[(|#{CC_ALL}*?[^\\])\]/m
# Matches an indexterm inline macro, which may span multiple lines.
#
# Examples
#
# indexterm:[Tigers,Big cats]
# (((Tigers,Big cats)))
# indexterm2:[Tigers]
# ((Tigers))
#
InlineIndextermMacroRx = /\\?(?:(indexterm2?):\[(#{CC_ALL}*?[^\\])\]|\(\((#{CC_ALL}+?)\)\)(?!\)))/m
# Matches either the kbd or btn inline macro.
#
# Examples
#
# kbd:[F3]
# kbd:[Ctrl+Shift+T]
# kbd:[Ctrl+\]]
# kbd:[Ctrl,T]
# btn:[Save]
#
InlineKbdBtnMacroRx = /(\\)?(kbd|btn):\[(#{CC_ALL}*?[^\\])\]/m
# Matches an implicit link and some of the link inline macro.
#
# Examples
#
# https://github.com
# https://github.com[GitHub]
# <https://github.com>
# link:https://github.com[]
#
# FIXME revisit! the main issue is we need different rules for implicit vs explicit
InlineLinkRx = %r((^|link:|#{CG_BLANK}|<|[>\(\)\[\];])(\\?(?:https?|file|ftp|irc)://[^\s\[\]<]*[^\s.,\[\]<])(?:\[(|#{CC_ALL}*?[^\\])\])?)m
# Match a link or e-mail inline macro.
#
# Examples
#
# link:path[label]
# mailto:doc.writer@example.com[]
#
# NOTE be as non-greedy as possible by not allowing space or left square bracket in target
InlineLinkMacroRx = /\\?(?:link|(mailto)):(|[^:\s\[][^\s\[]*)\[(|#{CC_ALL}*?[^\\])\]/m
# Matches the name of a macro.
#
MacroNameRx = /^#{CG_WORD}[-#{CC_WORD}]*$/
# Matches a stem (and alternatives, asciimath and latexmath) inline macro, which may span multiple lines.
#
# Examples
#
# stem:[x != 0]
# asciimath:[x != 0]
# latexmath:[\sqrt{4} = 2]
#
InlineStemMacroRx = /\\?(stem|(?:latex|ascii)math):([a-z]+(?:,[a-z]+)*)?\[(#{CC_ALL}*?[^\\])\]/m
# Matches a menu inline macro.
#
# Examples
#
# menu:File[Save As...]
# menu:View[Page Style > No Style]
# menu:View[Page Style, No Style]
#
InlineMenuMacroRx = /\\?menu:(#{CG_WORD}|[#{CC_WORD}&][^\n\[]*[^\s\[])\[ *(#{CC_ALL}*?[^\\])?\]/m
# Matches an implicit menu inline macro.
#
# Examples
#
# "File > New..."
#
InlineMenuRx = /\\?"([#{CC_WORD}&][^"]*?[ \n]+>[ \n]+[^"]*)"/
# Matches an inline passthrough, which may span multiple lines.
#
# Examples
#
# +text+
# `text` (compat)
#
# NOTE we always capture the attributes so we know when to use compatible (i.e., legacy) behavior
InlinePassRx = {
false => ['+', '`', /(^|[^#{CC_WORD};:])(?:\[([^\]]+)\])?(\\?(\+|`)(\S|\S#{CC_ALL}*?\S)\4)(?!#{CG_WORD})/m],
true => ['`', nil, /(^|[^`#{CC_WORD}])(?:\[([^\]]+)\])?(\\?(`)([^`\s]|[^`\s]#{CC_ALL}*?\S)\4)(?![`#{CC_WORD}])/m]
}
# Matches an inline plus passthrough spanning multiple lines, but only when it occurs directly
# inside constrained monospaced formatting in non-compat mode.
#
# Examples
#
# +text+
#
SinglePlusInlinePassRx = /^(\\)?\+(\S|\S#{CC_ALL}*?\S)\+$/m
# Matches several variants of the passthrough inline macro, which may span multiple lines.
#
# Examples
#
# +++text+++
# $$text$$
# pass:quotes[text]
#
# NOTE we have to support an empty pass:[] for compatibility with AsciiDoc Python
InlinePassMacroRx = /(?:(?:(\\?)\[([^\]]+)\])?(\\{0,2})(\+\+\+?|\$\$)(#{CC_ALL}*?)\4|(\\?)pass:([a-z]+(?:,[a-z]+)*)?\[(|#{CC_ALL}*?[^\\])\])/m
# Matches an xref (i.e., cross-reference) inline macro, which may span multiple lines.
#
# Examples
#
# <<id,reftext>>
# xref:id[reftext]
#
# NOTE special characters have already been escaped, hence the entity references
# NOTE { is included in start characters to support target that begins with attribute reference in title content
InlineXrefMacroRx = %r(\\?(?:<<([#{CC_WORD}#/.:{]#{CC_ALL}*?)>>|xref:([#{CC_WORD}#/.:{]#{CC_ALL}*?)\[(?:\]|(#{CC_ALL}*?[^\\])\])))m
## Layout
# Matches a trailing + preceded by at least one space character,
# which forces a hard line break (<br> tag in HTML output).
#
# NOTE AsciiDoc Python allows + to be preceded by TAB; Asciidoctor does not
#
# Examples
#
# Humpty Dumpty sat on a wall, +
# Humpty Dumpty had a great fall.
#
if RUBY_ENGINE == 'opal'
# NOTE In JavaScript, ^ and $ only match the start and end of line if the multiline flag is present
HardLineBreakRx = /^(#{CC_ANY}*) \+$/m
else
# NOTE In Ruby, ^ and $ always match start and end of line
HardLineBreakRx = /^(.*) \+$/
end
# Matches a Markdown horizontal rule.
#
# Examples
#
# --- or - - -
# *** or * * *
# ___ or _ _ _