No Lexer For Alias Found

3.5k Views Asked by At

I'm attempting to use Pygments and Beautiful Soup as a code highlighting solution for the blog software I'm building for Google App Engine.

How it works is that my HTML posts will use pre tags to identify code. Like this:

<p>Check out this cool code example<p>    
<pre class="python">
        import this
        def demo():
            pass</pre>

BeautifulSoup captures the part between the pre tag and passes it to Pygments. Pygments is suppose to check the class value and apply the correct lexar. Pygments then applies the formatting and replaces the original text with the formatted text. The solution is explained in more detail at SaltyCrane's Blog.

The Error

ClassNotFound: no lexer for alias [u'python'] found

Perhaps I just haven't imported the module correctly?

The code

from google.appengine.ext import db
import fix_path
import bs4
import pygments
from bs4 import BeautifulSoup
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter


    def formatter(p):
        soup = BeautifulSoup(p.otext)
        preblocks = soup.findAll('pre')
        for pre in preblocks:
            if pre.has_key('class'):
                code = ''.join([unicode(item) for item in pre.contents])
                code = unescape_html(code)
                lexer = lexers.get_lexer_by_name(pre['class'])
                formatter = formatters.HtmlFormatter()
                code_hl = highlight(code, lexer, formatter)
                pre.replaceWith(BeautifulSoup(code_hl))
            else:
                print "No Go"
        return unicode(soup)

The traceback

Traceback (most recent call last):
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "C:\Users\john\webdev\workspace\bsby\views.py", line 45, in post
    p.ftext = utils.formatter(p)
  File "C:\Users\john\webdev\workspace\bsby\utils.py", line 18, in formatter
    lexer = lexers.get_lexer_by_name(pre['class'])
  File "C:\Users\john\webdev\workspace\bsby\lib\pygments\lexers\__init__.py", line 80, in get_lexer_by_name
    raise ClassNotFound('no lexer for alias %r found' % _alias)
ClassNotFound: no lexer for alias [u'python'] found
1

There are 1 best solutions below

0
On BEST ANSWER

Yep, that was the issue. I had not imported the libraries I needed for the script to run. Here is the correct code.

import fix_path
import bs4
import pygments
from bs4 import BeautifulSoup
from pygments import highlight
from pygments import lexers
from pygments import formatters


    def formatter(p):
        soup = BeautifulSoup(p.otext)
        preblocks = soup.findAll('pre')
        for pre in preblocks:
            if pre.has_key('class'):
                code = ''.join([unicode(item) for item in pre.contents])
                lexer = lexers.get_lexer_by_name("python")
                formatter = formatters.HtmlFormatter()
                code_hl = highlight(code, lexer, formatter)
                pre.replaceWith(BeautifulSoup(code_hl))
                return unicode(soup)
            else:
                return null

The is one other issue....

I want to pull the lexar name dynamically like this...

lexer = lexers.get_lexer_by_name(pre['class'])

However, when I do that I get the following error.

ClassNotFound: no lexer for alias [u'python'] found

If I do

lexer = lexers.get_lexer_by_name("python")

it works, but only for python.