summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2019-03-15 18:45:47 +0100
committerJon Bergli Heier <snakebite@jvnv.net>2019-03-15 18:45:47 +0100
commitc57494ae405ffbdd8ec45f74ef04ea85895f1bf7 (patch)
treea0b888069205b7d8260c1862d300032a56b6fd30
parentc5ebdf4428d9d9aab04cf6085a231ed6ea5bf0ab (diff)
Port application to flask
-rw-r--r--.gitignore3
-rw-r--r--db.py56
-rw-r--r--pastepy.py292
-rw-r--r--pastepy/__init__.py8
-rw-r--r--pastepy/db.py68
-rw-r--r--pastepy/pastepy.py169
-rw-r--r--pastepy/static/edit.js (renamed from static/edit.js)0
-rw-r--r--pastepy/static/paste.css (renamed from static/paste.css)0
-rw-r--r--pastepy/static/view.js26
-rw-r--r--pastepy/templates/base.html (renamed from templates/base.html)6
-rw-r--r--pastepy/templates/full.html18
-rw-r--r--pastepy/templates/paste.html (renamed from templates/paste.html)12
-rw-r--r--pastepy/templates/view.html (renamed from templates/view.html)6
-rw-r--r--static/view.js5
-rw-r--r--templates/full.html18
-rw-r--r--templates/message.html5
16 files changed, 303 insertions, 389 deletions
diff --git a/.gitignore b/.gitignore
index eb0a79c..ce796f2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,6 @@
.*.swp
*.pyc
+__pycache__
/settings.py
/db
-templates/*.py
+*.cfg
diff --git a/db.py b/db.py
deleted file mode 100644
index 8b2591d..0000000
--- a/db.py
+++ /dev/null
@@ -1,56 +0,0 @@
-import settings
-
-from sqlalchemy import create_engine, Column, Integer, String, DateTime, Text, Index, ForeignKey
-from sqlalchemy.ext.declarative import declarative_base
-from sqlalchemy.orm import sessionmaker, relation, backref
-from sqlalchemy.orm.exc import NoResultFound
-
-engine = create_engine(settings.db_path)
-
-Base = declarative_base(bind = engine)
-
-class Paste(Base):
- __tablename__ = 'paste'
-
- id = Column(Integer, primary_key = True)
- hash = Column(String, unique = True, index = True)
- nick = Column(String)
- date = Column(DateTime, nullable = False)
- syntax = Column(String)
- title = Column(String)
- text = Column(Text, nullable = False)
- ip = Column(String)
-
- def __init__(self, hash, nick, date, syntax, title, text, ip=None):
- self.nick = nick
- self.hash = hash
- self.date = date
- self.syntax = syntax
- self.title = title
- self.text = text
- self.ip = ip
-
- def __repr__(self):
- return '<Paste(%d, "%s", "%s", "%s", "%s")>' % (self.id, self.hash, self.nick, self.date.ctime(), self.title)
-
-class Cache(Base):
- __tablename__ = 'cache'
-
- id = Column(Integer, primary_key = True)
- paste_hash = Column(String, ForeignKey('paste.hash'), index = True)
- paste = relation(Paste, primaryjoin = paste_hash == Paste.hash, lazy = False)
- syntax_name = Column(String)
- text = Column(Text, nullable = False)
-
- def __init__(self, hash, syntax_name, text):
- self.paste_hash = hash
- self.syntax_name = syntax_name
- self.text = text
-
- def __repr__(self):
- return '<Cache(%d, "%s")>' % (self.id, self.paste_hash)
-
-Base.metadata.create_all()
-Session = sessionmaker(bind = engine, autoflush = True, autocommit = False)
-
-# vim: noet ts=4
diff --git a/pastepy.py b/pastepy.py
deleted file mode 100644
index 96d3f40..0000000
--- a/pastepy.py
+++ /dev/null
@@ -1,292 +0,0 @@
-import cgi
-try:
- from http.cookies import SimpleCookie
-except ImportError:
- from Cookie import SimpleCookie
-import datetime
-import db
-import mimetypes
-import os
-import random
-
-import settings
-
-import jinja2
-try:
- import markdown
- has_markdown = True
-except ImportError:
- has_markdown = False
-import pygments
-from pygments import highlight
-from pygments.lexers import get_all_lexers, get_lexer_by_name
-from pygments.formatters import HtmlFormatter
-import six
-
-base62_alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
-
-def b2t(s):
- '''Convert binary types to text types.'''
- if isinstance(s, six.binary_type):
- return s.decode('utf-8')
- return s
-
-def t2b(s):
- '''Convert text types to binary types.'''
- if isinstance(s, six.text_type):
- return s.encode('utf-8')
- return s
-
-class PasteError(Exception): pass
-class UnknownSyntaxError(PasteError): pass
-
-class CustomHtmlFormatter(HtmlFormatter):
- def wrap(self, source, outfile):
- yield 0, '<pre>'
- line = 1
- for i, t in source:
- yield i, '<span id="codeline-%d">%s</span>' % (line, t)
- line += 1
- yield 0, '</pre>'
-
-class Paste(object):
- def __init__(self):
- lexers = dict([(x[0], x[1][0]) for x in get_all_lexers()])
- self.lexers = []
- removed = []
- for cat, ls in settings.categories:
- for l in ls:
- removed.append(l)
- self.lexers.append((cat, [(x, lexers[x]) for x in ls]))
- for l in removed:
- try:
- del lexers[l]
- except KeyError:
- pass
- rendered = []
- if has_markdown:
- rendered.append(('Rendered markdown', 'md-render'))
- if len(rendered):
- self.lexers.append(('Rendered', rendered))
- self.lexers.append(('Others' if settings.categories else 'Syntax', sorted(lexers.items(), key = lambda l: l[0].lower())))
- self.formatter = CustomHtmlFormatter(linenos = 'table', lineanchors = 'line', anchorlinenos = True)
-
- 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'):
- return self.render_template('message.html', {
- 'title': '%s &ndash; %s' % (settings.pastebin_name, title),
- 'header': title,
- 'text': msg,
- })
-
- def paste(self):
- c = SimpleCookie(self.environ['HTTP_COOKIE'] if 'HTTP_COOKIE' in self.environ else None)
- if self.environ['REQUEST_METHOD'] == 'POST':
- mp = cgi.FieldStorage(fp = self.environ['wsgi.input'], environ = self.environ, keep_blank_values = True)
- if mp['type'].value == 'Preview':
- return self.preview(mp)
- elif mp['type'].value == 'Paste':
- return self.add_paste(mp)
- else:
- return self.message('Invalid type "%s".' % mp['type'].value, 'Error')
- return self.render_template('paste.html', {
- 'title': settings.pastebin_name,
- 'header': settings.pastebin_name,
- 'lexers': self.lexers,
- 'nick': b2t(c['nick'].value if 'nick' in c else 'Anonymous'),
- '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:
- lexername, text = self.get_formatted(mp['syntax'].value, mp['text'].value)
- except UnknownSyntaxError:
- return self.message('Could not find lexer "%s".' % mp['syntax'].value, 'Error')
-
- # These values to be unicode in py2; in py3 they will be str which is OK.
- nick = b2t(mp['nick'].value or 'Anonymous')
- title = b2t(mp['title'].value or 'Untitled')
- return self.render_template('view.html', {
- 'title': settings.pastebin_name,
- 'header': '%s &ndash; Preview' % settings.pastebin_name,
- 'hash': None,
- 'nick': nick,
- 'date': '%s UTC' % datetime.datetime.utcnow().ctime(),
- 'syntax': lexername,
- 'pastetitle': title,
- 'text': text,
- 'rendered': (lexername or '').startswith('Rendered '),
- })
-
- def add_paste(self, mp):
- nick = b2t(mp['nick'].value or None)
- syntax = b2t(mp['syntax'].value or None)
- title = b2t(mp['title'].value or None)
- text = b2t(mp['text'].value.replace('\r', '') or None)
-
- hash = ''.join(random.choice(base62_alphabet) for x in range(5))
-
- try:
- session = db.Session()
- paste = db.Paste(hash, nick, datetime.datetime.utcnow(), syntax, title, text, self.environ['REMOTE_ADDR'])
- session.add(paste)
- session.commit()
- finally:
- session.close()
-
- headers = [('Location', '/view/%s' % hash)]
- c = SimpleCookie()
- if six.PY2:
- # In py2 we need nick to be a str, or non-ascii characters will fail with UnicodeDecodeError.
- # In py3 it must be a str or we will get garbage in the cookie value.
- nick = t2b(nick)
- c['nick'] = nick
- dt = (datetime.datetime.utcnow() + datetime.timedelta(days = 30)) if 'remember_me' in mp else datetime.datetime.utcfromtimestamp(0)
- c['nick']['expires'] = dt.strftime('%a, %d-%b-%y %H:%M:%S GMT')
- c['syntax'] = syntax
- dt = (datetime.datetime.utcnow() + datetime.timedelta(days = 30)) if 'remember_syntax' in mp else datetime.datetime.utcfromtimestamp(0)
- c['syntax']['expires'] = dt.strftime('%a, %d-%b-%y %H:%M:%S GMT')
- headers.append(('Set-Cookie', c['nick'].OutputString()))
- headers.append(('Set-Cookie', c['syntax'].OutputString()))
- self.start_response('302 Found', headers)
- return []
-
- def get_formatted(self, syntax, text):
- if syntax == 'md-render' and has_markdown:
- text = markdown.markdown(text, extensions = settings.markdown_extensions)
- lexername = 'Rendered markdown'
- else:
- try:
- lex = get_lexer_by_name(syntax or 'text')
- except pygments.util.ClassNotFound:
- raise UnknownSyntaxError(syntax)
- lexername = lex.name
- text = highlight(text, lex, self.formatter)
- return (lexername, text)
-
- def view(self):
- hash = self.path[1]
-
- try:
- session = db.Session()
- try:
- cache = session.query(db.Cache).filter_by(paste_hash = hash).one()
- paste = cache.paste
- except db.NoResultFound: # No cache found, generate it.
- try:
- paste = session.query(db.Paste).filter_by(hash = hash).one()
- except db.NoResultFound:
- self.start_response('404 Not Found', [])
- return []
- try:
- lexername, text = self.get_formatted(paste.syntax, paste.text)
- except UnknownSyntaxError:
- return self.message('Could not find the lexer "%s".' % paste.syntax, 'Error')
- cache = db.Cache(hash, lexername, text)
- session.add(cache)
- session.commit()
- # Workaround for attribute refresh.
- paste = cache.paste
- finally:
- session.close()
-
- return self.render_template('view.html', {
- 'title': '%s &ndash; View paste &ndash; %s' % (settings.pastebin_name, paste.title or 'Untitled'),
- 'header': '%s &ndash; View paste' % settings.pastebin_name,
- 'hash': hash,
- 'date': '%s UTC' % paste.date.ctime(),
- 'nick': b2t(paste.nick or 'Anonymous'),
- 'syntax': cache.syntax_name,
- 'pastetitle': b2t(paste.title or 'Untitled'),
- 'text': b2t(cache.text),
- 'rendered': (cache.syntax_name or '').startswith('Rendered '),
- })
-
- def raw(self):
- hash = self.path[1]
-
- try:
- session = db.Session()
- paste = session.query(db.Paste).filter_by(hash = hash).one()
- except db.NoResultFound:
- self.start_response('404 Not Found', [])
- return []
- finally:
- session.close()
-
- self.start_response('200 OK', [('Content-Type', 'text/plain; charset=UTF-8')])
- return [t2b(paste.text)]
-
- def full(self):
- hash = self.path[1]
-
- try:
- session = db.Session()
- try:
- cache = session.query(db.Cache).filter_by(paste_hash = hash).one()
- paste = cache.paste
- except db.NoResultFound:
- try:
- paste = session.query(db.Paste).filter_by(hash = hash).one()
- except db.NoResultFound:
- self.start_response('404 Not Found', [])
- return []
- try:
- lexername, text = self.get_formatted(paste.syntax, paste.text)
- except UnknownSyntaxError:
- return self.message('Could not find the lexer "%s".' % paste.syntax, 'Error')
- cache = db.Cache(hash, lexername, text)
- session.add(cache)
- session.commit()
- # Workaround for attribute refresh.
- paste = cache.paste
- finally:
- session.close()
-
- return self.render_template('full.html', {
- 'title': '%s &ndash; %s' % (settings.pastebin_name, paste.title or 'Untitled'),
- 'text': b2t(cache.text),
- 'rendered': (cache.syntax_name or '').startswith('Rendered '),
- })
-
- def highlight_stylesheet(self):
- self.start_response('200 OK', [('Content-Type', 'text/css')])
- return [t2b(self.formatter.get_style_defs())]
-
- def static(self):
- filename = settings.static_root + os.path.sep + self.path[1]
- if not self.path[1] in ('paste.css', 'edit.js', 'view.js') or not os.path.exists(filename):
- self.start_response('404 Not Found', [('Content-Type', 'text/html; charset=UTF-8'), ('Location', '/')])
- return ['<a href="/">asdf</a>']
- self.start_response('200 OK', [('Content-Type', mimetypes.guess_type(filename)[0] or 'text/plain; charset=UTF-8')])
- return open(filename, 'rb')
-
- def __call__(self, environ, start_response):
- self.environ = environ
- self.start_response = start_response
-
- path = self.environ['PATH_INFO'].split('/')[1:]
- module = path[0] or 'paste'
- if module in ('list', 'paste', 'view', 'raw', 'full', 'static', 'highlight_stylesheet'):
- self.path = path
- return getattr(self, module)()
- else:
- return self.message('Invalid module requested.', 'Error')
-
-if __name__ == '__main__':
- from wsgiref.simple_server import make_server
- httpd = make_server('', 8000, Paste())
- httpd.serve_forever()
-
-# vim: noet ts=4
diff --git a/pastepy/__init__.py b/pastepy/__init__.py
new file mode 100644
index 0000000..0cc4db6
--- /dev/null
+++ b/pastepy/__init__.py
@@ -0,0 +1,8 @@
+from flask import Flask
+
+app = Flask(__name__)
+app.config.from_pyfile('pastepy.cfg')
+
+with app.app_context():
+ from .pastepy import app as pastepy
+ app.register_blueprint(pastepy)
diff --git a/pastepy/db.py b/pastepy/db.py
new file mode 100644
index 0000000..0aa04a1
--- /dev/null
+++ b/pastepy/db.py
@@ -0,0 +1,68 @@
+from contextlib import contextmanager
+
+from flask import current_app
+from sqlalchemy import create_engine, Column, Integer, String, DateTime, Text, Index, ForeignKey
+from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy.orm import sessionmaker, relation, backref
+from sqlalchemy.orm.exc import NoResultFound
+
+engine = create_engine(current_app.config['DB_PATH'])
+
+Base = declarative_base(bind = engine)
+
+class Paste(Base):
+ __tablename__ = 'paste'
+
+ id = Column(Integer, primary_key = True)
+ hash = Column(String, unique = True, index = True)
+ nick = Column(String)
+ date = Column(DateTime, nullable = False)
+ syntax = Column(String)
+ title = Column(String)
+ text = Column(Text, nullable = False)
+ ip = Column(String)
+
+ def __init__(self, hash, nick, date, syntax, title, text, ip=None):
+ self.nick = nick
+ self.hash = hash
+ self.date = date
+ self.syntax = syntax
+ self.title = title
+ self.text = text
+ self.ip = ip
+
+ def __repr__(self):
+ return '<Paste(%d, "%s", "%s", "%s", "%s")>' % (self.id, self.hash, self.nick, self.date.ctime(), self.title)
+
+class Cache(Base):
+ __tablename__ = 'cache'
+
+ id = Column(Integer, primary_key = True)
+ paste_hash = Column(String, ForeignKey('paste.hash'), index = True)
+ paste = relation(Paste, primaryjoin = paste_hash == Paste.hash, lazy = False)
+ syntax_name = Column(String)
+ text = Column(Text, nullable = False)
+
+ def __init__(self, hash, syntax_name, text):
+ self.paste_hash = hash
+ self.syntax_name = syntax_name
+ self.text = text
+
+ def __repr__(self):
+ return '<Cache(%d, "%s")>' % (self.id, self.paste_hash)
+
+Base.metadata.create_all()
+Session = sessionmaker(bind = engine, autoflush = True, autocommit = False)
+
+@contextmanager
+def session_scope():
+ session = Session()
+ try:
+ session.expire_on_commit = False
+ yield session
+ session.commit()
+ except:
+ session.rollback()
+ raise
+ finally:
+ session.close()
diff --git a/pastepy/pastepy.py b/pastepy/pastepy.py
new file mode 100644
index 0000000..750d8fe
--- /dev/null
+++ b/pastepy/pastepy.py
@@ -0,0 +1,169 @@
+import datetime
+import random
+
+from flask import Blueprint, current_app, render_template, request, session, redirect, url_for
+try:
+ import markdown
+ has_markdown = True
+except ImportError:
+ has_markdown = False
+import pygments
+from pygments import highlight
+from pygments.lexers import get_all_lexers, get_lexer_by_name
+from pygments.formatters import HtmlFormatter
+
+from . import db
+
+app = Blueprint(__name__, 'pastepy')
+
+base62_alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
+
+class PasteError(Exception): pass
+class UnknownSyntaxError(PasteError): pass
+
+class CustomHtmlFormatter(HtmlFormatter):
+ def wrap(self, source, outfile):
+ yield 0, '<pre>'
+ line = 1
+ for i, t in source:
+ yield i, '<span id="codeline-{}">{}</span>'.format(i, t)
+ line += 1
+ yield 0, '</pre>'
+
+def init_lexers():
+ all_lexers = dict([(x[0], x[1][0]) for x in get_all_lexers()])
+ lexers = []
+ removed = []
+ for cat, ls in current_app.config['CATEGORIES']:
+ for l in ls:
+ removed.append(l)
+ lexers.append((cat, [(x, all_lexers[x]) for x in ls if x in all_lexers]))
+ for l in removed:
+ try:
+ del all_lexers[l]
+ except KeyError:
+ pass
+ rendered = []
+ if has_markdown:
+ rendered.append(('Rendered markdown', 'md-render'))
+ if len(rendered):
+ lexers.append(('Rendered', rendered))
+ lexers.append(('Others' if current_app.config['CATEGORIES'] else 'Syntax', sorted(all_lexers.items(), key = lambda l: l[0].lower())))
+
+ current_app.config['LEXERS'] = lexers
+ current_app.config['FORMATTER'] = CustomHtmlFormatter(linenos='table', lineanchors='line', anchorlinenos=True)
+
+init_lexers()
+
+@app.route('/')
+@app.route('/paste')
+def paste():
+ context = {
+ 'title': current_app.config['PASTEBIN_NAME'],
+ 'header': current_app.config['PASTEBIN_NAME'],
+ }
+ return render_template('paste.html', **context)
+
+def get_formatted(syntax, text):
+ if syntax == 'md-render' and has_markdown:
+ text = markdown.markdown(text, extensions=current_app.config['MARKDOWN_EXTENSIONS'])
+ lexername = 'Rendered markdown'
+ else:
+ try:
+ lex = get_lexer_by_name(syntax or 'text')
+ except pygments.util.ClassNotFound:
+ raise UnknownSyntaxError(syntax)
+ lexername = lex.name
+ text = highlight(text, lex, current_app.config['FORMATTER'])
+ return (lexername, text)
+
+def add_paste():
+ nick = request.form.get('nick')
+ syntax = request.form.get('syntax')
+ title = request.form.get('title')
+ text = request.form.get('text').replace('\r', '')
+
+ with db.session_scope() as db_session:
+ paste_hash = ''.join(random.choice(base62_alphabet) for x in range(5))
+ paste = db.Paste(paste_hash, nick, datetime.datetime.utcnow(), syntax, title, text, request.remote_addr)
+ db_session.add(paste)
+
+ if 'remember_me' in request.form:
+ session['nick'] = nick
+ else:
+ session.pop('nick', None)
+ if 'remember_syntax' in request.form:
+ session['syntax'] = syntax
+ else:
+ session.pop('syntax', None)
+ return redirect(url_for('.view_paste', paste_hash=paste_hash))
+
+def render_preview():
+ try:
+ lexername, text = get_formatted(request.form['syntax'], request.form['text'])
+ except UnknownSyntaxError:
+ abort(500, 'Could not find lexer "{}".'.format(request.form['syntax']))
+
+ nick = request.form['nick'] or 'Anonymous'
+ title = request.form['title'] or 'Untitled'
+ return render_template('view.html',
+ title='Preview',
+ hash=None,
+ nick=nick,
+ date='{} UTC'.format(datetime.datetime.utcnow().ctime()),
+ syntax=lexername,
+ pastetitle=title,
+ text=text,
+ rendered=(lexername or '').startswith('Rendered '),
+ )
+
+@app.route('/paste', methods=['POST'])
+def post_paste():
+ if request.form['type'] == 'Preview':
+ return render_preview()
+ if request.form['type'] == 'Paste':
+ return add_paste()
+ abort(404)
+
+@app.route('/view/<paste_hash>')
+@app.route('/full/<paste_hash>', endpoint='view_full')
+def view_paste(paste_hash):
+ with db.session_scope() as db_session:
+ try:
+ cache = db_session.query(db.Cache).filter_by(paste_hash=paste_hash).one()
+ paste = cache.paste
+ except db.NoResultFound:
+ try:
+ paste = db_session.query(db.Paste).filter_by(hash=paste_hash).one()
+ except db.NoResultFound:
+ abort(404)
+ try:
+ lexername, text = get_formatted(paste.syntax, paste.text)
+ except UnknownSyntaxError:
+ abort(500, 'Could not find the lexer "{}"'.format(paste.syntax))
+ cache = db.Cache(paste_hash, lexername, text)
+ db_session.add(cache)
+ template = 'full.html' if request.endpoint.endswith('.view_full') else 'view.html'
+ return render_template(template,
+ title='View paste \u2013 {}'.format(current_app.config['PASTEBIN_NAME'], paste.title or 'Untitled'),
+ hash=paste_hash,
+ date='{} UTC'.format(paste.date.ctime()),
+ nick=paste.nick or 'Anonymous',
+ syntax=cache.syntax_name,
+ pastetitle=paste.title or 'Untitled',
+ text=cache.text,
+ rendered=(cache.syntax_name or '').startswith('Rendered '),
+ )
+
+@app.route('/highlight_stylesheet')
+def highlight_stylesheet():
+ return current_app.config['FORMATTER'].get_style_defs(), 200, {'Content-Type': 'text/css'}
+
+@app.route('/raw/<paste_hash>')
+def raw(paste_hash):
+ with db.session_scope() as db_session:
+ try:
+ paste = db_session.query(db.Paste).filter_by(hash=paste_hash).one()
+ except db.NoResultFound:
+ abort(404)
+ return paste.text, 200, {'Content-Type': 'text/plain'}
diff --git a/static/edit.js b/pastepy/static/edit.js
index 8e754d8..8e754d8 100644
--- a/static/edit.js
+++ b/pastepy/static/edit.js
diff --git a/static/paste.css b/pastepy/static/paste.css
index 5e59d4d..5e59d4d 100644
--- a/static/paste.css
+++ b/pastepy/static/paste.css
diff --git a/pastepy/static/view.js b/pastepy/static/view.js
new file mode 100644
index 0000000..fd6db03
--- /dev/null
+++ b/pastepy/static/view.js
@@ -0,0 +1,26 @@
+function set_highlight(line) {
+ var line = 'codeline-' + line
+ var codeline = document.getElementById(line);
+ if (codeline) {
+ codeline.className = 'selected';
+ }
+}
+function cleanup() {
+ var elements = document.getElementsByClassName('selected');
+ for (var i = 0; i < elements.length; i++) {
+ elements[i].className = '';
+ }
+}
+window.onload = function(event) {
+ var line = document.location.hash.substr(6);
+ if (line) {
+ set_highlight(line);
+ }
+ var as = document.getElementsByClassName('linenodiv')[0].getElementsByTagName('a');
+ for (var i = 0; i < as.length; i++) {
+ as[i].onclick = function(event) {
+ cleanup();
+ set_highlight(event.target.text.trim());
+ }
+ }
+}
diff --git a/templates/base.html b/pastepy/templates/base.html
index 196a03e..e208b6b 100644
--- a/templates/base.html
+++ b/pastepy/templates/base.html
@@ -1,15 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
- <title>{{ title }}</title>
- <link rel="StyleSheet" href="/static/paste.css" type="text/css">
+ <title>{{ config.PASTEBIN_NAME }}{% if title %} &ndash; {{ title }}{% endif %}</title>
+ <link rel="StyleSheet" href="{{ url_for('static', filename='paste.css') }}" type="text/css">
{% block head %}
{% endblock %}
</head>
<body>
<div id="page">
<div id="page-header">
- <h1><a href="/">{{ header }}</a></h1>
+ <h1><a href="/">{{ title }}</a></h1>
</div>
<div id="page-content">
{% block content %}
diff --git a/pastepy/templates/full.html b/pastepy/templates/full.html
new file mode 100644
index 0000000..c60126e
--- /dev/null
+++ b/pastepy/templates/full.html
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <title>{{ title }}</title>
+ <link rel="StyleSheet" href="{{ url_for('.highlight_stylesheet') }}" type="text/css">
+ <link rel="StyleSheet" href="{{ url_for('static', filename='paste.css') }}" type="text/css">
+ <script type="text/javascript" src="{{ url_for('static', filename='view.js') }}"></script>
+</head>
+<body>
+{% if rendered %}
+<div class="paste-rendered">
+{% endif %}
+{{ text|safe }}
+{% if rendered %}
+</div>
+{% endif %}
+</body>
+</html>
diff --git a/templates/paste.html b/pastepy/templates/paste.html
index e22f76d..9a22e76 100644
--- a/templates/paste.html
+++ b/pastepy/templates/paste.html
@@ -1,26 +1,26 @@
{% extends "base.html" %}
{% block head %}
- <script src="/static/edit.js" type="text/javascript"></script>
+ <script src="{{ url_for('static', filename='edit.js') }}" type="text/javascript"></script>
{% endblock %}
{% block content %}
<form action="/paste" method="post" id="pasteform">
<ul>
<li id="syntaxli">
<select name="syntax" id="syntax">
- {% for cat, ls in lexers %}
+ {% for cat, ls in config.LEXERS %}
<optgroup label="{{ cat }}">
{% for k, v in ls %}
- <option value="{{ v }}"{% if v == syntax %} selected="selected"{% endif %}>{{ k }}</option>
+ <option value="{{ v }}"{% if v == (session.syntax or config.DEFAULT_SYNTAX) %} selected="selected"{% endif %}>{{ k }}</option>
{% endfor %}
</optgroup>
{% endfor %}
</select>
- <input type="checkbox" name="remember_syntax" id="remember_syntax"{% if remember_syntax %} checked{% endif %}>
+ <input type="checkbox" name="remember_syntax" id="remember_syntax"{% if session.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{% endif %}>
+ <input type="text" name="nick" id="nick" value="{{ session.nick }}">
+ <input type="checkbox" name="remember_me" id="remember_me"{% if session.nick %} checked{% endif %}>
<label for="remember_me">Remember me</label>
</li>
<li><input type="text" name="title" id="title" value="Untitled"></li>
diff --git a/templates/view.html b/pastepy/templates/view.html
index a1d00a7..b0b114c 100644
--- a/templates/view.html
+++ b/pastepy/templates/view.html
@@ -1,7 +1,7 @@
{% extends "base.html" %}
{% block head %}
- <link rel="StyleSheet" href="/highlight_stylesheet" type="text/css">
- <script type="text/javascript" src="/static/view.js"></script>
+ <link rel="StyleSheet" href="{{ url_for('.highlight_stylesheet') }}" type="text/css">
+ <script type="text/javascript" src="{{ url_for('static', filename='view.js') }}"></script>
{% endblock %}
{% block content %}
<p><a href="/">New paste</a></p>
@@ -20,7 +20,7 @@
{% if rendered %}
<div class="paste-rendered">
{% endif %}
- {{ text }}
+ {{ text|safe }}
{% if rendered %}
</div>
{% endif %}
diff --git a/static/view.js b/static/view.js
deleted file mode 100644
index c855609..0000000
--- a/static/view.js
+++ /dev/null
@@ -1,5 +0,0 @@
-window.onload = function() {
- var line = 'code' + document.location.hash.substr(1);
- var codeline = document.getElementById(line);
- codeline.className = 'selected';
-}
diff --git a/templates/full.html b/templates/full.html
deleted file mode 100644
index dfa0606..0000000
--- a/templates/full.html
+++ /dev/null
@@ -1,18 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-<head>
- <title>{{ title }}</title>
- <link rel="StyleSheet" href="/highlight_stylesheet" type="text/css">
- <link rel="StyleSheet" href="/static/paste.css" type="text/css">
- <script type="text/javascript" src="/static/view.js"></script>
-</head>
-<body>
-{% if rendered %}
-<div class="paste-rendered">
-{% endif %}
-{{ text }}
-{% if rendered %}
-</div>
-{% endif %}
-</body>
-</html>
diff --git a/templates/message.html b/templates/message.html
deleted file mode 100644
index 540b64a..0000000
--- a/templates/message.html
+++ /dev/null
@@ -1,5 +0,0 @@
-{% extends "base.html" %}
-{% block content %}
- <p><a href="/">New paste</a></p>
- {{ text }}
-{% endblock %}