##// END OF EJS Templates
vcs: refactor get_lexer for nodes so it can be used in external code.
marcink -
r1357:3b528eef default
parent child Browse files
Show More
@@ -446,25 +446,23 b' class FileNode(Node):'
446 def mimetype_main(self):
446 def mimetype_main(self):
447 return self.mimetype.split('/')[0]
447 return self.mimetype.split('/')[0]
448
448
449 @LazyProperty
449 @classmethod
450 def lexer(self):
450 def get_lexer(cls, filename, content=None):
451 """
452 Returns pygment's lexer class. Would try to guess lexer taking file's
453 content, name and mimetype.
454 """
455 from pygments import lexers
451 from pygments import lexers
456
452
453 extension = filename.split('.')[-1]
457 lexer = None
454 lexer = None
455
458 try:
456 try:
459 lexer = lexers.guess_lexer_for_filename(
457 lexer = lexers.guess_lexer_for_filename(
460 self.name, self.content, stripnl=False)
458 filename, content, stripnl=False)
461 except lexers.ClassNotFound:
459 except lexers.ClassNotFound:
462 lexer = None
460 lexer = None
463
461
464 # try our EXTENSION_MAP
462 # try our EXTENSION_MAP
465 if not lexer:
463 if not lexer:
466 try:
464 try:
467 lexer_class = LANGUAGES_EXTENSIONS_MAP.get(self.extension)
465 lexer_class = LANGUAGES_EXTENSIONS_MAP.get(extension)
468 if lexer_class:
466 if lexer_class:
469 lexer = lexers.get_lexer_by_name(lexer_class[0])
467 lexer = lexers.get_lexer_by_name(lexer_class[0])
470 except lexers.ClassNotFound:
468 except lexers.ClassNotFound:
@@ -476,6 +474,14 b' class FileNode(Node):'
476 return lexer
474 return lexer
477
475
478 @LazyProperty
476 @LazyProperty
477 def lexer(self):
478 """
479 Returns pygment's lexer class. Would try to guess lexer taking file's
480 content, name and mimetype.
481 """
482 return self.get_lexer(self.name, self.content)
483
484 @LazyProperty
479 def lexer_alias(self):
485 def lexer_alias(self):
480 """
486 """
481 Returns first alias of the lexer guessed for this file.
487 Returns first alias of the lexer guessed for this file.
General Comments 0
You need to be logged in to leave comments. Login now