ruSTLa 0.38.0

A reStructuredText → LarST ⊂ LaTeX transpiler.
Documentation
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
/*!
This submodule contains different regex patterns that can be compiled into finite automata.
This is done lazily in the module `crate::parser::automata`.

Copyright © 2020 Santtu Söderholm
*/

#[derive(Copy, Clone, Debug)]
/// An enum of transition regex pattern names, both for body and inline level elements.
pub enum Pattern {
    // Body elements, possibly nested
    Attribution,
    EmptyLine,
    Bullet,
    Citation,
    Comment,
    Enumerator,
    ExplicitMarkup,
    Directive,
    DocTest,
    FieldMarker,
    Footnote,
    HyperlinkTarget,
    IndentedLiteralBlock,
    Line,
    LineBlock,
    OptionMarker,
    Paragraph,
    QuotedLiteralBlock,
    Text,

    // Inline Elements for parsing Strings
    Escape,
    StrongEmphasis, // **strongly emphasised text**
    Emphasis,       // *emphasized text*
    Interpreted,    // Plain interpreted text with the default role set by transpiler.
    PhraseRef,      // A reference in the form `text with spaces`__?
    SimpleRef,      // A reference that doesn't need backticks: reference__?
    Literal,        // Code
    FootNoteRef,
    CitationRef,
    InlineTarget,    // Reference target in inline text: _`target label`
    SubstitutionRef, // Reference to substitution definition. Is replaced by the definition
    ImplicitURL,
    StandaloneHyperlink,
    WhiteSpace,

    // A+ specific
    AplusColBreak,
    AplusQuestionnaireDirective,
}

/// A pattern for matching attributions inside block quotes.
pub const ATTRIBUTION_PATTERN: &'static str = r"^(\s*)(?:--|---|—) *";

/// A pattern for matching blank lines, as in lines that contain nothing but whitespace.
pub const BLANK_LINE_PATTERN: &'static str = r"^\s*$";

/// A pattern for matching bullet list bullets.
pub const BULLET_PATTERN: &'static str = r"^(\s*)([+\-*\u{2022}\u{2023}\u{2043}])(?: +|$)";

/// A pattern for recognizing enumerators of enumerated lists.
pub const ENUMERATOR_PATTERN: &'static str = r#"^(?x)
    (?P<indent>
      \s*
    )
    (?:
      \(
        (?:
          (?P<arabic_parens>
            [0-9]+
          )
          |
          (?P<lower_roman_parens>
            [ivxlcdm]+
          )
          |
          (?P<upper_roman_parens>
            [IVXLCDM]+
          )
          |
          (?P<lower_alpha_parens>
            [a-z]
          )
          |
          (?P<upper_alpha_parens>
            [A-Z]
          )
          |
          (?P<auto_enumerator_parens>
            \#
          )
        )
      \)
      |
        (?:
          (?P<arabic_rparen>
            [0-9]+
          )
          |
          (?P<lower_roman_rparen>
            [ivxlcdm]+
          )
          |
          (?P<upper_roman_rparen>
            [IVXLCDM]+
          )
          |
          (?P<lower_alpha_rparen>
            [a-z]
          )
          |
          (?P<upper_alpha_rparen>
            [A-Z]
          )
          |
          (?P<auto_enumerator_rparen>
            \#
          )
        )
      \)
      |
        (?:
          (?P<arabic_period>
            [0-9]+
          )
          |
          (?P<lower_roman_period>
            [ivxlcdm]+
          )
          |
          (?P<upper_roman_period>
            [IVXLCDM]+
          )
          |
          (?P<lower_alpha_period>
            [a-z]
          )
          |
          (?P<upper_alpha_period>
            [A-Z]
          )
          |
          (?P<auto_enumerator_period>
            \#
          )
        )
        \.
    )
    (?P<after_marker>
      \ +|$
    )
"#;

/// A pattern that signifies the start of a field list, such as a bibliography.
/// Colons inside field names `:field name:` must be escaped if followed by whitespace,
/// as ": " signifies the end of a list marker.
pub const FIELD_MARKER_PATTERN: &'static str = r"^(\s*):([\S&&[^\\]]|\S.*?[\S&&[^\\]]):(?: +|$)";

/// A pattern for matching against an indented block of text when in `State::LiteralBlock`.
pub const INDENTED_LITERAL_BLOCK_PATTERN: &'static str = r"^(\s+)\S";

