aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xtrac2md.py82
1 files changed, 38 insertions, 44 deletions
diff --git a/trac2md.py b/trac2md.py
index 4453b09..5900a19 100755
--- a/trac2md.py
+++ b/trac2md.py
@@ -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)