1
1
mirror of https://github.com/chubin/cheat.sh.git synced 2024-11-23 02:25:53 +03:00

Hotfix unicode processing for Python 3 (fixes #265)

This fix is temprorary, and must be implemented properly,
after Python 2 support will be fully dropped.
This commit is contained in:
Igor Chubin 2020-11-29 21:37:30 +00:00
parent 011a077915
commit d2b8697ea2
2 changed files with 5 additions and 2 deletions

View File

@ -230,7 +230,7 @@ def _beautify(text, filetype, add_comments=False, remove_text=False):
# or remove the text completely. Otherwise the code has to remain aligned # or remove the text completely. Otherwise the code has to remain aligned
unindent_code = add_comments or remove_text unindent_code = add_comments or remove_text
lines = [x.rstrip('\n') for x in text.splitlines()] lines = [x.decode("utf-8").rstrip('\n') for x in text.splitlines()]
lines = _cleanup_lines(lines) lines = _cleanup_lines(lines)
lines_classes = zip(_classify_lines(lines), lines) lines_classes = zip(_classify_lines(lines), lines)
lines_classes = _wrap_lines(lines_classes, unindent_code=unindent_code) lines_classes = _wrap_lines(lines_classes, unindent_code=unindent_code)
@ -292,6 +292,7 @@ def beautify(text, lang, options):
# if mode is unknown, just don't transform the text at all # if mode is unknown, just don't transform the text at all
return text return text
if isinstance(text, str):
text = text.encode('utf-8') text = text.encode('utf-8')
digest = "t:%s:%s:%s" % (hashlib.md5(text).hexdigest(), lang, mode) digest = "t:%s:%s:%s" % (hashlib.md5(text).hexdigest(), lang, mode)

View File

@ -40,6 +40,8 @@ def _filter_by_keyword(answer, keyword, options):
def _split_paragraphs(text): def _split_paragraphs(text):
answer = [] answer = []
paragraph = "" paragraph = ""
if isinstance(text, bytes):
text = text.decode("utf-8")
for line in text.splitlines(): for line in text.splitlines():
if line == "": if line == "":
answer.append(paragraph) answer.append(paragraph)