Added Code Chunks support
Currently with inline style sheets. That will definitely change soon.
This commit is contained in:
parent
61a60f7cf7
commit
1765929555
@ -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
46
blog/static/css/code.css
Normal 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;
|
||||
}
|
||||
|
15
blog/templates/blog/code-chunk.html
Normal file
15
blog/templates/blog/code-chunk.html
Normal 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 %}
|
0
blog/templatetags/__init__.py
Normal file
0
blog/templatetags/__init__.py
Normal file
36
blog/templatetags/code_chunks.py
Normal file
36
blog/templatetags/code_chunks.py
Normal 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)
|
||||
|
@ -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'),
|
||||
)
|
||||
|
@ -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', {})
|
||||
|
||||
|
91
gergelypolonkaiweb/solarized_dark.py
Normal file
91
gergelypolonkaiweb/solarized_dark.py
Normal file
@ -0,0 +1,91 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
pygments.styles.solarized_dark
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Solarized style. See http://ethanschoonover.com/solarized for details.
|
||||
Solarized is created by Ethan Schoonover.
|
||||
"""
|
||||
|
||||
from pygments.style import Style
|
||||
from pygments.token import Comment, Error, Generic, Keyword, Literal, Name, Number, Operator, Other, Punctuation, String, Text, Token, Whitespace
|
||||
|
||||
class SolarizedDarkStyle(Style):
|
||||
"""
|
||||
Solarized style.
|
||||
"""
|
||||
|
||||
default_style = ''
|
||||
|
||||
styles = {
|
||||
Comment: '#586e75',
|
||||
Comment.Multiline: '#586e75',
|
||||
Comment.Preproc: '#586e75',
|
||||
Comment.Single: '#586e75',
|
||||
Comment.Special: '#586e75',
|
||||
Error: '',
|
||||
Generic: '',
|
||||
Generic.Deleted: '',
|
||||
Generic.Emph: '',
|
||||
Generic.Error: '',
|
||||
Generic.Heading: '',
|
||||
Generic.Inserted: '',
|
||||
Generic.Output: '',
|
||||
Generic.Prompt: '',
|
||||
Generic.Strong: '',
|
||||
Generic.Subheading: '',
|
||||
Generic.Traceback: '',
|
||||
Keyword: '#b58900',
|
||||
Keyword.Constant: '',
|
||||
Keyword.Declaration: '',
|
||||
Keyword.Namespace: '',
|
||||
Keyword.Pseudo: '',
|
||||
Keyword.Reserved: '',
|
||||
Keyword.Type: '',
|
||||
Literal: '',
|
||||
Literal.Date: '',
|
||||
Name: '',
|
||||
Name.Attribute: '',
|
||||
Name.Builtin: '#2aa198',
|
||||
Name.Builtin.Pseudo: '',
|
||||
Name.Class: '',
|
||||
Name.Constant: '',
|
||||
Name.Decorator: '',
|
||||
Name.Entity: '',
|
||||
Name.Exception: '',
|
||||
Name.Function: '#93a1a1',
|
||||
Name.Label: '',
|
||||
Name.Namespace: '',
|
||||
Name.Other: '',
|
||||
Name.Property: '',
|
||||
Name.Tag: '',
|
||||
Name.Variable: '#268Bd2',
|
||||
Name.Variable.Class: '',
|
||||
Name.Variable.Global: '',
|
||||
Name.Variable.Instance: '',
|
||||
Number: '#2aa198',
|
||||
Number.Float: '#2aa198',
|
||||
Number.Hex: '#2aa198',
|
||||
Number.Integer: '#2aa198',
|
||||
Number.Integer.Long: '#2aa198',
|
||||
Number.Oct: '#2aa198',
|
||||
Operator: '#859900',
|
||||
Operator.Word: '#859900',
|
||||
Other: '',
|
||||
Punctuation: '',
|
||||
String: '#2aa198',
|
||||
String.Backtick: '#2aa198',
|
||||
String.Char: '#2aa198',
|
||||
String.Doc: '#2aa198',
|
||||
String.Double: '#2aa198',
|
||||
String.Escape: '#2aa198',
|
||||
String.Heredoc: '#2aa198',
|
||||
String.Interpol: '#2aa198',
|
||||
String.Other: '#2aa198',
|
||||
String.Regex: '#2aa198',
|
||||
String.Single: '#2aa198',
|
||||
String.Symbol: '#2aa198',
|
||||
Text: '',
|
||||
Token: '#93a1a1',
|
||||
Whitespace: ''
|
||||
}
|
@ -12,6 +12,7 @@
|
||||
<link rel="stylesheet" type="text/css" href="{% static "css/front.css" %}" media="screen" />
|
||||
<link rel="stylesheet" type="text/css" href="{% static "css/blog.css" %}" media="screen" />
|
||||
<link rel="stylesheet" type="text/css" href="{% static "css/resume.css" %}" media="screen" />
|
||||
<link rel="stylesheet" type="text/css" href="{% static "css/code.css" %}" media="screen" />
|
||||
{% endblock %}
|
||||
{% endcompress %}
|
||||
<link rel="alternate" type="application/rss+xml" title="Gergely Polonkai's Blog - RSS Feed" href="{{ app.request.scheme }}://{{ app.request.host }}{% url 'blog:feed' %}" />
|
||||
|
Loading…
Reference in New Issue
Block a user