]> granicus.if.org Git - icinga2/blob - doc/update-links.py
2006f06d90dac7ed47e93d17cf709d2b1b562887
[icinga2] / doc / update-links.py
1 #!/usr/bin/env python
2 # Icinga 2
3 # Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software Foundation
17 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 import os
20 import sys
21 import re
22
23 if len(sys.argv) < 2:
24     print "Syntax: %s <md-files>"
25     print ""
26     print "Updates inter-chapter links in the specified Markdown files."
27     sys.exit(1)
28
29 anchors = {}
30
31 for file in sys.argv[1:]:
32     text = open(file).read()
33     for match in re.finditer(r"<a id=\"(?P<id>.*?)\">", text):
34         id = match.group("id")
35
36         if id in anchors:
37             print "Anchor '%s' is used multiple times: in %s and %s" % (id, file, anchors[id])
38
39         anchors[match.group("id")] = file
40
41 def update_anchor(match):
42     id = match.group("id")
43
44     try:
45         file = os.path.basename(anchors[id])
46     except KeyError:
47         print "Unmatched anchor: %s" % (id)
48         file = ""
49
50     return "[%s](%s#%s)" % (match.group("text"), file, id)
51
52 for file in sys.argv[1:]:
53     text = open(file).read()
54     new_text = re.sub(r"\[(?P<text>.*?)\]\((?P<file>[0-9-a-z\.]+)?#(?P<id>[^#\)]+)\)", update_anchor, text)
55     open(file, "w").write(new_text)