Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_hljs(filename, language):
assert get_language(filename) == language
if encoding_match:
encoding = encoding_match.group(1)
else:
raise exc
try:
lines = raw_content.decode(encoding.decode('utf-8')).split('\n')
except UnicodeDecodeError as exc2:
raise exc2 from exc
del raw_content
# If the first line is a shebang,
if lines[0].startswith('#!'):
# prioritize its declaration over the extension.
language = get_language(lines[0]) or language
super().__init__(prefix=f'```{language}', suffix='```', **kwargs)
if line_span:
line_span = sorted(line_span)
if min(line_span) < 1 or max(line_span) > len(lines):
raise ValueError("Linespan goes out of bounds.")
lines = lines[line_span[0] - 1:line_span[1]]
for line in lines:
self.add_line(line)
def __init__(self, fp, line_span=None, language_hints=(), **kwargs):
language = ''
for hint in language_hints:
language = get_language(hint)
if language:
break
if not language:
try:
language = get_language(fp.name)
except AttributeError:
pass
raw_content = fp.read()
try:
lines = raw_content.decode('utf-8').split('\n')
except UnicodeDecodeError as exc:
# This file isn't UTF-8.
def __init__(self, fp, line_span=None, language_hints=(), **kwargs):
language = ''
for hint in language_hints:
language = get_language(hint)
if language:
break
if not language:
try:
language = get_language(fp.name)
except AttributeError:
pass
raw_content = fp.read()
try:
lines = raw_content.decode('utf-8').split('\n')
except UnicodeDecodeError as exc:
# This file isn't UTF-8.
# By Python and text-editor convention,
# there may be a hint as to what the actual encoding is
# near the start of the file.
encoding_match = self.__encoding_regex.search(raw_content[:128])