2 # Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+
9 print "Syntax: %s <md-files>" % sys.argv[0]
11 print "Updates inter-chapter links in the specified Markdown files."
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")
22 print "Error: Anchor '%s' is used multiple times: in %s and %s" % (id, file, anchors[id])
24 anchors[match.group("id")] = file
26 def update_anchor(match):
27 id = match.group("id")
30 file = os.path.basename(anchors[id])
32 print "Error: Unmatched anchor: %s" % (id)
35 return "[%s](%s#%s)" % (match.group("text"), file, id)
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)