[Cryptech-Commits] [wiki] 55/75: Simplify substitution loops

git at cryptech.is git at cryptech.is
Fri Oct 8 18:52:21 UTC 2021


This is an automated email from the git hooks/post-receive script.

sra at hactrn.net pushed a commit to branch production
in repository wiki.

commit 5a91960371555fc4fb7638c479a6a27b70768085
Author: Rob Austein <sra at hactrn.net>
AuthorDate: Tue Feb 16 01:52:00 2021 +0000

    Simplify substitution loops
---
 trac2md.py | 82 +++++++++++++++++++++++++++++---------------------------------
 1 file 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)



More information about the Commits mailing list