diff --git a/lib/cheat_wrapper.py b/lib/cheat_wrapper.py index 60e5a81..6f89b66 100644 --- a/lib/cheat_wrapper.py +++ b/lib/cheat_wrapper.py @@ -21,7 +21,7 @@ MYDIR = os.path.abspath(os.path.join(__file__, '..', '..')) sys.path.append("%s/lib/" % MYDIR) from globals import error, ANSI2HTML, COLOR_STYLES from buttons import TWITTER_BUTTON, GITHUB_BUTTON, GITHUB_BUTTON_FOOTER -from languages_data import LEXER, LANGUAGE_ALIAS +from languages_data import LEXER, get_lexer_name from get_answer import get_topic_type, get_topics_list, get_answer, find_answer_by_keyword from beautifier import code_blocks # import beautifier @@ -94,7 +94,7 @@ def _colorize_ansi_answer(topic, answer, color_style, # pylint: di lexer_class = LEXER['bash'] if '/' in topic: section_name = topic.split('/', 1)[0].lower() - section_name = LANGUAGE_ALIAS.get(section_name, section_name) + section_name = get_lexer_name(section_name) lexer_class = LEXER.get(section_name, lexer_class) if section_name == 'php': answer = "\n" % answer diff --git a/lib/get_answer.py b/lib/get_answer.py index e4363cf..61e4c70 100644 --- a/lib/get_answer.py +++ b/lib/get_answer.py @@ -27,7 +27,7 @@ import time import beautifier from globals import MYDIR, PATH_TLDR_PAGES, PATH_CHEAT_PAGES, PATH_CHEAT_SHEETS, COLOR_STYLES from adapter_learnxiny import get_learnxiny, get_learnxiny_list, is_valid_learnxy -from languages_data import LANGUAGE_ALIAS, SO_NAME +from languages_data import LANGUAGE_ALIAS, SO_NAME, rewrite_editor_section_name from colorize_internal import colorize_internal # pylint: enable=wrong-import-position,wrong-import-order @@ -37,6 +37,7 @@ MAX_SEARCH_LEN = 20 INTERNAL_TOPICS = [ ':cht.sh', + ':cht.sh-posix', ':bash_completion', ':emacs', ':emacs-ivy', @@ -431,8 +432,10 @@ def get_answer(topic, keyword, options="", request_options=None): # pylint: disa return query section_name, rest = query.split('/', 1) + if ':' in section_name: + section_name = rewrite_editor_section_name(section_name) + section_name = SO_NAME.get(section_name, section_name) - print("%s/%s" % (section_name, rest)) return "%s/%s" % (section_name, rest) diff --git a/lib/languages_data.py b/lib/languages_data.py index 5c60e86..16f6c47 100644 --- a/lib/languages_data.py +++ b/lib/languages_data.py @@ -122,3 +122,72 @@ SO_NAME = { 'vb' : 'vba', 'mathematica': 'wolfram-mathematica', } + + +# +# conversion of internal programmin language names +# into canonical cheat.sh names +# + +ATOM_FT_NAME = { +} + +EMACS_FT_NAME = { +} + +SUBLIME_FT_NAME = { +} + +VIM_FT_NAME = { + 'asm': 'assembler', + 'javascript': 'js', +} + +VSCODE_FT_NAME = { +} + +def rewrite_editor_section_name(section_name): + """ + section name cen be specified in form "editor:editor-filetype" + and it will be rewritten into form "filetype" + basing on the editor filetypes names data. + If editor name is unknown, it is just cut off: notepad:js => js + + Known editors: + * atom + * vim + * emacs + * sublime + * vscode + + >>> rewrite_editor_section_name('js') + 'js' + >>> rewrite_editor_section_name('vscode:js') + 'js' + """ + if ':' not in section_name: + return section_name + + editor_name, section_name = section_name.split(':', 1) + editor_name_mapping = { + 'atom': ATOM_FT_NAME, + 'emacs': EMACS_FT_NAME, + 'sublime': SUBLIME_FT_NAME, + 'vim': VIM_FT_NAME, + 'vscode': VSCODE_FT_NAME, + } + if editor_name not in editor_name_mapping: + return section_name + return editor_name_mapping[editor_name].get(section_name, section_name) + +def get_lexer_name(section_name): + """ + Rewrite `section_name` for the further lexer search (for syntax highlighting) + """ + if ':' in section_name: + section_name = rewrite_editor_section_name(section_name) + return LANGUAGE_ALIAS.get(section_name, section_name) + +if __name__ == "__main__": + import doctest + doctest.testmod()