Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a library that will detect the source code language of a block of code? [closed]

Tags:

Writing a python script and it needs to find out what language a block of code is written in. I could easily write this myself, but I'd like to know if a solution already exists.

Pygments is insufficient and unreliable.

like image 300
andrewrk Avatar asked Nov 28 '08 06:11

andrewrk


2 Answers

Pygments can guess too. Here is an example from the documentation:

>>> from pygments.lexers import guess_lexer, guess_lexer_for_filename  >>> guess_lexer('#!/usr/bin/python\nprint "Hello World!"') <pygments.lexers.PythonLexer>  >>> guess_lexer_for_filename('test.py', 'print "Hello World!"') <pygments.lexers.PythonLexer> 
like image 185
Ali Afshar Avatar answered Dec 18 '22 20:12

Ali Afshar


I guess you should try what this very site uses: google-code-prettify (from this question)

[EDIT]J.F. Sebastian pointed me to Pygments (see this answer)

like image 37
Aaron Digulla Avatar answered Dec 18 '22 20:12

Aaron Digulla