Code beautification

This commit is contained in:
Gergely Polonkai 2014-04-21 10:39:43 +02:00
parent 6ee2ddb2af
commit 337e1eea2b
7 changed files with 154 additions and 42 deletions

View File

@ -25,7 +25,12 @@ class CodeFormatter(HtmlFormatter):
def hilite(language, code):
lexer = get_lexer_by_name(language)
formatter = CodeFormatter(style = SolarizedDarkStyle, linenos = False, cssclass = language + " code", noclasses = True)
formatter = CodeFormatter(
style = SolarizedDarkStyle,
linenos = False,
cssclass = language + " code",
noclasses = True
)
return highlight(code, lexer, formatter)
@register.filter(needs_autoescape = True)
@ -74,8 +79,12 @@ def insert_code_chunks(value, autoescape=None):
start += diff
end += diff
try:
chunk = CodeChunk.objects.get(language = match.group('lang'), slug = match.group('slug'))
# TODO: This is an ugly hack, as it includes template logic in code. BAD!
chunk = CodeChunk.objects.get(
language = match.group('lang'),
slug = match.group('slug')
)
# TODO: This is an ugly hack, as it includes template logic in
# code. BAD!
newstr = "<div class=\"code-chunk\">" + hilite(match.group('lang'), chunk.content) + "</div>"
except CodeChunk.DoesNotExist:
newstr = ""
@ -94,7 +103,8 @@ def insert_code_chunks(value, autoescape=None):
start += diff
end += diff
# TODO: This is an ugly hack, as it includes template logic in code. BAD!
# TODO: This is an ugly hack, as it includes template logic in code.
# BAD!
newstr = "<div class=\"code-chunk\">" + hilite(match.group('lang'), match.group('code')) + "</div>"
newlen = len(newstr)

View File

@ -2,13 +2,49 @@ from django.conf.urls import patterns, url
from blog import views
urlpatterns = patterns('',
url(r'^$', views.index, name = 'index'),
url(r'^page/(?P<page>\d+)$', views.indexpage, name = 'indexpage'),
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+)/?$', views.datelist, name = 'datelist'),
url(r'^(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/page/(?P<page>\d+)$', views.datepage, name = 'datepage'),
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'),
url(
r'^$',
views.index,
name = 'index'
),
url(
r'^page/(?P<page>\d+)$',
views.indexpage,
name = 'indexpage'
),
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+)/?$',
views.datelist,
name = 'datelist'
),
url(
r'^(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/page/(?P<page>\d+)$',
views.datepage,
name = 'datepage'
),
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

@ -13,10 +13,10 @@ def listing(request, tag, year, month, day, page):
view = 'index'
if (tag == None):
view = "index"
view = 'index'
else:
kwargs['tags__slug'] = tag
view = "tag"
view = 'tag'
if (year != None):
kwargs['created_at__year'] = year
@ -40,7 +40,11 @@ def listing(request, tag, year, month, day, page):
if paginator.num_pages > 1:
view = view + 'page'
return render(request, 'blog/listing.html', { 'posts': posts, 'tag': tag, 'view': "blog:" + view })
return render(request, 'blog/listing.html', {
'posts': posts,
'tag': tag,
'view': "blog:" + view
})
def index(request):
return listing(request, None, None, None, None, 1)
@ -61,7 +65,13 @@ def datepage(request, year, month, day, page):
return listing(request, None, year, month, day, page)
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)
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
)
next_post = Post.objects.filter(created_at__gt = post.created_at).order_by('created_at')[0:1]
prev_post = Post.objects.filter(created_at__lt = post.created_at).order_by('-created_at')[0:1]
@ -75,7 +85,11 @@ def read(request, year, month, day, slug):
else:
prev_post = prev_post[0]
return render(request, 'blog/view.html', {'post': post, 'prev_post': prev_post, 'next_post': next_post})
return render(request, 'blog/view.html', {
'post': post,
'prev_post': prev_post,
'next_post': next_post,
})
def codechunk(request, language, slug):
chunk = get_object_or_404(CodeChunk, language = language, slug = slug)

View File

@ -7,12 +7,19 @@ import os
from random import choice
def randomheader(request):
header_file = choice(filter(lambda x: os.path.isfile(settings.HEADER_DIR + os.path.sep + x), os.listdir(settings.HEADER_DIR)))
header_file = choice(filter(
lambda x: os.path.isfile(
settings.HEADER_DIR + os.path.sep + x
),
os.listdir(settings.HEADER_DIR)
))
return {'header': header_file}
def tagcloud(request):
tagcloud = []
tagcloudlist = Tag.objects.annotate(ct=Count('taggit_taggeditem_items')).order_by('-ct')
tagcloudlist = Tag.objects.annotate(
ct = Count('taggit_taggeditem_items')
).order_by('-ct')
if (len(tagcloudlist) > 0):
tmax = tagcloudlist[0].ct
@ -26,7 +33,11 @@ def tagcloud(request):
if (tagcount >= tmin):
size = int(floor((5.0 * (tagcount - tmin)) / (tmax - tmin)))
tagcloud.append({'name': cloudelement.name, 'slug': cloudelement.slug, 'size': size})
tagcloud.append({
'name': cloudelement.name,
'slug': cloudelement.slug,
'size': size
})
tagcloud.sort(key = itemgetter('name'))

View File

@ -8,7 +8,21 @@
"""
from pygments.style import Style
from pygments.token import Comment, Error, Generic, Keyword, Literal, Name, Number, Operator, Other, Punctuation, String, Text, Token, Whitespace
from pygments.token import \
Comment, \
Error, \
Generic, \
Keyword, \
Literal, \
Name, \
Number, \
Operator, \
Other, \
Punctuation, \
String, \
Text, \
Token, \
Whitespace
class SolarizedDarkStyle(Style):
"""

View File

@ -10,13 +10,40 @@ handler403 = 'basics.views.forbidden'
handler400 = 'basics.views.badrequest'
urlpatterns = patterns('',
# Examples:
url(r'^$', 'blog.views.mainpage', name='home'),
url(r'^google150e54bda5f96d97', 'basics.views.googlevalidator'),
url(r'^robots\.txt$', TemplateView.as_view(template_name='robots.txt', content_type = 'text/plain')),
url(r'^blog/', include('blog.urls', namespace='blog')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('basics.urls', namespace='basics')),
url(
r'^$',
'blog.views.mainpage',
name = 'home'
),
url(
r'^google150e54bda5f96d97',
'basics.views.googlevalidator'
),
url(
r'^robots\.txt$',
TemplateView.as_view(template_name = 'robots.txt', content_type = 'text/plain')
),
url(
r'^blog/',
include(
'blog.urls',
namespace = 'blog'
)
),
# Uncomment the admin/doc line below to enable admin documentation:
#url(
# r'^admin/doc/',
# include('django.contrib.admindocs.urls')
# ),
url(
r'^admin/',
include(admin.site.urls)
),
url(
r'^',
include(
'basics.urls',
namespace = 'basics'
)
),
)