auxiliary extension map for when no language can be found, not to overwrite detected language

This commit is contained in:
Alan Hamlett 2015-06-12 17:25:04 -07:00
parent 2880add4c5
commit 368fecfcf1

View file

@ -26,7 +26,7 @@ from pygments.lexers import guess_lexer, guess_lexer_for_filename
log = logging.getLogger('WakaTime') log = logging.getLogger('WakaTime')
# force file name extensions to be recognized as a certain language # extensions taking priority over lexer
EXTENSIONS = { EXTENSIONS = {
'j2': 'HTML', 'j2': 'HTML',
'markdown': 'Markdown', 'markdown': 'Markdown',
@ -34,6 +34,8 @@ EXTENSIONS = {
'mdown': 'Markdown', 'mdown': 'Markdown',
'twig': 'Twig', 'twig': 'Twig',
} }
# lexers to human readable languages
TRANSLATIONS = { TRANSLATIONS = {
'CSS+Genshi Text': 'CSS', 'CSS+Genshi Text': 'CSS',
'CSS+Lasso': 'CSS', 'CSS+Lasso': 'CSS',
@ -45,6 +47,11 @@ TRANSLATIONS = {
'RHTML': 'HTML', 'RHTML': 'HTML',
} }
# extensions for when no lexer is found
AUXILIARY_EXTENSIONS = {
'vb': 'VB.net',
}
def guess_language(file_name): def guess_language(file_name):
"""Guess lexer and language for a file. """Guess lexer and language for a file.
@ -58,11 +65,17 @@ def guess_language(file_name):
# guess language from file extension # guess language from file extension
if file_name: if file_name:
language = guess_language_from_extension(file_name.rsplit('.', 1)[-1]) language = get_language_from_extension(file_name, EXTENSIONS)
# get language from lexer if we didn't have a hard-coded extension rule # get language from lexer if we didn't have a hard-coded extension rule
if language is None and lexer: if language is None and lexer:
language = translate_language(u(lexer.name)) language = u(lexer.name)
if language is None:
language = get_language_from_extension(file_name, AUXILIARY_EXTENSIONS)
if language is not None:
language = translate_language(language)
return language, lexer return language, lexer
@ -104,15 +117,18 @@ def smart_guess_lexer(file_name):
return lexer return lexer
def guess_language_from_extension(extension): def get_language_from_extension(file_name, extension_map):
"""Checks hard-coded extension map for a matching language. """Returns a matching language for the given file_name using extension_map.
""" """
extension = file_name.rsplit('.', 1)[-1] if len(file_name.rsplit('.', 1)) > 1 else None
if extension: if extension:
if extension in EXTENSIONS: if extension in extension_map:
return EXTENSIONS[extension] return extension_map[extension]
if extension.lower() in EXTENSIONS: if extension.lower() in extension_map:
return EXTENSIONS[extension.lower()] return extension_map[extension.lower()]
return None return None