forked from j-hc/revanced-magisk-module
-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
markdown_to_telegraph.py
51 lines (44 loc) · 2.18 KB
/
markdown_to_telegraph.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
from telegraph import Telegraph
from gh_md_to_html import core_converter
import re
class MarkdownToTelegraph:
def __init__(self, short_name: str, author_name: str, author_url: str) -> None:
self.short_name = short_name
self.author_name = author_name
self.author_url = author_url
self.telegraph = Telegraph()
self.telegraph.create_account(short_name=self.short_name, author_name=self.author_name, author_url=self.author_url)
def filter_html_tags(self, html_string: str) -> str:
# Replace h1 tags with h3 tags
html_string = re.sub(r"<h1.*?>", "<h3>", html_string)
html_string = re.sub(r"</h1>", "</h3>", html_string)
# Replace h2 tags with h3 tags
html_string = re.sub(r"<h2.*?>", "<h3>", html_string)
html_string = re.sub(r"</h2>", "</h3>", html_string)
# Replace span tags with p tags using regex
html_string = re.sub(r"<span.*?>", "<p>", html_string)
html_string = re.sub(r"</span>", "</p>", html_string)
return html_string
def convert_file(self, markdown_file: str) -> str:
markdown_string = open(markdown_file, "r").read()
html_string = core_converter.markdown(markdown_string)
html_string = self.filter_html_tags(html_string)
return html_string
def convert_string(self, markdown_string: str) -> str:
html_string = core_converter.markdown(markdown_string)
html_string = self.filter_html_tags(html_string)
return html_string
def create_page(self, title: str, html_content: str) -> str:
response = self.telegraph.create_page(
title,
html_content=html_content,
author_name=self.author_name,
author_url=self.author_url
)
return response['url']
def create_page_from_file(self, title: str, markdown_file: str) -> str:
html_content = self.convert_file(markdown_file)
return self.create_page(title, html_content)
def create_page_from_string(self, title: str, markdown_string: str) -> str:
html_string = self.convert_string(markdown_string)
return self.create_page(title, html_content=html_string)