Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/highlightjs.py
# Copyright (C) 2017-2019 The Software Heritage developers | # Copyright (C) 2017-2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import functools | import functools | ||||
from typing import Dict | from typing import Dict | ||||
from pygments.lexers import ( | from pygments.lexers import ( | ||||
get_all_lexers, | get_all_lexers, | ||||
get_lexer_for_filename | get_lexer_for_filename | ||||
) | ) | ||||
import sentry_sdk | |||||
# set of languages ids that can be highlighted | # set of languages ids that can be highlighted | ||||
# by highlight.js library | # by highlight.js library | ||||
_hljs_languages = set([ | _hljs_languages = set([ | ||||
'1c', 'abnf', 'accesslog', 'actionscript', 'ada', 'angelscript', 'apache', | '1c', 'abnf', 'accesslog', 'actionscript', 'ada', 'angelscript', 'apache', | ||||
'applescript', 'arcade', 'arduino', 'armasm', 'asciidoc', 'aspectj', | 'applescript', 'arcade', 'arduino', 'armasm', 'asciidoc', 'aspectj', | ||||
'autohotkey', 'autoit', 'avrasm', 'awk', 'axapta', 'bash', 'basic', 'bnf', | 'autohotkey', 'autoit', 'avrasm', 'awk', 'axapta', 'bash', 'basic', 'bnf', | ||||
'brainfuck', 'cal', 'capnproto', 'ceylon', 'clean', 'clojure', | 'brainfuck', 'cal', 'capnproto', 'ceylon', 'clean', 'clojure', | ||||
▲ Show 20 Lines • Show All 290 Lines • ▼ Show 20 Lines | if filename: | ||||
if ext in _hljs_languages_aliases: | if ext in _hljs_languages_aliases: | ||||
return _hljs_languages_aliases[ext] | return _hljs_languages_aliases[ext] | ||||
# otherwise use Pygments language database | # otherwise use Pygments language database | ||||
lexer = None | lexer = None | ||||
# try to find a Pygment lexer | # try to find a Pygment lexer | ||||
try: | try: | ||||
lexer = get_lexer_for_filename(filename) | lexer = get_lexer_for_filename(filename) | ||||
except Exception: | except Exception as exc: | ||||
pass | sentry_sdk.capture_exception(exc) | ||||
# if there is a correspondence between the lexer and an hljs | # if there is a correspondence between the lexer and an hljs | ||||
# language, return it | # language, return it | ||||
if lexer and lexer.name in _pygments_lexer_to_hljs_language: | if lexer and lexer.name in _pygments_lexer_to_hljs_language: | ||||
return _pygments_lexer_to_hljs_language[lexer.name] | return _pygments_lexer_to_hljs_language[lexer.name] | ||||
# otherwise, try to find a match between the file extensions | # otherwise, try to find a match between the file extensions | ||||
# associated to the lexer and the hljs language aliases | # associated to the lexer and the hljs language aliases | ||||
if lexer: | if lexer: | ||||
exts = [ext.replace('*.', '') for ext in lexer.filenames] | exts = [ext.replace('*.', '') for ext in lexer.filenames] | ||||
Show All 32 Lines |