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 | ||||
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 | ||||
import sentry_sdk | import sentry_sdk | ||||
# set of languages ids that can be highlighted | from django.contrib.staticfiles.finders import find | ||||
# by highlight.js library | |||||
_hljs_languages = set( | |||||
[ | |||||
"1c", | |||||
"abnf", | |||||
"accesslog", | |||||
"actionscript", | |||||
"ada", | |||||
"angelscript", | |||||
"apache", | |||||
"applescript", | |||||
"arcade", | |||||
"arduino", | |||||
"armasm", | |||||
"asciidoc", | |||||
"aspectj", | |||||
"autohotkey", | |||||
"autoit", | |||||
"avrasm", | |||||
"awk", | |||||
"axapta", | |||||
"bash", | |||||
"basic", | |||||
"bnf", | |||||
"brainfuck", | |||||
"cal", | |||||
"capnproto", | |||||
"ceylon", | |||||
"clean", | |||||
"clojure", | |||||
"clojure-repl", | |||||
"cmake", | |||||
"coffeescript", | |||||
"coq", | |||||
"cos", | |||||
"cpp", | |||||
"crmsh", | |||||
"crystal", | |||||
"cs", | |||||
"csp", | |||||
"css", | |||||
"d", | |||||
"dart", | |||||
"delphi", | |||||
"diff", | |||||
"django", | |||||
"dns", | |||||
"dockerfile", | |||||
"dos", | |||||
"dsconfig", | |||||
"dts", | |||||
"dust", | |||||
"ebnf", | |||||
"elixir", | |||||
"elm", | |||||
"erb", | |||||
"erlang", | |||||
"erlang-repl", | |||||
"excel", | |||||
"fix", | |||||
"flix", | |||||
"fortran", | |||||
"fsharp", | |||||
"gams", | |||||
"gauss", | |||||
"gcode", | |||||
"gherkin", | |||||
"glsl", | |||||
"gml", | |||||
"go", | |||||
"golo", | |||||
"gradle", | |||||
"groovy", | |||||
"haml", | |||||
"handlebars", | |||||
"haskell", | |||||
"haxe", | |||||
"hsp", | |||||
"htmlbars", | |||||
"http", | |||||
"hy", | |||||
"inform7", | |||||
"ini", | |||||
"irpf90", | |||||
"isbl", | |||||
"java", | |||||
"javascript", | |||||
"jboss-cli", | |||||
"json", | |||||
"julia", | |||||
"julia-repl", | |||||
"kotlin", | |||||
"lasso", | |||||
"ldif", | |||||
"leaf", | |||||
"less", | |||||
"lisp", | |||||
"livecodeserver", | |||||
"livescript", | |||||
"llvm", | |||||
"lsl", | |||||
"lua", | |||||
"makefile", | |||||
"markdown", | |||||
"mathematica", | |||||
"matlab", | |||||
"maxima", | |||||
"mel", | |||||
"mercury", | |||||
"mipsasm", | |||||
"mizar", | |||||
"mojolicious", | |||||
"monkey", | |||||
"moonscript", | |||||
"n1ql", | |||||
"nginx", | |||||
"nimrod", | |||||
"nix", | |||||
"nsis", | |||||
"objectivec", | |||||
"ocaml", | |||||
"openscad", | |||||
"oxygene", | |||||
"parser3", | |||||
"perl", | |||||
"pf", | |||||
"pgsql", | |||||
"php", | |||||
"plaintext", | |||||
"pony", | |||||
"powershell", | |||||
"processing", | |||||
"profile", | |||||
"prolog", | |||||
"properties", | |||||
"protobuf", | |||||
"puppet", | |||||
"purebasic", | |||||
"python", | |||||
"q", | |||||
"qml", | |||||
"r", | |||||
"reasonml", | |||||
"rib", | |||||
"roboconf", | |||||
"routeros", | |||||
"rsl", | |||||
"ruby", | |||||
"ruleslanguage", | |||||
"rust", | |||||
"sas", | |||||
"scala", | |||||
"scheme", | |||||
"scilab", | |||||
"scss", | |||||
"shell", | |||||
"smali", | |||||
"smalltalk", | |||||
"sml", | |||||
"sqf", | |||||
"sql", | |||||
"stan", | |||||
"stata", | |||||
"step21", | |||||
"stylus", | |||||
"subunit", | |||||
"swift", | |||||
"taggerscript", | |||||
"tap", | |||||
"tcl", | |||||
"tex", | |||||
"thrift", | |||||
"tp", | |||||
"twig", | |||||
"typescript", | |||||
"vala", | |||||
"vbnet", | |||||
"vbscript", | |||||
"vbscript-html", | |||||
"verilog", | |||||
"vhdl", | |||||
"vim", | |||||
"x86asm", | |||||
"xl", | |||||
"xml", | |||||
"xquery", | |||||
"yaml", | |||||
"zephir", | |||||
] | |||||
) | |||||
with open(str(find("json/highlightjs-languages.json")), "r") as _hljs_languages_file: | |||||
_hljs_languages_data = json.load(_hljs_languages_file) | |||||
# set of languages ids that can be highlighted by highlight.js library | |||||
_hljs_languages = set(_hljs_languages_data["languages"]) | |||||
# languages aliases defined in highlight.js | # languages aliases defined in highlight.js | ||||
_hljs_languages_aliases = { | _hljs_languages_aliases = { | ||||
"ado": "stata", | **_hljs_languages_data["languages_aliases"], | ||||
"adoc": "asciidoc", | "ml": "ocaml", | ||||
"ahk": "autohotkey", | |||||
"aj": "aspectj", | |||||
"apacheconf": "apache", | |||||
"arm": "armasm", | |||||
"as": "actionscript", | |||||
"asc": "asciidoc", | |||||
"atom": "xml", | |||||
"bas": "basic", | |||||
"bat": "dos", | |||||
"bf": "brainfuck", | |||||
"bind": "dns", | |||||
"bsl": "1c", | "bsl": "1c", | ||||
"c-al": "cal", | |||||
"c": "cpp", | |||||
"c++": "cpp", | |||||
"capnp": "capnproto", | |||||
"cc": "cpp", | |||||
"clj": "clojure", | |||||
"cls": "cos", | |||||
"cmake.in": "cmake", | |||||
"cmd": "dos", | |||||
"coffee": "coffeescript", | |||||
"console": "shell", | |||||
"cr": "crystal", | |||||
"craftcms": "twig", | |||||
"crm": "crmsh", | |||||
"csharp": "cs", | |||||
"cson": "coffeescript", | |||||
"dcl": "clean", | |||||
"dfm": "delphi", | |||||
"do": "stata", | |||||
"docker": "dockerfile", | |||||
"dpr": "delphi", | |||||
"dst": "dust", | |||||
"dtsi": "dts", | |||||
"ep": "mojolicious", | "ep": "mojolicious", | ||||
"erl": "erlang", | |||||
"ex": "elixir", | |||||
"exs": "elixir", | |||||
"f90": "fortran", | |||||
"f95": "fortran", | |||||
"feature": "gherkin", | |||||
"freepascal": "delphi", | |||||
"fs": "fsharp", | |||||
"fsx": "fsharp", | |||||
"gemspec": "ruby", | |||||
"GML": "gml", | |||||
"gms": "gams", | |||||
"golang": "go", | |||||
"graph": "roboconf", | |||||
"gss": "gauss", | |||||
"gyp": "python", | |||||
"h": "cpp", | |||||
"h++": "cpp", | |||||
"hbs": "handlebars", | |||||
"hpp": "cpp", | |||||
"hs": "haskell", | |||||
"html": "xml", | |||||
"html.handlebars": "handlebars", | |||||
"html.hbs": "handlebars", | |||||
"https": "http", | |||||
"hx": "haxe", | |||||
"hylang": "hy", | |||||
"i7": "inform7", | |||||
"i7x": "inform7", | |||||
"iced": "coffeescript", | |||||
"icl": "clean", | |||||
"ino": "arduino", | |||||
"instances": "roboconf", | |||||
"ipynb": "json", | |||||
"irb": "ruby", | |||||
"jinja": "django", | |||||
"js": "javascript", | |||||
"jsp": "java", | |||||
"jsx": "javascript", | |||||
"k": "q", | |||||
"kdb": "q", | |||||
"kt": "kotlin", | |||||
"lassoscript": "lasso", | |||||
"lazarus": "delphi", | |||||
"lc": "livecode", | "lc": "livecode", | ||||
"lfm": "delphi", | |||||
"ll": "llvm", | |||||
"lpr": "delphi", | |||||
"ls": "livescript", | |||||
"m": "matlab", | |||||
"mak": "makefile", | |||||
"md": "markdown", | |||||
"mikrotik": "routeros", | |||||
"mips": "mipsasm", | |||||
"mk": "monkey", | |||||
"mkd": "markdown", | |||||
"mkdown": "markdown", | |||||
"ml": "ocaml", | |||||
"mli": "ocaml", | |||||
"mm": "objectivec", | |||||
"mma": "mathematica", | |||||
"moo": "mercury", | |||||
"moon": "moonscript", | |||||
"nav": "cal", | |||||
"nb": "mathematica", | |||||
"nc": "gcode", | |||||
"nginxconf": "nginx", | |||||
"ni": "inform7", | |||||
"nim": "nimrod", | |||||
"nixos": "nix", | |||||
"nsi": "nsis", | |||||
"obj-c": "objectivec", | |||||
"objc": "objectivec", | |||||
"osascript": "applescript", | |||||
"osl": "rsl", | |||||
"p": "parser3", | "p": "parser3", | ||||
"p21": "step21", | |||||
"pas": "delphi", | |||||
"pascal": "delphi", | |||||
"patch": "diff", | |||||
"pb": "purebasic", | |||||
"pbi": "purebasic", | |||||
"pcmk": "crmsh", | |||||
"pde": "processing", | "pde": "processing", | ||||
"pf.conf": "pf", | |||||
"php3": "php", | |||||
"php4": "php", | |||||
"php5": "php", | |||||
"php6": "php", | |||||
"php7": "php", | |||||
"pl": "perl", | |||||
"plist": "xml", | |||||
"pm": "perl", | |||||
"podspec": "ruby", | |||||
"postgres": "pgsql", | |||||
"postgresql": "pgsql", | |||||
"pp": "puppet", | |||||
"proto": "protobuf", | |||||
"ps": "powershell", | |||||
"ps1": "powershell", | |||||
"psd1": "powershell", | |||||
"psm1": "powershell", | |||||
"py": "python", | |||||
"qt": "qml", | |||||
"rb": "ruby", | |||||
"re": "reasonml", | |||||
"rei": "reasonml", | |||||
"rs": "rust", | |||||
"rsc": "routeros", | "rsc": "routeros", | ||||
"rss": "xml", | |||||
"rst": "nohighlight", | |||||
"s": "armasm", | "s": "armasm", | ||||
"SAS": "sas", | |||||
"scad": "openscad", | |||||
"sci": "scilab", | |||||
"scm": "scheme", | |||||
"sh": "bash", | |||||
"sig": "sml", | |||||
"sl": "rsl", | "sl": "rsl", | ||||
"st": "smalltalk", | |||||
"step": "step21", | |||||
"stp": "step21", | |||||
"styl": "stylus", | |||||
"sv": "verilog", | |||||
"svh": "verilog", | |||||
"tao": "xl", | |||||
"thor": "ruby", | |||||
"tk": "tcl", | |||||
"toml": "ini", | |||||
"ts": "typescript", | |||||
"txt": "nohighlight", | |||||
"v": "coq", | |||||
"vb": "vbnet", | |||||
"vbs": "vbscript", | |||||
"vhd": "vhdl", | |||||
"wildfly-cli": "jboss-cli", | |||||
"wl": "mathematica", | |||||
"wls": "mathematica", | |||||
"xhtml": "xml", | |||||
"xjb": "xml", | |||||
"xls": "excel", | |||||
"xlsx": "excel", | |||||
"xpath": "xquery", | |||||
"xpo": "axapta", | |||||
"xpp": "axapta", | |||||
"xq": "xquery", | |||||
"xqy": "xquery", | |||||
"xsd": "xml", | |||||
"xsl": "xml", | |||||
"YAML": "yaml", | |||||
"yml": "yaml", | |||||
"zep": "zephir", | |||||
"zone": "dns", | |||||
"zsh": "bash", | |||||
} | } | ||||
# dictionary mapping pygment lexers to hljs languages | # dictionary mapping pygment lexers to hljs languages | ||||
_pygments_lexer_to_hljs_language = {} # type: Dict[str, str] | _pygments_lexer_to_hljs_language = {} # type: Dict[str, str] | ||||
# dictionary mapping mime types to hljs languages | # dictionary mapping mime types to hljs languages | ||||
_mime_type_to_hljs_language = { | _mime_type_to_hljs_language = { | ||||
"text/x-c": "cpp", | "text/x-c": "c", | ||||
"text/x-c++": "cpp", | "text/x-c++": "cpp", | ||||
"text/x-msdos-batch": "dos", | "text/x-msdos-batch": "dos", | ||||
"text/x-lisp": "lisp", | "text/x-lisp": "lisp", | ||||
"text/x-shellscript": "bash", | "text/x-shellscript": "bash", | ||||
} | } | ||||
# dictionary mapping filenames to hljs languages | # dictionary mapping filenames to hljs languages | ||||
_filename_to_hljs_language = { | _filename_to_hljs_language = { | ||||
▲ Show 20 Lines • Show All 105 Lines • Show Last 20 Lines |