Login

Tag "beautifulsoup"

Snippet List

urlize HTML

The default Django urlize filter does not work with html nicely so here I've used an HTML parser [BeautifulSoup](http://www.crummy.com/software/BeautifulSoup/) to quickly search through each text node and run the django urlize filter on it. Optimizations could be made to include a regex in the soup.findAll() method's text argument to only search those text nodes which matched a url pattern. You could also modify the method to convert the text to urls, such as using your own custom url filter.

  • beautifulsoup
  • html
  • urlize
  • parser
  • html parser
Read More

Code syntax highlighting templatetag

Replaces <code> blocks with syntax highlighted code. Use CSS to actually get the colours you want, look at pygments documentation for extracting css for various styles. This snippet has the advantage of falling back on <pre> if anything goes wrong, and attempting to guess the syntax of code, falling back on python.

  • templatetag
  • pygments
  • highlighting
  • beautifulsoup
  • templatetags
  • syntax
  • syntax-highlightin
Read More

Revisiting Pygments and Markdown

A variation on a theme, inspired by [snippet 39][39] and [snippet 119][119]. The intent is to provide a more generic and simple mechanism for combining [Markdown][markdown] with [Pygments][pygments]. Common scenarios could include blogging or commenting. Snippet 119 seemed too specific and perhaps not as efficient, needing to process the HTML twice to accomplish it's ends. The one snag in the implementation is the need to use a tag other than `code` as a wrapper. See the comments for details. You will need the [BeautifulSoup][soup] module installed. Sample usage: from django.db import models class Blog(models.Model): '''Bare bones blogging model''' title = models.CharField(maxlength=255) slug = models.SlugField(maxlength=255, prepopulate_from=('title',)) pub_date = models.DateTimeField() # the cooked view, cached for quick retrieval blog = models.TextField() # the raw markdown-encoded text, saved for subsequent edits markdown = models.TextField() def save(self): from datetime import datetime if not self.id and not self.pub_date: self.pub_date = datetime.now() self.blog = pygmented_markdown(self.markdown) super(Blog, self).save() [39]: http://www.djangosnippets.org/snippets/39/ [119]: http://www.djangosnippets.org/snippets/119/ [soup]: http://www.crummy.com/software/BeautifulSoup/ [markdown]: http://www.freewisdom.org/projects/python-markdown/Installation [pygments]: http://pygments.org/

  • pygments
  • beautifulsoup
  • markdown
Read More

Find all links in a value and display them separatley

This is a simple filter I use to display a list of links from a blog entry off in the sidebar ([example](http://www2.jeffcroft.com/blog/2007/feb/25/two-new-django-sites-both-source-available/)). Requires beautifulsoup. Originally by [Nathan Borror](http://playgroundblues.com), tweaked slightly by me.

  • template
  • filter
  • beautifulsoup
Read More

Parsing and Highlighting &lt;code&gt; Blocks

This function takes a string (most likely from a template), searches it for `<code>[...]</code>`, highlights it with Pygments, and returns the entire thing back, as a string. (Note: the `<code>[...]</code>` must have a class corresponding to the language inside. If it lacks the class, then it's silently ignored.)

  • pygments
  • beautifulsoup
Read More

5 snippets posted so far.