/// A pattern for matching against an "quoted" block of text when in `State::LiteralBlock`.
pub const QUOTED_LITERAL_BLOCK_PATTERN: &'static str =
    r#"^(\s*)(!|"|#|\$|%|&|'|\(|\)|\*|\+|,|-|\.|/|:|;|<|=|>|\?|@|\[|\\|\]|\^|_|`|\{|\||\}|~)"#;

/// A pattern that matches the tops of bottoms of grid tables.
pub const GRID_TABLE_TOP_AND_BOT_PATTERN: &'static str = r#"^(\s*)\+-[-+]+-\+ *$"#;

/// A pattern that matches the tops and bottoms of simple tables.
pub const SIMPLE_TABLE_TOP_PATTERN: &'static str = r#"^(\s*)=+( +=+)+ *$"#;

/// A pattern for matching against the bottoms of entire simple tables and their header rows.
pub const SIMPLE_TABLE_BOTTOM_PATTERN: &'static str = r#"^(\s*)=+( +=+)+ *$"#;

// ========================
// Explicit markup patterns
// ========================

/// A single pattern for recognizing footnotes.
pub const FOOTNOTE_PATTERN: &'static str = r#"^(?x)
    (?P<indent>
        \s*
    )
    \.\.\ +\[
        (?:
            (?P<manual>\d+)
            |(?P<autonumbered>\#)
            |\#(?P<simplename>[a-zA-Z][a-zA-Z0-9]+(?:[-+._:][a-zA-Z0-9]+)*)
            |(?P<autosymbol>\*)
        )
    \](?:\ +|$) # space after marker
"#;

/// A pattern for matching against manually numbered footnotes.
pub const MANUAL_FOOTNOTE_PATTERN: &'static str = r"^(\s*)\.\.[ ]+\[(\d+)\](?:[ ]+|$)";

/// A footnote pattern with the symbol '#' for a label.
/// This triggers automatic numbering for the footnote to be generated.
pub const AUTO_NUM_FOOTNOTE_PATTERN: &'static str = r"^(\s*)\.\.[ ]+\[(\#)\](?:[ ]+|$)";

/// Similar to `AUTO_NUM_FOONOTE_PATTERN`, except allows referencing the same footnote
/// multiple times, as there is a simple reference name pointing to the footnote.
pub const SIMPLE_NAME_FOOTNOTE_PATTERN: &'static str =
    r"^(\s*)\.\.[ ]+\[\#([a-zA-Z][a-zA-Z0-9]+(?:[-+._:][a-zA-Z0-9]+)*)\](?:[ ]+|$)";

/// Prompts the generation of symbolic footnotes, with automatic reference mark generation.
pub const AUTO_SYM_FOOTNOTE_PATTERN: &'static str = r"^(\s*)\.\.[ ]+\[(\*)\](?:[ ]+|$)";

/// A pattern for matching against citations.
/// Similar to `FOOTNOTE_PATTERN`, but only
/// recognizes simple reference names in labels.
pub const CITATION_PATTERN: &'static str =
    r"^(\s*)\.\.[ ]+\[([a-zA-Z][a-zA-Z0-9]*(?:[-+._:][a-zA-Z0-9]+)*)\](?:[ ]+|$)";

/// A pattern for matching hyperlink targets. A hyperlink target may either be labeled with a simple reference name or
/// with and underscore `_`, the latter of which signifies an anonymous link.
pub const HYPERLINK_TARGET_PATTERN: &'static str =
    r"^(\s*)\.\.[ ]+_([a-zA-Z0-9][a-zA-Z0-9 ]*(?:[-+._:][a-zA-Z0-9 ]+)*[a-zA-Z0-9]+|_):(?:[ ]+|$)";

/// A pattern for matching substitution definitions, a.k.a. macros.
pub const SUBSTITUTION_DEF_PATTERN: &'static str = r"^(\s*)\.\.[ ]+\|(\S|\S.*\S)\| ::(?:[ ]+|$)";

/// A pattern for matching directives. The directive label is used to determine the type of directive
/// inside a transition function. The label itself is a simple reference name (an identifier).
pub const DIRECTIVE_PATTERN: &'static str =
    r"^(\s*)\.\.[ ]+([a-zA-Z][a-zA-Z0-9]+(?:[-+._:][a-zA-Z0-9]+)*)[ ]?::(?:[ ]+|$)";

///
/// A pattern for recognizing comments, after no other explicit markup pattern has matched.
pub const COMMENT_PATTERN: &'static str = r"^(\s*)\.\.(?: +|$)";

/// A pattern for recognizing lines related to section titles and transitions.
pub const LINE_PATTERN: &'static str = r#"^(!+|"+|#+|\$+|%+|&+|'+|\(+|\)+|\*+|\++|,+|-+|\.+|/+|:+|;+|<+|=+|>+|\?+|@+|\[+|\\+|\]+|\^+|_+|`+|\{+|\|+|\}+|~+) *$"#;

/// A pattern for detecting any text, possibly beginning with whitespace.
/// This pattern should generally be tested against only after all other
/// possibilities have been eliminated.
pub const TEXT_PATTERN: &'static str = r"^(\s*)\S";

// =================
//  Inline patterns
// =================

pub const INLINE_WHITESPACE_PATTERN: &'static str = r"^\s+";

pub const STRONG_EMPH_PATTERN: &str = r#"(?x)^
  (?P<lookbehind>
    [-:/'"<(\[{\p{Ps}\p{Pi}\p{Pf}\p{Pd}\p{Po}\s&&[^\\*]]
  )?
  (?P<markup_start>
    \*\*
  )
  (?P<content>
    [\S&&[^\\]]|\S[\S\s]*?[\S&&[^\\]]
  )
  (?P<markup_end>
    \*\*
  )
  (?P<lookahead>
    \s|[-.,:;!?\\/'")\]}>\p{Pe}\p{Pi}\p{Pf}\p{Pd}\p{Po}&&[^*]]|$
  )
"#;

pub const EMPH_PATTERN: &str = r#"(?x)^
  (?P<lookbehind>
    [-:/'"<(\[{\p{Ps}\p{Pi}\p{Pf}\p{Pd}\p{Po}\s&&[^\\*]]
  )?
  (?P<markup_start>
    \*
  )
  (?P<content>
    [\S&&[^\\]]|\S[\S\s]*?[\S&&[^\\]]
  )
  (?P<markup_end>
    \*
  )
  (?P<lookahead>
    \s|[-.,:;!?\\/'")\]}>\p{Pe}\p{Pi}\p{Pf}\p{Pd}\p{Po}&&[^*]]|$
  )
"#;

pub const LITERAL_PATTERN: &str = r#"(?x)^
  (?P<lookbehind>
    [-:/'"<(\[{\p{Ps}\p{Pi}\p{Pf}\p{Pd}\p{Po}\s&&[^\\*]]
  )?
  (?P<markup_start>
    ``
  )
  (?P<content>
    [\S&&[^\\]]|\S[\S\s]*?[\S&&[^\\]]
  )
  (?P<markup_end>
    ``
  )
  (?P<lookahead>
    \s|[-.,:;!?\\/'")\]}>\p{Pe}\p{Pi}\p{Pf}\p{Pd}\p{Po}&&[^*]]|$
  )
"#;

pub const INLINE_TARGET_PATTERN: &str = r#"(?x)^
  (?P<lookbehind>
    [-:/'"<(\[{\p{Ps}\p{Pi}\p{Pf}\p{Pd}\p{Po}\s&&[^\\*]]
  )?
  (?P<markup_start>
    _`
  )
  (?P<content>
    [\S&&[^\\]]|\S[\S\s]*?[\S&&[^\\]]
  )
  (?P<markup_end>
    `
  )
  (?P<lookahead>
    \s|[-.,:;!?\\/'")\]}>\p{Pe}\p{Pi}\p{Pf}\p{Pd}\p{Po}&&[^*]]|$
  )
"#;

pub const INTERPRETED_TEXT_PATTERN: &str = r#"(?x)^
  (?P<lookbehind>
    [-:/'"<(\[{\p{Ps}\p{Pi}\p{Pf}\p{Pd}\p{Po}\s&&[^\\*]]
  )?

  (?P<front_role_marker>
    :(?P<front_role>
      \S|\S.*?\S
    ):
  )?
  (?P<markup_start>
    `
  )
  (?P<content>
    [\S&&[^\\]]|\S[\S\s]*?[\S&&[^\\]]
  )
  (?P<markup_end>
    `
  )
  (?P<back_role_marker>
    :(?P<back_role>
      \S|\S.*?\S
    ):
  )?
  (?P<lookahead>
    \s|[-.,:;!?\\/'")\]}>\p{Pe}\p{Pi}\p{Pf}\p{Pd}\p{Po}&&[^*]]|$
  )
"#;

pub const PHRASE_REF_PATTERN: &str = r#"(?x)^
  (?P<lookbehind>
    [-:/'"<(\[{\p{Ps}\p{Pi}\p{Pf}\p{Pd}\p{Po}\s&&[^\\*]]
  )?
  (?P<markup_start>
    `
  )
  (?P<content>
    [\S&&[^\\]]|\S[\S\s]*?[\S&&[^\\]]
  )
  (?P<embeded_uri_container>
    \s+#space character one or more times
    <
    (?P<embedded_uri>
      [\S&&[^\\]]|\S[\S\s]*?[\S&&[^\\]]
    )
    >
  )?
  (?P<markup_end>
    `
  )
  (?P<ref_type>
    __?
  )
  (?P<lookahead>
    \s|[-.,:;!?\\/'")\]}>\p{Pe}\p{Pi}\p{Pf}\p{Pd}\p{Po}&&[^*]]|$
  )
"#;

pub const SIMPLE_REF_PATTERN: &str = r#"(?x)^
  (?P<lookbehind>
    [-:/'"<(\[{\p{Ps}\p{Pi}\p{Pf}\p{Pd}\p{Po}\s&&[^\\*]]
  )?
  (?P<content>
    [a-zA-Z0-9]+(?:[-_.:+][a-zA-Z0-9]+)*
  )
  (?P<ref_type>
    __?
  )
  (?P<lookahead>
    \s|[-.,:;!?\\/'")\]}>\p{Pe}\p{Pi}\p{Pf}\p{Pd}\p{Po}&&[^*]]|$
  )
"#;

pub const FOOTNOTE_REF_PATTERN: &str = r#"(?x)^
(?P<lookbehind>
    [-:/'"<(\[{\p{Ps}\p{Pi}\p{Pf}\p{Pd}\p{Po}\s&&[^\\*]]
)?
(?P<markup_start>
    \[
)
(?P<content>
    (?P<number>
        [0-9]+
    )
    |
    (?P<auto_number>
        \#
    )
    |\#
    (?P<auto_number_label>
        [a-zA-Z](-?[a-zA-Z0-9]+)*
    )
    |
    (?P<symbol>
        \*
    )
)
(?P<markup_end>
    \]
)
(?P<ref_type>
    __?
)
(?P<lookahead>
    \s|[-.,:;!?\\/'")\]}>\p{Pe}\p{Pi}\p{Pf}\p{Pd}\p{Po}&&[^*]]|$
)
"#;

pub const CITATION_REF_PATTERN: &str = r#"(?x)^
  (?P<lookbehind>
    [-:/'"<(\[{\p{Ps}\p{Pi}\p{Pf}\p{Pd}\p{Po}\s&&[^\\*]]
  )?
  (?P<markup_start>
    \[
  )
  (?P<content>
    [a-zA-Z0-9][a-zA-Z0-9]*(?:[-_.]?[a-zA-Z0-9]+)*
  )
  (?P<markup_end>
    \]_
  )
  (?P<lookahead>
    \s|[-.,:;!?\\/'")\]}>\p{Pe}\p{Pi}\p{Pf}\p{Pd}\p{Po}&&[^*]]|$
  )
"#;

pub const SUBSTITUTION_REF_PATTERN: &str = r#"(?x)^
  (?P<lookbehind>
    [-:/'"<(\[{\p{Ps}\p{Pi}\p{Pf}\p{Pd}\p{Po}\s&&[^\\*]]
  )?
  (?P<markup_start>
    \|
  )
  (?P<content>
    [\S&&[^\\]]|\S[\S\s]*?[\S&&[^\\]]
  )
  (?P<markup_end>
    \|
  )
  (?P<ref_type>
    __?
  )?
  (?P<lookahead>
    \s|[-.,:;!?\\/'")\]}>\p{Pe}\p{Pi}\p{Pf}\p{Pd}\p{Po}&&[^*]]|$
  )
"#;

pub const URI_PATTERN: &str = r#"(?x)^
(?P<lookbehind>
  \s|[-:/'"<(\[{\p{Ps}\p{Pi}\p{Pf}\p{Pd}\p{Po}]
)?
(?P<content>
  (?P<absolute>
    (?:
      (?P<scheme> # 😵
        about|acap|addbook|afp|afs|aim|callto|castanet|chttp|cid|crid|data|dav|dict|dns|eid|fax|feed|file|finger|freenet|ftp|go|gopher|
        gsm-sms|h323|h324|hdl|hnews|http|https|hydra|iioploc|ilu|im|imap|info|ior|ipp|irc|iris.beep|iseek|jar|javascript|jdbc|ldap|lifn|
        livescript|lrq|mailbox|mailserver|mailto|md5|mid|mocha|modem|mtqp|mupdate|news|nfs|nntp|opaquelocktoken|phone|pop|pop3|pres|printer|
        prospero|rdar|res|rtsp|rvp|rwhois|rx|sdp|service|shttp|sip|sips|smb|snews|snmp|soap.beep|soap.beeps|ssh|t120|tag|tcp|tel|telephone|
        telnet|tftp|tip|tn3270|tv|urn|uuid|vemmi|videotex|view-source|wais|whodp|whois++|x-man-page|xmlrpc.beep|xmlrpc.beeps|z39.50r|z39.50s
      )
      :
    )
    (?P<authority>
      (//?)?
      (?:
        (?P<userinfo>
          [A-Za-z0-9]+(?:[.][A-Za-z0-9]+)*
        )@
      )?
      (?P<host>
        [a-zA-Z0-9]+(?:[-.+][a-zA-Z0-9]+)* # hostname
        | [0-9]{1,3}(?:\.[0-9]{1,3}){3} # IPv4
        # TODO: add IPv6 pattern here
      )
      (?::
        (?P<port>[0-9]+)
      )?
    )?
    (?P<path>
      /?[a-zA-Z0-9~_.]*(?:/[A-Za-z0-9~_.]+)*/?
    )
    [_~*/=+a-zA-Z0-9] # Allowed URI suffixes
    (?:\?
      (?P<query>
        [=&a-zA-Z0-9]+
        [_~*/=+a-zA-Z0-9] # Allowed URI suffixes
      )
    )?
    (?:\#
      (?P<fragment>
        [a-zA-Z0-9]+
        [_~*/=+a-zA-Z0-9] # Allowed URI suffixes
      )
    )?
  )
  | # if not absolute uri, then email
  (?P<email>
    [-_a-zA-Z0-9]+
    (?:\.[-_!~*'{|}/\#?\^`&=+$%a-zA-Z0-9]+)*
    @
    [-_a-zA-Z0-9]+
    (?:[.-][a-zA-Z0-9]+)*
  )
)
(?P<lookahead>
  \s|[-.,:;!?\\/'")\]}>\p{Pe}\p{Pi}\p{Pf}\p{Pd}\p{Po}]|$
)
"#;

/// A pattern for inerpreting inline text as is, without any markup.
pub const INLINE_TEXT_PATTERN: &'static str = r"^(\S+)";

// ======================
//  A+ specific patterns
// ======================

/// ### APLUS_COL_BREAK_PATTERN
/// A regex pattern relatex to detecting column breaks in multi-column
/// A+ directives, such as points of interest.
pub const APLUS_COL_BREAK_PATTERN: &str = r#"^(\s+)::newcol"#;

/// ### APLUS_PICK_ONE_PATTERN
pub const APLUS_QUESTIONNAIRE_DIRECTIVE_PATTERN: &'static str =
    r"^(\s*)\.\.[ ]+(pick-one|pick-any|freetext)::(?:[ ]+|$)";

// /// ### APLUS_PICK_ANY_PATTERN
// pub const APLUS_PICK_ANY_PATTERN: &'static str = r"^(\s*)\.\.[ ]+pick-any::(?:[ ]+|$)";

/// ### APLUS_PICK_ONE_CHOICE_PATTERN
/// Correct answers in `pick-one` and `pick-any` directives are marked with `*`.
/// A `pick-any` question may have neutral options, which are marked with `?`.
/// Neutral options are always counted as correct, whether the student selected them or not.
/// Initially selected options may be set with `+`.
/// The initially selected options are pre-selected when the exercise is loaded.
/// The `+` character is written before `*` or `?` if they are combined.
pub const APLUS_PICK_ONE_CHOICE_PATTERN: &'static str =
    r"^(\s*)(?P<pre_selected>\+)?(?P<correct>\*)?(?P<enumerator>[a-zA-Z0-9])\.(?:[ ]+|$)";

/// ### APLUS_PICK_ANY_CHOICE_PATTERN
/// Correct answers in `pick-one` and `pick-any` directives are marked with `*`.
/// A `pick-any` question may have neutral options, which are marked with `?`.
/// Neutral options are always counted as correct, whether the student selected them or not.
/// Initially selected options may be set with `+`.
/// The initially selected options are pre-selected when the exercise is loaded.
/// The `+` character is written before `*` or `?` if they are combined.
pub const APLUS_PICK_ANY_CHOICE_PATTERN: &'static str = r"^(\s*)(?P<pre_selected>\+)?(?:(?P<neutral>\?)|(?P<correct>\*))?(?P<enumerator>[a-zA-Z0-9])\.(?:[ ]+|$)";

// /// ### APLUS_FREETEXT_PATTERN
// pub const APLUS_FREETEXT_PATTERN: &'static str = r"^(\s*)\.\.[ ]+freetext::(?:[ ]+|$)";