Skip to content

Commit 2e6ed2d

Browse files
committed
added filetype handling for adapters (#187)
1 parent 33d692c commit 2e6ed2d

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

lib/adapter/adapter.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ def _get_output_format(self, topic):
117117
return 'text'
118118
return self._output_format
119119

120+
# pylint: disable=unused-argument
121+
@staticmethod
122+
def _get_filetype(topic):
123+
"""
124+
Return language name (filetype) for `topic`
125+
"""
126+
return None
127+
120128
def get_page_dict(self, topic, request_options=None):
121129
"""
122130
Return page dict for `topic`
@@ -139,6 +147,11 @@ def get_page_dict(self, topic, request_options=None):
139147
'format': self._get_output_format(topic),
140148
}
141149
answer_dict.update(answer)
150+
151+
# pylint: disable=assignment-from-none
152+
filetype = self._get_filetype(topic)
153+
if filetype:
154+
answer_dict["filetype"] = filetype
142155
return answer_dict
143156

144157
@classmethod

lib/adapter/cmd.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def _get_list(self, prefix=None):
111111
return list("rfc/%s" % x for x in range(1, 8649))
112112

113113
def is_found(self, topic):
114-
return True
114+
return True
115115

116116
class AdapterOeis(CommandAdapter):
117117
"""
@@ -124,6 +124,13 @@ class AdapterOeis(CommandAdapter):
124124
_cache_needed = True
125125
_command = ["share/adapters/oeis.sh"]
126126

127+
@staticmethod
128+
def _get_filetype(topic):
129+
if "/" in topic:
130+
language = topic.split("/")[-1].lower()
131+
return language
132+
return "bash"
133+
127134
def _get_command(self, topic, request_options=None):
128135
cmd = self._command[:]
129136
if not cmd[0].startswith("/"):

lib/frontend/ansi.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,15 @@ def _limited_answer(answer):
5858

5959
def _colorize_ansi_answer(topic, answer, color_style, # pylint: disable=too-many-arguments
6060
highlight_all=True, highlight_code=False,
61-
unindent_code=False):
61+
unindent_code=False, language=None):
6262

6363
color_style = color_style or "native"
6464
lexer_class = languages_data.LEXER['bash']
6565
if '/' in topic:
66-
section_name = topic.split('/', 1)[0].lower()
66+
if language is None:
67+
section_name = topic.split('/', 1)[0].lower()
68+
else:
69+
section_name = language
6770
section_name = languages_data.get_lexer_name(section_name)
6871
lexer_class = languages_data.LEXER.get(section_name, lexer_class)
6972
if section_name == 'php':
@@ -127,7 +130,8 @@ def _visualize(answers, request_options, search_mode=False):
127130
highlight_all=highlight,
128131
highlight_code=(topic_type == 'question'
129132
and not request_options.get('add_comments')
130-
and not request_options.get('remove_text')))
133+
and not request_options.get('remove_text')),
134+
language=answer_dict.get("filetype"))
131135

132136
if request_options.get('no-terminal'):
133137
result = remove_ansi(result)

lib/postprocessing.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ def _answer_add_comments(answer, request_options=None):
1212
return answer
1313

1414
topic = answer['topic']
15-
filetype = 'bash'
16-
if '/' in topic:
17-
filetype = topic.split('/', 1)[0]
18-
if filetype.startswith('q:'):
19-
filetype = filetype[2:]
15+
if "filetype" in answer:
16+
filetype = answer["filetype"]
17+
else:
18+
filetype = 'bash'
19+
if '/' in topic:
20+
filetype = topic.split('/', 1)[0]
21+
if filetype.startswith('q:'):
22+
filetype = filetype[2:]
2023

2124
answer['answer'] = fmt.comments.beautify(
2225
answer['answer'], filetype, request_options)
2326
answer['format'] = 'code'
27+
answer['filetype'] = filetype
2428
return answer
2529

2630
def _answer_filter_by_keyword(answer, keyword, options, request_options=None):

0 commit comments

Comments
 (0)