Implement quote and code

This commit is contained in:
Ngô Ngọc Đức Huy 2024-08-17 00:37:27 +07:00
parent dbcf3cbd92
commit 718eb615ef
Signed by: xarvos
GPG key ID: 904AF1C7CDF695C3
2 changed files with 48 additions and 13 deletions

View file

@ -351,14 +351,31 @@ def print_status(status: Status, width: int = get_term_width(), padding: int = 0
def print_html(text, width=get_term_width(), padding=0):
first = True
for paragraph in html_to_paragraphs(text):
if not first:
print_out("" * padding)
for line in paragraph:
is_in_pre = False
for line in html_to_paragraphs(text):
if line == '<blockquote>':
print_out("" * padding + "┌<yellow>Quote</yellow>" + "" * (width - 6))
width -= 1
padding += 1
elif line == '</blockquote>':
width += 1
padding -= 1
print_out("" * padding + "" + "" * (width - 1))
elif line == '<pre>':
print_out("" * padding + "┌<yellow>Code</yellow>" + "" * (width - 5))
width -= 1
padding += 1
is_in_pre = True
elif line == '</pre>':
width += 1
padding -= 1
print_out("" * padding + "" + "" * (width - 1))
is_in_pre = False
elif is_in_pre:
print_out("" * padding + line)
else:
for subline in wc_wrap(line, width):
print_out("" * padding + subline)
first = False
def print_poll(poll: Poll):

View file

@ -39,6 +39,7 @@ class HTMLTextParser(HTMLParser):
self.current_a = ''
self.list_stack = deque()
self.list_level = -1
self.is_in_pre = False
# for output
self.links = []
@ -48,6 +49,7 @@ class HTMLTextParser(HTMLParser):
def commit(self):
"""Commit any dangling lines."""
if self.line != '':
self.lines.append(self.line)
self.line = ''
@ -86,8 +88,15 @@ class HTMLTextParser(HTMLParser):
self.line += '<bold>'
elif tag in ('s', 'del'):
self.line += '<strikethrough>'
elif tag in ('u'):
elif tag == 'u':
self.line += '<underline>'
elif tag in ('blockquote', 'pre'):
self.commit()
self.lines.append(f'<{tag}>')
if tag == 'pre':
self.is_in_pre = True
elif tag == 'code':
self.line += '<green>'
def handle_endtag(self, tag):
if tag == 'p':
@ -110,7 +119,6 @@ class HTMLTextParser(HTMLParser):
self.list_stack.pop()
self.list_level -= 1
elif tag == 'li':
if self.line:
self.commit()
elif tag in ('i', 'em'):
self.line += '</italic>'
@ -118,13 +126,23 @@ class HTMLTextParser(HTMLParser):
self.line += '</bold>'
elif tag in ('s', 'del'):
self.line += '</strikethrough>'
elif tag in ('u'):
elif tag == 'u':
self.line += '</underline>'
elif tag in ('blockquote', 'pre'):
self.commit()
self.lines.append(f'</{tag}>')
if tag == 'pre':
self.is_in_pre = False
elif tag == 'code':
self.line += '</green>'
def handle_data(self, data):
data = data.replace('<', '\\<')
if self.is_in_a:
self.current_a += data
elif self.is_in_pre:
for line in data.splitlines():
self.lines.append(line)
else:
self.line += data
@ -170,7 +188,7 @@ def html_to_paragraphs(html):
paragraphs.append(f'[{i + 1}]: {link}')
for i, link in enumerate(images):
paragraphs.append(f'[img#{i + 1}]: {link}')
return [paragraphs]
return paragraphs
def format_content(content):