]> granicus.if.org Git - icinga2/blob - doc/update-links.py
Merge pull request #7502 from Icinga/feature/docs-debugger-2-11
[icinga2] / doc / update-links.py
1 #!/usr/bin/env python
2 # Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+
3
4 import os
5 import sys
6 import re
7
8 if len(sys.argv) < 2:
9     print "Syntax: %s <md-files>" % sys.argv[0]
10     print ""
11     print "Updates inter-chapter links in the specified Markdown files."
12     sys.exit(1)
13
14 anchors = {}
15
16 for file in sys.argv[1:]:
17     text = open(file).read()
18     for match in re.finditer(r"<a id=\"(?P<id>.*?)\">", text):
19         id = match.group("id")
20
21         if id in anchors:
22             print "Error: Anchor '%s' is used multiple times: in %s and %s" % (id, file, anchors[id])
23
24         anchors[match.group("id")] = file
25
26 def update_anchor(match):
27     id = match.group("id")
28
29     try:
30         file = os.path.basename(anchors[id])
31     except KeyError:
32         print "Error: Unmatched anchor: %s" % (id)
33         file = ""
34
35     return "[%s](%s#%s)" % (match.group("text"), file, id)
36
37 for file in sys.argv[1:]:
38     text = open(file).read()
39     print "> Processing file '%s'..." % (file)
40     new_text = re.sub(r"\[(?P<text>.*?)\]\((?P<file>[0-9-a-z\.]+)?#(?P<id>[^#\)]+)\)", update_anchor, text)
41     open(file, "w").write(new_text)