import re from markdownify import MarkdownConverter, chomp class WikiConverter(MarkdownConverter): def convert_a(self, el, text, convert_as_inline): prefix, suffix, text = chomp(text) if not text: return "" return "%s%s%s" % (prefix, text, suffix) integer_rgx = re.compile("^[0-9]*$") @staticmethod def is_intable(string: str): if not string or not string.isdigit(): return False if WikiConverter.integer_rgx.match(string): return True def convert_img(self, el, text, convert_as_inline): convert_as_inline = True alt = el.attrs.get("alt", None) or "" if ( convert_as_inline and el.parent.name not in self.options["keep_inline_images_in"] ): return alt return alt def convert_li(self, el, text, convert_as_inline): parent = el.parent if parent is not None and parent.name == "ol": start = parent.get("start") if start and WikiConverter.is_intable(start.strip()): start = int(start.strip()) else: start = 1 bullet = "%s." % (start + parent.index(el)) else: depth = -1 while el: if el.name == "ul": depth += 1 el = el.parent bullets = self.options["bullets"] bullet = bullets[depth % len(bullets)] return "%s %s\n" % (bullet, (text or "").strip())