-
Notifications
You must be signed in to change notification settings - Fork 0
/
replace_link_suffixes.py
90 lines (73 loc) · 2.43 KB
/
replace_link_suffixes.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
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2021 Robin Vobruba <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""
This is part of the [MoVeDo](https://github.com/movedo) project.
See LICENSE.md for copyright information.
Replaces the file extensions/suffixes of certain links in an input document.
It is implemented as a Pandoc filter using panflute.
This might typicaly be used to convert local links to *.md to *.html
while converting the format from Markdown to HTML,
as to maintain local cross-linking wihtin the used format.
Usage example:
$ pandoc -f markdown -t markdown --markdown-headings=atx \
-M rls_relative_only=True \
-M rls_ext_from=".md" \
-M rls_ext_to=".html" \
--filter replace_link_suffixes.py \
-o output.md \
input.md
"""
from _common import check_version, is_rel_path, get_arg
check_version()
import re
import panflute as pf
# constants
REGEX_REF_DELETER = re.compile(r'#.*$')
REGEX_PATH_DELETER = re.compile(r'^.*#')
# parameters
relative_only = True
ext_from = '.<unset_ext_from>'
ext_to = '.<unset_ext_to>'
def prepare(doc):
"""The panflute filter init method."""
global relative_only, ext_from, ext_to
relative_only = get_arg(doc, 'rls_relative_only', 'True') == 'True'
ext_from = get_arg(doc, 'rls_ext_from')
ext_to = get_arg(doc, 'rls_ext_to')
def replace_link_suffix(url):
"""If the URL fits, we replace the file suffix."""
if not is_rel_path(url) and relative_only:
return url
path = re.sub(REGEX_REF_DELETER, '', url)
ref = re.sub(REGEX_PATH_DELETER, '', url)
if ref == url:
ref = None
if path.endswith(ext_from):
url = path[:-len(ext_from)] + ext_to
if ref is not None:
url = url + '#' + ref
return url
def action(elem, doc):
"""The panflute filter main method, called once per element."""
if isinstance(elem, pf.Link):
elem.url = replace_link_suffix(elem.url)
# TODO Also do this with HTML links (using BeautifulSoup, see other filters)
return elem
def finalize(doc):
"""The panflute filter "destructor" method."""
pass
def main(doc=None):
"""
NOTE: The main function has to be exactly like this
if we want to be able to run filters automatically
with '-F panflute'
"""
return pf.run_filter(
action,
prepare=prepare,
finalize=finalize,
doc=doc)
if __name__ == '__main__':
main()