Added Code Chunks support

Currently with inline style sheets. That will definitely change soon.
This commit is contained in:
2013-10-02 21:49:55 +02:00
parent 61a60f7cf7
commit 1765929555
9 changed files with 211 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
from django.db import models
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify
from taggit.managers import TaggableManager
class Post(models.Model):
@@ -19,3 +20,18 @@ class Post(models.Model):
self.slug = slugify(self.title)
super(Post, self).save(*args, **kwargs)
class CodeChunk(models.Model):
language = models.CharField(max_length = 20)
created_at = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length = 100)
slug = models.SlugField(editable = False, max_length = 100)
description = models.TextField(blank=True)
content = models.TextField()
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.title)
super(CodeChunk, self).save(*args, **kwargs)

46
blog/static/css/code.css Normal file
View File

@@ -0,0 +1,46 @@
/*
Document : code
Created on : 2012.09.04., 10:05:47
Author : polonkai.gergely
Description:
Purpose of the stylesheet follows.
*/
.code-chunk {
background-color: #b5b5b5;
padding: 10px;
}
.code-chunk .code-title {
text-indent: 0 !important;
font-size: 120%;
font-weight: bold;
}
.code-chunk .code-description {
border: 1px solid #333;
background-color: #d9d9d9;
padding: 3px;
text-indent: 0 !important;
margin: .5em 0 0 0 !important;
font-size: 75%;
color: #444;
}
.code-chunk .code {
font-family: monospace;
background-color: #002b36;
padding: 5px;
height: 300px;
overflow: auto;
}
.code-chunk .code * {
font-family: monospace;
}
.code-chunk .code ol {
background-color: #002b36;
color: #586e75;
}

View File

@@ -0,0 +1,15 @@
{% extends 'front_base.html' %}
{% load code_chunks %}
{% block content %}
<div class="code-chunk">
<p class="code-title">{{ codechunk.title }}</p>
{{ codechunk.content|syhilite:codechunk.language }}
{% if codechunk.description %}
<div class="code-description">
{{ codechunk.description|safe }}
</div>
{% endif %}
</div>
{% endblock %}

View File

View File

@@ -0,0 +1,36 @@
from django import template
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe
import logging
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from gergelypolonkaiweb.solarized_dark import SolarizedDarkStyle
register = template.Library()
class CodeFormatter(HtmlFormatter):
def wrap(self, source, outfile):
return self._wrap_code(source)
def _wrap_code(self, source):
yield 0, "<pre class=\"" + self.cssclass + "\"><ol>"
for i, t in source:
if i == 1:
t = "<li>" + t + "</li>"
yield i, t
yield 0, "</ol></pre>"
@register.filter(needs_autoescape=True)
@stringfilter
def syhilite(value, language, autoescape=None):
if language == "php":
value = "<?php\n" + value
lexer = get_lexer_by_name(language)
formatter = CodeFormatter(style = SolarizedDarkStyle, linenos = False, cssclass = language + " code", noclasses = True)
html = highlight(value, lexer, formatter)
css = formatter.get_style_defs(['.code-chunk .code'])
return mark_safe(html)

View File

@@ -7,5 +7,6 @@ urlpatterns = patterns('',
url(r'^tag/(?P<tag>[^/]+)$', views.taglist, name='taglist'),
url(r'^tag/(?P<tag>[^/]+)/page/(?P<page>\d+)$', views.tagpage, name='tagpage'),
url(r'^(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/(?P<slug>[^/]+)$', views.read, name='read'),
url(r'^code-chunk/(?P<language>[^/]+)/(?P<slug>[^/]+)$', views.codechunk, name='codechunk'),
url(r'^feed$', views.feed, name='feed'),
)

View File

@@ -1,7 +1,7 @@
import datetime
from django.shortcuts import render, get_object_or_404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from blog.models import Post
from blog.models import Post, CodeChunk
def mainpage(request):
last_posts = Post.objects.filter(draft = False).order_by('-created_at')[:5]
@@ -42,6 +42,10 @@ def read(request, year, month, day, slug):
post = get_object_or_404(Post, created_at__year=int(year), created_at__month=int(month), created_at__day=int(day), slug=slug, draft=False);
return render(request, 'blog/view.html', {'post': post})
def codechunk(request, language, slug):
chunk = get_object_or_404(CodeChunk, language=language, slug=slug)
return render(request, 'blog/code-chunk.html', {'codechunk': chunk})
def feed(request):
return render(request, 'blog/feed.xml', {})