Changeset View
Changeset View
Standalone View
Standalone View
swh/web/utils/highlightjs.py
# Copyright (C) 2017-2022 The Software Heritage developers | # Copyright (C) 2017-2022 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 | ||||
import json | import json | ||||
from typing import Dict | from typing import Dict | ||||
from pygments.lexers import get_all_lexers, get_lexer_for_filename | from pygments.lexers import get_all_lexers, get_lexer_for_filename | ||||
from pygments.util import ClassNotFound | |||||
from django.contrib.staticfiles.finders import find | from django.contrib.staticfiles.finders import find | ||||
from swh.web.utils.exc import sentry_capture_exception | |||||
@functools.lru_cache() | @functools.lru_cache() | ||||
def _hljs_languages_data(): | def _hljs_languages_data(): | ||||
with open(str(find("json/highlightjs-languages.json")), "r") as hljs_languages_file: | with open(str(find("json/highlightjs-languages.json")), "r") as hljs_languages_file: | ||||
return json.load(hljs_languages_file) | return json.load(hljs_languages_file) | ||||
# set of languages ids that can be highlighted by highlight.js library | # set of languages ids that can be highlighted by highlight.js library | ||||
▲ Show 20 Lines • Show All 111 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 as exc: | except ClassNotFound: | ||||
sentry_capture_exception(exc) | pass | ||||
# 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 |