Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
admonitions.py
Go to the documentation of this file.
1# Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2# for details. All rights reserved. Use of this source code is governed by a
3# BSD-style license that can be found in the LICENSE file.
4#
5"""Preprocesses Markdown source and converts admonitions written in blockquotes.
6
7Usage: convert_admonitions(str)
8"""
9
10import re
11
12_RECOGNIZED_ADMONITIONS = {
13 "Source to read": "sourcecode",
14 "Trying it": "tryit",
15 "Warning": "warning"
16}
17
18
19def convert_admonitions(content: str) -> str:
20 """Convert blockquotes into admonitions if they start with a marker.
21
22 Blockquotes starting with `> **marker**` are converted either into
23 sidenotes (`<span class="aside"/>`) or into admonitions to be
24 processed by an admonition extension later.
25 """
26 processed = []
27 current_admonition = None
28 indent = ''
29 for line in content.split('\n'):
30 if current_admonition is not None:
31 if line.startswith('>'):
32 processed.append(indent + line[1:])
33 continue
34 if current_admonition == 'Note':
35 note = processed.pop()
36 processed.pop()
37 processed[-1] = processed[
38 -1] + f' <span class="aside" markdown=1>{note}</span>'
39 current_admonition = None
40 elif line.startswith('> **') and line.endswith('**'):
41 current_admonition = re.match(r'^> \*\*(.*)\*\*$', line)[1]
42 if current_admonition == 'Note':
43 indent = ''
44
45 # Drop all empy lines preceeding the side note.
46 while processed[-1] == '':
47 processed.pop()
48
49 # Do not try to attach sidenote to the section title.
50 if processed[-1].startswith('#'):
51 processed.append('')
52 else:
53 # Start an admonition using Python markdown syntax.
54 processed.append(
55 f'!!! {_RECOGNIZED_ADMONITIONS[current_admonition]} "{current_admonition}"'
56 )
57 current_admonition = True
58 indent = ' '
59 continue
60 processed.append(line)
61 return "\n".join(processed)