Snippet List
Template filter to add the given number of tabs to the beginning of each line. Useful for keeping markup pretty, plays well with Markdown.
Usage:
{{ content|indent:"2" }}
{{ content|markdown|indent:"2" }}
- filter
- markup
- pretty
- indent
- indentation
This is a field that allows multiple markup types but also stores the pre-rendered result in the database which offers an advantage over calling one of the render methods each time. Example usage looks like:
class BlogPost(models.Model):
...
post = MarkupField()
the various extra fields can then be accessed as follows:
BlogPost.objects.get(pk=1).post # raw content
BlogPost.objects.get(pk=1).post_markup_type # markup type (plain text, html, markdown, rest, textile)
BlogPost.objects.get(pk=1).post_rendered # content of post rendered to html
BlogPost.objects.get(pk=1).post_as_html # property that access post_rendered but marked safe for easy use in templates
After writing my initial version of this I was pointed at the similar http://www.djangosnippets.org/snippets/1169/
I find mine a bit more useful as it includes ReST and includes a mark_safe call to allow showing the rendered HTML directly. I have however borrowed the nice idea of dynamically building MARKUP_TYPES from #1169.
Also available via http://gist.github.com/67724.
- fields
- rest
- markup
- markdown
- textile
- restructuredtext
### Simple wiki with MediaWiki and Markdown Support
Once you install the mwlib you can use mwit to convert Mediawiki markup to HTML. I am include the model that uses it to hopefully provide a good example. I maintain a version and only one copy of each wiki entry in the main table and archive replaced markup into another table, you will need to create the archive model or remove that section of code. The line ending changes in mwit are so that it will work with IE.
- markup
- markdown
- mediawiki
- wiki
- archive
Personally I hate using markdown for text input just so it can be converted into HTML. Markdown languages almost always don't support some thing I want to do; thus, why not just use HTML in the first place. Well because you don't want anybody posting any kind of HTML on your site.
Solution, instead of making your users learn markdown, let them enter HTML and filter out bad tags. This is a filter I use to filter HTML for only certain allowed tags. The allowed tags can be configured with the **allowedhtml** list.
To make your text input even more user friendly use a Javascript HTML editor like [FCK Editor](http://www.fckeditor.net/) so your users will have a nice GUI editor.
- template
- filter
- markup
- markdown
- html
I updated [MarkdownTextField](http://www.djangosnippets.org/snippets/882/) to have some choices in markup. It currently support for Markdown, Textile, Plain Text, and Plain HTML. It will add `%s_html` for the complied HTML and `%s_markup_choices` for a drop down of markup choices.
Usage:
class MyModel(models.Model):
description = MarkupTextField()
- model
- markup
- markdown
- textile
- field
I wanted to have the possibility to use a wiki-like markup style in my flatpages for various purposes (embedding images, quoting, etc.)
After a few dead ends I came up with this, which is quite nice I think.
> It basically takes a named tag, loads the corresponding template, passes in all arguments, renders the template and replaces the named tag with the result.
*The markup looks like this:*
> [[example:value to pass|option1=somevalue option2=values can have spaces too! without having to put them in quotes option3=some other value]]
*This results in:*
* Filter tries to load wiki/wiki_example.html
* If it is loaded, it passes an WikiElement containing the value and the options to the template, renders it and replaces the tag with the rendered template
*In the "wiki/wiki_example.html" template you can use it like this:*
{{param.value}}
{{param.opts.option1}}
Or loop over param.opts.iteritems.
- template
- filter
- markup
- wiki
Based heavily on [snippet #119](/snippets/119/), this is an all-in-one function which applies Markdown and typogrify, and adds Pygments highlighting (selected from a class name or by having Pygments guess the language) to any `<code>` elements found in the text.
It also adds some niceties for picking up useful arguments to Markdown and Pygments, and registers itself as a markup filter with the `template_utils` formatter.
Requirements:
* [BeautifulSoup](http://www.crummy.com/software/BeautifulSoup/)
* [Pygments](http://pygments.org/)
* [python-markdown](http://www.freewisdom.org/projects/python-markdown/)
* [template_utils](http://code.google.com/p/django-template-utils/)
* [typogrify](http://code.google.com/p/typogrify/)
- pygments
- markup
- markdown
- typogrify
This method lets you define your markup language and then processes your entries and puts the HTML output in another field on your database.
I came from a content management system that worked like this and to me it makes sense. Your system doesn't have to process your entry every time it has to display it. You would just call the "*_html" field in your template.
Requires:
Django .96
[Markdown](http://cheeseshop.python.org/pypi/Markdown/1.6 "Python Package Index: Markdown")
[Textile](http://cheeseshop.python.org/pypi/textile "Python Package Index: Textile")
- admin
- model
- markup
- markdown
- textile
Ever since django.contrib.markup appeared I've added a `markup_lang` field to my models where I want to support multiple input formats. This filter lets you pass the filter name as a string (from your model field, for example) and it will call the appropriate filter. I use None when the text is HTML, in which case it will return as-is.
Example:
class Article(models.Model):
content = models.TextField(null=False, default="")
markup_lang = models.CharField(maxlength=20, blank=True)
a = Article(content="**Test!**", markup_lang='textile')
b = Article(content="<h1>Hello!</h1>")
And in a template:
{% for article in article_list %}
{{ article.content|render_markup:article.markup_lang }}
{% endfor %}
I'm a big fan of Markdown, and often set up models to automatically apply it to certain fields before saving. But that's not really flexible, because if I then distribute the code someone else might want to use reStructuredText or Textile or whatever, and then they have to hack my code.
So here's a function which looks for a setting called `MARKUP_FILTER` and, based on what it finds there (see the docstring for what it looks at), chooses a text-to-HTML conversion function and applies it to a piece of text. Since Textile, Markdown and reStructuredText all support various useful options, it has the ability to pick up arbitrary keyword arguments and pass them through.
- markup
- markdown
- textile
- restructuredtext
13 snippets posted so far.