diff options
author | Rob Austein <sra@hactrn.net> | 2021-02-16 01:52:00 +0000 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2021-02-16 01:52:00 +0000 |
commit | 5a91960371555fc4fb7638c479a6a27b70768085 (patch) | |
tree | 24fb67a840c967181357aeea28b31f71a835e4ff | |
parent | 6c097fdeb067cbd14fbea438d3be0a6aff30fc59 (diff) |
Simplify substitution loops
-rwxr-xr-x | trac2md.py | 82 |
1 files changed, 38 insertions, 44 deletions
@@ -42,24 +42,24 @@ def convert_headers(line): try: level = header.search(line).group(1) if level: - line = "%s %s" % ('#' * level_count, level.rstrip("= \r\t")) + line = "{} {}".format('#' * level_count, level.rstrip("= \r\t")) break # No need to check other heading levels except: pass # Try the next heading level return line -def convert_traclink_to_creolelink(line): +def convert_traclink_to_creolelink(m): # Convert Trac's native link form to Creole's, so that rest of the code only has to deal with one format. # Creole's is easier to parse and harder to confuse with partially converted Markdown. - for m in traclink_pattern.finditer(line): - text = m.group(1).strip() - if " " in text: - line = line.replace(m.group(0), "[[{0[0]}|{0[1]}]]".format(text.split(" ", 1))) - elif ":" in text or camelcase_pattern.match(text): - line = line.replace(m.group(0), "[[{}]]".format(text)) - return line + text = m.group(1).strip() + if " " in text: + return "[[{0[0]}|{0[1]}]]".format(text.split(" ", 1)) + elif ":" in text or camelcase_pattern.match(text): + return "[[{}]]".format(text) + else: + return m.group(0) # Probably most of the non-wiki scheme tests should become a table in an @@ -67,38 +67,32 @@ def convert_traclink_to_creolelink(line): # # { "source:fee/fie/foe/fum": "https://git.cryptech.is/blarg/blee/blue" } -def convert_wikilinks(line, slug, giturl): - for m in wikilink_pattern.finditer(line): - scheme, link, text = [p.strip() if p else p for p in m.groups()] - if text is None: - text = link - if any(link.startswith(q) and link.endswith(q) for q in ('"', "'")): - link = link[1:-1] - if any(text.startswith(q) and text.endswith(q) for q in ('"', "'")): - text = text[1:-1] - if text == link and link.startswith("http") and "://" in link: - mdlink = "<{}>".format(link) - elif scheme == "attachment:": - mdlink = "[{}]({{attach}}{}/{})".format(text, slug, link) - elif scheme in ("source:", "browser:"): - mdlink = "[{}]({}/{})".format(text, giturl.rstrip("/"), link.lstrip("/")) - elif scheme == "wiki:" or (scheme is None and camelcase_pattern.match(link)): - mdlink = "[{}]({{filename}}{}.md)".format(text, link) - else: - mdlink = "[{}]({})".format(text, link) - line = line.replace(m.group(0), mdlink) - return line - - -def convert_image(line, slug): - for m in image_pattern.finditer(line): - text = m.group(1).split(",")[0].strip() - if "://" in text: - mdlink = "<img src=\"{}\">".format(text) - else: - mdlink = "![{}]({{attach}}{}/{})".format(text, slug, quote(text, "")) - line = line.replace(m.group(0), mdlink) - return line +def convert_wikilinks(m, slug, giturl): + scheme, link, text = [p.strip() if p else p for p in m.groups()] + if text is None: + text = link + if any(link.startswith(q) and link.endswith(q) for q in ('"', "'")): + link = link[1:-1] + if any(text.startswith(q) and text.endswith(q) for q in ('"', "'")): + text = text[1:-1] + if text == link and link.startswith("http") and "://" in link: + return "<{}>".format(link) + elif scheme == "attachment:": + return "[{}]({{attach}}{}/{})".format(text, slug, link) + elif scheme in ("source:", "browser:"): + return "[{}]({}/{})".format(text, giturl.rstrip("/"), link.lstrip("/")) + elif scheme == "wiki:" or (scheme is None and camelcase_pattern.match(link)): + return "[{}]({{filename}}{}.md)".format(text, link) + else: + return "[{}]({})".format(text, link) + + +def convert_image(m, slug): + text = m.group(1).split(",")[0].strip() + if "://" in text: + return "<img src=\"{}\">".format(text) + else: + return "![{}]({{attach}}{}/{})".format(text, slug, quote(text, "")) def WikiToMD(content, slug): @@ -127,7 +121,7 @@ def WikiToMD(content, slug): line = camelcase_pattern.sub(r"[[\1]]", line) # Convert TracLinks to WikiCreole links to simplify remaining processing - line = convert_traclink_to_creolelink(line) + line = traclink_pattern.sub(convert_traclink_to_creolelink, line) # Convert tables. References: # https://github.github.com/gfm/#tables-extension- @@ -186,13 +180,13 @@ def WikiToMD(content, slug): line = convert_headers(line) # Convert images - line = convert_image(line, slug) + line = image_pattern.sub(lambda m: convert_image(m, slug), line) # Delete Trac macros that have no useful counterpart line = delete_pattern.sub("", line) # Convert wiki links - line = convert_wikilinks(line, slug, "https://git.cryptech.is/") + line = wikilink_pattern.sub(lambda m: convert_wikilinks(m, slug, "https://git.cryptech.is/"), line) # Convert striked through text line = strikethrough_pattern.sub(r"<s>\1</s>", line) |