limit language detection to known file extensions, unless file contents has a vim modeline

This commit is contained in:
Alan Hamlett 2015-06-15 18:26:10 -07:00
parent 368fecfcf1
commit 6591c4e5e9

View file

@ -20,7 +20,8 @@ if sys.version_info[0] == 2:
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments_py2'))
else:
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments_py3'))
from pygments.lexers import guess_lexer, guess_lexer_for_filename
from pygments.lexers import get_lexer_by_name, guess_lexer_for_filename
from pygments.modeline import get_filetype_from_buffer
log = logging.getLogger('WakaTime')
@ -92,11 +93,11 @@ def smart_guess_lexer(file_name):
text = get_file_contents(file_name)
try:
guess_1 = guess_lexer(text)
guess_1 = guess_lexer_for_filename(file_name, text)
except:
guess_1 = None
try:
guess_2 = guess_lexer_for_filename(file_name, text)
guess_2 = guess_lexer_using_modeline(text)
except:
guess_2 = None
try:
@ -132,6 +133,21 @@ def get_language_from_extension(file_name, extension_map):
return None
def guess_lexer_using_modeline(text):
"""Guess lexer for given text using Vim modeline.
"""
file_type = get_filetype_from_buffer(text)
if file_type is not None:
try:
return get_lexer_by_name(file_type)
except:
pass
return None
def translate_language(language):
"""Turns Pygments lexer class name string into human-readable language.
"""