-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmarkdownifytg.py
74 lines (50 loc) · 2.18 KB
/
markdownifytg.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import re
from markdownify import (
MarkdownConverter,
abstract_inline_conversion, chomp,
)
html_heading_re = re.compile(r'(h[1-6]|header|title)')
class Converter(MarkdownConverter):
convert_b = abstract_inline_conversion(lambda self: '**')
convert_i = abstract_inline_conversion(lambda self: '__')
convert_em = abstract_inline_conversion(lambda self: '__')
def convert_header(self, el, text, convert_as_inline):
return '\n' + super().convert_b(el, text, convert_as_inline) + '\n'
def convert_hn(self, n, el, text, convert_as_inline):
return '\n' + super().convert_b(el, text, convert_as_inline) + '\n'
def convert_hr(self, el, text, convert_as_inline):
return ''
def convert_title(self, el, text, convert_as_inline):
return super().convert_b(el, text, convert_as_inline) + '\n'
def convert_formula(self, el, text, convert_as_inline):
return '🔢\n'
def convert_a(self, el, text, convert_as_inline):
prefix, suffix, text = chomp(text)
if not text:
return ''
href = el.get('href')
return f'[{text}]({href})'
def convert_img(self, el, text, convert_as_inline):
return '🖼️\n'
def convert_table(self, el, text, convert_as_inline):
return '🔢\n'
class SnippetConverter(MarkdownConverter):
convert_highlight = abstract_inline_conversion(lambda self: '**')
convert_i = abstract_inline_conversion(lambda self: '')
convert_header = abstract_inline_conversion(lambda self: '')
def convert_hn(self, n, el, text, convert_as_inline):
return text
def convert_hr(self, el, text, convert_as_inline):
return ''
def convert_title(self, el, text, convert_as_inline):
return text
def convert_formula(self, el, text, convert_as_inline):
return '🔢\n'
def convert_img(self, el, text, convert_as_inline):
return '🖼️\n'
def convert_table(self, el, text, convert_as_inline):
return '🔢\n'
md_converter = Converter(escape_asterisks=False)
highlight_md_converter = SnippetConverter(escape_asterisks=False)
def md(html, **options):
return Converter(**options).convert(html)