3 # Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org)
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.
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.
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.
24 print "Syntax: %s <md-files>"
26 print "Updates inter-chapter links in the specified Markdown files."
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")
37 print "Anchor '%s' is used multiple times: in %s and %s" % (id, file, anchors[id])
39 anchors[match.group("id")] = file
41 def update_anchor(match):
42 id = match.group("id")
45 file = os.path.basename(anchors[id])
47 print "Unmatched anchor: %s" % (id)
50 return "[%s](%s#%s)" % (match.group("text"), file, id)
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)