-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.py
executable file
·57 lines (45 loc) · 2.01 KB
/
main.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
import re
from mkdocs.plugins import BasePlugin
from mkdocs.config import config_options
def replace_autolink_references(markdown, ref_prefix, target_url):
if "<num>" not in ref_prefix:
ref_prefix = ref_prefix + "<num>"
find_regex = re.compile(
r"(?<![#\[/])" + ref_prefix.replace(r"<num>", r"(?P<num>[-\w]+)"), re.IGNORECASE
)
linked_ref = rf"[{ref_prefix}](" + target_url + r")"
replace_text = linked_ref.replace(r"<num>", r"\g<num>")
markdown = re.sub(find_regex, replace_text, markdown)
return markdown
class AutoLinkOption(config_options.OptionallyRequired):
def run_validation(self, values):
if not isinstance(values, list):
raise config_options.ValidationError("Expected a list of autolinks.")
for autolink in values:
if "reference_prefix" not in autolink:
raise config_options.ValidationError(
"Expected a 'reference_prefix' in autolinks."
)
if "target_url" not in autolink:
raise config_options.ValidationError(
"Expected a 'target_url' in autolinks."
)
if "<num>" not in autolink["target_url"]:
raise config_options.ValidationError("Missing '<num>' in 'target_url'.")
return values
class AutolinkReference(BasePlugin):
config_scheme = (("autolinks", AutoLinkOption(required=True)),)
def on_page_markdown(self, markdown, **kwargs):
"""
Takes an article written in markdown and looks for the
presence of a ticket reference and replaces it with autual link
to the ticket.
:param markdown: Original article in markdown format
:param kwargs: Other parameters (won't be used here)
:return: Modified markdown
"""
for autolink in self.config["autolinks"]:
markdown = replace_autolink_references(
markdown, autolink["reference_prefix"], autolink["target_url"]
)
return markdown