Code beautification
This commit is contained in:
parent
6ee2ddb2af
commit
337e1eea2b
@ -25,7 +25,12 @@ class CodeFormatter(HtmlFormatter):
|
|||||||
|
|
||||||
def hilite(language, code):
|
def hilite(language, code):
|
||||||
lexer = get_lexer_by_name(language)
|
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)
|
return highlight(code, lexer, formatter)
|
||||||
|
|
||||||
@register.filter(needs_autoescape = True)
|
@register.filter(needs_autoescape = True)
|
||||||
@ -74,8 +79,12 @@ def insert_code_chunks(value, autoescape=None):
|
|||||||
start += diff
|
start += diff
|
||||||
end += diff
|
end += diff
|
||||||
try:
|
try:
|
||||||
chunk = CodeChunk.objects.get(language = match.group('lang'), slug = match.group('slug'))
|
chunk = CodeChunk.objects.get(
|
||||||
# TODO: This is an ugly hack, as it includes template logic in code. BAD!
|
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>"
|
newstr = "<div class=\"code-chunk\">" + hilite(match.group('lang'), chunk.content) + "</div>"
|
||||||
except CodeChunk.DoesNotExist:
|
except CodeChunk.DoesNotExist:
|
||||||
newstr = ""
|
newstr = ""
|
||||||
@ -94,7 +103,8 @@ def insert_code_chunks(value, autoescape=None):
|
|||||||
start += diff
|
start += diff
|
||||||
end += 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>"
|
newstr = "<div class=\"code-chunk\">" + hilite(match.group('lang'), match.group('code')) + "</div>"
|
||||||
|
|
||||||
newlen = len(newstr)
|
newlen = len(newstr)
|
||||||
|
54
blog/urls.py
54
blog/urls.py
@ -2,13 +2,49 @@ from django.conf.urls import patterns, url
|
|||||||
from blog import views
|
from blog import views
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
url(r'^$', views.index, name = 'index'),
|
url(
|
||||||
url(r'^page/(?P<page>\d+)$', views.indexpage, name = 'indexpage'),
|
r'^$',
|
||||||
url(r'^tag/(?P<tag>[^/]+)$', views.taglist, name = 'taglist'),
|
views.index,
|
||||||
url(r'^tag/(?P<tag>[^/]+)/page/(?P<page>\d+)$', views.tagpage, name = 'tagpage'),
|
name = 'index'
|
||||||
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(
|
||||||
url(r'^(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/(?P<slug>[^/]+)$', views.read, name = 'read'),
|
r'^page/(?P<page>\d+)$',
|
||||||
url(r'^code-chunk/(?P<language>[^/]+)/(?P<slug>[^/]+)$', views.codechunk, name = 'codechunk'),
|
views.indexpage,
|
||||||
url(r'^feed$', views.feed, name = 'feed'),
|
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'
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
@ -13,10 +13,10 @@ def listing(request, tag, year, month, day, page):
|
|||||||
view = 'index'
|
view = 'index'
|
||||||
|
|
||||||
if (tag == None):
|
if (tag == None):
|
||||||
view = "index"
|
view = 'index'
|
||||||
else:
|
else:
|
||||||
kwargs['tags__slug'] = tag
|
kwargs['tags__slug'] = tag
|
||||||
view = "tag"
|
view = 'tag'
|
||||||
|
|
||||||
if (year != None):
|
if (year != None):
|
||||||
kwargs['created_at__year'] = year
|
kwargs['created_at__year'] = year
|
||||||
@ -40,7 +40,11 @@ def listing(request, tag, year, month, day, page):
|
|||||||
if paginator.num_pages > 1:
|
if paginator.num_pages > 1:
|
||||||
view = view + 'page'
|
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):
|
def index(request):
|
||||||
return listing(request, None, None, None, None, 1)
|
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)
|
return listing(request, None, year, month, day, page)
|
||||||
|
|
||||||
def read(request, year, month, day, slug):
|
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]
|
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]
|
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:
|
else:
|
||||||
prev_post = prev_post[0]
|
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):
|
def codechunk(request, language, slug):
|
||||||
chunk = get_object_or_404(CodeChunk, language = language, slug = slug)
|
chunk = get_object_or_404(CodeChunk, language = language, slug = slug)
|
||||||
|
@ -7,12 +7,19 @@ import os
|
|||||||
from random import choice
|
from random import choice
|
||||||
|
|
||||||
def randomheader(request):
|
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}
|
return {'header': header_file}
|
||||||
|
|
||||||
def tagcloud(request):
|
def tagcloud(request):
|
||||||
tagcloud = []
|
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):
|
if (len(tagcloudlist) > 0):
|
||||||
tmax = tagcloudlist[0].ct
|
tmax = tagcloudlist[0].ct
|
||||||
@ -26,7 +33,11 @@ def tagcloud(request):
|
|||||||
|
|
||||||
if (tagcount >= tmin):
|
if (tagcount >= tmin):
|
||||||
size = int(floor((5.0 * (tagcount - tmin)) / (tmax - 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'))
|
tagcloud.sort(key = itemgetter('name'))
|
||||||
|
|
||||||
|
@ -8,7 +8,21 @@
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from pygments.style import Style
|
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):
|
class SolarizedDarkStyle(Style):
|
||||||
"""
|
"""
|
||||||
|
@ -10,13 +10,40 @@ handler403 = 'basics.views.forbidden'
|
|||||||
handler400 = 'basics.views.badrequest'
|
handler400 = 'basics.views.badrequest'
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
# Examples:
|
url(
|
||||||
url(r'^$', 'blog.views.mainpage', name='home'),
|
r'^$',
|
||||||
url(r'^google150e54bda5f96d97', 'basics.views.googlevalidator'),
|
'blog.views.mainpage',
|
||||||
url(r'^robots\.txt$', TemplateView.as_view(template_name='robots.txt', content_type = 'text/plain')),
|
name = 'home'
|
||||||
url(r'^blog/', include('blog.urls', namespace='blog')),
|
),
|
||||||
# Uncomment the admin/doc line below to enable admin documentation:
|
url(
|
||||||
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
|
r'^google150e54bda5f96d97',
|
||||||
url(r'^admin/', include(admin.site.urls)),
|
'basics.views.googlevalidator'
|
||||||
url(r'^', include('basics.urls', namespace='basics')),
|
),
|
||||||
|
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'
|
||||||
|
)
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user