from __future__ import print_function import re, sys def urlify (t): return t.replace(" ", "_") + ".html" def linkify (src, urlify=urlify): collect = [] def s (m): contents = m.group(1) collect.append(contents) link = urlify(contents) return "[[{1}]]".format(link, contents) src = re.sub(r"\[\[([\w_\- ]+?)\]\]", s, src) return (src, collect) if __name__ == "__main__": src = sys.stdin.read() src, links = linkify(src) for l in links: print (l) print (src)