File size: 1,540 Bytes
d93fd32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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())