From b58c60bcc4a6f3d3ccf4194ef862a808fdc3313b Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Sun, 14 Feb 2021 01:55:38 +0000 Subject: Hack images, store outputs in git again for now Easier to track what each script change does if we keep the before and after versions of the markdown in git too. Clean this up eventually, but simplifies development. --- tools/trac2md.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'tools') diff --git a/tools/trac2md.py b/tools/trac2md.py index 838a36f..214471f 100755 --- a/tools/trac2md.py +++ b/tools/trac2md.py @@ -17,6 +17,7 @@ wikilink_pattern = re.compile('\[http(.*)\]') wikilink_extract = re.compile('\[(.*)\]') strikethrough_pattern = re.compile('~~(.*)~~') camelcase_pattern = re.compile("!(\w+)") +image_pattern = re.compile("\[\[Image\((.*)\)\]\]") wikiheading_patterns = tuple((level, re.compile("^{} (.*)[ \t]*=*$".format("=" * level))) for level in range(1, 7)) @@ -104,6 +105,18 @@ def convert_strike(line): pass return line +def convert_image(line): + image_result = image_pattern.search(line) + if image_result: + try: + image_text = image_result.group(1).split(",")[0].strip() + old_text = image_result.group(0) + new_text = "".format(image_text) + line = line.replace(old_text, new_text) + except: + pass + return line + def WikiToMD(content): ''' Convert wiki/RST format to Markdown. Code blocks, bold/italics, wiki links, lists, striked text, and headers. ''' @@ -196,6 +209,9 @@ def WikiToMD(content): # Convert striked through text line = convert_strike(line) + # Convert images + line = convert_image(line) + # Convert bold and italic text (do this last) line = line.replace("'''", "**") # Convert bold text line = line.replace("''", "*") # Convert italic text @@ -204,11 +220,3 @@ def WikiToMD(content): new_content.extend(tail) return "".join(new_content) - -if __name__ == "__main__": - for f in sys.argv[1:]: - d = WikiToMD(open(f, "r").read()) - newf = f.replace(".trac", ".md") - with open(newf, "w") as fp: - fp.write(d) - pass -- cgit v1.2.3