diff --git a/witchie/output.py b/witchie/output.py index 807b460..5306c6b 100644 --- a/witchie/output.py +++ b/witchie/output.py @@ -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 == '
': + print_out("│" * padding + "┌Quote" + "─" * (width - 6)) + width -= 1 + padding += 1 + elif line == '
': + width += 1 + padding -= 1 + print_out("│" * padding + "└" + "─" * (width - 1)) + elif line == '
':
+            print_out("│" * padding + "┌Code" + "─" * (width - 5))
+            width -= 1
+            padding += 1
+            is_in_pre = True
+        elif line == '
': + 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): diff --git a/witchie/utils/__init__.py b/witchie/utils/__init__.py index 64ceaf0..92fe5f6 100644 --- a/witchie/utils/__init__.py +++ b/witchie/utils/__init__.py @@ -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,8 +49,9 @@ class HTMLTextParser(HTMLParser): def commit(self): """Commit any dangling lines.""" - self.lines.append(self.line) - self.line = '' + if self.line != '': + self.lines.append(self.line) + self.line = '' def handle_starttag(self, tag, attrs): if tag in ('img', 'br', 'hr', 'wbr'): @@ -86,8 +88,15 @@ class HTMLTextParser(HTMLParser): self.line += '' elif tag in ('s', 'del'): self.line += '' - elif tag in ('u'): + elif tag == 'u': self.line += '' + 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 += '' def handle_endtag(self, tag): if tag == 'p': @@ -110,21 +119,30 @@ class HTMLTextParser(HTMLParser): self.list_stack.pop() self.list_level -= 1 elif tag == 'li': - if self.line: - self.commit() + self.commit() elif tag in ('i', 'em'): self.line += '' elif tag in ('b', 'strong'): self.line += '' elif tag in ('s', 'del'): self.line += '' - elif tag in ('u'): + elif tag == 'u': self.line += '' + elif tag in ('blockquote', 'pre'): + self.commit() + self.lines.append(f'') + if tag == 'pre': + self.is_in_pre = False + elif tag == 'code': + self.line += '' 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):