diff --git a/lib/adapter_learnxiny.py b/lib/adapter_learnxiny.py index 6ab5d95..d234e01 100644 --- a/lib/adapter_learnxiny.py +++ b/lib/adapter_learnxiny.py @@ -24,9 +24,10 @@ class LearnXYAdapter(object): code_mode = True continue else: - return answer + code_mode = False if code_mode: answer.append(line.rstrip('\n')) + return answer def _extract_blocks(self): lines = self._whole_cheatsheet @@ -87,6 +88,39 @@ class LearnXYAdapter(object): # Various cheat sheets # +class LearnLuaAdapter(LearnXYAdapter): + _prefix = "lua" + _filename = "lua.html.markdown" + + def _is_block_separator(self, before, now, after): + if (re.match('-----+', before) + and re.match('-------+', after) + and re.match('--\s+[0-9]+\.', now)): + block_name = re.sub('--+\s+[0-9]+\.\s*', '', now) + block_name = '_'.join(block_name.strip('.').strip().split()) + replace_with = { + '1_Metatables_and_metamethods': + 'Metatables', + '2_Class-like_tables_and_inheritance': + 'Class-like_tables', + 'Variables_and_flow_control': + 'Flow_control', + } + if block_name in replace_with: + block_name = replace_with[block_name] + return block_name + else: + return None + + @staticmethod + def _cut_block(block): + answer = block[2:-1] + if answer[0].split() == '': + answer = answer[1:] + if answer[-1].split() == '': + answer = answer[:1] + return answer + class LearnPHPAdapter(LearnXYAdapter): _prefix = "php" _filename = "php.html.markdown" @@ -129,13 +163,50 @@ class LearnPythonAdapter(LearnXYAdapter): answer = answer[:1] return answer +class LearnPerlAdapter(LearnXYAdapter): + _prefix = "perl" + _filename = "perl.html.markdown" + + def _is_block_separator(self, before, now, after): + if re.match('####+\s+', now): + block_name = re.sub('#+\s', '', now) + block_name = '_'.join(block_name.strip().split()) + replace_with = { + 'Conditional_and_looping_constructs': + 'Control_Flow', + 'Perl_variable_types': + 'Types', + 'Files_and_I/O': + 'Files', + 'Writing_subroutines': + 'Subroutines', + } + if block_name in replace_with: + block_name = replace_with[block_name] + return block_name + else: + return None + + @staticmethod + def _cut_block(block): + answer = block[2:] + if len(answer) == 0: + return answer + if answer[0].split() == '': + answer = answer[1:] + if answer[-1].split() == '': + answer = answer[:1] + return answer + # # Exported functions # ADAPTERS = { + 'lua' : LearnLuaAdapter(), 'python' : LearnPythonAdapter(), 'php' : LearnPHPAdapter(), + 'perl' : LearnPerlAdapter(), } def get_learnxiny(topic): diff --git a/lib/cheat_wrapper.py b/lib/cheat_wrapper.py index 54cbaf2..76b8113 100644 --- a/lib/cheat_wrapper.py +++ b/lib/cheat_wrapper.py @@ -18,7 +18,7 @@ import redis import colored from pygments import highlight as pygments_highlight -from pygments.lexers import BashLexer, GoLexer, ScalaLexer, RustLexer, PythonLexer, PhpLexer +import pygments.lexers # from pygments.lexers import BashLexer, GoLexer, ScalaLexer, RustLexer, PythonLexer, PhpLexer, PerlLexer from pygments.formatters import TerminalFormatter, Terminal256Formatter from pygments.styles import get_all_styles @@ -37,11 +37,15 @@ from adapter_learnxiny import get_learnxiny, get_learnxiny_list, is_valid_learnx # globals INTERNAL_TOPICS = [":list", ":firstpage", ':post', ':bash_completion', ':help', ':styles', ':styles-demo', ':emacs', ':fish', ':bash', ':zsh'] LEXER = { - "go" : GoLexer, - "scala" : ScalaLexer, - "rust" : RustLexer, - "python": PythonLexer, - "php" : PhpLexer, + "go" : pygments.lexers.GoLexer, + "elixir": pygments.lexers.ElixirLexer, + "js" : pygments.lexers.JavascriptLexer, + "lua" : pygments.lexers.LuaLexer, + "scala" : pygments.lexers.ScalaLexer, + "rust" : pygments.lexers.RustLexer, + "perl" : pygments.lexers.PerlLexer, + "python": pygments.lexers.PythonLexer, + "php" : pygments.lexers.PhpLexer, } REDIS = redis.StrictRedis(host='localhost', port=6379, db=0) @@ -510,7 +514,7 @@ def cheat_wrapper(query, request_options=None, html=False): answer = colorize_internal(topic, answer, html) else: color_style = color_style or "native" - lexer = BashLexer + lexer = pygments.lexers.BashLexer for lexer_name, lexer_value in LEXER.items(): if topic.startswith("%s/" % lexer_name): color_style = color_style or "monokai" diff --git a/share/firstpage.txt b/share/firstpage.txt index 3bea41f..a7295db 100644 --- a/share/firstpage.txt +++ b/share/firstpage.txt @@ -11,10 +11,10 @@ d88' `"Y8 888P"Y88b d88' `88b `P )88b 888 d88( "8 888P"Y88b * the fastest way to find | you need * provides access to community driven cheat sheets repositories - * delivers <746> cheat sheets in <6> areas and growing - * covers UNIX/Linux commands, programming languages, ANSI codes etc. + * delivers <783> cheat sheets in <8> areas and growing + * covers UNIX/Linux commands and programming languages * programming languages cheat sheets are under: - + * supports bash completion (add {/:bash_completion} to your {~/.bashrc})