Skip to content

Commit

Permalink
Adding syntax highlighting to displayed SQL.
Browse files Browse the repository at this point in the history
  • Loading branch information
coleifer committed Dec 31, 2014
1 parent 5515948 commit 5a57f8c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 6 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
flask
pygments
-e git+git://github.com/coleifer/peewee.git#egg=peewee
20 changes: 18 additions & 2 deletions sqlite_browser/sqlite_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,24 @@

try:
from flask import (
Flask, abort, escape, flash, make_response, redirect, render_template,
request, url_for)
Flask, abort, escape, flash, make_response, Markup, redirect,
render_template, request, url_for)
except ImportError:
raise RuntimeError('Unable to import flask module. Install by running '
'pip install flask')

try:
from pygments import formatters, highlight, lexers
except ImportError:
import warnings
warnings.warn('pygments library not found.', ImportWarning)
syntax_highlight = lambda data: '<pre>%s</pre>' % data
else:
def syntax_highlight(data):
lexer = lexers.get_lexer_by_name('sql')
formatter = formatters.HtmlFormatter(linenos=False)
return highlight(data, lexer, formatter)

try:
from peewee import __version__
except ImportError:
Expand Down Expand Up @@ -528,6 +540,10 @@ def value_filter(value, max_length=50):
value)
return value

@app.template_filter('highlight')
def highlight_filter(data):
return Markup(syntax_highlight(data))

def get_query_images():
accum = []
image_dir = os.path.join(app.static_folder, 'img')
Expand Down
61 changes: 61 additions & 0 deletions sqlite_browser/static/css/syntax-highlight.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
.hll { background-color: #ffffcc }
.c { color: #888888 } /* Comment */
.err { color: #FF0000; background-color: #FFAAAA } /* Error */
.k { color: #008800; font-weight: bold } /* Keyword */
.o { color: #333333 } /* Operator */
.cm { color: #888888 } /* Comment.Multiline */
.cp { color: #557799 } /* Comment.Preproc */
.c1 { color: #888888 } /* Comment.Single */
.cs { color: #cc0000; font-weight: bold } /* Comment.Special */
.gd { color: #A00000 } /* Generic.Deleted */
.ge { font-style: italic } /* Generic.Emph */
.gr { color: #FF0000 } /* Generic.Error */
.gh { color: #000080; font-weight: bold } /* Generic.Heading */
.gi { color: #00A000 } /* Generic.Inserted */
.go { color: #888888 } /* Generic.Output */
.gp { color: #c65d09; font-weight: bold } /* Generic.Prompt */
.gs { font-weight: bold } /* Generic.Strong */
.gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.gt { color: #0044DD } /* Generic.Traceback */
.kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.kp { color: #003388; font-weight: bold } /* Keyword.Pseudo */
.kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.kt { color: #333399; font-weight: bold } /* Keyword.Type */
.m { color: #6600EE; font-weight: bold } /* Literal.Number */
.s { background-color: #fff0f0 } /* Literal.String */
.na { color: #0000CC } /* Name.Attribute */
.nb { color: #007020 } /* Name.Builtin */
.nc { color: #BB0066; font-weight: bold } /* Name.Class */
.no { color: #003366; font-weight: bold } /* Name.Constant */
.nd { color: #555555; font-weight: bold } /* Name.Decorator */
.ni { color: #880000; font-weight: bold } /* Name.Entity */
.ne { color: #FF0000; font-weight: bold } /* Name.Exception */
.nf { color: #0066BB; font-weight: bold } /* Name.Function */
.nl { color: #997700; font-weight: bold } /* Name.Label */
.nn { color: #0e84b5; font-weight: bold } /* Name.Namespace */
.nt { color: #007700 } /* Name.Tag */
.nv { color: #996633 } /* Name.Variable */
.ow { color: #000000; font-weight: bold } /* Operator.Word */
.w { color: #bbbbbb } /* Text.Whitespace */
.mf { color: #6600EE; font-weight: bold } /* Literal.Number.Float */
.mh { color: #005588; font-weight: bold } /* Literal.Number.Hex */
.mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.mo { color: #4400EE; font-weight: bold } /* Literal.Number.Oct */
.sb { background-color: #fff0f0 } /* Literal.String.Backtick */
.sc { color: #0044DD } /* Literal.String.Char */
.sd { color: #DD4422 } /* Literal.String.Doc */
.s2 { background-color: #fff0f0 } /* Literal.String.Double */
.se { color: #666666; font-weight: bold; background-color: #fff0f0 } /* Literal.String.Escape */
.sh { background-color: #fff0f0 } /* Literal.String.Heredoc */
.si { background-color: #eeeeee } /* Literal.String.Interpol */
.sx { color: #DD2200; background-color: #fff0f0 } /* Literal.String.Other */
.sr { color: #000000; background-color: #fff0ff } /* Literal.String.Regex */
.s1 { background-color: #fff0f0 } /* Literal.String.Single */
.ss { color: #AA6600 } /* Literal.String.Symbol */
.bp { color: #007020 } /* Name.Builtin.Pseudo */
.vc { color: #336699 } /* Name.Variable.Class */
.vg { color: #dd7700; font-weight: bold } /* Name.Variable.Global */
.vi { color: #3333BB } /* Name.Variable.Instance */
.il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
1 change: 1 addition & 0 deletions sqlite_browser/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<meta name="robots" content="noindex">
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/bootstrap.min.css') }}" />
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/sqlbrowse.css') }}" />
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/syntax-highlight.css') }}" />
{% block extra_head %}{% endblock %}
<script src="{{ url_for('static', filename='js/jquery-1.11.0.min.js') }}" type="text/javascript"></script>
<script src="{{ url_for('static', filename='js/bootstrap.min.js') }}"></script>
Expand Down
8 changes: 4 additions & 4 deletions sqlite_browser/templates/table_structure.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
$('a.view-sql').on('click', function(e) {
e.preventDefault();
var elem = $(this),
pre = elem.siblings('pre'),
pre = elem.siblings('div'),
modalDiv = $('div#sql-modal');
modalDiv.find('h4.modal-title').text(elem.data('name'));
modalDiv.find('.modal-body').empty().append(pre.clone().show());
Expand All @@ -20,7 +20,7 @@

{% block inner_content %}
<h3 id="sql">SQL</h3>
<pre>{{ table_sql }}</pre>
{{ table_sql|highlight }}

<h3 id="columns">
<p class="pull-right"><a class="btn btn-primary btn-sm" href="{{ url_for('add_column', table=table) }}">Add column</a></p>
Expand Down Expand Up @@ -119,7 +119,7 @@ <h3 id="indexes">
</td>
<td>
<a class="view-sql" data-name="{{ index.name }}" href="#">SQL</a>
<pre style="display:none;">{{ index.sql }}</pre>
<div style="display:none;">{{ index.sql|highlight }}</div>
</td>
<td>
<a href="{{ url_for('drop_index', table=table, name=index.name) }}">Drop</a>
Expand Down Expand Up @@ -149,7 +149,7 @@ <h3 id="triggers">
<td>{{ trigger.name }}</td>
<td>
<a class="view-sql" data-name="{{ trigger.name }}" href="#">SQL</a>
<pre style="display:none;">{{ trigger.sql }}</pre>
<div style="display:none;">{{ trigger.sql|highlight }}</div>
</td>
<td>
<a href="{{ url_for('drop_trigger', table=table, name=trigger.name) }}">Drop</a>
Expand Down

0 comments on commit 5a57f8c

Please sign in to comment.