summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pastepy.py30
-rw-r--r--templates/__init__.py3
-rw-r--r--templates/base.html (renamed from templates/base.tmpl)12
-rw-r--r--templates/message.html5
-rw-r--r--templates/message.tmpl5
-rw-r--r--templates/paste.html (renamed from templates/paste.tmpl)28
-rw-r--r--templates/view.html26
-rw-r--r--templates/view.tmpl29
8 files changed, 68 insertions, 70 deletions
diff --git a/pastepy.py b/pastepy.py
index 9697fac..fafc81e 100644
--- a/pastepy.py
+++ b/pastepy.py
@@ -7,8 +7,8 @@ import os
import random
import settings
-import templates
+import jinja2
try:
import markdown
has_markdown = True
@@ -57,13 +57,20 @@ class Paste(object):
random.seed()
+ self.template_env = jinja2.Environment(loader = jinja2.FileSystemLoader('templates'))
+
+ def render_template(self, name, args):
+ template = self.template_env.get_template(name)
+ data = template.render(**args).encode('utf-8')
+ self.start_response('200 OK', [('Content-Type', 'text/html; charset=UTF-8'), ('Content-Length', str(len(data)))])
+ return [data]
+
def message(self, msg, title = 'Message'):
- self.start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8')])
- return [str(templates.message(searchList = {
+ return self.render_template('message.html', {
'title': '%s – %s' % (settings.pastebin_name, title),
'header': title,
'text': msg,
- }))]
+ })
def paste(self):
c = Cookie.SimpleCookie(self.environ['HTTP_COOKIE'] if 'HTTP_COOKIE' in self.environ else None)
@@ -75,8 +82,7 @@ class Paste(object):
return self.add_paste(mp)
else:
return self.message('Invalid type "%s".' % mp['type'].value, 'Error')
- self.start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8')])
- return [str(templates.paste(searchList = {
+ return self.render_template('paste.html', {
'title': settings.pastebin_name,
'header': settings.pastebin_name,
'lexers': self.lexers,
@@ -84,7 +90,7 @@ class Paste(object):
'syntax': c['syntax'].value if 'syntax' in c else settings.default,
'remember_me': 'nick' in c,
'remember_syntax': 'syntax' in c,
- }))]
+ })
def preview(self, mp):
try:
@@ -92,8 +98,7 @@ class Paste(object):
except UnknownSyntaxError:
return self.message('Could not find lexer "%s".' % mp['syntax'].value, 'Error')
- self.start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8')])
- return [str(templates.view(searchList = {
+ return self.render_template('view.html', {
'title': settings.pastebin_name,
'header': '%s – Preview' % settings.pastebin_name,
'hash': None,
@@ -103,7 +108,7 @@ class Paste(object):
'pastetitle': mp['title'].value.decode('utf8') or 'Untitled',
'text': text,
'rendered': (lexername or '').startswith('Rendered '),
- }))]
+ })
def add_paste(self, mp):
nick = mp['nick'].value.decode('utf8') or None
@@ -172,8 +177,7 @@ class Paste(object):
finally:
session.close()
- self.start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8')])
- return [str(templates.view(searchList = {
+ return self.render_template('view.html', {
'title': '%s – View paste – %s' % (settings.pastebin_name, paste.title or 'Untitled'),
'header': '%s – View paste' % settings.pastebin_name,
'hash': hash,
@@ -183,7 +187,7 @@ class Paste(object):
'pastetitle': paste.title or 'Untitled',
'text': cache.text,
'rendered': (cache.syntax_name or '').startswith('Rendered '),
- }))]
+ })
def raw(self):
hash = self.path[1]
diff --git a/templates/__init__.py b/templates/__init__.py
deleted file mode 100644
index defb657..0000000
--- a/templates/__init__.py
+++ /dev/null
@@ -1,3 +0,0 @@
-from paste import paste
-from view import view
-from message import message
diff --git a/templates/base.tmpl b/templates/base.html
index 6374752..196a03e 100644
--- a/templates/base.tmpl
+++ b/templates/base.html
@@ -1,19 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
- <title>$title</title>
+ <title>{{ title }}</title>
<link rel="StyleSheet" href="/static/paste.css" type="text/css">
-#block head
-#end block
+{% block head %}
+{% endblock %}
</head>
<body>
<div id="page">
<div id="page-header">
- <h1><a href="/">$header</a></h1>
+ <h1><a href="/">{{ header }}</a></h1>
</div>
<div id="page-content">
-#block content
-#end block
+{% block content %}
+{% endblock %}
</div>
</div>
</body>
diff --git a/templates/message.html b/templates/message.html
new file mode 100644
index 0000000..540b64a
--- /dev/null
+++ b/templates/message.html
@@ -0,0 +1,5 @@
+{% extends "base.html" %}
+{% block content %}
+ <p><a href="/">New paste</a></p>
+ {{ text }}
+{% endblock %}
diff --git a/templates/message.tmpl b/templates/message.tmpl
deleted file mode 100644
index 9f69ebc..0000000
--- a/templates/message.tmpl
+++ /dev/null
@@ -1,5 +0,0 @@
-#extends templates.base
-#def content
- <p><a href="/">New paste</a></p>
- $text
-#end def
diff --git a/templates/paste.tmpl b/templates/paste.html
index f9c9504..e22f76d 100644
--- a/templates/paste.tmpl
+++ b/templates/paste.html
@@ -1,26 +1,26 @@
-#extends templates.base
-#def head
+{% extends "base.html" %}
+{% block head %}
<script src="/static/edit.js" type="text/javascript"></script>
-#end def
-#def content
+{% endblock %}
+{% block content %}
<form action="/paste" method="post" id="pasteform">
<ul>
<li id="syntaxli">
<select name="syntax" id="syntax">
-#for cat, ls in $lexers
- <optgroup label="$cat">
-#for k, v in $ls
- <option value="$v"#if $v == $syntax# selected="selected"#end if#>$k</option>
-#end for
+ {% for cat, ls in lexers %}
+ <optgroup label="{{ cat }}">
+ {% for k, v in ls %}
+ <option value="{{ v }}"{% if v == syntax %} selected="selected"{% endif %}>{{ k }}</option>
+ {% endfor %}
</optgroup>
-#end for
+ {% endfor %}
</select>
- <input type="checkbox" name="remember_syntax" id="remember_syntax" #if $remember_syntax#checked #end if#>
+ <input type="checkbox" name="remember_syntax" id="remember_syntax"{% if remember_syntax %} checked{% endif %}>
<label for="remember_syntax">Remember syntax</label>
</li>
<li>
- <input type="text" name="nick" id="nick" value="$nick">
- <input type="checkbox" name="remember_me" id="remember_me" #if $remember_me#checked #end if#>
+ <input type="text" name="nick" id="nick" value="{{ nick }}">
+ <input type="checkbox" name="remember_me" id="remember_me"{% if remember_me %} checked{% endif %}>
<label for="remember_me">Remember me</label>
</li>
<li><input type="text" name="title" id="title" value="Untitled"></li>
@@ -31,4 +31,4 @@
</li>
</ul>
</form>
-#end def
+{% endblock %}
diff --git a/templates/view.html b/templates/view.html
new file mode 100644
index 0000000..0a20a62
--- /dev/null
+++ b/templates/view.html
@@ -0,0 +1,26 @@
+{% extends "base.html" %}
+{% block head %}
+ <link rel="StyleSheet" href="/highlight_stylesheet" type="text/css">
+ <script type="text/javascript" src="/static/view.js"></script>
+{% endblock %}
+{% block content %}
+ <p><a href="/">New paste</a></p>
+ <div id="paste">
+ <h3>{{ pastetitle }}</h3>
+ <div id="info">Pasted by <span id="nick">{{ nick }}</span> on <span id="date">{{date }}</span>
+ {% if syntax %} as <span id="syntax-type">{{ syntax }}</span>{% endif %}
+ </div>
+ <div id="alt-links">
+ {% if hash %}
+ <a href="/raw/{{ hash }}">Download as plain text</a>
+ {% endif %}
+ </div>
+ {% if rendered %}
+ <div class="paste-rendered">
+ {% endif %}
+ {{ text }}
+ {% if rendered %}
+ </div>
+ {% endif %}
+ </div>
+{% endblock %}
diff --git a/templates/view.tmpl b/templates/view.tmpl
deleted file mode 100644
index a23c36e..0000000
--- a/templates/view.tmpl
+++ /dev/null
@@ -1,29 +0,0 @@
-#extends templates.base
-#def head
- <link rel="StyleSheet" href="/highlight_stylesheet" type="text/css">
- <script type="text/javascript" src="/static/view.js"></script>
-#end def
-#def content
- <p><a href="/">New paste</a></p>
- <div id="paste">
- <h3>$pastetitle</h3>
- <div id="info">Pasted by <span id="nick">$nick</span> on <span id="date">$date</span>#slurp
-#if $syntax
- as <span id="syntax-type">$syntax</span>#slurp
-#end if
-.
-</div>
- <div id="alt-links">
-#if $hash
- <a href="/raw/$hash">Download as plain text</a>
-#end if
- </div>
-#if $rendered
- <div class="paste-rendered">
-#end if
- $text
-#if $rendered
- </div>
-#end if
- </div>
-#end def