From 0df159ae92095f789bc8f14a7dee47421f4f1474 Mon Sep 17 00:00:00 2001 From: Nick Sergeant Date: Mon, 23 Apr 2012 14:34:21 -0400 Subject: [PATCH] Markdown support. --- media/css/style.css | 11 ++++++++++ media/css/style.scss | 13 +++++++++++ settings.py | 1 + snipts/models.py | 22 ++++++++++++++----- .../templates/snipts/snipt-js-template.html | 8 ++++++- snipts/templates/snipts/snipt-list.html | 8 ++++++- snipts/templatetags/snipt_tags.py | 6 ++++- 7 files changed, 61 insertions(+), 8 deletions(-) diff --git a/media/css/style.css b/media/css/style.css index 7356d04..2701fdb 100644 --- a/media/css/style.css +++ b/media/css/style.css @@ -511,6 +511,11 @@ article.snipt div.container section.code a.expand:focus { article.snipt div.container section.code textarea.raw { display: none; } +article.snipt div.container section.code div.markdown { + margin: 0 15px; + min-height: 173px; + padding: 13px 0 4px 0; +} article.snipt div.container section.emacs a.expand, article.snipt div.container section.default a.expand { -webkit-box-shadow: 0 -25px 25px #f8f8f8; -moz-box-shadow: 0 -25px 25px #f8f8f8; @@ -562,6 +567,9 @@ article.snipt div.container:after { width: 318px; z-index: 51; } +article.snipt div.container div.markdown h1, article.snipt div.container div.markdown h2, article.snipt div.container div.markdown h3, article.snipt div.container div.markdown h4, article.snipt div.container div.markdown h5, article.snipt div.container div.markdown h6 { + margin-bottom: 9px; +} article.snipt aside { float: right; margin: 23px 30px 0 0; @@ -695,6 +703,9 @@ article.snipt div.expanded section.code { article.snipt div.expanded section.code div.highlight pre { padding-bottom: 60px; } +article.snipt div.expanded section.code div.markdown { + padding-bottom: 52px; +} article.snipt div.expanded section.code a.expand { background-image: url("/static/images/collapse.png"); -webkit-box-shadow: none; diff --git a/media/css/style.scss b/media/css/style.scss index 92e83e6..e35200b 100644 --- a/media/css/style.scss +++ b/media/css/style.scss @@ -586,6 +586,11 @@ article.snipt { textarea.raw { display: none; } + div.markdown { + margin: 0 15px; + min-height: 173px; + padding: 13px 0 4px 0; + } } section.emacs, section.default { a.expand { @@ -638,6 +643,11 @@ article.snipt { width: 318px; z-index: 51; } + div.markdown { + h1, h2, h3, h4, h5, h6 { + margin-bottom: 9px; + } + } } aside { float: right; @@ -787,6 +797,9 @@ article.snipt { padding-bottom: 60px; } } + div.markdown { + padding-bottom: 52px; + } a.expand { background-image: url('/static/images/collapse.png'); -webkit-box-shadow: none; diff --git a/settings.py b/settings.py index cf72503..35faf25 100644 --- a/settings.py +++ b/settings.py @@ -144,6 +144,7 @@ INSTALLED_APPS = ( 'compressor', 'django_bcrypt', 'haystack', + 'markdown_deux', 'pagination', 'postmark', 'registration', diff --git a/snipts/models.py b/snipts/models.py index 7ed2259..d03a846 100644 --- a/snipts/models.py +++ b/snipts/models.py @@ -6,6 +6,7 @@ from django.db import models from taggit.managers import TaggableManager from taggit.utils import edit_string_for_tags +from markdown_deux import markdown from pygments import highlight from pygments.lexers import get_lexer_by_name from pygments.formatters import HtmlFormatter @@ -46,13 +47,21 @@ class Snipt(models.Model): if not self.key: self.key = md5.new(self.slug).hexdigest() - self.stylized = highlight(self.code, - get_lexer_by_name(self.lexer, encoding='UTF-8'), - HtmlFormatter()) + if self.lexer == 'markdown': + self.stylized = markdown(self.code, 'default') + else: + self.stylized = highlight(self.code, + get_lexer_by_name(self.lexer, encoding='UTF-8'), + HtmlFormatter()) self.line_count = len(self.code.split('\n')) + if self.lexer == 'markdown': + lexer_for_embedded = 'text' + else: + lexer_for_embedded = self.lexer + embedded = highlight(self.code, - get_lexer_by_name(self.lexer, encoding='UTF-8'), + get_lexer_by_name(lexer_for_embedded, encoding='UTF-8'), HtmlFormatter( style='native', noclasses=True, @@ -113,7 +122,10 @@ class Snipt(models.Model): @property def lexer_name(self): - return get_lexer_by_name(self.lexer).name + if self.lexer == 'markdown': + return 'Markdown' + else: + return get_lexer_by_name(self.lexer).name class Favorite(models.Model): snipt = models.ForeignKey(Snipt) diff --git a/snipts/templates/snipts/snipt-js-template.html b/snipts/templates/snipts/snipt-js-template.html index 13fa852..d83cc58 100644 --- a/snipts/templates/snipts/snipt-js-template.html +++ b/snipts/templates/snipts/snipt-js-template.html @@ -10,7 +10,13 @@

<% if (snipt.title) { %><%= snipt.title %><% } else { %>Untitled <% } %>

- <%= snipt.stylized %> + <% if (snipt.lexer == 'markdown') { %> +
+ <%= snipt.stylized %> +
+ <% } else { %> + <%= snipt.stylized %> + <% } %> <% if (snipt.line_count > 8 && !window.detail) { %> Expand diff --git a/snipts/templates/snipts/snipt-list.html b/snipts/templates/snipts/snipt-list.html index fdc00ad..417d992 100644 --- a/snipts/templates/snipts/snipt-list.html +++ b/snipts/templates/snipts/snipt-list.html @@ -11,7 +11,13 @@

{% if snipt.title %}{{ snipt.title }}{% else %}Untitled{% endif %}

- {{ snipt.stylized|safe }} + {% if snipt.lexer == 'markdown' %} +
+ {{ snipt.stylized|safe }} +
+ {% else %} + {{ snipt.stylized|safe }} + {% endif %} {% if snipt.line_count > 8 and not detail %} Expand diff --git a/snipts/templatetags/snipt_tags.py b/snipts/templatetags/snipt_tags.py index a5d53e0..feccdae 100644 --- a/snipts/templatetags/snipt_tags.py +++ b/snipts/templatetags/snipt_tags.py @@ -45,7 +45,11 @@ def snipts_count_for_user(context): @tag(register, [Constant('as'), Variable()]) def get_lexers(context, asvar): - context[asvar] = sorted(get_all_lexers()) + lexers = list(get_all_lexers()) + lexers.append(('Markdown', ('markdown',),)) + lexers = sorted(lexers) + + context[asvar] = lexers return ''