Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/highlightjs.py
Show All 35 Lines | _hljs_languages = set([ | ||||
'stan', 'stata', 'step21', 'stylus', 'subunit', 'swift', 'taggerscript', | 'stan', 'stata', 'step21', 'stylus', 'subunit', 'swift', 'taggerscript', | ||||
'tap', 'tcl', 'tex', 'thrift', 'tp', 'twig', 'typescript', 'vala', | 'tap', 'tcl', 'tex', 'thrift', 'tp', 'twig', 'typescript', 'vala', | ||||
'vbnet', 'vbscript', 'vbscript-html', 'verilog', 'vhdl', 'vim', 'x86asm', | 'vbnet', 'vbscript', 'vbscript-html', 'verilog', 'vhdl', 'vim', 'x86asm', | ||||
'xl', 'xml', 'xquery', 'yaml', 'zephir' | 'xl', 'xml', 'xquery', 'yaml', 'zephir' | ||||
]) | ]) | ||||
# languages aliases defined in highlight.js | # languages aliases defined in highlight.js | ||||
_hljs_languages_aliases = { | _hljs_languages_aliases = { | ||||
vlorentz: Where is that list coming from? | |||||
Done Inline ActionsGenerated from the highlightjs language aliases and completed by hand. anlambert: Generated from the highlightjs language aliases and completed by hand. | |||||
Not Done Inline ActionsWould it be possible to send highlightjs a PR to add the aliases; and bundle their data file directly? vlorentz: Would it be possible to send highlightjs a PR to add the aliases; and bundle their data file… | |||||
'ado': 'stata', | 'ado': 'stata', | ||||
'adoc': 'asciidoc', | 'adoc': 'asciidoc', | ||||
'ahk': 'autohotkey', | 'ahk': 'autohotkey', | ||||
'aj': 'aspectj', | |||||
'apacheconf': 'apache', | 'apacheconf': 'apache', | ||||
'arm': 'armasm', | 'arm': 'armasm', | ||||
'as': 'actionscript', | 'as': 'actionscript', | ||||
'asc': 'angelscript', | 'asc': 'asciidoc', | ||||
'atom': 'xml', | 'atom': 'xml', | ||||
'bas': 'basic', | |||||
'bat': 'dos', | 'bat': 'dos', | ||||
'bf': 'brainfuck', | 'bf': 'brainfuck', | ||||
'bind': 'dns', | 'bind': 'dns', | ||||
'bsl': '1c', | |||||
'c-al': 'cal', | |||||
'c': 'cpp', | 'c': 'cpp', | ||||
'c++': 'cpp', | 'c++': 'cpp', | ||||
'capnp': 'capnproto', | 'capnp': 'capnproto', | ||||
'cc': 'cpp', | 'cc': 'cpp', | ||||
'clj': 'clojure', | 'clj': 'clojure', | ||||
'cls': 'cos', | 'cls': 'cos', | ||||
'cmake.in': 'cmake', | 'cmake.in': 'cmake', | ||||
'cmd': 'dos', | 'cmd': 'dos', | ||||
'coffee': 'coffeescript', | 'coffee': 'coffeescript', | ||||
'console': 'shell', | 'console': 'shell', | ||||
'cr': 'crystal', | 'cr': 'crystal', | ||||
'craftcms': 'twig', | 'craftcms': 'twig', | ||||
'crm': 'crmsh', | 'crm': 'crmsh', | ||||
'csharp': 'cs', | 'csharp': 'cs', | ||||
'cson': 'coffeescript', | 'cson': 'coffeescript', | ||||
'dcl': 'clean', | 'dcl': 'clean', | ||||
'dfm': 'delphi', | 'dfm': 'delphi', | ||||
'do': 'stata', | 'do': 'stata', | ||||
'docker': 'dockerfile', | 'docker': 'dockerfile', | ||||
'dpr': 'delphi', | 'dpr': 'delphi', | ||||
'dst': 'dust', | 'dst': 'dust', | ||||
'dtsi': 'dts', | |||||
'ep': 'mojolicious', | |||||
'erl': 'erlang', | 'erl': 'erlang', | ||||
'ex': 'elixir', | |||||
'exs': 'elixir', | |||||
'f90': 'fortran', | 'f90': 'fortran', | ||||
'f95': 'fortran', | 'f95': 'fortran', | ||||
'feature': 'gherkin', | 'feature': 'gherkin', | ||||
'freepascal': 'delphi', | 'freepascal': 'delphi', | ||||
'fs': 'fsharp', | 'fs': 'fsharp', | ||||
'fsx': 'fsharp', | |||||
'gemspec': 'ruby', | 'gemspec': 'ruby', | ||||
'GML': 'gml', | 'GML': 'gml', | ||||
'gms': 'gams', | 'gms': 'gams', | ||||
'golang': 'go', | 'golang': 'go', | ||||
'graph': 'roboconf', | 'graph': 'roboconf', | ||||
'gss': 'gauss', | 'gss': 'gauss', | ||||
'gyp': 'python', | 'gyp': 'python', | ||||
'h': 'cpp', | 'h': 'cpp', | ||||
'h++': 'cpp', | 'h++': 'cpp', | ||||
'hbs': 'handlebars', | 'hbs': 'handlebars', | ||||
'hpp': 'cpp', | 'hpp': 'cpp', | ||||
'hs': 'haskell', | 'hs': 'haskell', | ||||
'html': 'xml', | 'html': 'xml', | ||||
'html.handlebars': 'handlebars', | 'html.handlebars': 'handlebars', | ||||
'html.hbs': 'handlebars', | 'html.hbs': 'handlebars', | ||||
'https': 'http', | 'https': 'http', | ||||
'hx': 'haxe', | 'hx': 'haxe', | ||||
'hylang': 'hy', | 'hylang': 'hy', | ||||
'i7': 'inform7', | 'i7': 'inform7', | ||||
'i7x': 'inform7', | |||||
'iced': 'coffeescript', | 'iced': 'coffeescript', | ||||
'icl': 'clean', | 'icl': 'clean', | ||||
'ino': 'arduino', | |||||
'instances': 'roboconf', | 'instances': 'roboconf', | ||||
'ipynb': 'json', | 'ipynb': 'json', | ||||
'irb': 'ruby', | 'irb': 'ruby', | ||||
'jinja': 'django', | 'jinja': 'django', | ||||
'js': 'javascript', | 'js': 'javascript', | ||||
'jsp': 'java', | 'jsp': 'java', | ||||
'jsx': 'javascript', | 'jsx': 'javascript', | ||||
'k': 'q', | 'k': 'q', | ||||
'kdb': 'q', | 'kdb': 'q', | ||||
'kt': 'kotlin', | 'kt': 'kotlin', | ||||
'lassoscript': 'lasso', | 'lassoscript': 'lasso', | ||||
'lazarus': 'delphi', | 'lazarus': 'delphi', | ||||
'lc': 'livecode', | |||||
'lfm': 'delphi', | 'lfm': 'delphi', | ||||
'll': 'llvm', | |||||
'lpr': 'delphi', | 'lpr': 'delphi', | ||||
'ls': 'livescript', | 'ls': 'livescript', | ||||
'm': 'mercury', | 'm': 'matlab', | ||||
'mak': 'makefile', | 'mak': 'makefile', | ||||
'md': 'markdown', | 'md': 'markdown', | ||||
'mikrotik': 'routeros', | 'mikrotik': 'routeros', | ||||
'mips': 'mipsasm', | 'mips': 'mipsasm', | ||||
'mk': 'makefile', | 'mk': 'monkey', | ||||
'mkd': 'markdown', | 'mkd': 'markdown', | ||||
'mkdown': 'markdown', | 'mkdown': 'markdown', | ||||
'ml': 'ocaml', | 'ml': 'ocaml', | ||||
'mli': 'ocaml', | |||||
'mm': 'objectivec', | 'mm': 'objectivec', | ||||
'mma': 'mathematica', | 'mma': 'mathematica', | ||||
'moo': 'mercury', | 'moo': 'mercury', | ||||
'moon': 'moonscript', | 'moon': 'moonscript', | ||||
'nav': 'cal', | |||||
'nb': 'mathematica', | |||||
'nc': 'gcode', | 'nc': 'gcode', | ||||
'nginxconf': 'nginx', | 'nginxconf': 'nginx', | ||||
'ni': 'inform7', | |||||
'nim': 'nimrod', | 'nim': 'nimrod', | ||||
'nixos': 'nix', | 'nixos': 'nix', | ||||
'nsi': 'nsis', | |||||
'obj-c': 'objectivec', | 'obj-c': 'objectivec', | ||||
'objc': 'objectivec', | 'objc': 'objectivec', | ||||
'osascript': 'applescript', | 'osascript': 'applescript', | ||||
'osl': 'rsl', | |||||
'p': 'parser3', | |||||
'p21': 'step21', | 'p21': 'step21', | ||||
'pas': 'delphi', | 'pas': 'delphi', | ||||
'pascal': 'delphi', | 'pascal': 'delphi', | ||||
'patch': 'diff', | 'patch': 'diff', | ||||
'pb': 'purebasic', | 'pb': 'purebasic', | ||||
'pbi': 'purebasic', | 'pbi': 'purebasic', | ||||
'pcmk': 'crmsh', | 'pcmk': 'crmsh', | ||||
'pde': 'processing', | |||||
'pf.conf': 'pf', | 'pf.conf': 'pf', | ||||
'php3': 'php', | 'php3': 'php', | ||||
'php4': 'php', | 'php4': 'php', | ||||
'php5': 'php', | 'php5': 'php', | ||||
'php6': 'php', | 'php6': 'php', | ||||
'php7': 'php', | 'php7': 'php', | ||||
'pl': 'perl', | 'pl': 'perl', | ||||
'plist': 'xml', | 'plist': 'xml', | ||||
'pm': 'perl', | 'pm': 'perl', | ||||
'podspec': 'ruby', | 'podspec': 'ruby', | ||||
'postgres': 'pgsql', | 'postgres': 'pgsql', | ||||
'postgresql': 'pgsql', | 'postgresql': 'pgsql', | ||||
'pp': 'puppet', | 'pp': 'puppet', | ||||
'proto': 'protobuf', | |||||
'ps': 'powershell', | 'ps': 'powershell', | ||||
'ps1': 'powershell', | |||||
'psd1': 'powershell', | |||||
'psm1': 'powershell', | |||||
'py': 'python', | 'py': 'python', | ||||
'qt': 'qml', | 'qt': 'qml', | ||||
'rb': 'ruby', | 'rb': 'ruby', | ||||
're': 'reasonml', | 're': 'reasonml', | ||||
'rei': 'reasonml', | |||||
'rs': 'rust', | 'rs': 'rust', | ||||
'rsc': 'routeros', | |||||
'rss': 'xml', | 'rss': 'xml', | ||||
'rst': 'nohighlight', | 'rst': 'nohighlight', | ||||
's': 'armasm', | |||||
'SAS': 'sas', | 'SAS': 'sas', | ||||
'scad': 'openscad', | 'scad': 'openscad', | ||||
'sci': 'scilab', | 'sci': 'scilab', | ||||
'scm': 'scheme', | |||||
'sh': 'bash', | 'sh': 'bash', | ||||
'sig': 'sml', | |||||
'sl': 'rsl', | |||||
'st': 'smalltalk', | 'st': 'smalltalk', | ||||
'step': 'step21', | 'step': 'step21', | ||||
'stp': 'step21', | 'stp': 'step21', | ||||
'styl': 'stylus', | 'styl': 'stylus', | ||||
'sv': 'verilog', | 'sv': 'verilog', | ||||
'svh': 'verilog', | 'svh': 'verilog', | ||||
'tao': 'xl', | 'tao': 'xl', | ||||
'thor': 'ruby', | 'thor': 'ruby', | ||||
'tk': 'tcl', | 'tk': 'tcl', | ||||
'toml': 'ini', | 'toml': 'ini', | ||||
'ts': 'typescript', | 'ts': 'typescript', | ||||
'txt': 'nohighlight', | 'txt': 'nohighlight', | ||||
'v': 'verilog', | 'v': 'coq', | ||||
'vb': 'vbnet', | 'vb': 'vbnet', | ||||
'vbs': 'vbscript', | 'vbs': 'vbscript', | ||||
'vhd': 'vhdl', | |||||
'wildfly-cli': 'jboss-cli', | 'wildfly-cli': 'jboss-cli', | ||||
'wl': 'mathematica', | |||||
'wls': 'mathematica', | |||||
'xhtml': 'xml', | 'xhtml': 'xml', | ||||
'xjb': 'xml', | 'xjb': 'xml', | ||||
'xls': 'excel', | 'xls': 'excel', | ||||
'xlsx': 'excel', | 'xlsx': 'excel', | ||||
'xpath': 'xquery', | 'xpath': 'xquery', | ||||
'xpo': 'axapta', | |||||
'xpp': 'axapta', | |||||
'xq': 'xquery', | 'xq': 'xquery', | ||||
'xqy': 'xquery', | |||||
'xsd': 'xml', | 'xsd': 'xml', | ||||
'xsl': 'xml', | 'xsl': 'xml', | ||||
'YAML': 'yaml', | 'YAML': 'yaml', | ||||
'yml': 'yaml', | 'yml': 'yaml', | ||||
'zep': 'zephir', | 'zep': 'zephir', | ||||
'zone': 'dns', | 'zone': 'dns', | ||||
'zsh': 'bash' | 'zsh': 'bash' | ||||
} | } | ||||
# dictionary mapping pygment lexers to hljs languages | # dictionary mapping pygment lexers to hljs languages | ||||
_pygments_lexer_to_hljs_language = {} | _pygments_lexer_to_hljs_language = {} | ||||
# 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': 'cpp', | ||||
'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 = { | ||||
'cmakelists.txt': 'cmake' | 'cmakelists.txt': 'cmake', | ||||
'.htaccess': 'apache', | |||||
'httpd.conf': 'apache', | |||||
'access.log': 'accesslog', | |||||
'nginx.log': 'accesslog', | |||||
'resolv.conf': 'dns', | |||||
'dockerfile': 'docker', | |||||
'nginx.conf': 'nginx', | |||||
'pf.conf': 'pf' | |||||
} | } | ||||
# function to fill the above dictionaries | # function to fill the above dictionaries | ||||
def _init_pygments_to_hljs_map(): | def _init_pygments_to_hljs_map(): | ||||
if len(_pygments_lexer_to_hljs_language) == 0: | if len(_pygments_lexer_to_hljs_language) == 0: | ||||
for lexer in get_all_lexers(): | for lexer in get_all_lexers(): | ||||
lexer_name = lexer[0] | lexer_name = lexer[0] | ||||
Show All 21 Lines | def get_hljs_language_from_filename(filename): | ||||
Returns: | Returns: | ||||
highlight.js language id or None if no correspondence has been found | highlight.js language id or None if no correspondence has been found | ||||
""" | """ | ||||
_init_pygments_to_hljs_map() | _init_pygments_to_hljs_map() | ||||
if filename: | if filename: | ||||
filename_lower = filename.lower() | filename_lower = filename.lower() | ||||
if filename_lower in _filename_to_hljs_language: | if filename_lower in _filename_to_hljs_language: | ||||
return _filename_to_hljs_language[filename_lower] | return _filename_to_hljs_language[filename_lower] | ||||
if filename_lower in _hljs_languages: | |||||
return filename_lower | |||||
exts = filename_lower.split('.') | exts = filename_lower.split('.') | ||||
# check if file extension matches an hljs language | # check if file extension matches an hljs language | ||||
# also handle .ext.in cases | # also handle .ext.in cases | ||||
for ext in reversed(exts[-2:]): | for ext in reversed(exts[-2:]): | ||||
if ext in _hljs_languages: | if ext in _hljs_languages: | ||||
return ext | return ext | ||||
if ext in _hljs_languages_aliases: | if ext in _hljs_languages_aliases: | ||||
return _hljs_languages_aliases[ext] | return _hljs_languages_aliases[ext] | ||||
Show All 36 Lines |
Where is that list coming from?