1
1
mirror of https://github.com/chubin/cheat.sh.git synced 2024-12-14 18:41:36 +03:00
cheat.sh/lib/adapter_learnxiny.py

524 lines
16 KiB
Python
Raw Normal View History

2017-05-28 17:39:09 +03:00
#!/usr/bin/python
import os
import re
class LearnXYAdapter(object):
_learn_xy_path = "/home/igor/git/github.com/adambard/learnxinyminutes-docs"
def __init__(self):
self._whole_cheatsheet = self._read_cheatsheet()
self._blocks = self._extract_blocks()
2017-05-29 00:46:35 +03:00
self._topics_list = [x for x,y in self._blocks] + [":learn"]
2017-05-28 17:39:09 +03:00
def _read_cheatsheet(self):
filename = os.path.join(self._learn_xy_path, self._filename)
with open(filename) as f:
code_mode = False
answer = []
for line in f.readlines():
if line.startswith('```'):
if not code_mode:
code_mode = True
continue
else:
2017-06-02 18:53:19 +03:00
code_mode = False
2017-05-28 17:39:09 +03:00
if code_mode:
answer.append(line.rstrip('\n'))
2017-06-02 18:53:19 +03:00
return answer
2017-05-28 17:39:09 +03:00
def _extract_blocks(self):
lines = self._whole_cheatsheet
answer = []
block = []
2017-05-29 00:46:35 +03:00
block_name = "Start"
2017-05-28 17:39:09 +03:00
for before, now, after in zip([""]+lines, lines, lines[1:]):
new_block_name = self._is_block_separator(before, now, after)
if new_block_name:
print new_block_name
if block_name:
answer.append((block_name, self._cut_block(block)))
block_name = new_block_name
block = []
continue
else:
block.append(before)
2017-05-29 01:22:46 +03:00
answer.append((block_name, self._cut_block(block)))
2017-05-28 17:39:09 +03:00
return answer
def is_valid(self, name):
2017-05-29 00:46:35 +03:00
for x in self._topics_list:
2017-05-28 17:39:09 +03:00
if x == name:
return True
return False
def get_list(self, prefix=False):
if prefix:
2017-05-29 00:46:35 +03:00
return ["%s/%s" % (self._prefix, x) for x in self._topics_list]
2017-05-28 17:39:09 +03:00
else:
2017-05-29 00:46:35 +03:00
return self._topics_list
2017-05-28 17:39:09 +03:00
def get_cheat_sheet(self, name, partial=False):
2017-05-29 00:46:35 +03:00
if name == ":list":
return "\n".join(self.get_list()) + "\n"
if name == ":learn":
return "\n".join(self._whole_cheatsheet) + "\n"
2017-05-28 17:39:09 +03:00
if partial:
possible_names = []
for x, y in self._blocks:
if x.startswith(name):
possible_names.append(x)
if len(possible_names) == 0 or len(possible_names) > 1:
return None
name = possible_names[0]
for x, y in self._blocks:
if x == name:
return "\n".join(y)
return None
#
# Various cheat sheets
#
class LearnClojureAdapter(LearnXYAdapter):
_prefix = "clojure"
_filename = "clojure.html.markdown"
def _is_block_separator(self, before, now, after):
if ( re.match(r'\s*$', before)
and re.match(r';\s*', now)
and re.match(r';;;;;;+', after)):
block_name = re.sub(';\s*', '', now)
block_name = '_'.join([x.strip(",&:") for x in block_name.strip(", ").split()])
# replace_with = {
# 'More_about_Objects':
# 'Prototypes',
# }
# for k in replace_with:
# if k in block_name:
# block_name = replace_with[k]
return block_name
else:
return None
@staticmethod
def _cut_block(block):
answer = block[2:]
if answer[0].split() == '':
answer = answer[1:]
if answer[-1].split() == '':
answer = answer[:1]
return answer
class LearnElixirAdapter(LearnXYAdapter):
_prefix = "elixir"
_filename = "elixir.html.markdown"
def _is_block_separator(self, before, now, after):
if ( re.match(r'## ---*', before)
and re.match(r'## --', now)
and re.match(r'## ---*', after)):
block_name = re.sub('## --\s*', '', now)
block_name = '_'.join(block_name.strip(", ").split())
replace_with = {
'More_about_Objects':
'Prototypes',
}
for k in replace_with:
if k in block_name:
block_name = replace_with[k]
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 LearnElmAdapter(LearnXYAdapter):
_prefix = "elm"
_filename = "elm.html.markdown"
def _is_block_separator(self, before, now, after):
if ( re.match('\s*', before)
and re.match(r'\{--.*--\}', now)
and re.match('\s*', after)):
block_name = re.sub('\{--+\s*', '', now)
block_name = re.sub('--\}', '', block_name)
block_name = '_'.join(block_name.strip(", ").split())
replace_with = {
'More_about_Objects':
'Prototypes',
}
for k in replace_with:
if k in block_name:
block_name = replace_with[k]
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 LearnErlangAdapter(LearnXYAdapter):
_prefix = "erlang"
_filename = "erlang.html.markdown"
def _is_block_separator(self, before, now, after):
if (re.match('%%%%%%+', before)
and re.match('%%\s+[0-9]+\.', now)
and re.match('%%%%%%+', after)):
block_name = re.sub('%%+\s+[0-9]+\.\s*', '', now)
block_name = '_'.join(block_name.strip('.').strip().split())
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
2017-06-02 18:53:19 +03:00
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 LearnJavaScriptAdapter(LearnXYAdapter):
_prefix = "js"
_filename = "javascript.html.markdown"
2017-05-28 17:39:09 +03:00
def _is_block_separator(self, before, now, after):
if ( re.match('//////+', before)
and re.match('//+\s+[0-9]+\.', now)
and re.match('\s*', after)):
block_name = re.sub('//+\s+[0-9]+\.\s*', '', now)
block_name = '_'.join(block_name.strip(", ").split())
replace_with = {
'More_about_Objects':
'Prototypes',
}
for k in replace_with:
if k in block_name:
block_name = replace_with[k]
2017-05-28 17:39:09 +03:00
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
2017-05-28 17:39:09 +03:00
class LearnJuliaAdapter(LearnXYAdapter):
_prefix = "julia"
_filename = "julia.html.markdown"
def _is_block_separator(self, before, now, after):
if ( re.match('####+', before)
and re.match(r'##\s*', now)
and re.match('####+', after)):
block_name = re.sub(r'##\s+[0-9]+\.\s*', '', now)
block_name = '_'.join(block_name.strip(", ").split())
#replace_with = {
# 'More_about_Objects':
# 'Prototypes',
#}
#for k in replace_with:
# if k in block_name:
# block_name = replace_with[k]
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 LearnHaskellAdapter(LearnXYAdapter):
_prefix = "haskell"
_filename = "haskell.html.markdown"
def _is_block_separator(self, before, now, after):
if ( re.match('------+', before)
and re.match('--+\s+[0-9]+\.', now)
and re.match('------+', after)):
block_name = re.sub('--+\s+[0-9]+\.\s*', '', now)
block_name = '_'.join(block_name.strip(", ").split())
replace_with = {
'More_about_Objects':
'Prototypes',
}
for k in replace_with:
if k in block_name:
block_name = replace_with[k]
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 LearnKotlinAdapter(LearnXYAdapter):
_prefix = "kotlin"
_filename = "kotlin.html.markdown"
2017-05-28 17:39:09 +03:00
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().split())
return block_name
else:
return None
@staticmethod
def _cut_block(block):
2017-05-29 01:22:46 +03:00
answer = block[2:-1]
if answer[0].split() == '':
answer = answer[1:]
if answer[-1].split() == '':
answer = answer[:1]
return answer
2017-05-28 17:39:09 +03:00
class LearnOCamlAdapter(LearnXYAdapter):
_prefix = "ocaml"
_filename = "ocaml.html.markdown"
def _is_block_separator(self, before, now, after):
if ( re.match('\s*', before)
and re.match(r'\(\*\*\*+', now)
and re.match('\s*', after)):
block_name = re.sub(r'\(\*\*\*+\s*', '', now)
block_name = re.sub(r'\s*\*\*\*\)', '', block_name)
block_name = '_'.join(block_name.strip(", ").split())
replace_with = {
'More_about_Objects':
'Prototypes',
}
for k in replace_with:
if k in block_name:
block_name = replace_with[k]
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
2017-06-02 18:53:19 +03:00
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
class LearnPHPAdapter(LearnXYAdapter):
_prefix = "php"
_filename = "php.html.markdown"
def _is_block_separator(self, before, now, after):
if (re.match(r'/\*\*\*\*\*+', before)
and re.match(r'\s*\*/', after)
and re.match(r'\s*\*\s*', now)):
block_name = re.sub(r'\s*\*\s*', '', now)
block_name = re.sub(r'&', '', block_name)
block_name = '_'.join(block_name.strip().split())
return block_name
else:
return None
@staticmethod
def _cut_block(block):
return block[2:]
class LearnPythonAdapter(LearnXYAdapter):
_prefix = "python"
_filename = "python.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().split())
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 LearnRubyAdapter(LearnXYAdapter):
_prefix = "ruby"
_filename = "ruby.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().split())
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
2017-05-28 17:39:09 +03:00
#
# Exported functions
#
ADAPTERS = {
'clojure' : LearnClojureAdapter(),
'elixir' : LearnElixirAdapter(),
'elm' : LearnElmAdapter(),
'erlang' : LearnErlangAdapter(),
'haskell' : LearnHaskellAdapter(),
'js' : LearnJavaScriptAdapter(),
'julia' : LearnJuliaAdapter(),
'kotlin' : LearnKotlinAdapter(),
2017-06-02 18:53:19 +03:00
'lua' : LearnLuaAdapter(),
'ocaml' : LearnOCamlAdapter(),
2017-05-28 17:39:09 +03:00
'python' : LearnPythonAdapter(),
'php' : LearnPHPAdapter(),
2017-06-02 18:53:19 +03:00
'perl' : LearnPerlAdapter(),
'ruby' : LearnRubyAdapter(),
2017-05-28 17:39:09 +03:00
}
def get_learnxiny(topic):
lang, topic = topic.split('/', 1)
if lang not in ADAPTERS:
return ''
return ADAPTERS[lang].get_cheat_sheet(topic)
def get_learnxiny_list():
answer = []
for k,v in ADAPTERS.items():
answer += v.get_list(prefix=True)
return answer
def is_valid_learnxy(topic):
lang, topic = topic.split('/', 1)
if lang not in ADAPTERS:
return False
return ADAPTERS[lang].is_valid(topic)