Ver Mensaje Individual
  #1 (permalink)  
Antiguo 06/03/2014, 21:01
minimal
 
Fecha de Ingreso: diciembre-2011
Mensajes: 408
Antigüedad: 12 años, 10 meses
Puntos: 18
hola crear shorcodes con python y pygments

hola estoy tratando de crear shorcodes con python y pygments + Markdown + BeautifulSoup pero no me anda me gustaria saber como hacerlo andar

Código Python:
Ver original
  1. # Pygments: http://pygments.org -- a generic syntax highlighter.
  2. from pygments import highlight
  3. from pygments.formatters import HtmlFormatter
  4. from pygments.lexers import get_lexer_by_name, guess_lexer
  5.  
  6. # Python Markdown (dropped in my project directory)
  7. from markdown import markdown
  8.  
  9. # BeautifulSoup: http://www.crummy.com/software/BeautifulSoup/
  10. from BeautifulSoup import BeautifulSoup
  11.  
  12. @register.filter
  13. def render(content, safe="unsafe"):
  14.     """Render this content for display."""
  15.  
  16.     # First, pull out all the <code></code> blocks, to keep them away
  17.     # from Markdown (and preserve whitespace).
  18.     soup = BeautifulSoup(unicode(content))
  19.     code_blocks = soup.findAll(u'code')
  20.     #for block in code_blocks:
  21.     #    block.replaceWith(u'<code class="removed"></code>')
  22.  
  23.     # Run the post through markdown.
  24.     if safe == u"unsafe":
  25.         safe_mode = False
  26.     else:
  27.         safe_mode = True
  28.     markeddown = markdown(unicode(soup), safe_mode=safe_mode)
  29.     # Replace the pulled code blocks with syntax-highlighted versions.
  30.     soup = BeautifulSoup(markeddown)
  31.     empty_code_blocks, index = soup.findAll(u'code', u'removed'), 0
  32.     print empty_code_blocks
  33.     formatter = HtmlFormatter(cssclass=u'source')
  34.     for block in code_blocks:
  35.         if block.has_key(u'class'):
  36.             # <code class='python'>python code</code>
  37.             language = block[u'class']
  38.         else:
  39.             # <code>plain text, whitespace-preserved</code>
  40.             language = u'text'
  41.         try:
  42.             lexer = get_lexer_by_name(language, stripnl=True, encoding=u'UTF-8')
  43.         except ValueError, e:
  44.             try:
  45.                 # Guess a lexer by the contents of the block.
  46.                 lexer = guess_lexer(block.renderContents())
  47.             except ValueError, e:
  48.                 # Just make it plain text.
  49.                 lexer = get_lexer_by_name(u'text', stripnl=True, encoding=u'UTF-8')
  50.        
  51.         #empty_code_blocks[index].replaceWith(
  52.         #        highlight(block.renderContents(), lexer, formatter))
  53.         index = index + 1
  54.  
  55.     return unicode(soup)

me dice error fuera de rango ademas lo quiero aplicar a un shortcode