|
1 | 1 | from termcolor import colored |
2 | 2 | import requests |
3 | 3 | from rich.console import Console |
| 4 | +from rich.markdown import Markdown |
| 5 | +from rich.style import Style |
4 | 6 | import sys as sys |
5 | 7 |
|
6 | 8 | # Required for Questions Panel |
|
9 | 11 | from collections import defaultdict |
10 | 12 | from simple_term_menu import TerminalMenu |
11 | 13 | import webbrowser |
12 | | -from pygments import highlight |
13 | | -from pygments.lexers.markup import MarkdownLexer |
14 | | -from pygments.formatters import TerminalFormatter |
15 | 14 |
|
16 | 15 | from .error import SearchError |
17 | 16 | from .save import SaveSearchResults |
@@ -88,16 +87,19 @@ def populate_answer_data(self, questions_list): |
88 | 87 | def return_formatted_ans(self, ques_id): |
89 | 88 | # This function uses pygments lexers ad formatters to format the content in the preview screen |
90 | 89 | body_markdown = self.answer_data[int(ques_id)] |
91 | | - if(body_markdown): |
92 | | - body_markdown = str(body_markdown) |
93 | | - xml_markup_replacement = [("&", "&"), ("<", "<"), (">", ">"), (""", "\""), ("'", "\'"), ("'", "\'")] |
94 | | - for convert_from, convert_to in xml_markup_replacement: |
95 | | - body_markdown = body_markdown.replace(convert_from, convert_to) |
96 | | - lexer = MarkdownLexer() |
97 | | - formatter = TerminalFormatter() |
98 | | - highlighted = highlight(body_markdown, lexer, formatter) |
99 | | - else: |
100 | | - highlighted = "Answer not viewable. Press enter to open in a browser" |
| 90 | + body_markdown = str(body_markdown) |
| 91 | + xml_markup_replacement = [("&", "&"), ("<", "<"), (">", ">"), (""", "\""), ("'", "\'"), ("'", "\'")] |
| 92 | + for convert_from, convert_to in xml_markup_replacement: |
| 93 | + body_markdown = body_markdown.replace(convert_from, convert_to) |
| 94 | + width = os.get_terminal_size().columns |
| 95 | + console = Console(width=width-4) |
| 96 | + markdown = Markdown(body_markdown, hyperlinks=False) |
| 97 | + with console.capture() as capture: |
| 98 | + console.print(markdown) |
| 99 | + highlighted = capture.get() |
| 100 | + box_replacement = [("─", "-"), ("═","="), ("║","|"), ("│", "|"), ('┌', '+'), ("└", "+"), ("┐", "+"), ("┘", "+"), ("╔", "+"), ("╚", "+"), ("╗","+"), ("╝", "+"), ("•","*")] |
| 101 | + for convert_from, convert_to in box_replacement: |
| 102 | + highlighted = highlighted.replace(convert_from, convert_to) |
101 | 103 | return highlighted |
102 | 104 |
|
103 | 105 | def navigate_questions_panel(self): |
|
0 commit comments