Skip to content

Commit 456a157

Browse files
committed
Change code block style of Stack Exchange Question Panel
1 parent bc06fb2 commit 456a157

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

requirements.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ configparser==5.0.2
1212
crayons==0.4.0
1313
selenium==3.141.0
1414
webdriver-manager==3.3.0
15-
simple-term-menu==1.0.1
16-
jupyterlab-pygments==0.1.2
15+
simple-term-menu==1.0.1

src/arguments/utility.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from termcolor import colored
22
import requests
33
from rich.console import Console
4+
from rich.markdown import Markdown
5+
from rich.style import Style
46
import sys as sys
57

68
# Required for Questions Panel
@@ -9,9 +11,6 @@
911
from collections import defaultdict
1012
from simple_term_menu import TerminalMenu
1113
import webbrowser
12-
from pygments import highlight
13-
from pygments.lexers.markup import MarkdownLexer
14-
from pygments.formatters import TerminalFormatter
1514

1615
from .error import SearchError
1716
from .save import SaveSearchResults
@@ -88,16 +87,19 @@ def populate_answer_data(self, questions_list):
8887
def return_formatted_ans(self, ques_id):
8988
# This function uses pygments lexers ad formatters to format the content in the preview screen
9089
body_markdown = self.answer_data[int(ques_id)]
91-
if(body_markdown):
92-
body_markdown = str(body_markdown)
93-
xml_markup_replacement = [("&amp;", "&"), ("&lt;", "<"), ("&gt;", ">"), ("&quot;", "\""), ("&apos;", "\'"), ("&#39;", "\'")]
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 = [("&amp;", "&"), ("&lt;", "<"), ("&gt;", ">"), ("&quot;", "\""), ("&apos;", "\'"), ("&#39;", "\'")]
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)
101103
return highlighted
102104

103105
def navigate_questions_panel(self):

0 commit comments

Comments
 (0)