Skip to content

Instantly share code, notes, and snippets.

@tungd
Created September 1, 2011 16:33
Show Gist options
  • Save tungd/1186583 to your computer and use it in GitHub Desktop.
Save tungd/1186583 to your computer and use it in GitHub Desktop.
Sample Cactus template tags support
from django import template
from django.utils.safestring import mark_safe
from django.utils.encoding import force_unicode
# pip install markdown
from markdown import markdown
register = template.Library()
class MarkdownNode(template.Node):
def __init__(self, nodelist):
self.nodelist = nodelist
def render(self, context):
value = self.nodelist.render(context)
try:
return mark_safe(markdown(value))
except ImportError:
return force_unicode(value)
@register.tag(name="markdown")
def markdown_tag(parser, token):
nodelist = parser.parse(("endmarkdown",))
parser.delete_first_token()
return MarkdownNode(nodelist)
{% extends "base.html" %}
{% block content %}
{% markdown %}
# Welcome to Cactus!
Taken from [Markdown Cheat Sheet](http://warpedvisions.org/projects/markdown-cheat-sheet/).
# Header 1 #
## Header 2 ##
### Header 3 ###
#### Header 4 ####
##### Header 5 #####
[Link back to H2](#id-goes-here)
This is a paragraph, which is text surrounded by whitespace. Paragraphs can be on one
line (or many), and can drone on for hours.
Here is a Markdown link to [Warped](http://warpedvisions.org), and a literal .
Now some SimpleLinks, like one to [google] (automagically links to are-you-
feeling-lucky), a [wiki: test] link to a Wikipedia page, and a link to
[foldoc: CPU]s at foldoc.
Now some inline markup like _italics_, **bold**, and `code()`. Note that underscores in
words are ignored in Markdown Extra.
![picture alt](/images/photo.jpeg "Title is optional")
> Blockquotes are like quoted text in email replies
>> And, they can be nested
* Bullet lists are easy too
- Another one
+ Another one
1. A numbered list
2. Which is numbered
3. With periods and a space
And now some code:
// Code is just text indented a bit
which(is_easy) to_remember();
Text with
two trailing spaces
(on the right)
can be used
for things like poems
### Horizontal rules
--------------------------
<div class="custom-class" markdown="1">
This is a div wrapping some Markdown plus. Without the DIV attribute, it ignores the
block.
</div>
{% endmarkdown %}
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment