1
1
mirror of https://github.com/chubin/cheat.sh.git synced 2024-11-23 10:35:29 +03:00

fixed the bug with cheat sheets in subdirs

This commit is contained in:
Igor Chubin 2019-05-09 10:04:16 +02:00
parent 4baf2c3fb1
commit 426dbb9be4
2 changed files with 43 additions and 5 deletions

View File

@ -16,6 +16,26 @@ def _remove_initial_underscore(filename):
filename = filename[1:]
return filename
def _sanitize_dirnames(filename, restore=False):
"""
Remove (or add) leading _ in the directories names in `filename`
The `restore` param means that the path name should be restored from the queryname,
i.e. conversion should be done in the opposite direction
"""
parts = filename.split('/')
newparts = []
for part in parts[:-1]:
if restore:
newparts.append('_'+part)
continue
if part.startswith('_'):
newparts.append(part[1:])
else:
newparts.append(part)
newparts.append(parts[-1])
return "/".join(newparts)
class CheatSheets(GitRepositoryAdapter):
"""
@ -35,19 +55,37 @@ class CheatSheets(GitRepositoryAdapter):
hidden_files = ["_info.yaml"]
answer = []
prefix = os.path.join(
self.local_repository_location(),
self._cheatsheet_files_prefix)
for mask in ['*', '*/*']:
template = os.path.join(
self.local_repository_location(),
self._cheatsheet_files_prefix,
prefix,
mask)
answer += [
os.path.basename(f_name)
_sanitize_dirnames(f_name[len(prefix):])
for f_name in glob.glob(template)
if not os.path.isdir(f_name) and f_name not in hidden_files]
if not os.path.isdir(f_name)
and os.path.basename(f_name) not in hidden_files]
return sorted(answer)
def _get_page(self, topic, request_options=None):
filename = os.path.join(
self.local_repository_location(),
self._cheatsheet_files_prefix,
_sanitize_dirnames(topic, restore=True))
if os.path.exists(filename):
answer = self._format_page(open(filename, 'r').read())
else:
# though it should not happen
answer = "%s:%s not found" % (str(self.__class__), topic)
return answer.decode('utf-8')
class CheatSheetsDir(CheatSheets):
"""

View File

@ -47,7 +47,7 @@ class RepositoryAdapter(Adapter):
answer = self._format_page(open(filename, 'r').read())
else:
# though it should not happen
answer = filename + " " + str(self.__class__)
answer = "%s:%s not found" % (str(self.__class__), topic)
return answer.decode('utf-8')